dom/downloads/tests/test_downloads_large.html

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

mercurial