summaryrefslogtreecommitdiffstats
path: root/dom/downloads/tests/clear_all_done_helper.js
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/clear_all_done_helper.js
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/clear_all_done_helper.js')
-rw-r--r--dom/downloads/tests/clear_all_done_helper.js67
1 files changed, 67 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();
+ });
+}