1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/downloads/test/unit/test_cancel_download_files_removed.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// This file tests Bug 593815 - specifically that downloaded files 1.8 +// are cleaned up correctly when a download is cancelled 1.9 + 1.10 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.11 +Components.utils.import("resource://gre/modules/NetUtil.jsm"); 1.12 +do_load_manifest("test_downloads.manifest"); 1.13 + 1.14 +let httpserver = null; 1.15 +let currentTest = 0; 1.16 + 1.17 +function WindowContext() { } 1.18 +WindowContext.prototype = { 1.19 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor]), 1.20 + getInterface: XPCOMUtils.generateQI([Ci.nsIURIContentListener, 1.21 + Ci.nsILoadGroup]), 1.22 + 1.23 + /* nsIURIContentListener */ 1.24 + onStartURIOpen: function (uri) { }, 1.25 + isPreferred: function (type, desiredtype) { return false; }, 1.26 + 1.27 + /* nsILoadGroup */ 1.28 + addRequest: function (request, context) { }, 1.29 + removeRequest: function (request, context, status) { } 1.30 +}; 1.31 + 1.32 +let DownloadListener = { 1.33 + set : null, 1.34 + init: function () { 1.35 + Services.obs.addObserver(this, "dl-start", true); 1.36 + Services.obs.addObserver(this, "dl-done", true); 1.37 + Services.obs.addObserver(this, "dl-cancel", true); 1.38 + Services.obs.addObserver(this, "dl-fail", true); 1.39 + }, 1.40 + 1.41 + // check that relevant cancel operations have been performed 1.42 + // currently this just means removing the target file 1.43 + onCancel: function(aSubject, aTopic, aData) { 1.44 + let dl = aSubject.QueryInterface(Ci.nsIDownload); 1.45 + do_check_false(dl.targetFile.exists()); 1.46 + runNextTest(); 1.47 + }, 1.48 + 1.49 + observe: function (aSubject, aTopic, aData) { 1.50 + switch(aTopic) { 1.51 + case "dl-start" : 1.52 + // cancel, pause, or resume the download 1.53 + // depending on parameters in this.set 1.54 + let dl = aSubject.QueryInterface(Ci.nsIDownload); 1.55 + 1.56 + if (this.set.doPause) { 1.57 + downloadUtils.downloadManager.pauseDownload(dl.id); 1.58 + } 1.59 + 1.60 + if (this.set.doResume) { 1.61 + downloadUtils.downloadManager.resumeDownload(dl.id); 1.62 + } 1.63 + 1.64 + downloadUtils.downloadManager.cancelDownload(dl.id); 1.65 + break; 1.66 + case "dl-cancel" : 1.67 + this.onCancel(aSubject, aTopic, aData); 1.68 + break; 1.69 + case "dl-fail" : 1.70 + do_throw("Download failed"); 1.71 + break; 1.72 + case "dl-done" : 1.73 + do_throw("Download finished"); 1.74 + break; 1.75 + } 1.76 + }, 1.77 + 1.78 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, 1.79 + Ci.nsISupportsWeakReference]) 1.80 +} 1.81 + 1.82 +// loads the uri given in tests[currentTest] 1.83 +// finishes tests if there are no more test files listed 1.84 +function runNextTest() 1.85 +{ 1.86 + if (currentTest == tests.length) { 1.87 + httpserver.stop(do_test_finished); 1.88 + return; 1.89 + } 1.90 + 1.91 + let set = DownloadListener.set = tests[currentTest]; 1.92 + currentTest++; 1.93 + 1.94 + let channel = NetUtil.newChannel("http://localhost:" + 1.95 + httpserver.identity.primaryPort + 1.96 + set.serverPath); 1.97 + let uriloader = Cc["@mozilla.org/uriloader;1"].getService(Ci.nsIURILoader); 1.98 + uriloader.openURI(channel, Ci.nsIURILoader.IS_CONTENT_PREFERRED, 1.99 + new WindowContext()); 1.100 +} 1.101 + 1.102 +// sends the responses for the files. sends the same content twice if we resume 1.103 +// the download 1.104 +function getResponse(aSet) { 1.105 + return function(aMetadata, aResponse) { 1.106 + aResponse.setHeader("Content-Type", "text/plain", false); 1.107 + if (aMetadata.hasHeader("Range")) { 1.108 + var matches = aMetadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/); 1.109 + aResponse.setStatusLine(aMetadata.httpVersion, 206, "Partial Content"); 1.110 + aResponse.bodyOutputStream.write(aSet.data, aSet.data.length); 1.111 + return; 1.112 + } 1.113 + aResponse.setHeader("Accept-Ranges", "bytes", false); 1.114 + aResponse.setHeader("Content-Disposition", "attachment; filename=test.txt;", false); 1.115 + aResponse.bodyOutputStream.write(aSet.data, aSet.data.length); 1.116 + } 1.117 +} 1.118 + 1.119 +// files to be downloaded. For these tests, files will be cancelled either: 1.120 +// 1.) during the download 1.121 +// 2.) while they are paused 1.122 +// 3.) after they have been resumed 1.123 +let tests = [ 1.124 + { serverPath: "/test1.html", data: "Test data 1" }, 1.125 + { serverPath: "/test2.html", data: "Test data 2", doPause: true }, 1.126 + { serverPath: "/test3.html", data: "Test data 3", doPause: true, doResume: true}, 1.127 +]; 1.128 + 1.129 +function run_test() { 1.130 + if (oldDownloadManagerDisabled()) { 1.131 + return; 1.132 + } 1.133 + 1.134 + // setup a download listener to run tests during and after the download 1.135 + DownloadListener.init(); 1.136 + Services.prefs.setBoolPref("browser.download.manager.showWhenStarting", false); 1.137 + 1.138 + httpserver = new HttpServer(); 1.139 + httpserver.start(-1); 1.140 + do_test_pending(); 1.141 + 1.142 + // setup files to be download, each with the same suggested filename 1.143 + // from the server, but with different contents 1.144 + for(let i = 0; i < tests.length; i++) { 1.145 + let set = tests[i]; 1.146 + httpserver.registerPathHandler(set.serverPath, getResponse(set)); 1.147 + } 1.148 + 1.149 + runNextTest(); // start downloading the first file 1.150 +}