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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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 bug 406857 to make sure a download's referrer doesn't disappear when
     7  * retrying the download.
     8  */
    10 function run_test()
    11 {
    12   if (oldDownloadManagerDisabled()) {
    13     return;
    14   }
    16   let dm = Cc["@mozilla.org/download-manager;1"].
    17            getService(Ci.nsIDownloadManager);
    18   let db = dm.DBConnection;
    19   var httpserv = new HttpServer();
    20   httpserv.start(-1);
    22   let stmt = db.createStatement(
    23     "INSERT INTO moz_downloads (source, target, state, referrer) " +
    24     "VALUES (?1, ?2, ?3, ?4)");
    26   // Download from the test http server
    27   stmt.bindByIndex(0, "http://localhost:"+ httpserv.identity.primaryPort +
    28                       "/httpd.js");
    30   // Download to a temp local file
    31   let file = Cc["@mozilla.org/file/directory_service;1"].
    32              getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    33   file.append("retry");
    34   file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    35   stmt.bindByIndex(1, Cc["@mozilla.org/network/io-service;1"].
    36     getService(Ci.nsIIOService).newFileURI(file).spec);
    38   // Start it as canceled
    39   stmt.bindByIndex(2, dm.DOWNLOAD_CANCELED);
    41   // Add a referrer to make sure it doesn't disappear
    42   let referrer = "http://referrer.goes/here";
    43   stmt.bindByIndex(3, referrer);
    45   // Add it!
    46   stmt.execute();
    47   stmt.finalize();
    49   let listener = {
    50     onDownloadStateChange: function(aState, aDownload)
    51     {
    52       switch (aDownload.state) {
    53         case dm.DOWNLOAD_DOWNLOADING:
    54           do_check_eq(aDownload.referrer.spec, referrer);
    55           break;
    56         case dm.DOWNLOAD_FINISHED:
    57           do_check_eq(aDownload.referrer.spec, referrer);
    59           dm.removeListener(listener);
    60           try { file.remove(false); } catch(e) { /* stupid windows box */ }
    61           httpserv.stop(do_test_finished);
    62           break;
    63         case dm.DOWNLOAD_FAILED:
    64         case dm.DOWNLOAD_CANCELED:
    65           httpserv.stop(function () {});
    66           do_throw("Unexpected download state change received, state: " +
    67                    aDownload.state);
    68           break;
    69       }
    70     }
    71   };
    72   dm.addListener(listener);
    74   // Retry the download, and wait.
    75   dm.retryDownload(1);
    77   do_test_pending();
    78 }

mercurial