summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/downloads/tests
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/mozapps/downloads/tests')
-rw-r--r--toolkit/mozapps/downloads/tests/browser/browser.ini24
-rw-r--r--toolkit/mozapps/downloads/tests/browser/browser_save_wrongextension.js98
-rw-r--r--toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_blob.js118
-rw-r--r--toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_delayedbutton.js96
-rw-r--r--toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_dialog_layout.js103
-rw-r--r--toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_extension.js58
-rw-r--r--toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_policy.js68
-rw-r--r--toolkit/mozapps/downloads/tests/browser/example.jnlp0
-rw-r--r--toolkit/mozapps/downloads/tests/browser/example.jnlp^headers^1
-rw-r--r--toolkit/mozapps/downloads/tests/browser/head.js17
-rw-r--r--toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE0
-rw-r--r--toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE^headers^1
-rw-r--r--toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif1
-rw-r--r--toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif^headers^1
-rw-r--r--toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt1
-rw-r--r--toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt^headers^2
-rw-r--r--toolkit/mozapps/downloads/tests/moz.build8
-rw-r--r--toolkit/mozapps/downloads/tests/unit/head_downloads.js5
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js398
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_lowMinutes.js56
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_syncedDownloadUtils.js28
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_unspecified_arguments.js33
-rw-r--r--toolkit/mozapps/downloads/tests/unit/xpcshell.ini7
23 files changed, 1124 insertions, 0 deletions
diff --git a/toolkit/mozapps/downloads/tests/browser/browser.ini b/toolkit/mozapps/downloads/tests/browser/browser.ini
new file mode 100644
index 0000000000..da1cf956e0
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/browser.ini
@@ -0,0 +1,24 @@
+[DEFAULT]
+support-files =
+ unknownContentType_dialog_layout_data.pif
+ unknownContentType_dialog_layout_data.pif^headers^
+ unknownContentType_dialog_layout_data.txt
+ unknownContentType_dialog_layout_data.txt^headers^
+ head.js
+
+[browser_save_wrongextension.js]
+[browser_unknownContentType_blob.js]
+[browser_unknownContentType_delayedbutton.js]
+skip-if =
+ os == "linux" && bits == 64 && debug # Bug 1747285
+ os == "linux" && fission && tsan # Bug 1747285
+[browser_unknownContentType_dialog_layout.js]
+[browser_unknownContentType_extension.js]
+support-files =
+ unknownContentType.EXE
+ unknownContentType.EXE^headers^
+[browser_unknownContentType_policy.js]
+skip-if = os != 'win' # jnlp file are not considered executable on macOS or Linux
+support-files =
+ example.jnlp
+ example.jnlp^headers^
diff --git a/toolkit/mozapps/downloads/tests/browser/browser_save_wrongextension.js b/toolkit/mozapps/downloads/tests/browser/browser_save_wrongextension.js
new file mode 100644
index 0000000000..ba1b6cde5c
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/browser_save_wrongextension.js
@@ -0,0 +1,98 @@
+/* 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/. */
+
+"use strict";
+
+let url =
+ "data:text/html,<a id='link' href='http://localhost:8000/thefile.js'>Link</a>";
+
+let MockFilePicker = SpecialPowers.MockFilePicker;
+MockFilePicker.init(window);
+
+let httpServer = null;
+
+add_task(async function test() {
+ const { HttpServer } = ChromeUtils.import(
+ "resource://testing-common/httpd.js"
+ );
+
+ httpServer = new HttpServer();
+ httpServer.start(8000);
+
+ function handleRequest(aRequest, aResponse) {
+ aResponse.setStatusLine(aRequest.httpVersion, 200);
+ aResponse.setHeader("Content-Type", "text/plain");
+ aResponse.write("Some Text");
+ }
+ httpServer.registerPathHandler("/thefile.js", handleRequest);
+
+ let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
+
+ let popupShownPromise = BrowserTestUtils.waitForEvent(document, "popupshown");
+
+ await BrowserTestUtils.synthesizeMouseAtCenter(
+ "#link",
+ { type: "contextmenu", button: 2 },
+ gBrowser.selectedBrowser
+ );
+ await popupShownPromise;
+
+ let tempDir = createTemporarySaveDirectory();
+ let destFile;
+
+ MockFilePicker.displayDirectory = tempDir;
+ MockFilePicker.showCallback = fp => {
+ let fileName = fp.defaultString;
+ destFile = tempDir.clone();
+ destFile.append(fileName);
+
+ MockFilePicker.setFiles([destFile]);
+ MockFilePicker.filterIndex = 1; // kSaveAsType_URL
+ };
+
+ let transferCompletePromise = new Promise(resolve => {
+ mockTransferCallback = resolve;
+ mockTransferRegisterer.register();
+ });
+
+ registerCleanupFunction(function () {
+ mockTransferRegisterer.unregister();
+ tempDir.remove(true);
+ });
+
+ document.getElementById("context-savelink").doCommand();
+
+ let contextMenu = document.getElementById("contentAreaContextMenu");
+ let popupHiddenPromise = BrowserTestUtils.waitForEvent(
+ document,
+ "popuphidden"
+ );
+ contextMenu.hidePopup();
+ await popupHiddenPromise;
+
+ await transferCompletePromise;
+
+ is(destFile.leafName, "thefile.js", "filename extension is not modified");
+
+ BrowserTestUtils.removeTab(tab);
+});
+
+add_task(async () => {
+ MockFilePicker.cleanup();
+ await new Promise(resolve => httpServer.stop(resolve));
+});
+
+Services.scriptloader.loadSubScript(
+ "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
+ this
+);
+
+function createTemporarySaveDirectory() {
+ let saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
+ saveDir.append("testsavedir");
+ if (!saveDir.exists()) {
+ saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
+ }
+ return saveDir;
+}
diff --git a/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_blob.js b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_blob.js
new file mode 100644
index 0000000000..7b3900f46f
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_blob.js
@@ -0,0 +1,118 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const UCT_URI = "chrome://mozapps/content/downloads/unknownContentType.xhtml";
+
+async function promiseDownloadFinished(list) {
+ return new Promise(resolve => {
+ list.addView({
+ onDownloadChanged(download) {
+ info("Download changed!");
+ if (download.succeeded || download.error) {
+ info("Download succeeded or errored");
+ list.removeView(this);
+ resolve(download);
+ }
+ },
+ });
+ });
+}
+
+/**
+ * Check that both in the download "what do you want to do with this file"
+ * dialog and in the about:downloads download list, we represent blob URL
+ * download sources using the principal (origin) that generated the blob.
+ */
+add_task(async function test_check_blob_origin_representation() {
+ forcePromptForFiles("text/plain", "txt");
+
+ await check_blob_origin(
+ "https://example.org/1",
+ "example.org",
+ "example.org"
+ );
+ await check_blob_origin(
+ "data:text/html,<body>Some Text<br>",
+ "(data)",
+ "blob"
+ );
+});
+
+async function check_blob_origin(pageURL, expectedSource, expectedListOrigin) {
+ await BrowserTestUtils.withNewTab(pageURL, async browser => {
+ // Ensure we wait for the download to finish:
+ let downloadList = await Downloads.getList(Downloads.PUBLIC);
+ let downloadPromise = promiseDownloadFinished(downloadList);
+
+ // Wait for the download prompting dialog
+ let dialogPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
+ null,
+ win => win.document.documentURI == UCT_URI
+ );
+
+ // create and click an <a download> link to a txt file.
+ await SpecialPowers.spawn(browser, [], () => {
+ // Use `eval` to get a blob URL scoped to content, so that content is
+ // actually allowed to open it and so we can check the origin is correct.
+ let url = content.eval(`
+ window.foo = new Blob(["Hello"], {type: "text/plain"});
+ URL.createObjectURL(window.foo)`);
+ let link = content.document.createElement("a");
+ link.href = url;
+ link.textContent = "Click me, click me, me me me";
+ link.download = "my-file.txt";
+ content.document.body.append(link);
+ link.click();
+ });
+
+ // Check what we display in the dialog
+ let dialogWin = await dialogPromise;
+ let source = dialogWin.document.getElementById("source");
+ is(
+ source.value,
+ expectedSource,
+ "Should list origin as source if available."
+ );
+
+ // Close the dialog
+ let closedPromise = BrowserTestUtils.windowClosed(dialogWin);
+ // Ensure we're definitely saving (otherwise this depends on mime service
+ // defaults):
+ dialogWin.document.getElementById("save").click();
+ let dialogNode = dialogWin.document.querySelector("dialog");
+ dialogNode.getButton("accept").disabled = false;
+ dialogNode.acceptDialog();
+ await closedPromise;
+
+ // Wait for the download to finish and ensure it is cleared up.
+ let download = await downloadPromise;
+ registerCleanupFunction(async () => {
+ let target = download.target.path;
+ await download.finalize();
+ await IOUtils.remove(target);
+ });
+
+ // Check that the same download is displayed correctly in about:downloads.
+ await BrowserTestUtils.withNewTab("about:downloads", async dlBrowser => {
+ let doc = dlBrowser.contentDocument;
+ let listNode = doc.getElementById("downloadsListBox");
+ await BrowserTestUtils.waitForMutationCondition(
+ listNode,
+ { childList: true, subtree: true, attributeFilter: ["value"] },
+ () =>
+ listNode.firstElementChild
+ ?.querySelector(".downloadDetailsNormal")
+ ?.getAttribute("value")
+ );
+ let download = listNode.firstElementChild;
+ let detailString = download.querySelector(".downloadDetailsNormal").value;
+ Assert.stringContains(
+ detailString,
+ expectedListOrigin,
+ "Should list origin in download list if available."
+ );
+ });
+ });
+}
diff --git a/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_delayedbutton.js b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_delayedbutton.js
new file mode 100644
index 0000000000..426a740cd7
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_delayedbutton.js
@@ -0,0 +1,96 @@
+/* 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/. */
+
+const UCT_URI = "chrome://mozapps/content/downloads/unknownContentType.xhtml";
+const LOAD_URI =
+ "http://mochi.test:8888/browser/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt";
+
+const DIALOG_DELAY =
+ Services.prefs.getIntPref("security.dialog_enable_delay") + 200;
+
+let UCTObserver = {
+ opened: PromiseUtils.defer(),
+ closed: PromiseUtils.defer(),
+
+ observe(aSubject, aTopic, aData) {
+ let win = aSubject;
+
+ switch (aTopic) {
+ case "domwindowopened":
+ win.addEventListener(
+ "load",
+ function onLoad(event) {
+ // Let the dialog initialize
+ SimpleTest.executeSoon(function () {
+ UCTObserver.opened.resolve(win);
+ });
+ },
+ { once: true }
+ );
+ break;
+
+ case "domwindowclosed":
+ if (win.location == UCT_URI) {
+ this.closed.resolve();
+ }
+ break;
+ }
+ },
+};
+
+function waitDelay(delay) {
+ return new Promise((resolve, reject) => {
+ /* eslint-disable mozilla/no-arbitrary-setTimeout */
+ window.setTimeout(resolve, delay);
+ });
+}
+
+add_task(async function test_unknownContentType_delayedbutton() {
+ info("Starting browser_unknownContentType_delayedbutton.js...");
+ forcePromptForFiles("text/plain", "txt");
+
+ Services.ww.registerNotification(UCTObserver);
+
+ await BrowserTestUtils.withNewTab(
+ {
+ gBrowser,
+ url: LOAD_URI,
+ waitForLoad: false,
+ waitForStateStop: true,
+ },
+ async function () {
+ let uctWindow = await UCTObserver.opened.promise;
+ let dialog = uctWindow.document.getElementById("unknownContentType");
+ let ok = dialog.getButton("accept");
+
+ SimpleTest.is(ok.disabled, true, "button started disabled");
+
+ await waitDelay(DIALOG_DELAY);
+
+ SimpleTest.is(ok.disabled, false, "button was enabled");
+
+ let focusOutOfDialog = SimpleTest.promiseFocus(window);
+ window.focus();
+ await focusOutOfDialog;
+
+ SimpleTest.is(ok.disabled, true, "button was disabled");
+
+ let focusOnDialog = SimpleTest.promiseFocus(uctWindow);
+ uctWindow.focus();
+ await focusOnDialog;
+
+ SimpleTest.is(ok.disabled, true, "button remained disabled");
+
+ await waitDelay(DIALOG_DELAY);
+ SimpleTest.is(ok.disabled, false, "button re-enabled after delay");
+
+ dialog.cancelDialog();
+ await UCTObserver.closed.promise;
+
+ Services.ww.unregisterNotification(UCTObserver);
+ uctWindow = null;
+ UCTObserver = null;
+ }
+ );
+});
diff --git a/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_dialog_layout.js b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_dialog_layout.js
new file mode 100644
index 0000000000..577e341f90
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_dialog_layout.js
@@ -0,0 +1,103 @@
+/* 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/. */
+
+/*
+ * The unknownContentType popup can have two different layouts depending on
+ * whether a helper application can be selected or not.
+ * This tests that both layouts have correct collapsed elements.
+ */
+
+const UCT_URI = "chrome://mozapps/content/downloads/unknownContentType.xhtml";
+
+let tests = [
+ {
+ // This URL will trigger the simple UI, where only the Save an Cancel buttons are available
+ url: "http://mochi.test:8888/browser/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif",
+ elements: {
+ basicBox: { collapsed: false },
+ normalBox: { collapsed: true },
+ },
+ },
+ {
+ // This URL will trigger the full UI
+ url: "http://mochi.test:8888/browser/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt",
+ elements: {
+ basicBox: { collapsed: true },
+ normalBox: { collapsed: false },
+ },
+ },
+];
+
+add_task(async function test_unknownContentType_dialog_layout() {
+ forcePromptForFiles("text/plain", "txt");
+ forcePromptForFiles("application/octet-stream", "pif");
+
+ for (let test of tests) {
+ let UCTObserver = {
+ opened: PromiseUtils.defer(),
+ closed: PromiseUtils.defer(),
+
+ observe(aSubject, aTopic, aData) {
+ let win = aSubject;
+
+ switch (aTopic) {
+ case "domwindowopened":
+ win.addEventListener(
+ "load",
+ function onLoad(event) {
+ // Let the dialog initialize
+ SimpleTest.executeSoon(function () {
+ UCTObserver.opened.resolve(win);
+ });
+ },
+ { once: true }
+ );
+ break;
+
+ case "domwindowclosed":
+ if (win.location == UCT_URI) {
+ this.closed.resolve();
+ }
+ break;
+ }
+ },
+ };
+
+ Services.ww.registerNotification(UCTObserver);
+ await BrowserTestUtils.withNewTab(
+ {
+ gBrowser,
+ url: test.url,
+ waitForLoad: false,
+ waitForStateStop: true,
+ },
+ async function () {
+ let uctWindow = await UCTObserver.opened.promise;
+
+ for (let [id, props] of Object.entries(test.elements)) {
+ let elem = uctWindow.dialog.dialogElement(id);
+ for (let [prop, value] of Object.entries(props)) {
+ SimpleTest.is(
+ elem[prop],
+ value,
+ "Element with id " +
+ id +
+ " has property " +
+ prop +
+ " set to " +
+ value
+ );
+ }
+ }
+ let focusOnDialog = SimpleTest.promiseFocus(uctWindow);
+ uctWindow.focus();
+ await focusOnDialog;
+
+ uctWindow.document.getElementById("unknownContentType").cancelDialog();
+ uctWindow = null;
+ Services.ww.unregisterNotification(UCTObserver);
+ }
+ );
+ }
+});
diff --git a/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_extension.js b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_extension.js
new file mode 100644
index 0000000000..1bb836c1d8
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_extension.js
@@ -0,0 +1,58 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const TEST_PATH = getRootDirectory(gTestPath).replace(
+ "chrome://mochitests/content",
+ "http://example.com"
+);
+
+/**
+ * Check that case-sensitivity doesn't cause us to duplicate
+ * file name extensions.
+ */
+add_task(async function test_download_filename_extension() {
+ forcePromptForFiles("application/octet-stream", "exe");
+ let windowObserver = BrowserTestUtils.domWindowOpenedAndLoaded();
+
+ let tab = await BrowserTestUtils.openNewForegroundTab({
+ gBrowser,
+ opening: TEST_PATH + "unknownContentType.EXE",
+ waitForLoad: false,
+ });
+ let win = await windowObserver;
+
+ let list = await Downloads.getList(Downloads.ALL);
+ let downloadFinishedPromise = new Promise(resolve => {
+ list.addView({
+ onDownloadChanged(download) {
+ if (download.stopped) {
+ list.removeView(this);
+ resolve(download);
+ }
+ },
+ });
+ });
+
+ let dialog = win.document.querySelector("dialog");
+ dialog.getButton("accept").removeAttribute("disabled");
+ dialog.acceptDialog();
+ let download = await downloadFinishedPromise;
+ // We cannot assume that the filename didn't change.
+ let filename = PathUtils.filename(download.target.path);
+ Assert.ok(
+ filename.indexOf(".") == filename.lastIndexOf("."),
+ "Should not duplicate extension"
+ );
+ Assert.ok(filename.endsWith(".EXE"), "Should not change extension");
+ await list.remove(download);
+ BrowserTestUtils.removeTab(tab);
+ try {
+ await IOUtils.remove(download.target.path);
+ } catch (ex) {
+ // Ignore errors in removing the file, the system may keep it locked and
+ // it's not a critical issue.
+ info("Failed to remove the file " + ex);
+ }
+});
diff --git a/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_policy.js b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_policy.js
new file mode 100644
index 0000000000..ccb0d957bb
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/browser_unknownContentType_policy.js
@@ -0,0 +1,68 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
+ "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
+);
+
+const TEST_PATH = getRootDirectory(gTestPath).replace(
+ "chrome://mochitests/content",
+ "http://example.com"
+);
+
+/**
+ * Check that policy allows certain extensions to be launched.
+ */
+add_task(async function test_download_jnlp_policy() {
+ forcePromptForFiles("application/x-java-jnlp-file", "jnlp");
+ let windowObserver = BrowserTestUtils.domWindowOpenedAndLoaded();
+
+ let tab = await BrowserTestUtils.openNewForegroundTab({
+ gBrowser,
+ opening: TEST_PATH + "example.jnlp",
+ waitForLoad: false,
+ });
+ let win = await windowObserver;
+
+ let dialog = win.document.querySelector("dialog");
+ let normalBox = win.document.getElementById("normalBox");
+ let basicBox = win.document.getElementById("basicBox");
+ is(normalBox.collapsed, !AppConstants.IS_ESR);
+ is(basicBox.collapsed, AppConstants.IS_ESR);
+ dialog.cancelDialog();
+ BrowserTestUtils.removeTab(tab);
+
+ await EnterprisePolicyTesting.setupPolicyEngineWithJson({
+ policies: {
+ ExemptDomainFileTypePairsFromFileTypeDownloadWarnings: [
+ {
+ file_extension: "jnlp",
+ domains: ["example.com"],
+ },
+ ],
+ },
+ });
+
+ windowObserver = BrowserTestUtils.domWindowOpenedAndLoaded();
+
+ tab = await BrowserTestUtils.openNewForegroundTab({
+ gBrowser,
+ opening: TEST_PATH + "example.jnlp",
+ waitForLoad: false,
+ });
+ win = await windowObserver;
+
+ dialog = win.document.querySelector("dialog");
+ normalBox = win.document.getElementById("normalBox");
+ basicBox = win.document.getElementById("basicBox");
+ is(normalBox.collapsed, false);
+ is(basicBox.collapsed, true);
+ dialog.cancelDialog();
+ BrowserTestUtils.removeTab(tab);
+
+ await EnterprisePolicyTesting.setupPolicyEngineWithJson({
+ policies: {},
+ });
+});
diff --git a/toolkit/mozapps/downloads/tests/browser/example.jnlp b/toolkit/mozapps/downloads/tests/browser/example.jnlp
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/example.jnlp
diff --git a/toolkit/mozapps/downloads/tests/browser/example.jnlp^headers^ b/toolkit/mozapps/downloads/tests/browser/example.jnlp^headers^
new file mode 100644
index 0000000000..fac0de2095
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/example.jnlp^headers^
@@ -0,0 +1 @@
+Content-Type: application/x-java-jnlp-file
diff --git a/toolkit/mozapps/downloads/tests/browser/head.js b/toolkit/mozapps/downloads/tests/browser/head.js
new file mode 100644
index 0000000000..a5536e95b2
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/head.js
@@ -0,0 +1,17 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+function forcePromptForFiles(mime, extension) {
+ const handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].getService(
+ Ci.nsIHandlerService
+ );
+ const mimeSvc = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
+
+ let txtHandlerInfo = mimeSvc.getFromTypeAndExtension(mime, extension);
+ txtHandlerInfo.preferredAction = Ci.nsIHandlerInfo.alwaysAsk;
+ txtHandlerInfo.alwaysAskBeforeHandling = true;
+ handlerSvc.store(txtHandlerInfo);
+ registerCleanupFunction(() => handlerSvc.remove(txtHandlerInfo));
+}
diff --git a/toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE b/toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE
diff --git a/toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE^headers^ b/toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE^headers^
new file mode 100644
index 0000000000..09b22facc0
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/unknownContentType.EXE^headers^
@@ -0,0 +1 @@
+Content-Type: application/octet-stream
diff --git a/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif
new file mode 100644
index 0000000000..9353d13126
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif
@@ -0,0 +1 @@
+Dummy content for unknownContentType_dialog_layout_data.pif
diff --git a/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif^headers^ b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif^headers^
new file mode 100644
index 0000000000..09b22facc0
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.pif^headers^
@@ -0,0 +1 @@
+Content-Type: application/octet-stream
diff --git a/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt
new file mode 100644
index 0000000000..77e7195596
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt
@@ -0,0 +1 @@
+Dummy content for unknownContentType_dialog_layout_data.txt
diff --git a/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt^headers^ b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt^headers^
new file mode 100644
index 0000000000..2a3c472e26
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/browser/unknownContentType_dialog_layout_data.txt^headers^
@@ -0,0 +1,2 @@
+Content-Type: text/plain
+Content-Disposition: attachment
diff --git a/toolkit/mozapps/downloads/tests/moz.build b/toolkit/mozapps/downloads/tests/moz.build
new file mode 100644
index 0000000000..ffff033bd3
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/moz.build
@@ -0,0 +1,8 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+XPCSHELL_TESTS_MANIFESTS += ["unit/xpcshell.ini"]
+BROWSER_CHROME_MANIFESTS += ["browser/browser.ini"]
diff --git a/toolkit/mozapps/downloads/tests/unit/head_downloads.js b/toolkit/mozapps/downloads/tests/unit/head_downloads.js
new file mode 100644
index 0000000000..f3178decef
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/head_downloads.js
@@ -0,0 +1,5 @@
+registerCleanupFunction(function () {
+ Services.startup.advanceShutdownPhase(
+ Services.startup.SHUTDOWN_PHASE_APPSHUTDOWNCONFIRMED
+ );
+});
diff --git a/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
new file mode 100644
index 0000000000..956601c71e
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -0,0 +1,398 @@
+/* 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/. */
+
+const { DownloadUtils } = ChromeUtils.importESModule(
+ "resource://gre/modules/DownloadUtils.sys.mjs"
+);
+
+const gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/);
+function _(str) {
+ return str.replace(/\./g, gDecimalSymbol);
+}
+
+function testConvertByteUnits(aBytes, aValue, aUnit) {
+ let [value, unit] = DownloadUtils.convertByteUnits(aBytes);
+ Assert.equal(value, aValue);
+ Assert.equal(unit, aUnit);
+}
+
+function testTransferTotal(aCurrBytes, aMaxBytes, aTransfer) {
+ let transfer = DownloadUtils.getTransferTotal(aCurrBytes, aMaxBytes);
+ Assert.equal(transfer, aTransfer);
+}
+
+// Get the em-dash character because typing it directly here doesn't work :(
+var gDash = DownloadUtils.getDownloadStatus(0)[0].match(/left (.) 0 bytes/)[1];
+
+var gVals = [
+ 0,
+ 100,
+ 2345,
+ 55555,
+ 982341,
+ 23194134,
+ 1482,
+ 58,
+ 9921949201,
+ 13498132,
+ Infinity,
+];
+
+function testStatus(aFunc, aCurr, aMore, aRate, aTest) {
+ dump("Status Test: " + [aCurr, aMore, aRate, aTest] + "\n");
+ let curr = gVals[aCurr];
+ let max = curr + gVals[aMore];
+ let speed = gVals[aRate];
+
+ let [status, last] = aFunc(curr, max, speed);
+
+ if (0) {
+ dump(
+ "testStatus(" +
+ aCurr +
+ ", " +
+ aMore +
+ ", " +
+ aRate +
+ ', ["' +
+ status.replace(gDash, "--") +
+ '", ' +
+ last.toFixed(3) +
+ "]);\n"
+ );
+ }
+
+ // Make sure the status text matches
+ Assert.equal(status, _(aTest[0].replace(/--/, gDash)));
+
+ // Make sure the lastSeconds matches
+ if (last == Infinity) {
+ Assert.equal(last, aTest[1]);
+ } else {
+ Assert.ok(Math.abs(last - aTest[1]) < 0.1);
+ }
+}
+
+function testFormattedTimeStatus(aSec, aExpected) {
+ dump("Formatted Time Status Test: [" + aSec + "]\n");
+
+ let status = DownloadUtils.getFormattedTimeStatus(aSec);
+ dump("Formatted Time Status Test Returns: (" + status.l10n.id + ")\n");
+
+ Assert.equal(status.l10n.id, aExpected);
+}
+
+function testURI(aURI, aDisp, aHost) {
+ dump("URI Test: " + [aURI, aDisp, aHost] + "\n");
+
+ let [disp, host] = DownloadUtils.getURIHost(aURI);
+
+ // Make sure we have the right display host and full host
+ Assert.equal(disp, aDisp);
+ Assert.equal(host, aHost);
+}
+
+function testGetReadableDates(aDate, aCompactValue) {
+ const now = new Date(2000, 11, 31, 11, 59, 59);
+
+ let [dateCompact] = DownloadUtils.getReadableDates(aDate, now);
+ Assert.equal(dateCompact, aCompactValue);
+}
+
+function testAllGetReadableDates() {
+ // This test cannot depend on the current date and time, or the date format.
+ // It depends on being run with the English localization, however.
+ const today_11_30 = new Date(2000, 11, 31, 11, 30, 15);
+ const today_12_30 = new Date(2000, 11, 31, 12, 30, 15);
+ const yesterday_11_30 = new Date(2000, 11, 30, 11, 30, 15);
+ const yesterday_12_30 = new Date(2000, 11, 30, 12, 30, 15);
+ const twodaysago = new Date(2000, 11, 29, 11, 30, 15);
+ const sixdaysago = new Date(2000, 11, 25, 11, 30, 15);
+ const sevendaysago = new Date(2000, 11, 24, 11, 30, 15);
+
+ let cDtf = Services.intl.DateTimeFormat;
+
+ testGetReadableDates(
+ today_11_30,
+ new cDtf(undefined, { timeStyle: "short" }).format(today_11_30)
+ );
+ testGetReadableDates(
+ today_12_30,
+ new cDtf(undefined, { timeStyle: "short" }).format(today_12_30)
+ );
+
+ testGetReadableDates(yesterday_11_30, "Yesterday");
+ testGetReadableDates(yesterday_12_30, "Yesterday");
+ testGetReadableDates(
+ twodaysago,
+ twodaysago.toLocaleDateString(undefined, { weekday: "long" })
+ );
+ testGetReadableDates(
+ sixdaysago,
+ sixdaysago.toLocaleDateString(undefined, { weekday: "long" })
+ );
+ testGetReadableDates(
+ sevendaysago,
+ sevendaysago.toLocaleDateString(undefined, { month: "long" }) +
+ " " +
+ sevendaysago.getDate().toString().padStart(2, "0")
+ );
+
+ let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
+
+ const dtOptions = { dateStyle: "long", timeStyle: "short" };
+ Assert.equal(
+ dateTimeFull,
+ new cDtf(undefined, dtOptions).format(today_11_30)
+ );
+}
+
+function run_test() {
+ testConvertByteUnits(-1, "-1", "bytes");
+ testConvertByteUnits(1, _("1"), "bytes");
+ testConvertByteUnits(42, _("42"), "bytes");
+ testConvertByteUnits(123, _("123"), "bytes");
+ testConvertByteUnits(1024, _("1.0"), "KB");
+ testConvertByteUnits(8888, _("8.7"), "KB");
+ testConvertByteUnits(59283, _("57.9"), "KB");
+ testConvertByteUnits(640000, _("625"), "KB");
+ testConvertByteUnits(1048576, _("1.0"), "MB");
+ testConvertByteUnits(307232768, _("293"), "MB");
+ testConvertByteUnits(1073741824, _("1.0"), "GB");
+
+ testTransferTotal(1, 1, _("1 of 1 bytes"));
+ testTransferTotal(234, 4924, _("234 bytes of 4.8 KB"));
+ testTransferTotal(94923, 233923, _("92.7 of 228 KB"));
+ testTransferTotal(4924, 94923, _("4.8 of 92.7 KB"));
+ testTransferTotal(2342, 294960345, _("2.3 KB of 281 MB"));
+ testTransferTotal(234, undefined, _("234 bytes"));
+ testTransferTotal(4889023, undefined, _("4.7 MB"));
+
+ if (0) {
+ // Help find some interesting test cases
+ let r = () => Math.floor(Math.random() * 10);
+ for (let i = 0; i < 100; i++) {
+ testStatus(r(), r(), r());
+ }
+ }
+
+ // First, test with rates, via getDownloadStatus...
+ let statusFunc = DownloadUtils.getDownloadStatus.bind(DownloadUtils);
+
+ testStatus(statusFunc, 2, 1, 7, [
+ "A few seconds left -- 2.3 of 2.4 KB (58 bytes/sec)",
+ 1.724,
+ ]);
+ testStatus(statusFunc, 1, 2, 6, [
+ "A few seconds left -- 100 bytes of 2.4 KB (1.4 KB/sec)",
+ 1.582,
+ ]);
+ testStatus(statusFunc, 4, 3, 9, [
+ "A few seconds left -- 959 KB of 1.0 MB (12.9 MB/sec)",
+ 0.004,
+ ]);
+ testStatus(statusFunc, 2, 3, 8, [
+ "A few seconds left -- 2.3 of 56.5 KB (9.2 GB/sec)",
+ 0.0,
+ ]);
+
+ testStatus(statusFunc, 8, 4, 3, [
+ "17s left -- 9.2 of 9.2 GB (54.3 KB/sec)",
+ 17.682,
+ ]);
+ testStatus(statusFunc, 1, 3, 2, [
+ "23s left -- 100 bytes of 54.4 KB (2.3 KB/sec)",
+ 23.691,
+ ]);
+ testStatus(statusFunc, 9, 3, 2, [
+ "23s left -- 12.9 of 12.9 MB (2.3 KB/sec)",
+ 23.691,
+ ]);
+ testStatus(statusFunc, 5, 6, 7, [
+ "25s left -- 22.1 of 22.1 MB (58 bytes/sec)",
+ 25.552,
+ ]);
+
+ testStatus(statusFunc, 3, 9, 3, [
+ "4m left -- 54.3 KB of 12.9 MB (54.3 KB/sec)",
+ 242.969,
+ ]);
+ testStatus(statusFunc, 2, 3, 1, [
+ "9m left -- 2.3 of 56.5 KB (100 bytes/sec)",
+ 555.55,
+ ]);
+ testStatus(statusFunc, 4, 3, 7, [
+ "15m left -- 959 KB of 1.0 MB (58 bytes/sec)",
+ 957.845,
+ ]);
+ testStatus(statusFunc, 5, 3, 7, [
+ "15m left -- 22.1 of 22.2 MB (58 bytes/sec)",
+ 957.845,
+ ]);
+
+ testStatus(statusFunc, 1, 9, 2, [
+ "1h 35m left -- 100 bytes of 12.9 MB (2.3 KB/sec)",
+ 5756.133,
+ ]);
+ testStatus(statusFunc, 2, 9, 6, [
+ "2h 31m left -- 2.3 KB of 12.9 MB (1.4 KB/sec)",
+ 9108.051,
+ ]);
+ testStatus(statusFunc, 2, 4, 1, [
+ "2h 43m left -- 2.3 of 962 KB (100 bytes/sec)",
+ 9823.41,
+ ]);
+ testStatus(statusFunc, 6, 4, 7, [
+ "4h 42m left -- 1.4 of 961 KB (58 bytes/sec)",
+ 16936.914,
+ ]);
+
+ testStatus(statusFunc, 6, 9, 1, [
+ "1d 13h left -- 1.4 KB of 12.9 MB (100 bytes/sec)",
+ 134981.32,
+ ]);
+ testStatus(statusFunc, 3, 8, 3, [
+ "2d 1h left -- 54.3 KB of 9.2 GB (54.3 KB/sec)",
+ 178596.872,
+ ]);
+ testStatus(statusFunc, 1, 8, 6, [
+ "77d 11h left -- 100 bytes of 9.2 GB (1.4 KB/sec)",
+ 6694972.47,
+ ]);
+ testStatus(statusFunc, 6, 8, 7, [
+ "1,979d 22h left -- 1.4 KB of 9.2 GB (58 bytes/sec)",
+ 171068089.672,
+ ]);
+
+ testStatus(statusFunc, 0, 0, 5, [
+ "Unknown time left -- 0 of 0 bytes (22.1 MB/sec)",
+ Infinity,
+ ]);
+ testStatus(statusFunc, 0, 6, 0, [
+ "Unknown time left -- 0 bytes of 1.4 KB (0 bytes/sec)",
+ Infinity,
+ ]);
+ testStatus(statusFunc, 6, 6, 0, [
+ "Unknown time left -- 1.4 of 2.9 KB (0 bytes/sec)",
+ Infinity,
+ ]);
+ testStatus(statusFunc, 8, 5, 0, [
+ "Unknown time left -- 9.2 of 9.3 GB (0 bytes/sec)",
+ Infinity,
+ ]);
+
+ // With rate equal to Infinity
+ testStatus(statusFunc, 0, 0, 10, [
+ "Unknown time left -- 0 of 0 bytes (Really fast)",
+ Infinity,
+ ]);
+ testStatus(statusFunc, 1, 2, 10, [
+ "A few seconds left -- 100 bytes of 2.4 KB (Really fast)",
+ 0,
+ ]);
+
+ // Now test without rates, via getDownloadStatusNoRate.
+ statusFunc = DownloadUtils.getDownloadStatusNoRate.bind(DownloadUtils);
+
+ testStatus(statusFunc, 2, 1, 7, [
+ "A few seconds left -- 2.3 of 2.4 KB",
+ 1.724,
+ ]);
+ testStatus(statusFunc, 1, 2, 6, [
+ "A few seconds left -- 100 bytes of 2.4 KB",
+ 1.582,
+ ]);
+ testStatus(statusFunc, 4, 3, 9, [
+ "A few seconds left -- 959 KB of 1.0 MB",
+ 0.004,
+ ]);
+ testStatus(statusFunc, 2, 3, 8, [
+ "A few seconds left -- 2.3 of 56.5 KB",
+ 0.0,
+ ]);
+
+ testStatus(statusFunc, 8, 4, 3, ["17s left -- 9.2 of 9.2 GB", 17.682]);
+ testStatus(statusFunc, 1, 3, 2, ["23s left -- 100 bytes of 54.4 KB", 23.691]);
+ testStatus(statusFunc, 9, 3, 2, ["23s left -- 12.9 of 12.9 MB", 23.691]);
+ testStatus(statusFunc, 5, 6, 7, ["25s left -- 22.1 of 22.1 MB", 25.552]);
+
+ testStatus(statusFunc, 3, 9, 3, ["4m left -- 54.3 KB of 12.9 MB", 242.969]);
+ testStatus(statusFunc, 2, 3, 1, ["9m left -- 2.3 of 56.5 KB", 555.55]);
+ testStatus(statusFunc, 4, 3, 7, ["15m left -- 959 KB of 1.0 MB", 957.845]);
+ testStatus(statusFunc, 5, 3, 7, ["15m left -- 22.1 of 22.2 MB", 957.845]);
+
+ testStatus(statusFunc, 1, 9, 2, [
+ "1h 35m left -- 100 bytes of 12.9 MB",
+ 5756.133,
+ ]);
+ testStatus(statusFunc, 2, 9, 6, [
+ "2h 31m left -- 2.3 KB of 12.9 MB",
+ 9108.051,
+ ]);
+ testStatus(statusFunc, 2, 4, 1, ["2h 43m left -- 2.3 of 962 KB", 9823.41]);
+ testStatus(statusFunc, 6, 4, 7, ["4h 42m left -- 1.4 of 961 KB", 16936.914]);
+
+ testStatus(statusFunc, 6, 9, 1, [
+ "1d 13h left -- 1.4 KB of 12.9 MB",
+ 134981.32,
+ ]);
+ testStatus(statusFunc, 3, 8, 3, [
+ "2d 1h left -- 54.3 KB of 9.2 GB",
+ 178596.872,
+ ]);
+ testStatus(statusFunc, 1, 8, 6, [
+ "77d 11h left -- 100 bytes of 9.2 GB",
+ 6694972.47,
+ ]);
+ testStatus(statusFunc, 6, 8, 7, [
+ "1,979d 22h left -- 1.4 KB of 9.2 GB",
+ 171068089.672,
+ ]);
+
+ testStatus(statusFunc, 0, 0, 5, [
+ "Unknown time left -- 0 of 0 bytes",
+ Infinity,
+ ]);
+ testStatus(statusFunc, 0, 6, 0, [
+ "Unknown time left -- 0 bytes of 1.4 KB",
+ Infinity,
+ ]);
+ testStatus(statusFunc, 6, 6, 0, [
+ "Unknown time left -- 1.4 of 2.9 KB",
+ Infinity,
+ ]);
+ testStatus(statusFunc, 8, 5, 0, [
+ "Unknown time left -- 9.2 of 9.3 GB",
+ Infinity,
+ ]);
+
+ testFormattedTimeStatus(-1, "downloading-file-opens-in-some-time-2");
+ // Passing in null will return a status of file-opens-in-seconds, as Math.floor(null) = 0
+ testFormattedTimeStatus(null, "downloading-file-opens-in-seconds-2");
+ testFormattedTimeStatus(0, "downloading-file-opens-in-seconds-2");
+ testFormattedTimeStatus(30, "downloading-file-opens-in-seconds-2");
+
+ testURI("http://www.mozilla.org/", "mozilla.org", "www.mozilla.org");
+ testURI(
+ "http://www.city.mikasa.hokkaido.jp/",
+ "city.mikasa.hokkaido.jp",
+ "www.city.mikasa.hokkaido.jp"
+ );
+ testURI("data:text/html,Hello World", "data resource", "data resource");
+ testURI(
+ "jar:http://www.mozilla.com/file!/magic",
+ "mozilla.com",
+ "www.mozilla.com"
+ );
+ testURI("file:///C:/Cool/Stuff/", "local file", "local file");
+ // Don't test for moz-icon if we don't have a protocol handler for it (e.g. b2g):
+ if ("@mozilla.org/network/protocol;1?name=moz-icon" in Cc) {
+ testURI("moz-icon:file:///test.extension", "local file", "local file");
+ testURI("moz-icon://.extension", "moz-icon resource", "moz-icon resource");
+ }
+ testURI("about:config", "about resource", "about resource");
+ testURI("invalid.uri", "", "");
+
+ testAllGetReadableDates();
+}
diff --git a/toolkit/mozapps/downloads/tests/unit/test_lowMinutes.js b/toolkit/mozapps/downloads/tests/unit/test_lowMinutes.js
new file mode 100644
index 0000000000..786da28b75
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_lowMinutes.js
@@ -0,0 +1,56 @@
+/* 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/. */
+
+/**
+ * Test bug 448344 to make sure when we're in low minutes, we show both minutes
+ * and seconds; but continue to show only minutes when we have plenty.
+ */
+
+const { DownloadUtils } = ChromeUtils.importESModule(
+ "resource://gre/modules/DownloadUtils.sys.mjs"
+);
+
+/**
+ * Print some debug message to the console. All arguments will be printed,
+ * separated by spaces.
+ *
+ * @param [arg0, arg1, arg2, ...]
+ * Any number of arguments to print out
+ * @usage _("Hello World") -> prints "Hello World"
+ * @usage _(1, 2, 3) -> prints "1 2 3"
+ */
+var _ = function (some, debug, text, to) {
+ print(Array.from(arguments).join(" "));
+};
+
+_("Make an array of time lefts and expected string to be shown for that time");
+var expectedTimes = [
+ [1.1, "A few seconds left", "under 4sec -> few"],
+ [2.5, "A few seconds left", "under 4sec -> few"],
+ [3.9, "A few seconds left", "under 4sec -> few"],
+ [5.3, "5s left", "truncate seconds"],
+ [1.1 * 60, "1m 6s left", "under 4min -> show sec"],
+ [2.5 * 60, "2m 30s left", "under 4min -> show sec"],
+ [3.9 * 60, "3m 54s left", "under 4min -> show sec"],
+ [5.3 * 60, "5m left", "over 4min -> only show min"],
+ [1.1 * 3600, "1h 6m left", "over 1hr -> show min/sec"],
+ [2.5 * 3600, "2h 30m left", "over 1hr -> show min/sec"],
+ [3.9 * 3600, "3h 54m left", "over 1hr -> show min/sec"],
+ [5.3 * 3600, "5h 18m left", "over 1hr -> show min/sec"],
+];
+_(expectedTimes.join("\n"));
+
+function run_test() {
+ expectedTimes.forEach(function ([time, expectStatus, comment]) {
+ _("Running test with time", time);
+ _("Test comment:", comment);
+ let [status, last] = DownloadUtils.getTimeLeft(time);
+
+ _("Got status:", status, "last:", last);
+ _("Expecting..", expectStatus);
+ Assert.equal(status, expectStatus);
+
+ _();
+ });
+}
diff --git a/toolkit/mozapps/downloads/tests/unit/test_syncedDownloadUtils.js b/toolkit/mozapps/downloads/tests/unit/test_syncedDownloadUtils.js
new file mode 100644
index 0000000000..d60baee447
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_syncedDownloadUtils.js
@@ -0,0 +1,28 @@
+/* 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/. */
+
+/**
+ * Test bug 420482 by making sure multiple consumers of DownloadUtils gets the
+ * same time remaining time if they provide the same time left but a different
+ * "last time".
+ */
+
+const { DownloadUtils } = ChromeUtils.importESModule(
+ "resource://gre/modules/DownloadUtils.sys.mjs"
+);
+
+function run_test() {
+ // Simulate having multiple downloads requesting time left
+ let downloadTimes = {};
+ for (let time of [1, 30, 60, 3456, 9999]) {
+ downloadTimes[time] = DownloadUtils.getTimeLeft(time)[0];
+ }
+
+ // Pretend we're a download status bar also asking for a time left, but we're
+ // using a different "last sec". We need to make sure we get the same time.
+ let lastSec = 314;
+ for (let [time, text] of Object.entries(downloadTimes)) {
+ Assert.equal(DownloadUtils.getTimeLeft(time, lastSec)[0], text);
+ }
+}
diff --git a/toolkit/mozapps/downloads/tests/unit/test_unspecified_arguments.js b/toolkit/mozapps/downloads/tests/unit/test_unspecified_arguments.js
new file mode 100644
index 0000000000..eb512d93ab
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_unspecified_arguments.js
@@ -0,0 +1,33 @@
+/* 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 passing null and nothing to various variable-arg DownloadUtils
+ * methods provide the same result.
+ */
+
+const { DownloadUtils } = ChromeUtils.importESModule(
+ "resource://gre/modules/DownloadUtils.sys.mjs"
+);
+
+function run_test() {
+ Assert.equal(
+ DownloadUtils.getDownloadStatus(1000, null, null, null) + "",
+ DownloadUtils.getDownloadStatus(1000) + ""
+ );
+ Assert.equal(
+ DownloadUtils.getDownloadStatus(1000, null, null) + "",
+ DownloadUtils.getDownloadStatus(1000, null) + ""
+ );
+
+ Assert.equal(
+ DownloadUtils.getTransferTotal(1000, null) + "",
+ DownloadUtils.getTransferTotal(1000) + ""
+ );
+
+ Assert.equal(
+ DownloadUtils.getTimeLeft(1000, null) + "",
+ DownloadUtils.getTimeLeft(1000) + ""
+ );
+}
diff --git a/toolkit/mozapps/downloads/tests/unit/xpcshell.ini b/toolkit/mozapps/downloads/tests/unit/xpcshell.ini
new file mode 100644
index 0000000000..703c84152d
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/xpcshell.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+head = head_downloads.js
+
+[test_DownloadUtils.js]
+[test_lowMinutes.js]
+[test_syncedDownloadUtils.js]
+[test_unspecified_arguments.js]