Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // This file tests Bug 593815 - specifically that downloaded files |
michael@0 | 5 | // are cleaned up correctly when a download is cancelled |
michael@0 | 6 | |
michael@0 | 7 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 8 | Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 9 | do_load_manifest("test_downloads.manifest"); |
michael@0 | 10 | |
michael@0 | 11 | let httpserver = null; |
michael@0 | 12 | let currentTest = 0; |
michael@0 | 13 | |
michael@0 | 14 | function WindowContext() { } |
michael@0 | 15 | WindowContext.prototype = { |
michael@0 | 16 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor]), |
michael@0 | 17 | getInterface: XPCOMUtils.generateQI([Ci.nsIURIContentListener, |
michael@0 | 18 | Ci.nsILoadGroup]), |
michael@0 | 19 | |
michael@0 | 20 | /* nsIURIContentListener */ |
michael@0 | 21 | onStartURIOpen: function (uri) { }, |
michael@0 | 22 | isPreferred: function (type, desiredtype) { return false; }, |
michael@0 | 23 | |
michael@0 | 24 | /* nsILoadGroup */ |
michael@0 | 25 | addRequest: function (request, context) { }, |
michael@0 | 26 | removeRequest: function (request, context, status) { } |
michael@0 | 27 | }; |
michael@0 | 28 | |
michael@0 | 29 | let DownloadListener = { |
michael@0 | 30 | set : null, |
michael@0 | 31 | init: function () { |
michael@0 | 32 | Services.obs.addObserver(this, "dl-start", true); |
michael@0 | 33 | Services.obs.addObserver(this, "dl-done", true); |
michael@0 | 34 | Services.obs.addObserver(this, "dl-cancel", true); |
michael@0 | 35 | Services.obs.addObserver(this, "dl-fail", true); |
michael@0 | 36 | }, |
michael@0 | 37 | |
michael@0 | 38 | // check that relevant cancel operations have been performed |
michael@0 | 39 | // currently this just means removing the target file |
michael@0 | 40 | onCancel: function(aSubject, aTopic, aData) { |
michael@0 | 41 | let dl = aSubject.QueryInterface(Ci.nsIDownload); |
michael@0 | 42 | do_check_false(dl.targetFile.exists()); |
michael@0 | 43 | runNextTest(); |
michael@0 | 44 | }, |
michael@0 | 45 | |
michael@0 | 46 | observe: function (aSubject, aTopic, aData) { |
michael@0 | 47 | switch(aTopic) { |
michael@0 | 48 | case "dl-start" : |
michael@0 | 49 | // cancel, pause, or resume the download |
michael@0 | 50 | // depending on parameters in this.set |
michael@0 | 51 | let dl = aSubject.QueryInterface(Ci.nsIDownload); |
michael@0 | 52 | |
michael@0 | 53 | if (this.set.doPause) { |
michael@0 | 54 | downloadUtils.downloadManager.pauseDownload(dl.id); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | if (this.set.doResume) { |
michael@0 | 58 | downloadUtils.downloadManager.resumeDownload(dl.id); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | downloadUtils.downloadManager.cancelDownload(dl.id); |
michael@0 | 62 | break; |
michael@0 | 63 | case "dl-cancel" : |
michael@0 | 64 | this.onCancel(aSubject, aTopic, aData); |
michael@0 | 65 | break; |
michael@0 | 66 | case "dl-fail" : |
michael@0 | 67 | do_throw("Download failed"); |
michael@0 | 68 | break; |
michael@0 | 69 | case "dl-done" : |
michael@0 | 70 | do_throw("Download finished"); |
michael@0 | 71 | break; |
michael@0 | 72 | } |
michael@0 | 73 | }, |
michael@0 | 74 | |
michael@0 | 75 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, |
michael@0 | 76 | Ci.nsISupportsWeakReference]) |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // loads the uri given in tests[currentTest] |
michael@0 | 80 | // finishes tests if there are no more test files listed |
michael@0 | 81 | function runNextTest() |
michael@0 | 82 | { |
michael@0 | 83 | if (currentTest == tests.length) { |
michael@0 | 84 | httpserver.stop(do_test_finished); |
michael@0 | 85 | return; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | let set = DownloadListener.set = tests[currentTest]; |
michael@0 | 89 | currentTest++; |
michael@0 | 90 | |
michael@0 | 91 | let channel = NetUtil.newChannel("http://localhost:" + |
michael@0 | 92 | httpserver.identity.primaryPort + |
michael@0 | 93 | set.serverPath); |
michael@0 | 94 | let uriloader = Cc["@mozilla.org/uriloader;1"].getService(Ci.nsIURILoader); |
michael@0 | 95 | uriloader.openURI(channel, Ci.nsIURILoader.IS_CONTENT_PREFERRED, |
michael@0 | 96 | new WindowContext()); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | // sends the responses for the files. sends the same content twice if we resume |
michael@0 | 100 | // the download |
michael@0 | 101 | function getResponse(aSet) { |
michael@0 | 102 | return function(aMetadata, aResponse) { |
michael@0 | 103 | aResponse.setHeader("Content-Type", "text/plain", false); |
michael@0 | 104 | if (aMetadata.hasHeader("Range")) { |
michael@0 | 105 | var matches = aMetadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/); |
michael@0 | 106 | aResponse.setStatusLine(aMetadata.httpVersion, 206, "Partial Content"); |
michael@0 | 107 | aResponse.bodyOutputStream.write(aSet.data, aSet.data.length); |
michael@0 | 108 | return; |
michael@0 | 109 | } |
michael@0 | 110 | aResponse.setHeader("Accept-Ranges", "bytes", false); |
michael@0 | 111 | aResponse.setHeader("Content-Disposition", "attachment; filename=test.txt;", false); |
michael@0 | 112 | aResponse.bodyOutputStream.write(aSet.data, aSet.data.length); |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | // files to be downloaded. For these tests, files will be cancelled either: |
michael@0 | 117 | // 1.) during the download |
michael@0 | 118 | // 2.) while they are paused |
michael@0 | 119 | // 3.) after they have been resumed |
michael@0 | 120 | let tests = [ |
michael@0 | 121 | { serverPath: "/test1.html", data: "Test data 1" }, |
michael@0 | 122 | { serverPath: "/test2.html", data: "Test data 2", doPause: true }, |
michael@0 | 123 | { serverPath: "/test3.html", data: "Test data 3", doPause: true, doResume: true}, |
michael@0 | 124 | ]; |
michael@0 | 125 | |
michael@0 | 126 | function run_test() { |
michael@0 | 127 | if (oldDownloadManagerDisabled()) { |
michael@0 | 128 | return; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | // setup a download listener to run tests during and after the download |
michael@0 | 132 | DownloadListener.init(); |
michael@0 | 133 | Services.prefs.setBoolPref("browser.download.manager.showWhenStarting", false); |
michael@0 | 134 | |
michael@0 | 135 | httpserver = new HttpServer(); |
michael@0 | 136 | httpserver.start(-1); |
michael@0 | 137 | do_test_pending(); |
michael@0 | 138 | |
michael@0 | 139 | // setup files to be download, each with the same suggested filename |
michael@0 | 140 | // from the server, but with different contents |
michael@0 | 141 | for(let i = 0; i < tests.length; i++) { |
michael@0 | 142 | let set = tests[i]; |
michael@0 | 143 | httpserver.registerPathHandler(set.serverPath, getResponse(set)); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | runNextTest(); // start downloading the first file |
michael@0 | 147 | } |