Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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>
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=1024" download="test.bin" id="download1">Download #1</a>
19 <pre id="test">
20 <script class="testbody" type="text/javascript;version=1.7">
22 // Testing a simple download, waiting for it to be done.
24 SimpleTest.waitForExplicitFinish();
26 var index = -1;
27 var todayDate = new Date();
28 var baseServeURL = "http://mochi.test:8888/tests/dom/downloads/tests/";
29 var lastKnownCurrentBytes = 0;
31 function next() {
32 index += 1;
33 if (index >= steps.length) {
34 ok(false, "Shouldn't get here!");
35 return;
36 }
37 try {
38 steps[index]();
39 } catch(ex) {
40 ok(false, "Caught exception", ex);
41 }
42 }
44 function checkConsistentDownloadAttributes(download) {
45 var href = document.getElementById("download1").getAttribute("href");
46 var expectedServeURL = baseServeURL + href;
47 var destinationRegEx = /test\(?[0-9]*\)?\.bin$/;
49 // bug 945323: Download path isn't honoring download attribute
50 ok(destinationRegEx.test(download.path),
51 "Download path '" + download.path +
52 "' should match '" + destinationRegEx + "' regexp.");
54 ok(download.startTime >= todayDate,
55 "Download start time should be greater than or equal to today");
57 is(download.error, null, "Download does not have an error");
59 is(download.url, expectedServeURL,
60 "Download URL = " + expectedServeURL);
61 ok(download.id !== null, "Download id is defined");
62 is(download.contentType, "application/octet-stream",
63 "Download content type is application/octet-stream");
64 }
66 function downloadChange(evt) {
67 var download = evt.download;
68 checkConsistentDownloadAttributes(download);
69 is(download.totalBytes, 1024, "Download total size is 1024 bytes");
71 if (download.state === "succeeded") {
72 is(download.currentBytes, 1024, "Download current size is 1024 bytes");
73 SimpleTest.finish();
74 } else if (download.state === "downloading") {
75 ok(download.currentBytes > lastKnownCurrentBytes,
76 "Download current size is larger than last download change event");
77 lastKnownCurrentBytes = download.currentBytes;
78 } else {
79 ok(false, "Unexpected download state = " + download.state);
80 }
81 }
83 function downloadStart(evt) {
84 var download = evt.download;
85 checkConsistentDownloadAttributes(download);
87 is(download.currentBytes, 0, "Download current size is zero");
88 is(download.state, "downloading", "Download state is downloading");
90 download.onstatechange = downloadChange;
91 }
93 var steps = [
94 // Start by setting the pref to true.
95 function() {
96 SpecialPowers.pushPrefEnv({
97 set: [["dom.mozDownloads.enabled", true]]
98 }, next);
99 },
101 // Setup the event listeners.
102 function() {
103 SpecialPowers.pushPermissions([
104 {type: "downloads", allow: true, context: document}
105 ], function() {
106 navigator.mozDownloadManager.ondownloadstart = downloadStart;
107 next();
108 });
109 },
111 // Click on the <a download> to start the download.
112 function() {
113 document.getElementById("download1").click();
114 }
115 ];
117 next();
119 </script>
120 </pre>
121 </body>
122 </html>