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