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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/downloads/test/unit/test_bug_406857.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * Test bug 406857 to make sure a download's referrer doesn't disappear when
    1.10 + * retrying the download.
    1.11 + */
    1.12 +
    1.13 +function run_test()
    1.14 +{
    1.15 +  if (oldDownloadManagerDisabled()) {
    1.16 +    return;
    1.17 +  }
    1.18 +
    1.19 +  let dm = Cc["@mozilla.org/download-manager;1"].
    1.20 +           getService(Ci.nsIDownloadManager);
    1.21 +  let db = dm.DBConnection;
    1.22 +  var httpserv = new HttpServer();
    1.23 +  httpserv.start(-1);
    1.24 +
    1.25 +  let stmt = db.createStatement(
    1.26 +    "INSERT INTO moz_downloads (source, target, state, referrer) " +
    1.27 +    "VALUES (?1, ?2, ?3, ?4)");
    1.28 +
    1.29 +  // Download from the test http server
    1.30 +  stmt.bindByIndex(0, "http://localhost:"+ httpserv.identity.primaryPort +
    1.31 +                      "/httpd.js");
    1.32 +
    1.33 +  // Download to a temp local file
    1.34 +  let file = Cc["@mozilla.org/file/directory_service;1"].
    1.35 +             getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    1.36 +  file.append("retry");
    1.37 +  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    1.38 +  stmt.bindByIndex(1, Cc["@mozilla.org/network/io-service;1"].
    1.39 +    getService(Ci.nsIIOService).newFileURI(file).spec);
    1.40 +
    1.41 +  // Start it as canceled
    1.42 +  stmt.bindByIndex(2, dm.DOWNLOAD_CANCELED);
    1.43 +
    1.44 +  // Add a referrer to make sure it doesn't disappear
    1.45 +  let referrer = "http://referrer.goes/here";
    1.46 +  stmt.bindByIndex(3, referrer);
    1.47 +
    1.48 +  // Add it!
    1.49 +  stmt.execute();
    1.50 +  stmt.finalize();
    1.51 +
    1.52 +  let listener = {
    1.53 +    onDownloadStateChange: function(aState, aDownload)
    1.54 +    {
    1.55 +      switch (aDownload.state) {
    1.56 +        case dm.DOWNLOAD_DOWNLOADING:
    1.57 +          do_check_eq(aDownload.referrer.spec, referrer);
    1.58 +          break;
    1.59 +        case dm.DOWNLOAD_FINISHED:
    1.60 +          do_check_eq(aDownload.referrer.spec, referrer);
    1.61 +
    1.62 +          dm.removeListener(listener);
    1.63 +          try { file.remove(false); } catch(e) { /* stupid windows box */ }
    1.64 +          httpserv.stop(do_test_finished);
    1.65 +          break;
    1.66 +        case dm.DOWNLOAD_FAILED:
    1.67 +        case dm.DOWNLOAD_CANCELED:
    1.68 +          httpserv.stop(function () {});
    1.69 +          do_throw("Unexpected download state change received, state: " +
    1.70 +                   aDownload.state);
    1.71 +          break;
    1.72 +      }
    1.73 +    }
    1.74 +  };
    1.75 +  dm.addListener(listener);
    1.76 +
    1.77 +  // Retry the download, and wait.
    1.78 +  dm.retryDownload(1);
    1.79 +
    1.80 +  do_test_pending();
    1.81 +}

mercurial