toolkit/components/downloads/test/unit/head_download_manager.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 // This file tests the download manager backend
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cu = Components.utils;
michael@0 10 const Cr = Components.results;
michael@0 11
michael@0 12 do_get_profile();
michael@0 13
michael@0 14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 15 Cu.import("resource://gre/modules/Services.jsm");
michael@0 16 Cu.import("resource://testing-common/httpd.js");
michael@0 17 Cu.import("resource://gre/modules/PlacesUtils.jsm");
michael@0 18 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 19 "resource://gre/modules/Promise.jsm");
michael@0 20
michael@0 21 var downloadUtils = { };
michael@0 22 XPCOMUtils.defineLazyServiceGetter(downloadUtils,
michael@0 23 "downloadManager",
michael@0 24 "@mozilla.org/download-manager;1",
michael@0 25 Ci.nsIDownloadManager);
michael@0 26
michael@0 27 function createURI(aObj)
michael@0 28 {
michael@0 29 var ios = Cc["@mozilla.org/network/io-service;1"].
michael@0 30 getService(Ci.nsIIOService);
michael@0 31 return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
michael@0 32 ios.newURI(aObj, null, null);
michael@0 33 }
michael@0 34
michael@0 35 var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
michael@0 36 getService(Ci.nsIProperties);
michael@0 37
michael@0 38 var provider = {
michael@0 39 getFile: function(prop, persistent) {
michael@0 40 persistent.value = true;
michael@0 41 if (prop == "DLoads") {
michael@0 42 var file = dirSvc.get("ProfD", Ci.nsILocalFile);
michael@0 43 file.append("downloads.rdf");
michael@0 44 return file;
michael@0 45 }
michael@0 46 print("*** Throwing trying to get " + prop);
michael@0 47 throw Cr.NS_ERROR_FAILURE;
michael@0 48 },
michael@0 49 QueryInterface: function(iid) {
michael@0 50 if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
michael@0 51 iid.equals(Ci.nsISupports)) {
michael@0 52 return this;
michael@0 53 }
michael@0 54 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 55 }
michael@0 56 };
michael@0 57 dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
michael@0 58
michael@0 59 var gDownloadCount = 0;
michael@0 60 /**
michael@0 61 * Adds a download to the DM, and starts it.
michael@0 62 * @param server: a HttpServer used to serve the sourceURI
michael@0 63 * @param aParams (optional): an optional object which contains the function
michael@0 64 * parameters:
michael@0 65 * resultFileName: leaf node for the target file
michael@0 66 * targetFile: nsIFile for the target (overrides resultFileName)
michael@0 67 * sourceURI: the download source URI
michael@0 68 * downloadName: the display name of the download
michael@0 69 * runBeforeStart: a function to run before starting the download
michael@0 70 * isPrivate: whether the download is private or not
michael@0 71 */
michael@0 72 function addDownload(server, aParams)
michael@0 73 {
michael@0 74 if (!server)
michael@0 75 do_throw("Must provide a valid server.");
michael@0 76 const PORT = server.identity.primaryPort;
michael@0 77 if (!aParams)
michael@0 78 aParams = {};
michael@0 79 if (!("resultFileName" in aParams))
michael@0 80 aParams.resultFileName = "download.result";
michael@0 81 if (!("targetFile" in aParams)) {
michael@0 82 aParams.targetFile = dirSvc.get("ProfD", Ci.nsIFile);
michael@0 83 aParams.targetFile.append(aParams.resultFileName);
michael@0 84 }
michael@0 85 if (!("sourceURI" in aParams))
michael@0 86 aParams.sourceURI = "http://localhost:" + PORT + "/head_download_manager.js";
michael@0 87 if (!("downloadName" in aParams))
michael@0 88 aParams.downloadName = null;
michael@0 89 if (!("runBeforeStart" in aParams))
michael@0 90 aParams.runBeforeStart = function () {};
michael@0 91
michael@0 92 const nsIWBP = Ci.nsIWebBrowserPersist;
michael@0 93 var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
michael@0 94 .createInstance(Ci.nsIWebBrowserPersist);
michael@0 95 persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
michael@0 96 nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
michael@0 97 nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
michael@0 98
michael@0 99 // it is part of the active downloads the moment addDownload is called
michael@0 100 gDownloadCount++;
michael@0 101
michael@0 102 let dm = downloadUtils.downloadManager;
michael@0 103 var dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
michael@0 104 createURI(aParams.sourceURI),
michael@0 105 createURI(aParams.targetFile), aParams.downloadName, null,
michael@0 106 Math.round(Date.now() * 1000), null, persist, aParams.isPrivate);
michael@0 107
michael@0 108 // This will throw if it isn't found, and that would mean test failure, so no
michael@0 109 // try catch block
michael@0 110 if (!aParams.isPrivate)
michael@0 111 var test = dm.getDownload(dl.id);
michael@0 112
michael@0 113 aParams.runBeforeStart.call(undefined, dl);
michael@0 114
michael@0 115 persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener);
michael@0 116 persist.savePrivacyAwareURI(dl.source, null, null, null, null, dl.targetFile,
michael@0 117 aParams.isPrivate);
michael@0 118
michael@0 119 return dl;
michael@0 120 }
michael@0 121
michael@0 122 function getDownloadListener()
michael@0 123 {
michael@0 124 return {
michael@0 125 onDownloadStateChange: function(aState, aDownload)
michael@0 126 {
michael@0 127 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_QUEUED)
michael@0 128 do_test_pending();
michael@0 129
michael@0 130 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED ||
michael@0 131 aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_CANCELED ||
michael@0 132 aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FAILED) {
michael@0 133 gDownloadCount--;
michael@0 134 do_test_finished();
michael@0 135 }
michael@0 136
michael@0 137 if (gDownloadCount == 0 && typeof httpserv != "undefined" && httpserv)
michael@0 138 {
michael@0 139 do_test_pending();
michael@0 140 httpserv.stop(do_test_finished);
michael@0 141 }
michael@0 142 },
michael@0 143 onStateChange: function(a, b, c, d, e) { },
michael@0 144 onProgressChange: function(a, b, c, d, e, f, g) { },
michael@0 145 onSecurityChange: function(a, b, c, d) { }
michael@0 146 };
michael@0 147 }
michael@0 148
michael@0 149 /**
michael@0 150 * Asynchronously adds visits to a page.
michael@0 151 *
michael@0 152 * @param aPlaceInfo
michael@0 153 * Can be an nsIURI, in such a case a single LINK visit will be added.
michael@0 154 * Otherwise can be an object describing the visit to add, or an array
michael@0 155 * of these objects:
michael@0 156 * { uri: nsIURI of the page,
michael@0 157 * transition: one of the TRANSITION_* from nsINavHistoryService,
michael@0 158 * [optional] title: title of the page,
michael@0 159 * [optional] visitDate: visit date in microseconds from the epoch
michael@0 160 * [optional] referrer: nsIURI of the referrer for this visit
michael@0 161 * }
michael@0 162 *
michael@0 163 * @return {Promise}
michael@0 164 * @resolves When all visits have been added successfully.
michael@0 165 * @rejects JavaScript exception.
michael@0 166 */
michael@0 167 function promiseAddVisits(aPlaceInfo)
michael@0 168 {
michael@0 169 let deferred = Promise.defer();
michael@0 170 let places = [];
michael@0 171 if (aPlaceInfo instanceof Ci.nsIURI) {
michael@0 172 places.push({ uri: aPlaceInfo });
michael@0 173 }
michael@0 174 else if (Array.isArray(aPlaceInfo)) {
michael@0 175 places = places.concat(aPlaceInfo);
michael@0 176 } else {
michael@0 177 places.push(aPlaceInfo)
michael@0 178 }
michael@0 179
michael@0 180 // Create mozIVisitInfo for each entry.
michael@0 181 let now = Date.now();
michael@0 182 for (let i = 0; i < places.length; i++) {
michael@0 183 if (!places[i].title) {
michael@0 184 places[i].title = "test visit for " + places[i].uri.spec;
michael@0 185 }
michael@0 186 places[i].visits = [{
michael@0 187 transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
michael@0 188 : places[i].transition,
michael@0 189 visitDate: places[i].visitDate || (now++) * 1000,
michael@0 190 referrerURI: places[i].referrer
michael@0 191 }];
michael@0 192 }
michael@0 193
michael@0 194 PlacesUtils.asyncHistory.updatePlaces(
michael@0 195 places,
michael@0 196 {
michael@0 197 handleError: function handleError(aResultCode, aPlaceInfo) {
michael@0 198 let ex = new Components.Exception("Unexpected error in adding visits.",
michael@0 199 aResultCode);
michael@0 200 deferred.reject(ex);
michael@0 201 },
michael@0 202 handleResult: function () {},
michael@0 203 handleCompletion: function handleCompletion() {
michael@0 204 deferred.resolve();
michael@0 205 }
michael@0 206 }
michael@0 207 );
michael@0 208
michael@0 209 return deferred.promise;
michael@0 210 }
michael@0 211
michael@0 212
michael@0 213 XPCOMUtils.defineLazyGetter(this, "Services", function() {
michael@0 214 Cu.import("resource://gre/modules/Services.jsm");
michael@0 215 return Services;
michael@0 216 });
michael@0 217
michael@0 218 // Disable alert service notifications
michael@0 219 Services.prefs.setBoolPref("browser.download.manager.showAlertOnComplete", false);
michael@0 220
michael@0 221 do_register_cleanup(function() {
michael@0 222 Services.obs.notifyObservers(null, "quit-application", null);
michael@0 223 });
michael@0 224
michael@0 225 function oldDownloadManagerDisabled() {
michael@0 226 try {
michael@0 227 // This method throws an exception if the old Download Manager is disabled.
michael@0 228 Services.downloads.activeDownloadCount;
michael@0 229 } catch (ex) {
michael@0 230 return true;
michael@0 231 }
michael@0 232 return false;
michael@0 233 }

mercurial