Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * Test bug 406857 to make sure a download's referrer doesn't disappear when |
michael@0 | 7 | * retrying the download. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | function run_test() |
michael@0 | 11 | { |
michael@0 | 12 | if (oldDownloadManagerDisabled()) { |
michael@0 | 13 | return; |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | let dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 17 | getService(Ci.nsIDownloadManager); |
michael@0 | 18 | let db = dm.DBConnection; |
michael@0 | 19 | var httpserv = new HttpServer(); |
michael@0 | 20 | httpserv.start(-1); |
michael@0 | 21 | |
michael@0 | 22 | let stmt = db.createStatement( |
michael@0 | 23 | "INSERT INTO moz_downloads (source, target, state, referrer) " + |
michael@0 | 24 | "VALUES (?1, ?2, ?3, ?4)"); |
michael@0 | 25 | |
michael@0 | 26 | // Download from the test http server |
michael@0 | 27 | stmt.bindByIndex(0, "http://localhost:"+ httpserv.identity.primaryPort + |
michael@0 | 28 | "/httpd.js"); |
michael@0 | 29 | |
michael@0 | 30 | // Download to a temp local file |
michael@0 | 31 | let file = Cc["@mozilla.org/file/directory_service;1"]. |
michael@0 | 32 | getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); |
michael@0 | 33 | file.append("retry"); |
michael@0 | 34 | file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
michael@0 | 35 | stmt.bindByIndex(1, Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 36 | getService(Ci.nsIIOService).newFileURI(file).spec); |
michael@0 | 37 | |
michael@0 | 38 | // Start it as canceled |
michael@0 | 39 | stmt.bindByIndex(2, dm.DOWNLOAD_CANCELED); |
michael@0 | 40 | |
michael@0 | 41 | // Add a referrer to make sure it doesn't disappear |
michael@0 | 42 | let referrer = "http://referrer.goes/here"; |
michael@0 | 43 | stmt.bindByIndex(3, referrer); |
michael@0 | 44 | |
michael@0 | 45 | // Add it! |
michael@0 | 46 | stmt.execute(); |
michael@0 | 47 | stmt.finalize(); |
michael@0 | 48 | |
michael@0 | 49 | let listener = { |
michael@0 | 50 | onDownloadStateChange: function(aState, aDownload) |
michael@0 | 51 | { |
michael@0 | 52 | switch (aDownload.state) { |
michael@0 | 53 | case dm.DOWNLOAD_DOWNLOADING: |
michael@0 | 54 | do_check_eq(aDownload.referrer.spec, referrer); |
michael@0 | 55 | break; |
michael@0 | 56 | case dm.DOWNLOAD_FINISHED: |
michael@0 | 57 | do_check_eq(aDownload.referrer.spec, referrer); |
michael@0 | 58 | |
michael@0 | 59 | dm.removeListener(listener); |
michael@0 | 60 | try { file.remove(false); } catch(e) { /* stupid windows box */ } |
michael@0 | 61 | httpserv.stop(do_test_finished); |
michael@0 | 62 | break; |
michael@0 | 63 | case dm.DOWNLOAD_FAILED: |
michael@0 | 64 | case dm.DOWNLOAD_CANCELED: |
michael@0 | 65 | httpserv.stop(function () {}); |
michael@0 | 66 | do_throw("Unexpected download state change received, state: " + |
michael@0 | 67 | aDownload.state); |
michael@0 | 68 | break; |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | }; |
michael@0 | 72 | dm.addListener(listener); |
michael@0 | 73 | |
michael@0 | 74 | // Retry the download, and wait. |
michael@0 | 75 | dm.retryDownload(1); |
michael@0 | 76 | |
michael@0 | 77 | do_test_pending(); |
michael@0 | 78 | } |