Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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 after downloading two files |
michael@0 | 5 | // with the same name, the download manager still points to the correct files |
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 | prevFiles : [], |
michael@0 | 32 | |
michael@0 | 33 | init: function () { |
michael@0 | 34 | Services.obs.addObserver(this, "dl-start", true); |
michael@0 | 35 | Services.obs.addObserver(this, "dl-done", true); |
michael@0 | 36 | }, |
michael@0 | 37 | |
michael@0 | 38 | observe: function (aSubject, aTopic, aData) { |
michael@0 | 39 | |
michael@0 | 40 | if (aTopic == "dl-start") { |
michael@0 | 41 | // pause the download if requested |
michael@0 | 42 | if (this.set.doPause) { |
michael@0 | 43 | let dl = aSubject.QueryInterface(Ci.nsIDownload); |
michael@0 | 44 | // Don't pause immediately, otherwise the external helper app handler |
michael@0 | 45 | // won't be able to assign a permanent file name. |
michael@0 | 46 | do_execute_soon(function() { |
michael@0 | 47 | downloadUtils.downloadManager.pauseDownload(dl.id); |
michael@0 | 48 | do_timeout(1000, function() { |
michael@0 | 49 | downloadUtils.downloadManager.resumeDownload(dl.id); |
michael@0 | 50 | }); |
michael@0 | 51 | }); |
michael@0 | 52 | } |
michael@0 | 53 | } else if (aTopic == "dl-done") { |
michael@0 | 54 | // check that no two files have the same filename in the download manager |
michael@0 | 55 | let file = aSubject.QueryInterface(Ci.nsIDownload).targetFile; |
michael@0 | 56 | for each (let prevFile in this.prevFiles) { |
michael@0 | 57 | do_check_neq(file.leafName, prevFile.leafName); |
michael@0 | 58 | } |
michael@0 | 59 | this.prevFiles.push(file); |
michael@0 | 60 | |
michael@0 | 61 | // get the contents of the file |
michael@0 | 62 | let fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); |
michael@0 | 63 | fis.init(file, -1, -1, 0); |
michael@0 | 64 | var cstream = Cc["@mozilla.org/intl/converter-input-stream;1"].createInstance(Ci.nsIConverterInputStream); |
michael@0 | 65 | cstream.init(fis, "UTF-8", 0, 0); |
michael@0 | 66 | |
michael@0 | 67 | let val = ""; |
michael@0 | 68 | let (str = {}) { |
michael@0 | 69 | let read = 0; |
michael@0 | 70 | do { |
michael@0 | 71 | read = cstream.readString(0xffffffff, str); |
michael@0 | 72 | val += str.value; |
michael@0 | 73 | } while (read != 0); |
michael@0 | 74 | } |
michael@0 | 75 | cstream.close(); |
michael@0 | 76 | |
michael@0 | 77 | // check if the file contents match the expected ones |
michael@0 | 78 | if (this.set.doPause) { |
michael@0 | 79 | do_check_eq(val, this.set.data + this.set.data); // files that have been paused have the same data twice |
michael@0 | 80 | } else { |
michael@0 | 81 | do_check_eq(val, this.set.data); |
michael@0 | 82 | } |
michael@0 | 83 | runNextTest(); // download the next file |
michael@0 | 84 | } |
michael@0 | 85 | }, |
michael@0 | 86 | |
michael@0 | 87 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, |
michael@0 | 88 | Ci.nsISupportsWeakReference]) |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | /* |
michael@0 | 92 | Each test will download a file from the server. |
michael@0 | 93 | */ |
michael@0 | 94 | function runNextTest() |
michael@0 | 95 | { |
michael@0 | 96 | if (currentTest == tests.length) { |
michael@0 | 97 | for each (var file in DownloadListener.prevFiles) { |
michael@0 | 98 | try { |
michael@0 | 99 | file.remove(false); |
michael@0 | 100 | } catch (ex) { |
michael@0 | 101 | try { |
michael@0 | 102 | do_report_unexpected_exception(ex, "while removing " + file.path); |
michael@0 | 103 | } catch (ex if ex == Components.results.NS_ERROR_ABORT) { |
michael@0 | 104 | /* swallow */ |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | httpserver.stop(do_test_finished); |
michael@0 | 109 | return; |
michael@0 | 110 | } |
michael@0 | 111 | let set = DownloadListener.set = tests[currentTest]; |
michael@0 | 112 | currentTest++; |
michael@0 | 113 | |
michael@0 | 114 | let channel = NetUtil.newChannel("http://localhost:" + |
michael@0 | 115 | httpserver.identity.primaryPort + |
michael@0 | 116 | set.serverURL); |
michael@0 | 117 | let uriloader = Cc["@mozilla.org/uriloader;1"].getService(Ci.nsIURILoader); |
michael@0 | 118 | uriloader.openURI(channel, Ci.nsIURILoader.IS_CONTENT_PREFERRED, |
michael@0 | 119 | new WindowContext()); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | // sends the responses for the files. sends the same content twice if we resume |
michael@0 | 123 | // the download |
michael@0 | 124 | function getResponse(aSet) { |
michael@0 | 125 | return function(aMetadata, aResponse) { |
michael@0 | 126 | aResponse.setHeader("Content-Type", "text/plain", false); |
michael@0 | 127 | if (aMetadata.hasHeader("Range")) { |
michael@0 | 128 | var matches = aMetadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/); |
michael@0 | 129 | aResponse.setStatusLine(aMetadata.httpVersion, 206, "Partial Content"); |
michael@0 | 130 | aResponse.bodyOutputStream.write(aSet.data, aSet.data.length); |
michael@0 | 131 | return; |
michael@0 | 132 | } |
michael@0 | 133 | aResponse.setHeader("Accept-Ranges", "bytes", false); |
michael@0 | 134 | aResponse.setHeader("Content-Disposition", "attachment; filename=test.txt;", false); |
michael@0 | 135 | aResponse.bodyOutputStream.write(aSet.data, aSet.data.length); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | // files to be downloaded. All files will have the same suggested filename, but |
michael@0 | 140 | // should contain different data. doPause will cause the download to pause and resume |
michael@0 | 141 | // itself |
michael@0 | 142 | let tests = [ |
michael@0 | 143 | { serverURL: "/test1.html", data: "Test data 1", doPause: false }, |
michael@0 | 144 | { serverURL: "/test2.html", data: "Test data 2", doPause: false }, |
michael@0 | 145 | { serverURL: "/test3.html", data: "Test data 3", doPause: true } |
michael@0 | 146 | ]; |
michael@0 | 147 | |
michael@0 | 148 | function run_test() { |
michael@0 | 149 | if (oldDownloadManagerDisabled()) { |
michael@0 | 150 | return; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | // setup a download listener to run tests after each download finished |
michael@0 | 154 | DownloadListener.init(); |
michael@0 | 155 | Services.prefs.setBoolPref("browser.download.manager.showWhenStarting", false); |
michael@0 | 156 | |
michael@0 | 157 | httpserver = new HttpServer(); |
michael@0 | 158 | httpserver.start(-1); |
michael@0 | 159 | do_test_pending(); |
michael@0 | 160 | |
michael@0 | 161 | // setup files to be download, each with the same suggested filename |
michael@0 | 162 | // from the server, but with different contents |
michael@0 | 163 | for(let i = 0; i < tests.length; i++) { |
michael@0 | 164 | let set = tests[i]; |
michael@0 | 165 | httpserver.registerPathHandler(set.serverURL, getResponse(set)); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | runNextTest(); // start downloading the first file |
michael@0 | 169 | } |