Bug 814702 - toolkit/components/downloads/test/unit/test_privatebrowsing_cancel.js fails on Birch; r=ehsan
authorAndres Hernandez <andres@appcoast.com>
Tue, 27 Nov 2012 14:23:19 -0600
changeset 114298 98edf6f6d9fe79541bbec1e0f4770fd6b0c2f1f5
parent 114297 c1859634b922dc54e27b8c5cb7631fc340db3e05
child 114299 9efd4d0da8934ab83d4b7c89fa6d3293891afe65
push id23913
push useremorley@mozilla.com
push dateWed, 28 Nov 2012 17:11:31 +0000
treeherdermozilla-central@17c267a881cf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs814702
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 814702 - toolkit/components/downloads/test/unit/test_privatebrowsing_cancel.js fails on Birch; r=ehsan
toolkit/components/downloads/test/browser/Makefile.in
toolkit/components/downloads/test/browser/browser_privatebrowsing_cancel.js
toolkit/components/downloads/test/unit/xpcshell.ini
--- a/toolkit/components/downloads/test/browser/Makefile.in
+++ b/toolkit/components/downloads/test/browser/Makefile.in
@@ -8,18 +8,19 @@ topsrcdir      = @top_srcdir@
 srcdir         = @srcdir@
 VPATH          = @srcdir@
 relativesrcdir = @relativesrcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MOCHITEST_BROWSER_FILES = \
   browser_nsIDownloadManagerUI.js \
+  head.js \
   $(NULL)
 
 ifdef MOZ_PER_WINDOW_PRIVATE_BROWSING
 MOCHITEST_BROWSER_FILES += \
   browser_privatebrowsing.js \
-  head.js \
+  browser_privatebrowsing_cancel.js \
   $(NULL)
 endif
 
 include $(topsrcdir)/config/rules.mk
copy from toolkit/components/downloads/test/unit/test_privatebrowsing_cancel.js
copy to toolkit/components/downloads/test/browser/browser_privatebrowsing_cancel.js
--- a/toolkit/components/downloads/test/unit/test_privatebrowsing_cancel.js
+++ b/toolkit/components/downloads/test/browser/browser_privatebrowsing_cancel.js
@@ -1,266 +1,116 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-/**
-  Make sure that the download manager service is given a chance to cancel the
-  private browisng mode transition.
-**/
-
-const Cm = Components.manager;
-
-const kPromptServiceUUID = "{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}";
-const kPromptServiceContractID = "@mozilla.org/embedcomp/prompt-service;1";
-
-// Save original prompt service factory
-const kPromptServiceFactory = Cm.getClassObject(Cc[kPromptServiceContractID],
-                                                Ci.nsIFactory);
-
-let fakePromptServiceFactory = {
-  createInstance: function(aOuter, aIid) {
-    if (aOuter != null)
-      throw Cr.NS_ERROR_NO_AGGREGATION;
-    return promptService.QueryInterface(aIid);
-  }
-};
-
-let promptService = {
-  _buttonChoice: 0,
-  _called: false,
-  wasCalled: function() {
-    let called = this._called;
-    this._called = false;
-    return called;
-  },
-  sayCancel: function() {
-    this._buttonChoice = 1;
-    this._called = false;
-  },
-  sayProceed: function() {
-    this._buttonChoice = 0;
-    this._called = false;
-  },
-  QueryInterface: function(aIID) {
-    if (aIID.equals(Ci.nsIPromptService) ||
-        aIID.equals(Ci.nsISupports)) {
-      return this;
-    }
-    throw Cr.NS_ERROR_NO_INTERFACE;
-  },
-  confirmEx: function(parent, title, text, buttonFlags,
-                      button0Title, button1Title, button2Title,
-                      checkMsg, checkState) {
-    this._called = true;
-    return this._buttonChoice;
-  }
-};
-
-Cm.QueryInterface(Ci.nsIComponentRegistrar)
-  .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
-                   kPromptServiceContractID, fakePromptServiceFactory);
-
-this.__defineGetter__("pb", function () {
-  delete this.pb;
-  try {
-    return this.pb = Cc["@mozilla.org/privatebrowsing;1"].
-                     getService(Ci.nsIPrivateBrowsingService);
-  } catch (e) {}
-  return this.pb = null;
-});
-
-this.__defineGetter__("dm", function() {
-  delete this.dm;
-  return this.dm = Cc["@mozilla.org/download-manager;1"].
-                   getService(Ci.nsIDownloadManager);
-});
-
-function run_test() {
-  if (!pb) // Private Browsing might not be available
-    return;
-
-  function finishTest() {
-    // Cancel Download-E
-    dm.cancelDownload(dlE.id);
-    dm.removeDownload(dlE.id);
-    dm.cleanUp();
-    do_check_eq(dm.activeDownloadCount, 0);
-
-    prefBranch.clearUserPref("browser.privatebrowsing.keep_current_session");
-    dm.removeListener(listener);
-    httpserv.stop(do_test_finished);
-
-    // Unregister the factory so we do not leak
-    Cm.QueryInterface(Ci.nsIComponentRegistrar)
-      .unregisterFactory(Components.ID(kPromptServiceUUID),
-                         fakePromptServiceFactory);
-
-    // Restore the original factory
-    Cm.QueryInterface(Ci.nsIComponentRegistrar)
-      .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
-                       kPromptServiceContractID, kPromptServiceFactory);
-  }
-
-  let prefBranch = Cc["@mozilla.org/preferences-service;1"].
-                   getService(Ci.nsIPrefBranch);
-  prefBranch.setBoolPref("browser.privatebrowsing.keep_current_session", true);
+function test() {
+  waitForExplicitFinish();
 
-  do_test_pending();
-  let httpserv = new HttpServer();
-  httpserv.registerDirectory("/file/", do_get_cwd());
-  httpserv.registerPathHandler("/noresume", function (meta, response) {
-    response.setHeader("Content-Type", "text/html", false);
-    response.setHeader("Accept-Ranges", "none", false);
-    response.write("foo");
-  });
-  httpserv.start(4444);
-
-  let tmpDir = Cc["@mozilla.org/file/directory_service;1"].
-               getService(Ci.nsIProperties).
-               get("TmpD", Ci.nsIFile);
-
-  // make sure we're starting with an empty DB
-  do_check_eq(dm.activeDownloadCount, 0);
-
+  let windowsToClose = [];
+  let publicDownload;
+  let privateDownload;
+  let tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
+  let httpServer = startServer();
   let listener = {
-    onDownloadStateChange: function(aState, aDownload)
-    {
+    onDownloadStateChange: function(aState, aDownload) {
       switch (aDownload.state) {
-        case dm.DOWNLOAD_QUEUED:
-        case dm.DOWNLOAD_DOWNLOADING:
-          if (aDownload.targetFile.equals(dlD.targetFile)) {
-            // Sanity check: Download-D must not be resumable
-            do_check_false(dlD.resumable);
-
-            // Cancel the transition
-            promptService.sayCancel();
-
-            // Enter private browsing mode immediately
-            pb.privateBrowsingEnabled = true;
-            do_check_true(promptService.wasCalled());
-            do_check_false(pb.privateBrowsingEnabled); // transition was canceled
-
-            // Check that Download-D status has not changed
-            do_check_neq(dlD.state, dm.DOWNLOAD_PAUSED);
-
-            // proceed with the transition
-            promptService.sayProceed();
-
-            // Try to enter the private browsing mode again
-            pb.privateBrowsingEnabled = true;
-            do_check_true(promptService.wasCalled());
-            do_check_true(pb.privateBrowsingEnabled);
-
-            // Check that Download-D is canceled and not accessible
-            do_check_eq(dlD.state, dm.DOWNLOAD_CANCELED);
-
-            // Exit private browsing mode
-            pb.privateBrowsingEnabled = false;
-            do_check_false(pb.privateBrowsingEnabled);
-
-            // Create Download-E
-            dlE = addDownload({
-              isPrivate: pb.privateBrowsingEnabled,
-              targetFile: fileE,
-              sourceURI: downloadESource,
-              downloadName: downloadEName
+        case Services.downloads.DOWNLOAD_QUEUED:
+        case Services.downloads.DOWNLOAD_DOWNLOADING:
+          if (aDownload.targetFile.equals(publicDownload.targetFile)) {
+            is(Services.downloads.activeDownloadCount, 1,
+              "Check public download is active");
+            is(publicDownload.resumable, false,
+               "Download must not be resumable");
+            isnot(publicDownload.cancelable, null,
+                  "Download must be cancelable");
+            // Cancel the download
+            Services.downloads.cancelDownload(publicDownload.id);
+          }
+          break;
+        case Services.downloads.DOWNLOAD_CANCELED:
+          if (aDownload.targetFile.equals(publicDownload.targetFile)) {
+            is(publicDownload.resumable, false,
+               "Download must not be resumable");
+            is(publicDownload.cancelable, null,
+               "Download must not be cancelable");
+            testOnWindow(true, function(aWin) {
+              is(Services.downloads.activeDownloadCount, 0,
+                 "Check public download is not active on private window");
+              privateDownload = testDownload(aWin, true);
+              // Try to cancel the download but will fail since it's private
+              try {
+                Services.downloads.cancelDownload(privateDownload.id);
+                ok(false, "Cancel private download should have failed.");
+              } catch (e) {
+                testOnWindow(false, function(aWin) {
+                  is(Services.downloads.activeDownloadCount, 0,
+                   "Check that there are no active downloads");
+                  cleanUp();
+                });
+              }
             });
-
-            // Wait for Download-E to start
-          } else if (aDownload.targetFile.equals(dlE.targetFile)) {
-            // Sanity check: Download-E must be resumable
-            do_check_true(dlE.resumable);
-
-            // Enter the private browsing mode
-            pb.privateBrowsingEnabled = true;
-            do_check_false(promptService.wasCalled());
-            do_check_true(pb.privateBrowsingEnabled);
-
-            // Create Download-F
-            dlF = addDownload({
-              isPrivate: pb.privateBrowsingEnabled,
-              targetFile: fileF,
-              sourceURI: downloadFSource,
-              downloadName: downloadFName
-            });
-
-            // Wait for Download-F to start
-          } else if (aDownload.targetFile.equals(dlF.targetFile)) {
-            // Sanity check: Download-F must be resumable
-            do_check_true(dlF.resumable);
-
-            // Cancel the transition
-            promptService.sayCancel();
-
-            // Exit private browsing mode immediately
-            pb.privateBrowsingEnabled = false;
-            do_check_true(promptService.wasCalled());
-            do_check_true(pb.privateBrowsingEnabled); // transition was canceled
-
-            // Check that Download-F status has not changed
-            do_check_neq(dlF.state, dm.DOWNLOAD_PAUSED);
-
-            // proceed with the transition
-            promptService.sayProceed();
-
-            // Try to exit the private browsing mode again
-            pb.privateBrowsingEnabled = false;
-            do_check_true(promptService.wasCalled());
-            do_check_false(pb.privateBrowsingEnabled);
-
-            // Simulate leaving PB mode
-            Services.obs.notifyObservers(null, "last-pb-context-exited", null);
-
-            // Check that Download-F is canceled and not accessible
-            do_check_eq(dlF.state, dm.DOWNLOAD_PAUSED);
-
-            finishTest();
           }
           break;
       }
     },
     onStateChange: function(a, b, c, d, e) { },
     onProgressChange: function(a, b, c, d, e, f, g) { },
     onSecurityChange: function(a, b, c, d) { }
   };
-
-  dm.addListener(listener);
+  Services.downloads.addListener(listener);
 
-  // properties of Download-D
-  const downloadDSource = "http://localhost:4444/noresume";
-  const downloadDDest = "download-file-D";
-  const downloadDName = "download-D";
+  registerCleanupFunction(function() {
+    Services.downloads.removeListener(listener);
+    windowsToClose.forEach(function(win) {
+      win.close();
+    });
+  });
 
-  // properties of Download-E
-  const downloadESource = "http://localhost:4444/file/head_download_manager.js";
-  const downloadEDest = "download-file-E";
-  const downloadEName = "download-E";
-
-  // properties of Download-F
-  const downloadFSource = "http://localhost:4444/file/test_privatebrowsing_cancel.js";
-  const downloadFDest = "download-file-F";
-  const downloadFName = "download-F";
+  function cleanUp() {
+    Services.downloads.removeDownload(publicDownload.id);
+    if (publicDownload.targetFile.exists()) {
+      publicDownload.targetFile.remove(false);
+    }
+    if (privateDownload.targetFile.exists()) {
+      privateDownload.targetFile.remove(false);
+    }
+    Services.downloads.cleanUp();
+    is(Services.downloads.activeDownloadCount, 0,
+      "Make sure we're starting with an empty DB");
+    httpServer.stop(finish);
+  }
 
-  // Create all target files
-  let fileD = tmpDir.clone();
-  fileD.append(downloadDDest);
-  fileD.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
-  let fileE = tmpDir.clone();
-  fileE.append(downloadEDest);
-  fileE.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
-  let fileF = tmpDir.clone();
-  fileF.append(downloadFDest);
-  fileF.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
+  function testOnWindow(aIsPrivate, aCallback) {
+    whenNewWindowLoaded(aIsPrivate, function(win) {
+      windowsToClose.push(win);
+      aCallback(win);
+    });
+  }
+
+  function createFile(aFileName) {
+    let file = tmpDir.clone();
+    file.append(aFileName);
+    file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
+    return file;
+  }
 
-  // Create Download-D
-  let dlD = addDownload({
-    isPrivate: pb.privateBrowsingEnabled,
-    targetFile: fileD,
-    sourceURI: downloadDSource,
-    downloadName: downloadDName
+  function testDownload(aWin, aIsPrivate) {
+    let file = createFile("download-file-" + aIsPrivate);
+    let sourceURI =
+      aIsPrivate ? "file/browser_privatebrowsing_cancel.js" : "noresume";
+    let downloadInfo = {
+      isPrivate: aIsPrivate,
+      targetFile: file,
+      sourceURI: "http://localhost:4444/" + sourceURI,
+      downloadName: "download-" + aIsPrivate
+    }
+    return addDownload(downloadInfo);
+  }
+
+  // Make sure we're starting with an empty DB
+  is(Services.downloads.activeDownloadCount, 0,
+     "Make sure we're starting with an empty DB");
+
+  testOnWindow(false, function(aWin) {
+    publicDownload = testDownload(aWin, false);
   });
-  downloadD = dlD.id;
-
-  let dlE, dlF;
-
-  // wait for Download-D to start
 }
--- a/toolkit/components/downloads/test/unit/xpcshell.ini
+++ b/toolkit/components/downloads/test/unit/xpcshell.ini
@@ -20,11 +20,12 @@ skip-if = os == "android"
 [test_guid.js]
 [test_history_expiration.js]
 [test_offline_support.js]
 [test_old_download_files_removed.js]
 [test_private_resume.js]
 [test_privatebrowsing.js]
 skip-if = perwindowprivatebrowsing
 [test_privatebrowsing_cancel.js]
+skip-if = perwindowprivatebrowsing
 [test_removeDownloadsByTimeframe.js]
 [test_resume.js]
 [test_sleep_wake.js]