|
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=10240" 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 resuming 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 var resuming = false; |
|
43 |
|
44 // Catch all error function. |
|
45 function error() { |
|
46 ok(false, "API failure"); |
|
47 SimpleTest.finish(); |
|
48 } |
|
49 |
|
50 function checkDownloadList(downloads) { |
|
51 ok(downloads.length == 0, "No downloads left"); |
|
52 SimpleTest.finish(); |
|
53 } |
|
54 |
|
55 function checkResumeSucceeded(download) { |
|
56 ok(download.state == "succeeded", "Download resumed successfully."); |
|
57 navigator.mozDownloadManager.clearAllDone() |
|
58 .then(checkDownloadList, error); |
|
59 } |
|
60 |
|
61 function downloadChange(evt) { |
|
62 var download = evt.download; |
|
63 |
|
64 if (download.state == "downloading" && !pausing) { |
|
65 pausing = true; |
|
66 download.pause(); |
|
67 } else if (download.state == "stopped" && !resuming) { |
|
68 resuming = true; |
|
69 ok(pausing, "Download stopped by pause()"); |
|
70 download.resume() |
|
71 .then(function() { checkResumeSucceeded(download); }, error); |
|
72 } |
|
73 } |
|
74 |
|
75 var steps = [ |
|
76 // Start by setting the pref to true. |
|
77 function() { |
|
78 SpecialPowers.pushPrefEnv({ |
|
79 set: [["dom.mozDownloads.enabled", true]] |
|
80 }, next); |
|
81 }, |
|
82 |
|
83 // Setup permission and clear current list. |
|
84 function() { |
|
85 SpecialPowers.pushPermissions([ |
|
86 {type: "downloads", allow: true, context: document} |
|
87 ], function() { |
|
88 navigator.mozDownloadManager.clearAllDone().then(next, error); |
|
89 }); |
|
90 }, |
|
91 |
|
92 function(downloads) { |
|
93 ok(downloads.length == 0, "Start with an empty download list."); |
|
94 next(); |
|
95 }, |
|
96 |
|
97 // Setup the event listeners. |
|
98 function() { |
|
99 navigator.mozDownloadManager.ondownloadstart = |
|
100 function(evt) { |
|
101 ok(true, "Download started"); |
|
102 evt.download.addEventListener("statechange", downloadChange); |
|
103 } |
|
104 next(); |
|
105 }, |
|
106 |
|
107 // Click on the <a download> to start the download. |
|
108 function() { |
|
109 document.getElementById("download1").click(); |
|
110 } |
|
111 ]; |
|
112 |
|
113 next(); |
|
114 |
|
115 </script> |
|
116 </pre> |
|
117 </body> |
|
118 </html> |