dom/downloads/tests/test_downloads_pause_remove.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 <!DOCTYPE html>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=938023
     5 -->
     6 <head>
     7   <title>Test for Bug 938023 Downloads API</title>
     8   <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     9   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    10   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    11 </head>
    12 <body>
    14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
    15 <p id="display"></p>
    16 <div id="content" style="display: none">
    17 </div>
    18 <a href="serve_file.sjs?contentType=application/octet-stream&size=102400&rate=1024" download="test.bin" id="download1">Large Download</a>
    19 <pre id="test">
    20 <script class="testbody" type="text/javascript;version=1.7">
    22 // Testing pausing a download and then removing it.
    24 SimpleTest.waitForExplicitFinish();
    26 var index = -1;
    28 function next(args) {
    29   index += 1;
    30   if (index >= steps.length) {
    31     ok(false, "Shouldn't get here!");
    32     return;
    33   }
    34   try {
    35     steps[index](args);
    36   } catch(ex) {
    37     ok(false, "Caught exception", ex);
    38   }
    39 }
    41 var pausing = false;
    43 // Catch all error function.
    44 function error() {
    45   ok(false, "API failure");
    46   SimpleTest.finish();
    47 }
    49 function checkDownloadList(downloads) {
    50   ok(downloads.length == 0, "No downloads left");
    51   SimpleTest.finish();
    52 }
    54 function checkRemoved(download) {
    55   ok(download.state == "finalized", "Download removed.");
    56   navigator.mozDownloadManager.getDownloads()
    57            .then(checkDownloadList, error);
    58 }
    60 function downloadChange(evt) {
    61   var download = evt.download;
    63   if (download.state == "downloading" && !pausing) {
    64     pausing = true;
    65     download.pause();
    66   } else if (download.state == "stopped") {
    67     ok(pausing, "Download stopped by pause()");
    68     navigator.mozDownloadManager.remove(download)
    69              .then(checkRemoved, error);
    70   }
    71 }
    73 var steps = [
    74   // Start by setting the pref to true.
    75   function() {
    76     SpecialPowers.pushPrefEnv({
    77       set: [["dom.mozDownloads.enabled", true]]
    78     }, next);
    79   },
    81   // Setup permission and clear current list.
    82   function() {
    83     SpecialPowers.pushPermissions([
    84       {type: "downloads", allow: true, context: document}
    85     ], function() {
    86       navigator.mozDownloadManager.clearAllDone().then(next, error);
    87     });
    88   },
    90   function(downloads) {
    91     ok(downloads.length == 0, "Start with an empty download list.");
    92     next();
    93   },
    95   // Setup the event listeners.
    96   function() {
    97     navigator.mozDownloadManager.ondownloadstart =
    98       function(evt) {
    99         ok(true, "Download started");
   100         evt.download.addEventListener("statechange", downloadChange);
   101       }
   102     next();
   103   },
   105   // Click on the <a download> to start the download.
   106   function() {
   107     document.getElementById("download1").click();
   108   }
   109 ];
   111 next();
   113 </script>
   114 </pre>
   115 </body>
   116 </html>

mercurial