summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/downloads/tests
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/mozapps/downloads/tests
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/mozapps/downloads/tests')
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/.eslintrc.js7
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/chrome.ini10
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_delayedbutton.xul117
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_dialog_layout.xul108
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif1
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif^headers^1
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt1
-rw-r--r--toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt^headers^2
-rw-r--r--toolkit/mozapps/downloads/tests/moz.build8
-rw-r--r--toolkit/mozapps/downloads/tests/unit/.eslintrc.js7
-rw-r--r--toolkit/mozapps/downloads/tests/unit/head_downloads.js5
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js131
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js237
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_lowMinutes.js55
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_syncedDownloadUtils.js26
-rw-r--r--toolkit/mozapps/downloads/tests/unit/test_unspecified_arguments.js25
-rw-r--r--toolkit/mozapps/downloads/tests/unit/xpcshell.ini10
17 files changed, 751 insertions, 0 deletions
diff --git a/toolkit/mozapps/downloads/tests/chrome/.eslintrc.js b/toolkit/mozapps/downloads/tests/chrome/.eslintrc.js
new file mode 100644
index 000000000..8c0f4f574
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/.eslintrc.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = {
+ "extends": [
+ "../../../../../testing/mochitest/chrome.eslintrc.js"
+ ]
+};
diff --git a/toolkit/mozapps/downloads/tests/chrome/chrome.ini b/toolkit/mozapps/downloads/tests/chrome/chrome.ini
new file mode 100644
index 000000000..b5a29cb45
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/chrome.ini
@@ -0,0 +1,10 @@
+[DEFAULT]
+skip-if = os == 'android'
+support-files =
+ unknownContentType_dialog_layout_data.pif
+ unknownContentType_dialog_layout_data.pif^headers^
+ unknownContentType_dialog_layout_data.txt
+ unknownContentType_dialog_layout_data.txt^headers^
+
+[test_unknownContentType_delayedbutton.xul]
+[test_unknownContentType_dialog_layout.xul]
diff --git a/toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_delayedbutton.xul b/toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_delayedbutton.xul
new file mode 100644
index 000000000..9bbec0f92
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_delayedbutton.xul
@@ -0,0 +1,117 @@
+<?xml version="1.0"?>
+<!-- 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.
+-->
+
+<window title="Unknown Content Type Dialog Test"
+ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
+ onload="doTest()">
+
+ <script type="application/javascript"
+ src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+
+ <script type="application/javascript"><![CDATA[
+ const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
+
+ Cu.import("resource://gre/modules/Services.jsm");
+ Cu.import("resource://gre/modules/Task.jsm");
+ Cu.import("resource://gre/modules/Promise.jsm");
+
+ const UCT_URI = "chrome://mozapps/content/downloads/unknownContentType.xul";
+ const LOAD_URI = "http://mochi.test:8888/chrome/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt";
+
+ const DIALOG_DELAY = Services.prefs.getIntPref("security.dialog_enable_delay") + 200;
+
+ let UCTObserver = {
+ opened: Promise.defer(),
+ closed: Promise.defer(),
+
+ observe: function(aSubject, aTopic, aData) {
+ let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
+
+ switch (aTopic) {
+ case "domwindowopened":
+ win.addEventListener("load", function onLoad(event) {
+ win.removeEventListener("load", onLoad, false);
+
+ // Let the dialog initialize
+ SimpleTest.executeSoon(function() {
+ UCTObserver.opened.resolve(win);
+ });
+ }, false);
+ break;
+
+ case "domwindowclosed":
+ if (win.location == UCT_URI) {
+ this.closed.resolve();
+ }
+ break;
+ }
+ }
+ };
+
+ Services.ww.registerNotification(UCTObserver);
+ SimpleTest.waitForExplicitFinish();
+ SimpleTest.requestFlakyTimeout("This test is testing a timing-based feature, so it really needs to wait a certain amount of time to verify that the feature worked.");
+
+ function waitDelay(delay) {
+ return new Promise((resolve, reject) => {
+ window.setTimeout(resolve, delay);
+ });
+ }
+
+ function doTest() {
+ Task.spawn(function test_aboutCrashed() {
+ let frame = document.getElementById("testframe");
+ frame.setAttribute("src", LOAD_URI);
+
+ let uctWindow = yield UCTObserver.opened.promise;
+ let ok = uctWindow.document.documentElement.getButton("accept");
+
+ SimpleTest.is(ok.disabled, true, "button started disabled");
+
+ yield waitDelay(DIALOG_DELAY);
+
+ SimpleTest.is(ok.disabled, false, "button was enabled");
+
+ focusOutOfDialog = SimpleTest.promiseFocus(window);
+ window.focus();
+ yield focusOutOfDialog;
+
+ SimpleTest.is(ok.disabled, true, "button was disabled");
+
+ focusOnDialog = SimpleTest.promiseFocus(uctWindow);
+ uctWindow.focus();
+ yield focusOnDialog;
+
+ SimpleTest.is(ok.disabled, true, "button remained disabled");
+
+ yield waitDelay(DIALOG_DELAY);
+ SimpleTest.is(ok.disabled, false, "button re-enabled after delay");
+
+ uctWindow.document.documentElement.cancelDialog();
+ yield UCTObserver.closed.promise;
+
+ Services.ww.unregisterNotification(UCTObserver);
+ uctWindow = null;
+ UCTObserver = null;
+ SimpleTest.finish();
+ });
+ }
+ ]]></script>
+
+ <body xmlns="http://www.w3.org/1999/xhtml">
+ <p id="display"></p>
+ <div id="content" style="display:none;"></div>
+ <pre id="test"></pre>
+ </body>
+
+ <iframe xmlns="http://www.w3.org/1999/xhtml"
+ id="testframe">
+ </iframe>
+</window>
diff --git a/toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_dialog_layout.xul b/toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_dialog_layout.xul
new file mode 100644
index 000000000..1210b908d
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/test_unknownContentType_dialog_layout.xul
@@ -0,0 +1,108 @@
+<?xml version="1.0"?>
+<!-- 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.
+-->
+
+<window title="Unknown Content Type Dialog Test"
+ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
+ onload="init()">
+
+ <script type="application/javascript"
+ src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+
+ <script type="application/javascript">
+ <![CDATA[
+const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
+
+const UCT_URI = "chrome://mozapps/content/downloads/unknownContentType.xul";
+
+let testIndex = -1;
+let tests = [
+ { // This URL will trigger the simple UI, where only the Save an Cancel buttons are available
+ url: "http://mochi.test:8888/chrome/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif",
+ elements: {
+ basicBox: { collapsed: false },
+ normalBox: { collapsed: true }
+ }
+ },
+ { // This URL will trigger the full UI
+ url: "http://mochi.test:8888/chrome/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt",
+ elements: {
+ basicBox: { collapsed: true },
+ normalBox: { collapsed: false }
+ }
+ }
+];
+
+let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
+ getService(Ci.nsIWindowWatcher);
+
+SimpleTest.waitForExplicitFinish();
+
+let windowObserver = {
+ observe: function(aSubject, aTopic, aData) {
+ let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
+
+ if (aTopic == "domwindowclosed") {
+ if (win.location == UCT_URI)
+ loadNextTest();
+ return;
+ }
+
+ // domwindowopened
+ win.addEventListener("load", function onLoad(event) {
+ win.removeEventListener("load", onLoad, false);
+
+ // Let the dialog initialize
+ SimpleTest.executeSoon(function() {
+ checkWindow(win);
+ });
+ }, false);
+ }
+};
+
+function init() {
+ ww.registerNotification(windowObserver);
+ loadNextTest();
+}
+
+function loadNextTest() {
+ if (!tests[++testIndex]) {
+ ww.unregisterNotification(windowObserver);
+ SimpleTest.finish();
+ return;
+ }
+ let frame = document.getElementById("testframe");
+ frame.setAttribute("src", tests[testIndex].url);
+}
+
+function checkWindow(win) {
+ for (let [id, props] of Object.entries(tests[testIndex].elements)) {
+ let elem = win.dialog.dialogElement(id);
+ for (let [prop, value] of Object.entries(props)) {
+ is(elem[prop], value,
+ "Element with id " + id + " has property " +
+ prop + " set to " + value);
+ }
+ }
+ win.document.documentElement.cancelDialog();
+}
+
+ ]]>
+ </script>
+
+ <body xmlns="http://www.w3.org/1999/xhtml">
+ <p id="display"></p>
+ <div id="content" style="display:none;"></div>
+ <pre id="test"></pre>
+ </body>
+
+ <iframe xmlns="http://www.w3.org/1999/xhtml"
+ id="testframe">
+ </iframe>
+</window>
diff --git a/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif b/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif
new file mode 100644
index 000000000..9353d1312
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif
@@ -0,0 +1 @@
+Dummy content for unknownContentType_dialog_layout_data.pif
diff --git a/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif^headers^ b/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif^headers^
new file mode 100644
index 000000000..09b22facc
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.pif^headers^
@@ -0,0 +1 @@
+Content-Type: application/octet-stream
diff --git a/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt b/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt
new file mode 100644
index 000000000..77e719559
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt
@@ -0,0 +1 @@
+Dummy content for unknownContentType_dialog_layout_data.txt
diff --git a/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt^headers^ b/toolkit/mozapps/downloads/tests/chrome/unknownContentType_dialog_layout_data.txt^headers^
new file mode 100644
index 000000000..2a3c472e2
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/chrome/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 000000000..a4b1efb9a
--- /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']
+MOCHITEST_CHROME_MANIFESTS += ['chrome/chrome.ini']
diff --git a/toolkit/mozapps/downloads/tests/unit/.eslintrc.js b/toolkit/mozapps/downloads/tests/unit/.eslintrc.js
new file mode 100644
index 000000000..d35787cd2
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/.eslintrc.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = {
+ "extends": [
+ "../../../../../testing/xpcshell/xpcshell.eslintrc.js"
+ ]
+};
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 000000000..4f199e5cf
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/head_downloads.js
@@ -0,0 +1,5 @@
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+do_register_cleanup(function() {
+ Services.obs.notifyObservers(null, "quit-application", null);
+});
diff --git a/toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js b/toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js
new file mode 100644
index 000000000..77249169d
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadPaths.js
@@ -0,0 +1,131 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
+/* ***** BEGIN LICENSE BLOCK *****
+ *
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+/**
+ * Tests for the "DownloadPaths.jsm" JavaScript module.
+ */
+
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var Cu = Components.utils;
+var Cr = Components.results;
+
+Cu.import("resource://gre/modules/DownloadPaths.jsm");
+
+/**
+ * Provides a temporary save directory.
+ *
+ * @returns nsIFile pointing to the new or existing directory.
+ */
+function createTemporarySaveDirectory()
+{
+ var saveDir = Cc["@mozilla.org/file/directory_service;1"].
+ getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
+ saveDir.append("testsavedir");
+ if (!saveDir.exists()) {
+ saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
+ }
+ return saveDir;
+}
+
+function testSplitBaseNameAndExtension(aLeafName, [aBase, aExt])
+{
+ var [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
+ do_check_eq(base, aBase);
+ do_check_eq(ext, aExt);
+
+ // If we modify the base name and concatenate it with the extension again,
+ // another roundtrip through the function should give a consistent result.
+ // The only exception is when we introduce an extension in a file name that
+ // didn't have one or that ended with one of the special cases like ".gz". If
+ // we avoid using a dot and we introduce at least another special character,
+ // the results are always consistent.
+ [base, ext] = DownloadPaths.splitBaseNameAndExtension("(" + base + ")" + ext);
+ do_check_eq(base, "(" + aBase + ")");
+ do_check_eq(ext, aExt);
+}
+
+function testCreateNiceUniqueFile(aTempFile, aExpectedLeafName)
+{
+ var createdFile = DownloadPaths.createNiceUniqueFile(aTempFile);
+ do_check_eq(createdFile.leafName, aExpectedLeafName);
+}
+
+function run_test()
+{
+ // Usual file names.
+ testSplitBaseNameAndExtension("base", ["base", ""]);
+ testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]);
+ testSplitBaseNameAndExtension("base.application", ["base", ".application"]);
+ testSplitBaseNameAndExtension("base.x.Z", ["base", ".x.Z"]);
+ testSplitBaseNameAndExtension("base.ext.Z", ["base", ".ext.Z"]);
+ testSplitBaseNameAndExtension("base.ext.gz", ["base", ".ext.gz"]);
+ testSplitBaseNameAndExtension("base.ext.Bz2", ["base", ".ext.Bz2"]);
+ testSplitBaseNameAndExtension("base..ext", ["base.", ".ext"]);
+ testSplitBaseNameAndExtension("base..Z", ["base.", ".Z"]);
+ testSplitBaseNameAndExtension("base. .Z", ["base. ", ".Z"]);
+ testSplitBaseNameAndExtension("base.base.Bz2", ["base.base", ".Bz2"]);
+ testSplitBaseNameAndExtension("base .ext", ["base ", ".ext"]);
+
+ // Corner cases. A name ending with a dot technically has no extension, but
+ // we consider the ending dot separately from the base name so that modifying
+ // the latter never results in an extension being introduced accidentally.
+ // Names beginning with a dot are hidden files on Unix-like platforms and if
+ // their name doesn't contain another dot they should have no extension, but
+ // on Windows the whole name is considered as an extension.
+ testSplitBaseNameAndExtension("base.", ["base", "."]);
+ testSplitBaseNameAndExtension(".ext", ["", ".ext"]);
+
+ // Unusual file names (not recommended as input to the function).
+ testSplitBaseNameAndExtension("base. ", ["base", ". "]);
+ testSplitBaseNameAndExtension("base ", ["base ", ""]);
+ testSplitBaseNameAndExtension("", ["", ""]);
+ testSplitBaseNameAndExtension(" ", [" ", ""]);
+ testSplitBaseNameAndExtension(" . ", [" ", ". "]);
+ testSplitBaseNameAndExtension(" .. ", [" .", ". "]);
+ testSplitBaseNameAndExtension(" .ext", [" ", ".ext"]);
+ testSplitBaseNameAndExtension(" .ext. ", [" .ext", ". "]);
+ testSplitBaseNameAndExtension(" .ext.gz ", [" .ext", ".gz "]);
+
+ var destDir = createTemporarySaveDirectory();
+ try {
+ // Single extension.
+ var tempFile = destDir.clone();
+ tempFile.append("test.txt");
+ testCreateNiceUniqueFile(tempFile, "test.txt");
+ testCreateNiceUniqueFile(tempFile, "test(1).txt");
+ testCreateNiceUniqueFile(tempFile, "test(2).txt");
+
+ // Double extension.
+ tempFile.leafName = "test.tar.gz";
+ testCreateNiceUniqueFile(tempFile, "test.tar.gz");
+ testCreateNiceUniqueFile(tempFile, "test(1).tar.gz");
+ testCreateNiceUniqueFile(tempFile, "test(2).tar.gz");
+
+ // Test automatic shortening of long file names. We don't know exactly how
+ // many characters are removed, because it depends on the name of the folder
+ // where the file is located.
+ tempFile.leafName = new Array(256).join("T") + ".txt";
+ var newFile = DownloadPaths.createNiceUniqueFile(tempFile);
+ do_check_true(newFile.leafName.length < tempFile.leafName.length);
+ do_check_eq(newFile.leafName.slice(-4), ".txt");
+
+ // Creating a valid file name from an invalid one is not always possible.
+ tempFile.append("file-under-long-directory.txt");
+ try {
+ DownloadPaths.createNiceUniqueFile(tempFile);
+ do_throw("Exception expected with a long parent directory name.")
+ } catch (e) {
+ // An exception is expected, but we don't know which one exactly.
+ }
+ } finally {
+ // Clean up the temporary directory.
+ destDir.remove(true);
+ }
+}
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 000000000..11e7776a7
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -0,0 +1,237 @@
+/* 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/. */
+
+var Cu = Components.utils;
+Cu.import("resource://gre/modules/DownloadUtils.jsm");
+
+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);
+ do_check_eq(value, aValue);
+ do_check_eq(unit, aUnit);
+}
+
+function testTransferTotal(aCurrBytes, aMaxBytes, aTransfer)
+{
+ let transfer = DownloadUtils.getTransferTotal(aCurrBytes, aMaxBytes);
+ do_check_eq(transfer, aTransfer);
+}
+
+// Get the em-dash character because typing it directly here doesn't work :(
+var gDash = DownloadUtils.getDownloadStatus(0)[0].match(/remaining (.) 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
+ do_check_eq(status, _(aTest[0].replace(/--/, gDash)));
+
+ // Make sure the lastSeconds matches
+ if (last == Infinity)
+ do_check_eq(last, aTest[1]);
+ else
+ do_check_true(Math.abs(last - aTest[1]) < .1);
+}
+
+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
+ do_check_eq(disp, aDisp);
+ do_check_eq(host, aHost);
+}
+
+
+function testGetReadableDates(aDate, aCompactValue)
+{
+ const now = new Date(2000, 11, 31, 11, 59, 59);
+
+ let [dateCompact] = DownloadUtils.getReadableDates(aDate, now);
+ do_check_eq(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);
+
+ // TODO: Remove Intl fallback when bug 1215247 is fixed.
+ const locale = typeof Intl === "undefined"
+ ? undefined
+ : Components.classes["@mozilla.org/chrome/chrome-registry;1"]
+ .getService(Components.interfaces.nsIXULChromeRegistry)
+ .getSelectedLocale("global", true);
+
+ let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].
+ getService(Components.interfaces.nsIScriptableDateFormat);
+
+ testGetReadableDates(today_11_30, dts.FormatTime("", dts.timeFormatNoSeconds,
+ 11, 30, 0));
+ testGetReadableDates(today_12_30, dts.FormatTime("", dts.timeFormatNoSeconds,
+ 12, 30, 0));
+ testGetReadableDates(yesterday_11_30, "Yesterday");
+ testGetReadableDates(yesterday_12_30, "Yesterday");
+ testGetReadableDates(twodaysago,
+ typeof Intl === "undefined"
+ ? twodaysago.toLocaleFormat("%A")
+ : twodaysago.toLocaleDateString(locale, { weekday: "long" }));
+ testGetReadableDates(sixdaysago,
+ typeof Intl === "undefined"
+ ? sixdaysago.toLocaleFormat("%A")
+ : sixdaysago.toLocaleDateString(locale, { weekday: "long" }));
+ testGetReadableDates(sevendaysago,
+ (typeof Intl === "undefined"
+ ? sevendaysago.toLocaleFormat("%B")
+ : sevendaysago.toLocaleDateString(locale, { month: "long" })) + " " +
+ sevendaysago.getDate().toString().padStart(2, "0"));
+
+ let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
+ do_check_eq(dateTimeFull, dts.FormatDateTime("", dts.dateFormatLong,
+ dts.timeFormatNoSeconds,
+ 2000, 12, 31, 11, 30, 0));
+}
+
+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 remaining -- 2.3 of 2.4 KB (58 bytes/sec)", 1.724]);
+ testStatus(statusFunc, 1, 2, 6, ["A few seconds remaining -- 100 bytes of 2.4 KB (1.4 KB/sec)", 1.582]);
+ testStatus(statusFunc, 4, 3, 9, ["A few seconds remaining -- 959 KB of 1.0 MB (12.9 MB/sec)", 0.004]);
+ testStatus(statusFunc, 2, 3, 8, ["A few seconds remaining -- 2.3 of 56.5 KB (9.2 GB/sec)", 0.000]);
+
+ testStatus(statusFunc, 8, 4, 3, ["17 seconds remaining -- 9.2 of 9.2 GB (54.3 KB/sec)", 17.682]);
+ testStatus(statusFunc, 1, 3, 2, ["23 seconds remaining -- 100 bytes of 54.4 KB (2.3 KB/sec)", 23.691]);
+ testStatus(statusFunc, 9, 3, 2, ["23 seconds remaining -- 12.9 of 12.9 MB (2.3 KB/sec)", 23.691]);
+ testStatus(statusFunc, 5, 6, 7, ["25 seconds remaining -- 22.1 of 22.1 MB (58 bytes/sec)", 25.552]);
+
+ testStatus(statusFunc, 3, 9, 3, ["4 minutes remaining -- 54.3 KB of 12.9 MB (54.3 KB/sec)", 242.969]);
+ testStatus(statusFunc, 2, 3, 1, ["9 minutes remaining -- 2.3 of 56.5 KB (100 bytes/sec)", 555.550]);
+ testStatus(statusFunc, 4, 3, 7, ["15 minutes remaining -- 959 KB of 1.0 MB (58 bytes/sec)", 957.845]);
+ testStatus(statusFunc, 5, 3, 7, ["15 minutes remaining -- 22.1 of 22.2 MB (58 bytes/sec)", 957.845]);
+
+ testStatus(statusFunc, 1, 9, 2, ["1 hour, 35 minutes remaining -- 100 bytes of 12.9 MB (2.3 KB/sec)", 5756.133]);
+ testStatus(statusFunc, 2, 9, 6, ["2 hours, 31 minutes remaining -- 2.3 KB of 12.9 MB (1.4 KB/sec)", 9108.051]);
+ testStatus(statusFunc, 2, 4, 1, ["2 hours, 43 minutes remaining -- 2.3 of 962 KB (100 bytes/sec)", 9823.410]);
+ testStatus(statusFunc, 6, 4, 7, ["4 hours, 42 minutes remaining -- 1.4 of 961 KB (58 bytes/sec)", 16936.914]);
+
+ testStatus(statusFunc, 6, 9, 1, ["1 day, 13 hours remaining -- 1.4 KB of 12.9 MB (100 bytes/sec)", 134981.320]);
+ testStatus(statusFunc, 3, 8, 3, ["2 days, 1 hour remaining -- 54.3 KB of 9.2 GB (54.3 KB/sec)", 178596.872]);
+ testStatus(statusFunc, 1, 8, 6, ["77 days, 11 hours remaining -- 100 bytes of 9.2 GB (1.4 KB/sec)", 6694972.470]);
+ testStatus(statusFunc, 6, 8, 7, ["1979 days, 22 hours remaining -- 1.4 KB of 9.2 GB (58 bytes/sec)", 171068089.672]);
+
+ testStatus(statusFunc, 0, 0, 5, ["Unknown time remaining -- 0 of 0 bytes (22.1 MB/sec)", Infinity]);
+ testStatus(statusFunc, 0, 6, 0, ["Unknown time remaining -- 0 bytes of 1.4 KB (0 bytes/sec)", Infinity]);
+ testStatus(statusFunc, 6, 6, 0, ["Unknown time remaining -- 1.4 of 2.9 KB (0 bytes/sec)", Infinity]);
+ testStatus(statusFunc, 8, 5, 0, ["Unknown time remaining -- 9.2 of 9.3 GB (0 bytes/sec)", Infinity]);
+
+ // With rate equal to Infinity
+ testStatus(statusFunc, 0, 0, 10, ["Unknown time remaining -- 0 of 0 bytes (Really fast)", Infinity]);
+ testStatus(statusFunc, 1, 2, 10, ["A few seconds remaining -- 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 remaining -- 2.3 of 2.4 KB", 1.724]);
+ testStatus(statusFunc, 1, 2, 6, ["A few seconds remaining -- 100 bytes of 2.4 KB", 1.582]);
+ testStatus(statusFunc, 4, 3, 9, ["A few seconds remaining -- 959 KB of 1.0 MB", 0.004]);
+ testStatus(statusFunc, 2, 3, 8, ["A few seconds remaining -- 2.3 of 56.5 KB", 0.000]);
+
+ testStatus(statusFunc, 8, 4, 3, ["17 seconds remaining -- 9.2 of 9.2 GB", 17.682]);
+ testStatus(statusFunc, 1, 3, 2, ["23 seconds remaining -- 100 bytes of 54.4 KB", 23.691]);
+ testStatus(statusFunc, 9, 3, 2, ["23 seconds remaining -- 12.9 of 12.9 MB", 23.691]);
+ testStatus(statusFunc, 5, 6, 7, ["25 seconds remaining -- 22.1 of 22.1 MB", 25.552]);
+
+ testStatus(statusFunc, 3, 9, 3, ["4 minutes remaining -- 54.3 KB of 12.9 MB", 242.969]);
+ testStatus(statusFunc, 2, 3, 1, ["9 minutes remaining -- 2.3 of 56.5 KB", 555.550]);
+ testStatus(statusFunc, 4, 3, 7, ["15 minutes remaining -- 959 KB of 1.0 MB", 957.845]);
+ testStatus(statusFunc, 5, 3, 7, ["15 minutes remaining -- 22.1 of 22.2 MB", 957.845]);
+
+ testStatus(statusFunc, 1, 9, 2, ["1 hour, 35 minutes remaining -- 100 bytes of 12.9 MB", 5756.133]);
+ testStatus(statusFunc, 2, 9, 6, ["2 hours, 31 minutes remaining -- 2.3 KB of 12.9 MB", 9108.051]);
+ testStatus(statusFunc, 2, 4, 1, ["2 hours, 43 minutes remaining -- 2.3 of 962 KB", 9823.410]);
+ testStatus(statusFunc, 6, 4, 7, ["4 hours, 42 minutes remaining -- 1.4 of 961 KB", 16936.914]);
+
+ testStatus(statusFunc, 6, 9, 1, ["1 day, 13 hours remaining -- 1.4 KB of 12.9 MB", 134981.320]);
+ testStatus(statusFunc, 3, 8, 3, ["2 days, 1 hour remaining -- 54.3 KB of 9.2 GB", 178596.872]);
+ testStatus(statusFunc, 1, 8, 6, ["77 days, 11 hours remaining -- 100 bytes of 9.2 GB", 6694972.470]);
+ testStatus(statusFunc, 6, 8, 7, ["1979 days, 22 hours remaining -- 1.4 KB of 9.2 GB", 171068089.672]);
+
+ testStatus(statusFunc, 0, 0, 5, ["Unknown time remaining -- 0 of 0 bytes", Infinity]);
+ testStatus(statusFunc, 0, 6, 0, ["Unknown time remaining -- 0 bytes of 1.4 KB", Infinity]);
+ testStatus(statusFunc, 6, 6, 0, ["Unknown time remaining -- 1.4 of 2.9 KB", Infinity]);
+ testStatus(statusFunc, 8, 5, 0, ["Unknown time remaining -- 9.2 of 9.3 GB", Infinity]);
+
+ 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 Components.classes) {
+ 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 000000000..75eff3370
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_lowMinutes.js
@@ -0,0 +1,55 @@
+/* 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.
+ */
+
+Components.utils.import("resource://gre/modules/DownloadUtils.jsm");
+
+/**
+ * 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.slice(arguments).join(" "));
+};
+
+_("Make an array of time lefts and expected string to be shown for that time");
+var expectedTimes = [
+ [1.1, "A few seconds remaining", "under 4sec -> few"],
+ [2.5, "A few seconds remaining", "under 4sec -> few"],
+ [3.9, "A few seconds remaining", "under 4sec -> few"],
+ [5.3, "5 seconds remaining", "truncate seconds"],
+ [1.1 * 60, "1 minute, 6 seconds remaining", "under 4min -> show sec"],
+ [2.5 * 60, "2 minutes, 30 seconds remaining", "under 4min -> show sec"],
+ [3.9 * 60, "3 minutes, 54 seconds remaining", "under 4min -> show sec"],
+ [5.3 * 60, "5 minutes remaining", "over 4min -> only show min"],
+ [1.1 * 3600, "1 hour, 6 minutes remaining", "over 1hr -> show min/sec"],
+ [2.5 * 3600, "2 hours, 30 minutes remaining", "over 1hr -> show min/sec"],
+ [3.9 * 3600, "3 hours, 54 minutes remaining", "over 1hr -> show min/sec"],
+ [5.3 * 3600, "5 hours, 18 minutes remaining", "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);
+ do_check_eq(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 000000000..86d810a9b
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_syncedDownloadUtils.js
@@ -0,0 +1,26 @@
+/* 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".
+ */
+
+var Cu = Components.utils;
+Cu.import("resource://gre/modules/DownloadUtils.jsm");
+
+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))
+ do_check_eq(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 000000000..02e27c92c
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/test_unspecified_arguments.js
@@ -0,0 +1,25 @@
+/* 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.
+ */
+
+var Cu = Components.utils;
+Cu.import("resource://gre/modules/DownloadUtils.jsm");
+
+function run_test()
+{
+ do_check_eq(DownloadUtils.getDownloadStatus(1000, null, null, null) + "",
+ DownloadUtils.getDownloadStatus(1000) + "");
+ do_check_eq(DownloadUtils.getDownloadStatus(1000, null, null) + "",
+ DownloadUtils.getDownloadStatus(1000, null) + "");
+
+ do_check_eq(DownloadUtils.getTransferTotal(1000, null) + "",
+ DownloadUtils.getTransferTotal(1000) + "");
+
+ do_check_eq(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 000000000..877816ef6
--- /dev/null
+++ b/toolkit/mozapps/downloads/tests/unit/xpcshell.ini
@@ -0,0 +1,10 @@
+[DEFAULT]
+head = head_downloads.js
+tail =
+skip-if = toolkit == 'android'
+
+[test_DownloadPaths.js]
+[test_DownloadUtils.js]
+[test_lowMinutes.js]
+[test_syncedDownloadUtils.js]
+[test_unspecified_arguments.js]