dom/downloads/tests/test_downloads_basic.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE html>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=938023
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 938023 Downloads API</title>
michael@0 8 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
michael@0 9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 11 </head>
michael@0 12 <body>
michael@0 13
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
michael@0 15 <p id="display"></p>
michael@0 16 <div id="content" style="display: none">
michael@0 17 </div>
michael@0 18 <a href="serve_file.sjs?contentType=application/octet-stream&size=1024" download="test.bin" id="download1">Download #1</a>
michael@0 19 <pre id="test">
michael@0 20 <script class="testbody" type="text/javascript;version=1.7">
michael@0 21
michael@0 22 // Testing a simple download, waiting for it to be done.
michael@0 23
michael@0 24 SimpleTest.waitForExplicitFinish();
michael@0 25
michael@0 26 var index = -1;
michael@0 27 var todayDate = new Date();
michael@0 28 var baseServeURL = "http://mochi.test:8888/tests/dom/downloads/tests/";
michael@0 29 var lastKnownCurrentBytes = 0;
michael@0 30
michael@0 31 function next() {
michael@0 32 index += 1;
michael@0 33 if (index >= steps.length) {
michael@0 34 ok(false, "Shouldn't get here!");
michael@0 35 return;
michael@0 36 }
michael@0 37 try {
michael@0 38 steps[index]();
michael@0 39 } catch(ex) {
michael@0 40 ok(false, "Caught exception", ex);
michael@0 41 }
michael@0 42 }
michael@0 43
michael@0 44 function checkConsistentDownloadAttributes(download) {
michael@0 45 var href = document.getElementById("download1").getAttribute("href");
michael@0 46 var expectedServeURL = baseServeURL + href;
michael@0 47 var destinationRegEx = /test\(?[0-9]*\)?\.bin$/;
michael@0 48
michael@0 49 // bug 945323: Download path isn't honoring download attribute
michael@0 50 ok(destinationRegEx.test(download.path),
michael@0 51 "Download path '" + download.path +
michael@0 52 "' should match '" + destinationRegEx + "' regexp.");
michael@0 53
michael@0 54 ok(download.startTime >= todayDate,
michael@0 55 "Download start time should be greater than or equal to today");
michael@0 56
michael@0 57 is(download.error, null, "Download does not have an error");
michael@0 58
michael@0 59 is(download.url, expectedServeURL,
michael@0 60 "Download URL = " + expectedServeURL);
michael@0 61 ok(download.id !== null, "Download id is defined");
michael@0 62 is(download.contentType, "application/octet-stream",
michael@0 63 "Download content type is application/octet-stream");
michael@0 64 }
michael@0 65
michael@0 66 function downloadChange(evt) {
michael@0 67 var download = evt.download;
michael@0 68 checkConsistentDownloadAttributes(download);
michael@0 69 is(download.totalBytes, 1024, "Download total size is 1024 bytes");
michael@0 70
michael@0 71 if (download.state === "succeeded") {
michael@0 72 is(download.currentBytes, 1024, "Download current size is 1024 bytes");
michael@0 73 SimpleTest.finish();
michael@0 74 } else if (download.state === "downloading") {
michael@0 75 ok(download.currentBytes > lastKnownCurrentBytes,
michael@0 76 "Download current size is larger than last download change event");
michael@0 77 lastKnownCurrentBytes = download.currentBytes;
michael@0 78 } else {
michael@0 79 ok(false, "Unexpected download state = " + download.state);
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 function downloadStart(evt) {
michael@0 84 var download = evt.download;
michael@0 85 checkConsistentDownloadAttributes(download);
michael@0 86
michael@0 87 is(download.currentBytes, 0, "Download current size is zero");
michael@0 88 is(download.state, "downloading", "Download state is downloading");
michael@0 89
michael@0 90 download.onstatechange = downloadChange;
michael@0 91 }
michael@0 92
michael@0 93 var steps = [
michael@0 94 // Start by setting the pref to true.
michael@0 95 function() {
michael@0 96 SpecialPowers.pushPrefEnv({
michael@0 97 set: [["dom.mozDownloads.enabled", true]]
michael@0 98 }, next);
michael@0 99 },
michael@0 100
michael@0 101 // Setup the event listeners.
michael@0 102 function() {
michael@0 103 SpecialPowers.pushPermissions([
michael@0 104 {type: "downloads", allow: true, context: document}
michael@0 105 ], function() {
michael@0 106 navigator.mozDownloadManager.ondownloadstart = downloadStart;
michael@0 107 next();
michael@0 108 });
michael@0 109 },
michael@0 110
michael@0 111 // Click on the <a download> to start the download.
michael@0 112 function() {
michael@0 113 document.getElementById("download1").click();
michael@0 114 }
michael@0 115 ];
michael@0 116
michael@0 117 next();
michael@0 118
michael@0 119 </script>
michael@0 120 </pre>
michael@0 121 </body>
michael@0 122 </html>
michael@0 123

mercurial