Bug 1370787 - add test for download file in PrivateBrowsing mode. r=smaug
authorYoshi Huang <allstars.chh@mozilla.com>
Thu, 31 Aug 2017 18:19:48 +0800
changeset 378182 4cabeb992038ff180944001cd634f4a2ad46e098
parent 378181 f789b5b305bb3934a3ab9caa19734d7c328a8a3e
child 378183 196ede21eb28e7a8ce4b72ff8f243b713f27121f
push id50200
push userarchaeopteryx@coole-files.de
push dateFri, 01 Sep 2017 08:44:00 +0000
treeherderautoland@10bb5830a5b2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1370787
milestone57.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 1370787 - add test for download file in PrivateBrowsing mode. r=smaug
uriloader/exthandler/tests/mochitest/blob.html
uriloader/exthandler/tests/mochitest/browser.ini
uriloader/exthandler/tests/mochitest/browser_ext_helper_pb.js
new file mode 100644
--- /dev/null
+++ b/uriloader/exthandler/tests/mochitest/blob.html
@@ -0,0 +1,17 @@
+<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,12 +1,14 @@
 [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]
 [browser_remember_download_option.js]
 [browser_web_protocol_handlers.js]
new file mode 100644
--- /dev/null
+++ b/uriloader/exthandler/tests/mochitest/browser_ext_helper_pb.js
@@ -0,0 +1,74 @@
+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();
+});