dom/downloads/tests/test_downloads_pause_remove.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/downloads/tests/test_downloads_pause_remove.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     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=1024" 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 removing 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 +
    1.46 +// Catch all error function.
    1.47 +function error() {
    1.48 +  ok(false, "API failure");
    1.49 +  SimpleTest.finish();
    1.50 +}
    1.51 +
    1.52 +function checkDownloadList(downloads) {
    1.53 +  ok(downloads.length == 0, "No downloads left");
    1.54 +  SimpleTest.finish();
    1.55 +}
    1.56 +
    1.57 +function checkRemoved(download) {
    1.58 +  ok(download.state == "finalized", "Download removed.");
    1.59 +  navigator.mozDownloadManager.getDownloads()
    1.60 +           .then(checkDownloadList, error);
    1.61 +}
    1.62 +
    1.63 +function downloadChange(evt) {
    1.64 +  var download = evt.download;
    1.65 +
    1.66 +  if (download.state == "downloading" && !pausing) {
    1.67 +    pausing = true;
    1.68 +    download.pause();
    1.69 +  } else if (download.state == "stopped") {
    1.70 +    ok(pausing, "Download stopped by pause()");
    1.71 +    navigator.mozDownloadManager.remove(download)
    1.72 +             .then(checkRemoved, error);
    1.73 +  }
    1.74 +}
    1.75 +
    1.76 +var steps = [
    1.77 +  // Start by setting the pref to true.
    1.78 +  function() {
    1.79 +    SpecialPowers.pushPrefEnv({
    1.80 +      set: [["dom.mozDownloads.enabled", true]]
    1.81 +    }, next);
    1.82 +  },
    1.83 +
    1.84 +  // Setup permission and clear current list.
    1.85 +  function() {
    1.86 +    SpecialPowers.pushPermissions([
    1.87 +      {type: "downloads", allow: true, context: document}
    1.88 +    ], function() {
    1.89 +      navigator.mozDownloadManager.clearAllDone().then(next, error);
    1.90 +    });
    1.91 +  },
    1.92 +
    1.93 +  function(downloads) {
    1.94 +    ok(downloads.length == 0, "Start with an empty download list.");
    1.95 +    next();
    1.96 +  },
    1.97 +
    1.98 +  // Setup the event listeners.
    1.99 +  function() {
   1.100 +    navigator.mozDownloadManager.ondownloadstart =
   1.101 +      function(evt) {
   1.102 +        ok(true, "Download started");
   1.103 +        evt.download.addEventListener("statechange", downloadChange);
   1.104 +      }
   1.105 +    next();
   1.106 +  },
   1.107 +
   1.108 +  // Click on the <a download> to start the download.
   1.109 +  function() {
   1.110 +    document.getElementById("download1").click();
   1.111 +  }
   1.112 +];
   1.113 +
   1.114 +next();
   1.115 +
   1.116 +</script>
   1.117 +</pre>
   1.118 +</body>
   1.119 +</html>

mercurial