toolkit/components/downloads/test/unit/test_resume.js

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 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /**
     6  * Test download resume to do real-resume instead of stalling-the-channel
     7  * resume. Also test bug 395537 for resuming files that are deleted before
     8  * they're resumed. Bug 398216 is checked by making sure the reported progress
     9  * and file size are updated for finished downloads.
    10  */
    12 const nsIF = Ci.nsIFile;
    13 const nsIDM = Ci.nsIDownloadManager;
    14 const nsIWBP = Ci.nsIWebBrowserPersist;
    15 const nsIWPL = Ci.nsIWebProgressListener;
    16 const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDM);
    18 function run_test()
    19 {
    20   if (oldDownloadManagerDisabled()) {
    21     return;
    22   }
    24   /**
    25    * 1. Create data for http server to send
    26    */
    27   // data starts at 10 bytes
    28   var data = "1234567890";
    29   // data * 10^4 = 100,000 bytes (actually 101,111 bytes with newline)
    30   for (var i = 0; i < 4; i++) {
    31     data = [data,data,data,data,data,data,data,data,data,data,"\n"].join("");
    32   }
    34   /**
    35    * 2. Start the http server that can handle resume
    36    */
    37   var httpserv = new HttpServer();
    38   var didResumeServer = false;
    39   httpserv.registerPathHandler("/resume", function(meta, resp) {
    40     var body = data;
    41     resp.setHeader("Content-Type", "text/html", false);
    42     if (meta.hasHeader("Range")) {
    43       // track that we resumed for testing
    44       didResumeServer = true;
    45       // Syntax: bytes=[from]-[to] (we don't support multiple ranges)
    46       var matches = meta.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
    47       var from = (matches[1] === undefined) ? 0 : matches[1];
    48       var to = (matches[2] === undefined) ? data.length - 1 : matches[2];
    49       if (from >= data.length) {
    50         resp.setStatusLine(meta.httpVersion, 416, "Start pos too high");
    51         resp.setHeader("Content-Range", "*/" + data.length, false);
    52         return;
    53       }
    54       body = body.substring(from, to + 1);
    55       // always respond to successful range requests with 206
    56       resp.setStatusLine(meta.httpVersion, 206, "Partial Content");
    57       resp.setHeader("Content-Range", from + "-" + to + "/" + data.length, false);
    58     }
    59     resp.bodyOutputStream.write(body, body.length);
    60   });
    61   httpserv.start(-1);
    63   /**
    64    * 3. Perform various actions for certain download states
    65    */
    66   var didPause = false;
    67   var didResumeDownload = false;
    68   dm.addListener({
    69     onDownloadStateChange: function(a, aDl) {
    70       if (aDl.state == nsIDM.DOWNLOAD_DOWNLOADING && !didPause) {
    71         /**
    72          * (1) queued -> downloading = pause the download
    73          */
    74         dm.pauseDownload(aDl.id);
    75       } else if (aDl.state == nsIDM.DOWNLOAD_PAUSED) {
    76         /**
    77          * (2) downloading -> paused = remove the file
    78          */
    79         didPause = true;
    80         // Test bug 395537 by removing the file before we actually resume
    81         aDl.targetFile.remove(false);
    82       } else if (aDl.state == nsIDM.DOWNLOAD_FINISHED) {
    83         /**
    84          * (4) downloading (resumed) -> finished = check tests
    85          */
    86         // did we pause at all?
    87         do_check_true(didPause);
    88         // did we real-resume and not fake-resume?
    89         do_check_true(didResumeDownload);
    90         // extra real-resume check for the server
    91         do_check_true(didResumeServer);
    92         // did we download the whole file?
    93         do_check_eq(data.length, aDl.targetFile.fileSize);
    94         // extra sanity checks on size (test bug 398216)
    95         do_check_eq(data.length, aDl.amountTransferred);
    96         do_check_eq(data.length, aDl.size);
    98         httpserv.stop(do_test_finished);
    99       }
   100     },
   101     onStateChange: function(a, b, aState, d, aDl) {
   102       if ((aState & nsIWPL.STATE_STOP) && didPause && !didResumeServer &&
   103           !didResumeDownload) {
   104         /**
   105          * (3) paused -> stopped = resume the download
   106          */
   107         dm.resumeDownload(aDl.id);
   108         didResumeDownload = true;
   109       }
   110     },
   111     onProgressChange: function(a, b, c, d, e, f, g) { },
   112     onSecurityChange: function(a, b, c, d) { }
   113   });
   114   dm.addListener(getDownloadListener());
   116   /**
   117    * 4. Start the download
   118    */
   119   var destFile = dirSvc.get("ProfD", nsIF);
   120   destFile.append("resumed");
   121   if (destFile.exists())
   122     destFile.remove(false);
   123   var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
   124                 createInstance(nsIWBP);
   125   persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
   126                          nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
   127                          nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
   128   var dl = dm.addDownload(nsIDM.DOWNLOAD_TYPE_DOWNLOAD,
   129                           createURI("http://localhost:" +
   130                                     httpserv.identity.primaryPort + "/resume"),
   131                           createURI(destFile), null, null,
   132                           Math.round(Date.now() * 1000), null, persist, false);
   133   persist.progressListener = dl.QueryInterface(nsIWPL);
   134   persist.saveURI(dl.source, null, null, null, null, dl.targetFile, null);
   136   // Mark as pending, so clear this when we actually finish the download
   137   do_test_pending();
   138 }

mercurial