1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/downloads/tests/test_downloads_basic.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 1.4 +<!DOCTYPE html> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=938023 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 938023 Downloads API</title> 1.11 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.12 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.14 +</head> 1.15 +<body> 1.16 + 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a> 1.18 +<p id="display"></p> 1.19 +<div id="content" style="display: none"> 1.20 +</div> 1.21 +<a href="serve_file.sjs?contentType=application/octet-stream&size=1024" download="test.bin" id="download1">Download #1</a> 1.22 +<pre id="test"> 1.23 +<script class="testbody" type="text/javascript;version=1.7"> 1.24 + 1.25 +// Testing a simple download, waiting for it to be done. 1.26 + 1.27 +SimpleTest.waitForExplicitFinish(); 1.28 + 1.29 +var index = -1; 1.30 +var todayDate = new Date(); 1.31 +var baseServeURL = "http://mochi.test:8888/tests/dom/downloads/tests/"; 1.32 +var lastKnownCurrentBytes = 0; 1.33 + 1.34 +function next() { 1.35 + index += 1; 1.36 + if (index >= steps.length) { 1.37 + ok(false, "Shouldn't get here!"); 1.38 + return; 1.39 + } 1.40 + try { 1.41 + steps[index](); 1.42 + } catch(ex) { 1.43 + ok(false, "Caught exception", ex); 1.44 + } 1.45 +} 1.46 + 1.47 +function checkConsistentDownloadAttributes(download) { 1.48 + var href = document.getElementById("download1").getAttribute("href"); 1.49 + var expectedServeURL = baseServeURL + href; 1.50 + var destinationRegEx = /test\(?[0-9]*\)?\.bin$/; 1.51 + 1.52 + // bug 945323: Download path isn't honoring download attribute 1.53 + ok(destinationRegEx.test(download.path), 1.54 + "Download path '" + download.path + 1.55 + "' should match '" + destinationRegEx + "' regexp."); 1.56 + 1.57 + ok(download.startTime >= todayDate, 1.58 + "Download start time should be greater than or equal to today"); 1.59 + 1.60 + is(download.error, null, "Download does not have an error"); 1.61 + 1.62 + is(download.url, expectedServeURL, 1.63 + "Download URL = " + expectedServeURL); 1.64 + ok(download.id !== null, "Download id is defined"); 1.65 + is(download.contentType, "application/octet-stream", 1.66 + "Download content type is application/octet-stream"); 1.67 +} 1.68 + 1.69 +function downloadChange(evt) { 1.70 + var download = evt.download; 1.71 + checkConsistentDownloadAttributes(download); 1.72 + is(download.totalBytes, 1024, "Download total size is 1024 bytes"); 1.73 + 1.74 + if (download.state === "succeeded") { 1.75 + is(download.currentBytes, 1024, "Download current size is 1024 bytes"); 1.76 + SimpleTest.finish(); 1.77 + } else if (download.state === "downloading") { 1.78 + ok(download.currentBytes > lastKnownCurrentBytes, 1.79 + "Download current size is larger than last download change event"); 1.80 + lastKnownCurrentBytes = download.currentBytes; 1.81 + } else { 1.82 + ok(false, "Unexpected download state = " + download.state); 1.83 + } 1.84 +} 1.85 + 1.86 +function downloadStart(evt) { 1.87 + var download = evt.download; 1.88 + checkConsistentDownloadAttributes(download); 1.89 + 1.90 + is(download.currentBytes, 0, "Download current size is zero"); 1.91 + is(download.state, "downloading", "Download state is downloading"); 1.92 + 1.93 + download.onstatechange = downloadChange; 1.94 +} 1.95 + 1.96 +var steps = [ 1.97 + // Start by setting the pref to true. 1.98 + function() { 1.99 + SpecialPowers.pushPrefEnv({ 1.100 + set: [["dom.mozDownloads.enabled", true]] 1.101 + }, next); 1.102 + }, 1.103 + 1.104 + // Setup the event listeners. 1.105 + function() { 1.106 + SpecialPowers.pushPermissions([ 1.107 + {type: "downloads", allow: true, context: document} 1.108 + ], function() { 1.109 + navigator.mozDownloadManager.ondownloadstart = downloadStart; 1.110 + next(); 1.111 + }); 1.112 + }, 1.113 + 1.114 + // Click on the <a download> to start the download. 1.115 + function() { 1.116 + document.getElementById("download1").click(); 1.117 + } 1.118 +]; 1.119 + 1.120 +next(); 1.121 + 1.122 +</script> 1.123 +</pre> 1.124 +</body> 1.125 +</html> 1.126 +