michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Test bug 406857 to make sure a download's referrer doesn't disappear when michael@0: * retrying the download. michael@0: */ michael@0: michael@0: function run_test() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: let dm = Cc["@mozilla.org/download-manager;1"]. michael@0: getService(Ci.nsIDownloadManager); michael@0: let db = dm.DBConnection; michael@0: var httpserv = new HttpServer(); michael@0: httpserv.start(-1); michael@0: michael@0: let stmt = db.createStatement( michael@0: "INSERT INTO moz_downloads (source, target, state, referrer) " + michael@0: "VALUES (?1, ?2, ?3, ?4)"); michael@0: michael@0: // Download from the test http server michael@0: stmt.bindByIndex(0, "http://localhost:"+ httpserv.identity.primaryPort + michael@0: "/httpd.js"); michael@0: michael@0: // Download to a temp local file michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); michael@0: file.append("retry"); michael@0: file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: stmt.bindByIndex(1, Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService).newFileURI(file).spec); michael@0: michael@0: // Start it as canceled michael@0: stmt.bindByIndex(2, dm.DOWNLOAD_CANCELED); michael@0: michael@0: // Add a referrer to make sure it doesn't disappear michael@0: let referrer = "http://referrer.goes/here"; michael@0: stmt.bindByIndex(3, referrer); michael@0: michael@0: // Add it! michael@0: stmt.execute(); michael@0: stmt.finalize(); michael@0: michael@0: let listener = { michael@0: onDownloadStateChange: function(aState, aDownload) michael@0: { michael@0: switch (aDownload.state) { michael@0: case dm.DOWNLOAD_DOWNLOADING: michael@0: do_check_eq(aDownload.referrer.spec, referrer); michael@0: break; michael@0: case dm.DOWNLOAD_FINISHED: michael@0: do_check_eq(aDownload.referrer.spec, referrer); michael@0: michael@0: dm.removeListener(listener); michael@0: try { file.remove(false); } catch(e) { /* stupid windows box */ } michael@0: httpserv.stop(do_test_finished); michael@0: break; michael@0: case dm.DOWNLOAD_FAILED: michael@0: case dm.DOWNLOAD_CANCELED: michael@0: httpserv.stop(function () {}); michael@0: do_throw("Unexpected download state change received, state: " + michael@0: aDownload.state); michael@0: break; michael@0: } michael@0: } michael@0: }; michael@0: dm.addListener(listener); michael@0: michael@0: // Retry the download, and wait. michael@0: dm.retryDownload(1); michael@0: michael@0: do_test_pending(); michael@0: }