dom/downloads/tests/test_downloads_pause_remove.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:ce271d4452fb
1 <!DOCTYPE html>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=938023
5 -->
6 <head>
7 <title>Test for Bug 938023 Downloads API</title>
8 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
11 </head>
12 <body>
13
14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
15 <p id="display"></p>
16 <div id="content" style="display: none">
17 </div>
18 <a href="serve_file.sjs?contentType=application/octet-stream&size=102400&rate=1024" download="test.bin" id="download1">Large Download</a>
19 <pre id="test">
20 <script class="testbody" type="text/javascript;version=1.7">
21
22 // Testing pausing a download and then removing it.
23
24 SimpleTest.waitForExplicitFinish();
25
26 var index = -1;
27
28 function next(args) {
29 index += 1;
30 if (index >= steps.length) {
31 ok(false, "Shouldn't get here!");
32 return;
33 }
34 try {
35 steps[index](args);
36 } catch(ex) {
37 ok(false, "Caught exception", ex);
38 }
39 }
40
41 var pausing = false;
42
43 // Catch all error function.
44 function error() {
45 ok(false, "API failure");
46 SimpleTest.finish();
47 }
48
49 function checkDownloadList(downloads) {
50 ok(downloads.length == 0, "No downloads left");
51 SimpleTest.finish();
52 }
53
54 function checkRemoved(download) {
55 ok(download.state == "finalized", "Download removed.");
56 navigator.mozDownloadManager.getDownloads()
57 .then(checkDownloadList, error);
58 }
59
60 function downloadChange(evt) {
61 var download = evt.download;
62
63 if (download.state == "downloading" && !pausing) {
64 pausing = true;
65 download.pause();
66 } else if (download.state == "stopped") {
67 ok(pausing, "Download stopped by pause()");
68 navigator.mozDownloadManager.remove(download)
69 .then(checkRemoved, error);
70 }
71 }
72
73 var steps = [
74 // Start by setting the pref to true.
75 function() {
76 SpecialPowers.pushPrefEnv({
77 set: [["dom.mozDownloads.enabled", true]]
78 }, next);
79 },
80
81 // Setup permission and clear current list.
82 function() {
83 SpecialPowers.pushPermissions([
84 {type: "downloads", allow: true, context: document}
85 ], function() {
86 navigator.mozDownloadManager.clearAllDone().then(next, error);
87 });
88 },
89
90 function(downloads) {
91 ok(downloads.length == 0, "Start with an empty download list.");
92 next();
93 },
94
95 // Setup the event listeners.
96 function() {
97 navigator.mozDownloadManager.ondownloadstart =
98 function(evt) {
99 ok(true, "Download started");
100 evt.download.addEventListener("statechange", downloadChange);
101 }
102 next();
103 },
104
105 // Click on the <a download> to start the download.
106 function() {
107 document.getElementById("download1").click();
108 }
109 ];
110
111 next();
112
113 </script>
114 </pre>
115 </body>
116 </html>

mercurial