Bug 766263 - Part 2: Add a regression test. r=mak.
authorMike Conley <mconley@mozilla.com>
Fri, 26 Oct 2012 13:17:18 -0400
changeset 111676 9a1be51f0a56ccc9ca4509c58f6b74c900bb7003
parent 111675 194dcfbc328daddabd340cdd0bf52d0250382f95
child 111677 b9a482aa8e57225e2a6297235048bf42c2347cd0
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersmak
bugs766263
milestone19.0a1
Bug 766263 - Part 2: Add a regression test. r=mak.
browser/components/downloads/test/browser/Makefile.in
browser/components/downloads/test/browser/browser_first_download_panel.js
browser/components/downloads/test/browser/head.js
--- a/browser/components/downloads/test/browser/Makefile.in
+++ b/browser/components/downloads/test/browser/Makefile.in
@@ -7,12 +7,13 @@ srcdir    = @srcdir@
 VPATH     = @srcdir@
 
 relativesrcdir = @relativesrcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MOCHITEST_BROWSER_FILES = \
   browser_basic_functionality.js \
+  browser_first_download_panel.js \
   head.js \
   $(NULL)
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/browser/components/downloads/test/browser/browser_first_download_panel.js
@@ -0,0 +1,56 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * Make sure the downloads panel only opens automatically on the first
+ * download it notices. All subsequent downloads, even across sessions, should
+ * not open the panel automatically.
+ */
+function gen_test()
+{
+  try {
+    // Ensure that state is reset in case previous tests didn't finish.
+    for (let yy in gen_resetState()) yield;
+
+    // With this set to false, we should automatically open the panel
+    // the first time a download is started.
+    DownloadsCommon.data.panelHasShownBefore = false;
+
+    prepareForPanelOpen();
+    DownloadsCommon.data._notifyNewDownload();
+    yield;
+
+    // If we got here, that means the panel opened.
+    DownloadsPanel.hidePanel();
+
+    ok(DownloadsCommon.data.panelHasShownBefore,
+       "Should have recorded that the panel was opened on a download.")
+
+    // Next, make sure that if we start another download, we don't open
+    // the panel automatically.
+    panelShouldNotOpen();
+    DownloadsCommon.data._notifyNewDownload();
+    yield waitFor(2);
+  } catch(e) {
+    ok(false, e);
+  } finally {
+    // Clean up when the test finishes.
+    for (let yy in gen_resetState()) yield;
+  }
+}
+
+/**
+ * Call this to record a test failure for the next time the downloads panel
+ * opens.
+ */
+function panelShouldNotOpen()
+{
+  // Hook to wait until the test data has been loaded.
+  let originalOnViewLoadCompleted = DownloadsPanel.onViewLoadCompleted;
+  DownloadsPanel.onViewLoadCompleted = function () {
+    DownloadsPanel.onViewLoadCompleted = originalOnViewLoadCompleted;
+    ok(false, "Should not have opened the downloads panel.");
+  };
+}
--- a/browser/components/downloads/test/browser/head.js
+++ b/browser/components/downloads/test/browser/head.js
@@ -7,17 +7,18 @@
  * Provides infrastructure for automated download components tests.
  */
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Globals
 
 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
                                   "resource://gre/modules/FileUtils.jsm");
-
+XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
+                                  "resource:///modules/DownloadsCommon.jsm");
 const nsIDM = Ci.nsIDownloadManager;
 
 let gTestTargetFile = FileUtils.getFile("TmpD", ["dm-ui-test.file"]);
 gTestTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
 registerCleanupFunction(function () {
   gTestTargetFile.remove(false);
 });
 
@@ -145,19 +146,23 @@ function gen_resetState()
         testRunner.continueTest();
       }
     });
     yield;
   } finally {
     statement.finalize();
   }
 
+  // Reset any prefs that might have been changed.
+  Services.prefs.clearUserPref("browser.download.panel.shown");
+
   // Ensure that the panel is closed and data is unloaded.
   DownloadsCommon.data.clear();
   DownloadsCommon.data._loadState = DownloadsCommon.data.kLoadNone;
+  DownloadsPanel.hidePanel();
 
   // Wait for focus on the main window.
   waitForFocus(testRunner.continueTest);
   yield;
 }
 
 function gen_addDownloadRows(aDataRows)
 {
@@ -222,8 +227,46 @@ function gen_openPanel(aData)
   // Wait for focus on the main window.
   waitForFocus(testRunner.continueTest);
   yield;
 
   // Open the downloads panel, waiting until loading is completed.
   DownloadsPanel.showPanel();
   yield;
 }
+
+/**
+ * Spin the event loop for aSeconds seconds, and then signal the test to
+ * continue.
+ *
+ * @param aSeconds the number of seconds to wait.
+ * @note This helper should _only_ be used when there's no valid event to
+ *       listen to and one can't be made.
+ */
+function waitFor(aSeconds)
+{
+  setTimeout(function() {
+    testRunner.continueTest();
+  }, aSeconds * 1000);
+}
+
+/**
+ * Make it so that the next time the downloads panel opens, we signal to
+ * continue the test. This function needs to be called each time you want
+ * to wait for the panel to open.
+ *
+ * Example usage:
+ *
+ * prepareForPanelOpen();
+ * // Do something to open the panel
+ * yield;
+ * // We can assume the panel is open now.
+ */
+function prepareForPanelOpen()
+{
+  // Hook to wait until the test data has been loaded.
+  let originalOnPopupShown = DownloadsPanel.onPopupShown;
+  DownloadsPanel.onPopupShown = function (aEvent) {
+    DownloadsPanel.onPopupShown = originalOnPopupShown;
+    DownloadsPanel.onPopupShown.apply(this, [aEvent]);
+    testRunner.continueTest();
+  };
+}