1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/downloads/tests/test_downloads_pause_resume.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 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=102400&rate=10240" download="test.bin" id="download1">Large Download</a> 1.22 +<pre id="test"> 1.23 +<script class="testbody" type="text/javascript;version=1.7"> 1.24 + 1.25 +// Testing pausing a download and then resuming it. 1.26 + 1.27 +SimpleTest.waitForExplicitFinish(); 1.28 + 1.29 +var index = -1; 1.30 + 1.31 +function next(args) { 1.32 + index += 1; 1.33 + if (index >= steps.length) { 1.34 + ok(false, "Shouldn't get here!"); 1.35 + return; 1.36 + } 1.37 + try { 1.38 + steps[index](args); 1.39 + } catch(ex) { 1.40 + ok(false, "Caught exception", ex); 1.41 + } 1.42 +} 1.43 + 1.44 +var pausing = false; 1.45 +var resuming = false; 1.46 + 1.47 +// Catch all error function. 1.48 +function error() { 1.49 + ok(false, "API failure"); 1.50 + SimpleTest.finish(); 1.51 +} 1.52 + 1.53 +function checkDownloadList(downloads) { 1.54 + ok(downloads.length == 0, "No downloads left"); 1.55 + SimpleTest.finish(); 1.56 +} 1.57 + 1.58 +function checkResumeSucceeded(download) { 1.59 + ok(download.state == "succeeded", "Download resumed successfully."); 1.60 + navigator.mozDownloadManager.clearAllDone() 1.61 + .then(checkDownloadList, error); 1.62 +} 1.63 + 1.64 +function downloadChange(evt) { 1.65 + var download = evt.download; 1.66 + 1.67 + if (download.state == "downloading" && !pausing) { 1.68 + pausing = true; 1.69 + download.pause(); 1.70 + } else if (download.state == "stopped" && !resuming) { 1.71 + resuming = true; 1.72 + ok(pausing, "Download stopped by pause()"); 1.73 + download.resume() 1.74 + .then(function() { checkResumeSucceeded(download); }, error); 1.75 + } 1.76 +} 1.77 + 1.78 +var steps = [ 1.79 + // Start by setting the pref to true. 1.80 + function() { 1.81 + SpecialPowers.pushPrefEnv({ 1.82 + set: [["dom.mozDownloads.enabled", true]] 1.83 + }, next); 1.84 + }, 1.85 + 1.86 + // Setup permission and clear current list. 1.87 + function() { 1.88 + SpecialPowers.pushPermissions([ 1.89 + {type: "downloads", allow: true, context: document} 1.90 + ], function() { 1.91 + navigator.mozDownloadManager.clearAllDone().then(next, error); 1.92 + }); 1.93 + }, 1.94 + 1.95 + function(downloads) { 1.96 + ok(downloads.length == 0, "Start with an empty download list."); 1.97 + next(); 1.98 + }, 1.99 + 1.100 + // Setup the event listeners. 1.101 + function() { 1.102 + navigator.mozDownloadManager.ondownloadstart = 1.103 + function(evt) { 1.104 + ok(true, "Download started"); 1.105 + evt.download.addEventListener("statechange", downloadChange); 1.106 + } 1.107 + next(); 1.108 + }, 1.109 + 1.110 + // Click on the <a download> to start the download. 1.111 + function() { 1.112 + document.getElementById("download1").click(); 1.113 + } 1.114 +]; 1.115 + 1.116 +next(); 1.117 + 1.118 +</script> 1.119 +</pre> 1.120 +</body> 1.121 +</html>