summaryrefslogtreecommitdiffstats
path: root/dom/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 /dom/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 'dom/downloads/tests')
-rw-r--r--dom/downloads/tests/clear_all_done_helper.js67
-rw-r--r--dom/downloads/tests/mochitest.ini15
-rw-r--r--dom/downloads/tests/serve_file.sjs170
-rw-r--r--dom/downloads/tests/test_downloads_bad_file.html93
-rw-r--r--dom/downloads/tests/test_downloads_basic.html128
-rw-r--r--dom/downloads/tests/test_downloads_large.html110
-rw-r--r--dom/downloads/tests/test_downloads_navigator_object.html75
-rw-r--r--dom/downloads/tests/test_downloads_pause_remove.html117
-rw-r--r--dom/downloads/tests/test_downloads_pause_resume.html121
9 files changed, 896 insertions, 0 deletions
diff --git a/dom/downloads/tests/clear_all_done_helper.js b/dom/downloads/tests/clear_all_done_helper.js
new file mode 100644
index 000000000..62fa1a2f3
--- /dev/null
+++ b/dom/downloads/tests/clear_all_done_helper.js
@@ -0,0 +1,67 @@
+/**
+ * A helper to clear out the existing downloads known to the mozDownloadManager
+ * / downloads.js.
+ *
+ * It exists because previously mozDownloadManager.clearAllDone() thought that
+ * when it returned that all the completed downloads would be cleared out. It
+ * was wrong and this led to various intermittent test failurse. In discussion
+ * on https://bugzil.la/979446#c13 and onwards, it was decided that
+ * clearAllDone() was in the wrong and that the jsdownloads API it depends on
+ * was not going to change to make it be in the right.
+ *
+ * The existing uses of clearAllDone() in tests seemed to be about:
+ * - Exploding if there was somehow still a download in progress
+ * - Clearing out the download list at the start of a test so that calls to
+ * getDownloads() wouldn't have to worry about existing downloads, etc.
+ *
+ * From discussion, the right way to handle clearing is to wait for the expected
+ * removal events to occur for the existing downloads. So that's what we do.
+ * We still generate a test failure if there are any in-progress downloads.
+ *
+ * @param {Boolean} [getDownloads=false]
+ * If true, invoke getDownloads after clearing the download list and return
+ * its value.
+ */
+function clearAllDoneHelper(getDownloads) {
+ var clearedPromise = new Promise(function(resolve, reject) {
+ function gotDownloads(downloads) {
+ // If there are no downloads, we're already done.
+ if (downloads.length === 0) {
+ resolve();
+ return;
+ }
+
+ // Track the set of expected downloads that will be finalized.
+ var expectedIds = new Set();
+ function changeHandler(evt) {
+ var download = evt.download;
+ if (download.state === "finalized") {
+ expectedIds.delete(download.id);
+ if (expectedIds.size === 0) {
+ resolve();
+ }
+ }
+ }
+ downloads.forEach(function(download) {
+ if (download.state === "downloading") {
+ ok(false, "A download is still active: " + download.path);
+ reject("Active download");
+ }
+ download.onstatechange = changeHandler;
+ expectedIds.add(download.id);
+ });
+ navigator.mozDownloadManager.clearAllDone();
+ }
+ function gotBadNews(err) {
+ ok(false, "Problem clearing all downloads: " + err);
+ reject(err);
+ }
+ navigator.mozDownloadManager.getDownloads().then(gotDownloads, gotBadNews);
+ });
+ if (!getDownloads) {
+ return clearedPromise;
+ }
+ return clearedPromise.then(function() {
+ return navigator.mozDownloadManager.getDownloads();
+ });
+}
diff --git a/dom/downloads/tests/mochitest.ini b/dom/downloads/tests/mochitest.ini
new file mode 100644
index 000000000..e13e4d887
--- /dev/null
+++ b/dom/downloads/tests/mochitest.ini
@@ -0,0 +1,15 @@
+[DEFAULT]
+# The actual requirement for mozDownloadManager is MOZ_GONK because of
+# the nsIVolumeService dependency. Until https://bugzil.la/1130264 is
+# addressed, there is no way for mulet to run these tests.
+run-if = toolkit == 'gonk'
+support-files =
+ serve_file.sjs
+ clear_all_done_helper.js
+
+[test_downloads_navigator_object.html]
+[test_downloads_basic.html]
+[test_downloads_large.html]
+[test_downloads_bad_file.html]
+[test_downloads_pause_remove.html]
+[test_downloads_pause_resume.html]
diff --git a/dom/downloads/tests/serve_file.sjs b/dom/downloads/tests/serve_file.sjs
new file mode 100644
index 000000000..d0171d7ca
--- /dev/null
+++ b/dom/downloads/tests/serve_file.sjs
@@ -0,0 +1,170 @@
+// Serves a file with a given mime type and size at an optionally given rate.
+
+function getQuery(request) {
+ var query = {};
+ request.queryString.split('&').forEach(function (val) {
+ var [name, value] = val.split('=');
+ query[name] = unescape(value);
+ });
+ return query;
+}
+
+function handleResponse() {
+ // Is this a rate limited response?
+ if (this.state.rate > 0) {
+ // Calculate how many bytes we have left to send.
+ var bytesToWrite = this.state.totalBytes - this.state.sentBytes;
+
+ // Do we have any bytes left to send? If not we'll just fall thru and
+ // cancel our repeating timer and finalize the response.
+ if (bytesToWrite > 0) {
+ // Figure out how many bytes to send, based on the rate limit.
+ bytesToWrite =
+ (bytesToWrite > this.state.rate) ? this.state.rate : bytesToWrite;
+
+ for (let i = 0; i < bytesToWrite; i++) {
+ try {
+ this.response.bodyOutputStream.write("0", 1);
+ } catch (e) {
+ // Connection was closed by client.
+ if (e == Components.results.NS_ERROR_NOT_AVAILABLE) {
+ // There's no harm in calling this multiple times.
+ this.response.finish();
+
+ // It's possible that our timer wasn't cancelled in time
+ // and we'll be called again.
+ if (this.timer) {
+ this.timer.cancel();
+ this.timer = null;
+ }
+
+ return;
+ }
+ }
+ }
+
+ // Update the number of bytes we've sent to the client.
+ this.state.sentBytes += bytesToWrite;
+
+ // Wait until the next call to do anything else.
+ return;
+ }
+ }
+ else {
+ // Not rate limited, write it all out.
+ for (let i = 0; i < this.state.totalBytes; i++) {
+ this.response.write("0");
+ }
+ }
+
+ // Finalize the response.
+ this.response.finish();
+
+ // All done sending, go ahead and cancel our repeating timer.
+ this.timer.cancel();
+
+ // Clear the timer.
+ this.timer = null;
+}
+
+function handleRequest(request, response) {
+ var query = getQuery(request);
+
+ // sending at a specific rate requires our response to be asynchronous so
+ // we handle all requests asynchronously. See handleResponse().
+ response.processAsync();
+
+ // Default status when responding.
+ var version = "1.1";
+ var statusCode = 200;
+ var description = "OK";
+
+ // Default values for content type, size and rate.
+ var contentType = "text/plain";
+ var contentRange = null;
+ var size = 1024;
+ var rate = 0;
+
+ // optional content type to be used by our response.
+ if ("contentType" in query) {
+ contentType = query["contentType"];
+ }
+
+ // optional size (in bytes) for generated file.
+ if ("size" in query) {
+ size = parseInt(query["size"]);
+ }
+
+ // optional range request check.
+ if (request.hasHeader("range")) {
+ version = "1.1";
+ statusCode = 206;
+ description = "Partial Content";
+
+ // We'll only support simple range byte style requests.
+ var [offset, total] = request.getHeader("range").slice("bytes=".length).split("-");
+ // Enforce valid Number values.
+ offset = parseInt(offset);
+ offset = isNaN(offset) ? 0 : offset;
+ // Same.
+ total = parseInt(total);
+ total = isNaN(total) ? 0 : total;
+
+ // We'll need to original total size as part of the Content-Range header
+ // value in our response.
+ var originalSize = size;
+
+ // If we have a total size requested, we must make sure to send that number
+ // of bytes only (minus the start offset).
+ if (total && total < size) {
+ size = total - offset;
+ } else if (offset) {
+ // Looks like we just have a byte offset to deal with.
+ size = size - offset;
+ }
+
+ // We specifically need to add a Content-Range header to all responses for
+ // requests that include a range request header.
+ contentRange = "bytes " + offset + "-" + (size - 1) + "/" + originalSize;
+ }
+
+ // optional rate (in bytes/s) at which to send the file.
+ if ("rate" in query) {
+ rate = parseInt(query["rate"]);
+ }
+
+ // The context for the responseHandler.
+ var context = {
+ response: response,
+ state: {
+ contentType: contentType,
+ totalBytes: size,
+ sentBytes: 0,
+ rate: rate
+ },
+ timer: null
+ };
+
+ // The notify implementation for the timer.
+ context.notify = handleResponse.bind(context);
+
+ context.timer =
+ Components.classes["@mozilla.org/timer;1"]
+ .createInstance(Components.interfaces.nsITimer);
+
+ // generate the content.
+ response.setStatusLine(version, statusCode, description);
+ response.setHeader("Content-Type", contentType, false);
+ if (contentRange) {
+ response.setHeader("Content-Range", contentRange, false);
+ }
+ response.setHeader("Content-Length", size.toString(), false);
+
+ // initialize the timer and start writing out the response.
+ context.timer.initWithCallback(
+ context,
+ 1000,
+ Components.interfaces.nsITimer.TYPE_REPEATING_SLACK
+ );
+
+}
diff --git a/dom/downloads/tests/test_downloads_bad_file.html b/dom/downloads/tests/test_downloads_bad_file.html
new file mode 100644
index 000000000..a2b3992e6
--- /dev/null
+++ b/dom/downloads/tests/test_downloads_bad_file.html
@@ -0,0 +1,93 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=960749
+-->
+<head>
+ <title>Test for Bug 960749 Downloads API</title>
+ <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=960749">Mozilla Bug 960749</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<a href="serve_file.sjs?contentType=application/octet-stream&size=1024" download=".&lt;.EVIL.&gt;\ / : * ? &quot; |file.bin" id="download1">Download #1</a>
+<pre id="test">
+<script class="testbody" type="text/javascript;version=1.7">
+
+// Testing a simple download, waiting for it to be done.
+
+SimpleTest.waitForExplicitFinish();
+
+var index = -1;
+var expected = "_.EVIL.__ _ _ _ _ _ _file.bin";
+
+function next() {
+ index += 1;
+ if (index >= steps.length) {
+ ok(false, "Shouldn't get here!");
+ return;
+ }
+ try {
+ steps[index]();
+ } catch(ex) {
+ ok(false, "Caught exception", ex);
+ }
+}
+
+function checkTargetFilename(download) {
+ ok(download.path.endsWith(expected),
+ "Download path leaf name '" + download.path +
+ "' should match '" + expected + "' filename.");
+
+ SimpleTest.finish();
+}
+
+function downloadChange(evt) {
+ var download = evt.download;
+
+ if (download.state === "succeeded") {
+ checkTargetFilename(download);
+ }
+}
+
+function downloadStart(evt) {
+ var download = evt.download;
+ download.onstatechange = downloadChange;
+}
+
+var steps = [
+ // Start by setting the pref to true.
+ function() {
+ SpecialPowers.pushPrefEnv({
+ set: [["dom.mozDownloads.enabled", true]]
+ }, next);
+ },
+
+ // Setup the event listeners.
+ function() {
+ SpecialPowers.pushPermissions([
+ {type: "downloads", allow: true, context: document}
+ ], function() {
+ navigator.mozDownloadManager.ondownloadstart = downloadStart;
+ next();
+ });
+ },
+
+ // Click on the <a download> to start the download.
+ function() {
+ document.getElementById("download1").click();
+ }
+];
+
+next();
+
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/downloads/tests/test_downloads_basic.html b/dom/downloads/tests/test_downloads_basic.html
new file mode 100644
index 000000000..051a1faa1
--- /dev/null
+++ b/dom/downloads/tests/test_downloads_basic.html
@@ -0,0 +1,128 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=938023
+-->
+<head>
+ <title>Test for Bug 938023 Downloads API</title>
+ <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<a href="serve_file.sjs?contentType=application/octet-stream&size=1024" download="test.bin" id="download1">Download #1</a>
+<pre id="test">
+<script class="testbody" type="text/javascript;version=1.7">
+
+// Testing a simple download, waiting for it to be done.
+
+SimpleTest.waitForExplicitFinish();
+
+var index = -1;
+var todayDate = new Date();
+var baseServeURL = "http://mochi.test:8888/tests/dom/downloads/tests/";
+var lastKnownCurrentBytes = 0;
+
+function next() {
+ index += 1;
+ if (index >= steps.length) {
+ ok(false, "Shouldn't get here!");
+ return;
+ }
+ try {
+ steps[index]();
+ } catch(ex) {
+ ok(false, "Caught exception", ex);
+ }
+}
+
+function checkConsistentDownloadAttributes(download) {
+ var href = document.getElementById("download1").getAttribute("href");
+ var expectedServeURL = baseServeURL + href;
+ var destinationRegEx = /test\(?[0-9]*\)?\.bin$/;
+
+ // bug 945323: Download path isn't honoring download attribute
+ ok(destinationRegEx.test(download.path),
+ "Download path '" + download.path +
+ "' should match '" + destinationRegEx + "' regexp.");
+
+ ok(download.startTime >= todayDate,
+ "Download start time should be greater than or equal to today");
+
+ is(download.error, null, "Download does not have an error");
+
+ is(download.url, expectedServeURL,
+ "Download URL = " + expectedServeURL);
+ ok(download.id !== null, "Download id is defined");
+ is(download.contentType, "application/octet-stream",
+ "Download content type is application/octet-stream");
+}
+
+function downloadChange(evt) {
+ var download = evt.download;
+ checkConsistentDownloadAttributes(download);
+ is(download.totalBytes, 1024, "Download total size is 1024 bytes");
+
+ if (download.state === "succeeded") {
+ is(download.currentBytes, 1024, "Download current size is 1024 bytes");
+ SimpleTest.finish();
+ } else if (download.state === "downloading") {
+ // Note that this case may or may not trigger, depending on whether the
+ // download is initially reported with 0 bytes (we should happen) or with
+ // 1024 bytes (we should not happen). If we do happen, an additional 8
+ // TEST-PASS events should be logged.
+ ok(download.currentBytes > lastKnownCurrentBytes,
+ "Download current size is larger than last download change event");
+ lastKnownCurrentBytes = download.currentBytes;
+ } else {
+ ok(false, "Unexpected download state = " + download.state);
+ }
+}
+
+function downloadStart(evt) {
+ var download = evt.download;
+ checkConsistentDownloadAttributes(download);
+
+ // We used to check that the currentBytes was 0. This was incorrect. It
+ // is very common to first hear about the download already at 1024 bytes.
+ is(download.state, "downloading", "Download state is downloading");
+
+ download.onstatechange = downloadChange;
+}
+
+var steps = [
+ // Start by setting the pref to true.
+ function() {
+ SpecialPowers.pushPrefEnv({
+ set: [["dom.mozDownloads.enabled", true]]
+ }, next);
+ },
+
+ // Setup the event listeners.
+ function() {
+ SpecialPowers.pushPermissions([
+ {type: "downloads", allow: true, context: document}
+ ], function() {
+ navigator.mozDownloadManager.ondownloadstart = downloadStart;
+ next();
+ });
+ },
+
+ // Click on the <a download> to start the download.
+ function() {
+ document.getElementById("download1").click();
+ }
+];
+
+next();
+
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/downloads/tests/test_downloads_large.html b/dom/downloads/tests/test_downloads_large.html
new file mode 100644
index 000000000..9f7f73c19
--- /dev/null
+++ b/dom/downloads/tests/test_downloads_large.html
@@ -0,0 +1,110 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=938023
+-->
+<head>
+ <title>Test for Bug 938023 Downloads API</title>
+ <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="clear_all_done_helper.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<a href="serve_file.sjs?contentType=application/octet-stream&size=102400" download="test.bin" id="download1">Large Download</a>
+<pre id="test">
+<script class="testbody" type="text/javascript;version=1.7">
+
+// Testing downloading a file, then checking getDownloads() and clearAllDone().
+
+SimpleTest.waitForExplicitFinish();
+
+var index = -1;
+
+function next(args) {
+ index += 1;
+ if (index >= steps.length) {
+ ok(false, "Shouldn't get here!");
+ return;
+ }
+ try {
+ steps[index](args);
+ } catch(ex) {
+ ok(false, "Caught exception", ex);
+ }
+}
+
+// Catch all error function.
+function error() {
+ ok(false, "API failure");
+ SimpleTest.finish();
+}
+
+function getDownloads(downloads) {
+ ok(downloads.length == 1, "One downloads after getDownloads");
+ clearAllDoneHelper(true).then(clearAllDone, error);
+}
+
+function clearAllDone(downloads) {
+ ok(downloads.length == 0, "No downloads after clearAllDone");
+ SimpleTest.finish();
+}
+
+function downloadChange(evt) {
+ var download = evt.download;
+
+ if (download.state == "succeeded") {
+ ok(download.totalBytes == 102400, "Download size is 100k bytes.");
+ navigator.mozDownloadManager.getDownloads().then(getDownloads, error);
+ }
+}
+
+var steps = [
+ // Start by setting the pref to true.
+ function() {
+ SpecialPowers.pushPrefEnv({
+ set: [["dom.mozDownloads.enabled", true]]
+ }, next);
+ },
+
+ // Setup permission and clear current list.
+ function() {
+ SpecialPowers.pushPermissions([
+ {type: "downloads", allow: true, context: document}
+ ], function() {
+ clearAllDoneHelper(true).then(next, error);
+ });
+ },
+
+ function(downloads) {
+ ok(downloads.length == 0, "Start with an empty download list.");
+ next();
+ },
+
+ // Setup the event listeners.
+ function() {
+ navigator.mozDownloadManager.ondownloadstart =
+ function(evt) {
+ ok(true, "Download started");
+ evt.download.addEventListener("statechange", downloadChange);
+ }
+ next();
+ },
+
+ // Click on the <a download> to start the download.
+ function() {
+ document.getElementById("download1").click();
+ }
+];
+
+next();
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/downloads/tests/test_downloads_navigator_object.html b/dom/downloads/tests/test_downloads_navigator_object.html
new file mode 100644
index 000000000..1c38388b7
--- /dev/null
+++ b/dom/downloads/tests/test_downloads_navigator_object.html
@@ -0,0 +1,75 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=938023
+-->
+<head>
+ <title>Test for Bug 938023 Downloads API</title>
+ <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+<iframe></iframe>
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript;version=1.7">
+
+SimpleTest.waitForExplicitFinish();
+
+var index = -1;
+
+function next() {
+ index += 1;
+ if (index >= steps.length) {
+ ok(false, "Shouldn't get here!");
+ return;
+ }
+ try {
+ steps[index]();
+ } catch(ex) {
+ ok(false, "Caught exception", ex);
+ }
+}
+
+var steps = [
+ // Start by setting the pref to true.
+ function() {
+ SpecialPowers.pushPrefEnv({
+ set: [["dom.mozDownloads.enabled", true]]
+ }, next);
+ },
+
+ function() {
+ SpecialPowers.pushPermissions([
+ {type: "downloads", allow: 0, context: document}
+ ], function() {
+ is(frames[0].navigator.mozDownloadManager, null, "navigator.mozDownloadManager is null when the page doesn't have permissions");
+ next();
+ });
+ },
+
+ function() {
+ SpecialPowers.pushPrefEnv({
+ set: [["dom.mozDownloads.enabled", false]]
+ }, function() {
+ is(navigator.mozDownloadManager, undefined, "navigator.mozDownloadManager is undefined");
+ next();
+ });
+ },
+
+ function() {
+ SimpleTest.finish();
+ }
+];
+
+next();
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/downloads/tests/test_downloads_pause_remove.html b/dom/downloads/tests/test_downloads_pause_remove.html
new file mode 100644
index 000000000..3b410a667
--- /dev/null
+++ b/dom/downloads/tests/test_downloads_pause_remove.html
@@ -0,0 +1,117 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=938023
+-->
+<head>
+ <title>Test for Bug 938023 Downloads API</title>
+ <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="clear_all_done_helper.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<a href="serve_file.sjs?contentType=application/octet-stream&size=102400&rate=1024" download="test.bin" id="download1">Large Download</a>
+<pre id="test">
+<script class="testbody" type="text/javascript;version=1.7">
+
+// Testing pausing a download and then removing it.
+
+SimpleTest.waitForExplicitFinish();
+
+var index = -1;
+
+function next(args) {
+ index += 1;
+ if (index >= steps.length) {
+ ok(false, "Shouldn't get here!");
+ return;
+ }
+ try {
+ steps[index](args);
+ } catch(ex) {
+ ok(false, "Caught exception", ex);
+ }
+}
+
+var pausing = false;
+
+// Catch all error function.
+function error() {
+ ok(false, "API failure");
+ SimpleTest.finish();
+}
+
+function checkDownloadList(downloads) {
+ ok(downloads.length == 0, "No downloads left");
+ SimpleTest.finish();
+}
+
+function checkRemoved(download) {
+ ok(download.state == "finalized", "Download removed.");
+ navigator.mozDownloadManager.getDownloads()
+ .then(checkDownloadList, error);
+}
+
+function downloadChange(evt) {
+ var download = evt.download;
+
+ if (download.state == "downloading" && !pausing) {
+ pausing = true;
+ download.pause();
+ } else if (download.state == "stopped") {
+ ok(pausing, "Download stopped by pause()");
+ navigator.mozDownloadManager.remove(download)
+ .then(checkRemoved, error);
+ }
+}
+
+var steps = [
+ // Start by setting the pref to true.
+ function() {
+ SpecialPowers.pushPrefEnv({
+ set: [["dom.mozDownloads.enabled", true]]
+ }, next);
+ },
+
+ // Setup permission and clear current list.
+ function() {
+ SpecialPowers.pushPermissions([
+ {type: "downloads", allow: true, context: document}
+ ], function() {
+ clearAllDoneHelper(true).then(next, error);
+ });
+ },
+
+ function(downloads) {
+ ok(downloads.length == 0, "Start with an empty download list.");
+ next();
+ },
+
+ // Setup the event listeners.
+ function() {
+ navigator.mozDownloadManager.ondownloadstart =
+ function(evt) {
+ ok(true, "Download started");
+ evt.download.addEventListener("statechange", downloadChange);
+ }
+ next();
+ },
+
+ // Click on the <a download> to start the download.
+ function() {
+ document.getElementById("download1").click();
+ }
+];
+
+next();
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/downloads/tests/test_downloads_pause_resume.html b/dom/downloads/tests/test_downloads_pause_resume.html
new file mode 100644
index 000000000..76e249e5a
--- /dev/null
+++ b/dom/downloads/tests/test_downloads_pause_resume.html
@@ -0,0 +1,121 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=938023
+-->
+<head>
+ <title>Test for Bug 938023 Downloads API</title>
+ <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="clear_all_done_helper.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<a href="serve_file.sjs?contentType=application/octet-stream&size=102400&rate=1024" download="test.bin" id="download1">Large Download</a>
+<pre id="test">
+<script class="testbody" type="text/javascript;version=1.7">
+
+// Testing pausing a download and then resuming it.
+
+SimpleTest.waitForExplicitFinish();
+
+var index = -1;
+
+function next(args) {
+ index += 1;
+ if (index >= steps.length) {
+ ok(false, "Shouldn't get here!");
+ return;
+ }
+ try {
+ steps[index](args);
+ } catch(ex) {
+ ok(false, "Caught exception", ex);
+ }
+}
+
+var pausing = false;
+var resuming = false;
+
+// Catch all error function.
+function error() {
+ ok(false, "API failure");
+ SimpleTest.finish();
+}
+
+function checkDownloadList(downloads) {
+ ok(downloads.length == 0, "No downloads left");
+ SimpleTest.finish();
+}
+
+function checkResumeSucceeded(download) {
+ ok(download.state == "succeeded", "Download resumed successfully.");
+ clearAllDoneHelper(true).then(checkDownloadList, error);
+}
+
+function downloadChange(evt) {
+ var download = evt.download;
+
+ info("got download event, state: " + download.state +
+ " current bytes: " + download.currentBytes +
+ " pausing?: " + pausing + " resuming?: " + resuming);
+ if (download.state == "downloading" && !pausing) {
+ pausing = true;
+ download.pause();
+ } else if (download.state == "stopped" && !resuming) {
+ resuming = true;
+ ok(pausing, "Download stopped by pause()");
+ download.resume()
+ .then(function() { checkResumeSucceeded(download); }, error);
+ }
+}
+
+var steps = [
+ // Start by setting the pref to true.
+ function() {
+ SpecialPowers.pushPrefEnv({
+ set: [["dom.mozDownloads.enabled", true]]
+ }, next);
+ },
+
+ // Setup permission and clear current list.
+ function() {
+ SpecialPowers.pushPermissions([
+ {type: "downloads", allow: true, context: document}
+ ], function() {
+ clearAllDoneHelper(true).then(next, error);
+ });
+ },
+
+ function(downloads) {
+ ok(downloads.length == 0, "Start with an empty download list.");
+ next();
+ },
+
+ // Setup the event listeners.
+ function() {
+ navigator.mozDownloadManager.ondownloadstart =
+ function(evt) {
+ ok(true, "Download started");
+ evt.download.addEventListener("statechange", downloadChange);
+ }
+ next();
+ },
+
+ // Click on the <a download> to start the download.
+ function() {
+ document.getElementById("download1").click();
+ }
+];
+
+next();
+
+</script>
+</pre>
+</body>
+</html>