Bug 1396184 - Rewrite the test for bug 1367581. r=mak draft
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Thu, 26 Oct 2017 17:07:24 +0100
changeset 686935 4a49ebe9cdee448b2ef53223b671d47f08b17f3a
parent 684703 d49501f258b105c5e2dcd0a59896ec1ceabf726b
child 737520 33c1fc5046fac96ded9de634300c47457d4c677d
push id86347
push userpaolo.mozmail@amadzone.org
push dateThu, 26 Oct 2017 16:08:24 +0000
reviewersmak
bugs1396184, 1367581
milestone58.0a1
Bug 1396184 - Rewrite the test for bug 1367581. r=mak MozReview-Commit-ID: 3XyVPVKSGF4
uriloader/exthandler/tests/mochitest/blob.html
uriloader/exthandler/tests/mochitest/browser.ini
uriloader/exthandler/tests/mochitest/browser_download_privatebrowsing.js
uriloader/exthandler/tests/mochitest/browser_ext_helper_pb.js
deleted file mode 100644
--- a/uriloader/exthandler/tests/mochitest/blob.html
+++ /dev/null
@@ -1,17 +0,0 @@
-<html>
-<head>
-<meta charset=UTF-8>
-<title>Test for Bug 13870787</title>
-<script>
-function load() {
-  var url = window.URL.createObjectURL(new Blob(["test"], {type:"text/plain"}));
-  var a = document.getElementById("download");
-  a.href = url;
-  a.download = "download.bin";
-}
-</script>
-</head>
-<body onload='load()'>
-<a id="download">download</a>
-</body>
-</html>
--- a/uriloader/exthandler/tests/mochitest/browser.ini
+++ b/uriloader/exthandler/tests/mochitest/browser.ini
@@ -1,15 +1,13 @@
 [DEFAULT]
 head = head.js
 support-files =
-  blob.html
   download_page.html
   download.bin
   protocolHandler.html
 
 [browser_auto_close_window.js]
 skip-if = !e10s # test relies on e10s behavior
 [browser_download_always_ask_preferred_app.js]
-[browser_ext_helper_pb.js]
-skip-if = os == "mac" # bug 1396184
+[browser_download_privatebrowsing.js]
 [browser_remember_download_option.js]
 [browser_web_protocol_handlers.js]
new file mode 100644
--- /dev/null
+++ b/uriloader/exthandler/tests/mochitest/browser_download_privatebrowsing.js
@@ -0,0 +1,117 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * Tests that downloads started from a private window by clicking on a link end
+ * up in the global list of private downloads (see bug 1367581).
+ */
+
+"use strict";
+
+Cu.import("resource://gre/modules/Downloads.jsm", this);
+Cu.import("resource://gre/modules/DownloadPaths.jsm", this);
+Cu.import("resource://testing-common/MockRegistrar.jsm", this);
+
+const TEST_TARGET_FILE_NAME = "test-download.txt";
+
+// While the previous test file should have deleted all the temporary files it
+// used, on Windows these might still be pending deletion on the physical file
+// system.  Thus, start from a new base number every time, to make a collision
+// with a file that is still pending deletion highly unlikely.
+let gFileCounter = Math.floor(Math.random() * 1000000);
+
+/**
+ * Returns a reference to a temporary file, that is guaranteed not to exist, and
+ * to have never been created before.
+ *
+ * @param aLeafName
+ *        Suggested leaf name for the file to be created.
+ *
+ * @return nsIFile pointing to a non-existent file in a temporary directory.
+ *
+ * @note It is not enough to delete the file if it exists, or to delete the file
+ *       after calling nsIFile.createUnique, because on Windows the delete
+ *       operation in the file system may still be pending, preventing a new
+ *       file with the same name to be created.
+ */
+function getTempFile(aLeafName) {
+  // Prepend a serial number to the extension in the suggested leaf name.
+  let [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
+  let leafName = base + "-" + gFileCounter + ext;
+  gFileCounter++;
+
+  // Get a file reference under the temporary directory for this test file.
+  let file = FileUtils.getFile("TmpD", [leafName]);
+  Assert.ok(!file.exists());
+
+  registerCleanupFunction(() => {
+    try {
+      file.remove(false);
+    } catch (ex) {
+      // On Windows, we may get an access denied error if the file existed
+      // before, and was recently deleted.
+      if (!(ex instanceof Components.Exception &&
+            (ex.result == Cr.NS_ERROR_FILE_ACCESS_DENIED ||
+             ex.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST ||
+             ex.result == Cr.NS_ERROR_FILE_NOT_FOUND))) {
+        throw ex;
+      }
+    }
+  });
+
+  return file;
+}
+
+add_task(async function test_setup() {
+  // Save downloads to disk without showing the dialog.
+  let cid = MockRegistrar.register("@mozilla.org/helperapplauncherdialog;1", {
+    QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]),
+    show(launcher) {
+      launcher.saveToDisk(null, false);
+    },
+    promptForSaveToFileAsync(launcher) {
+      // The dialog should create the empty placeholder file.
+      let file = getTempFile(TEST_TARGET_FILE_NAME);
+      file.create(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+      launcher.saveDestinationAvailable(file);
+    },
+  });
+  registerCleanupFunction(() => {
+    MockRegistrar.unregister(cid);
+  });
+});
+
+add_task(async function test_download_privatebrowsing() {
+  let privateList = await Downloads.getList(Downloads.PRIVATE);
+  let publicList = await Downloads.getList(Downloads.PUBLIC);
+
+  let win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
+  try {
+    let tab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser,
+      `data:text/html,<a download href="data:text/plain,">download</a>`);
+
+    let promiseNextPrivateDownload = new Promise(resolve => {
+      privateList.addView({
+        onDownloadAdded(download) {
+          privateList.removeView(this);
+          resolve(download);
+        },
+      });
+    });
+
+    await ContentTask.spawn(tab.linkedBrowser, {}, async function() {
+      content.document.querySelector("a").click();
+    });
+
+    // Wait for the download to finish so the file can be safely deleted later.
+    let download = await promiseNextPrivateDownload;
+    await download.whenSucceeded();
+
+    // Clean up after checking that there are no new public downloads either.
+    let publicDownloads = await publicList.getAll();
+    Assert.equal(publicDownloads.length, 0);
+    await privateList.removeFinished();
+  } finally {
+    await BrowserTestUtils.closeWindow(win);
+  }
+});
deleted file mode 100644
--- a/uriloader/exthandler/tests/mochitest/browser_ext_helper_pb.js
+++ /dev/null
@@ -1,74 +0,0 @@
-const { Downloads } = Cu.import("resource://gre/modules/Downloads.jsm", {});
-const URI = "http://example.com/browser/uriloader/exthandler/tests/mochitest/blob.html";
-
-add_task(async function testExtHelperInPrivateBrowsing() {
-  let win = await BrowserTestUtils.openNewBrowserWindow({private: true});
-  let browser = win.gBrowser.selectedBrowser;
-  browser.loadURI(URI);
-  await BrowserTestUtils.browserLoaded(browser);
-
-  let listener = {
-    _resolve: null,
-
-    onOpenWindow (aXULWindow) {
-      info("Download window shown...");
-      var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-                                .getInterface(Ci.nsIDOMWindow);
-      waitForFocus(function() {
-        // When the download dialog is shown, its accept button is only enabled
-        // after 1000ms (which is configured in the pref "security.dialog_enable_delay",
-        // see SharedPromptUtils.jsm.
-        setTimeout(() => {
-          // Set the choice to 'Save As File'.
-          let mode = domwindow.document.getElementById("mode");
-          let save = domwindow.document.getElementById("save");
-          let index = mode.getIndexOfItem(save);
-          mode.selectedItem = save;
-          domwindow.document.documentElement.acceptDialog();
-          // acceptDialog will close domwindow itself, so we don't have to close
-          // domwindow here.
-        }, 1000);
-      }, domwindow);
-    },
-
-    onCloseWindow: function(aXULWindow) {
-      info("onCloseWindow");
-      Services.wm.removeListener(listener);
-      this._resolve();
-    },
-    onWindowTitleChange: function(aXULWindow, aNewTitle) {},
-
-    waitForDownload: function() {
-      return new Promise(resolve => {
-        this._resolve = resolve;
-      });
-    }
-  };
-
-  Services.wm.addListener(listener);
-
-  await ContentTask.spawn(browser, {}, async function() {
-    var download = content.document.getElementById("download");
-    download.click();
-  });
-
-  // Wait until download is finished.
-  // However there seems to be no easy way to get notified when the download is
-  // completed, so we use onCloseWindow listener here.
-  await listener.waitForDownload();
-
-  let allList = await Downloads.getList(Downloads.ALL);
-  let allDownloads = await allList.getAll();
-  Assert.equal(allDownloads.length, 1, "Should have at least 1 download in ALL mode");
-
-  let publicList = await Downloads.getList(Downloads.PUBLIC);
-  let publicDownloads = await publicList.getAll();
-  Assert.equal(publicDownloads.length, 0, "Shouldn't have any download in normal mode");
-
-  let privateList = await Downloads.getList(Downloads.PRIVATE);
-  let privateDownloads = await privateList.getAll();
-  Assert.equal(privateDownloads.length, 1, "Should have 1 download in private mode");
-
-  win.close();
-  finish();
-});