blob: 62fa1a2f30d33a46604d7eadf122839d1ffa0070 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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();
});
}
|