toolkit/mozapps/downloads/tests/chrome/utils.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 /**
michael@0 6 * Provides utility functions for the download manager chrome tests.
michael@0 7 */
michael@0 8
michael@0 9 const Cc = Components.classes;
michael@0 10 const Ci = Components.interfaces;
michael@0 11 const Cr = Components.results;
michael@0 12
michael@0 13 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 14
michael@0 15 /**
michael@0 16 * Returns the toolkit implementation of the download manager UI service.
michael@0 17 * If the toolkit implementation of the service can't be found (e.g. because
michael@0 18 * SeaMonkey doesn't package that version but an own implementation that calls
michael@0 19 * different UI), then returns false (see bug 483781).
michael@0 20 *
michael@0 21 * @returns toolkit's nsIDownloadManagerUI implementation or false if not found
michael@0 22 */
michael@0 23 function getDMUI()
michael@0 24 {
michael@0 25 try {
michael@0 26 // This method throws an exception if the old Download Manager is disabled.
michael@0 27 Services.downloads.activeDownloadCount;
michael@0 28 } catch (ex) {
michael@0 29 return false;
michael@0 30 }
michael@0 31 if (Components.classesByID["{7dfdf0d1-aff6-4a34-bad1-d0fe74601642}"])
michael@0 32 return Components.classesByID["{7dfdf0d1-aff6-4a34-bad1-d0fe74601642}"].
michael@0 33 getService(Ci.nsIDownloadManagerUI);
michael@0 34 return false;
michael@0 35 }
michael@0 36
michael@0 37 /**
michael@0 38 * Adds a live download to the download manager.
michael@0 39 *
michael@0 40 * @param [optional] aName
michael@0 41 * The name that will be assigned to the downloaded file.
michael@0 42 * @returns an instance of nsIDownload.
michael@0 43 */
michael@0 44 function addDownload(aName) {
michael@0 45 function createURI(aObj) {
michael@0 46 let ios = Cc["@mozilla.org/network/io-service;1"].
michael@0 47 getService(Ci.nsIIOService);
michael@0 48 return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
michael@0 49 ios.newURI(aObj, null, null);
michael@0 50 }
michael@0 51
michael@0 52 function randomString() {
michael@0 53 let chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
michael@0 54 let string_length = 8;
michael@0 55 let randomstring = "";
michael@0 56 for (let i=0; i<string_length; i++) {
michael@0 57 let rnum = Math.floor(Math.random() * chars.length);
michael@0 58 randomstring += chars.substring(rnum, rnum+1);
michael@0 59 }
michael@0 60 return randomstring;
michael@0 61 }
michael@0 62
michael@0 63 let dm = Cc["@mozilla.org/download-manager;1"].
michael@0 64 getService(Ci.nsIDownloadManager);
michael@0 65 const nsIWBP = Ci.nsIWebBrowserPersist;
michael@0 66 let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
michael@0 67 .createInstance(Ci.nsIWebBrowserPersist);
michael@0 68 persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
michael@0 69 nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
michael@0 70 nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
michael@0 71 let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
michael@0 72 getService(Ci.nsIProperties);
michael@0 73 let dmFile = dirSvc.get("TmpD", Ci.nsIFile);
michael@0 74 dmFile.append(aName || ("dm-test-file-" + randomString()));
michael@0 75 if (dmFile.exists())
michael@0 76 throw "Download file already exists";
michael@0 77
michael@0 78 let dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
michael@0 79 createURI("http://example.com/httpd.js"),
michael@0 80 createURI(dmFile), null, null,
michael@0 81 Math.round(Date.now() * 1000), null, persist, false);
michael@0 82
michael@0 83 let privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 84 .getInterface(Ci.nsIWebNavigation)
michael@0 85 .QueryInterface(Ci.nsILoadContext);
michael@0 86
michael@0 87 persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener);
michael@0 88 persist.saveURI(dl.source, null, null, null, null, dl.targetFile, privacyContext);
michael@0 89
michael@0 90 return dl;
michael@0 91 }
michael@0 92
michael@0 93 /**
michael@0 94 * Used to populate the dm with dummy download data.
michael@0 95 *
michael@0 96 * @param aDownloadData
michael@0 97 * An array that contains the dummy download data to be added to the DM.
michael@0 98 * Expected fields are:
michael@0 99 * name, source, target, startTime, endTime, state, currBytes,
michael@0 100 * maxBytes, preferredAction, and autoResume
michael@0 101 */
michael@0 102 function populateDM(DownloadData)
michael@0 103 {
michael@0 104 let dm = Cc["@mozilla.org/download-manager;1"].
michael@0 105 getService(Ci.nsIDownloadManager);
michael@0 106 let db = dm.DBConnection;
michael@0 107
michael@0 108 let stmt = db.createStatement(
michael@0 109 "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " +
michael@0 110 "state, currBytes, maxBytes, preferredAction, autoResume) " +
michael@0 111 "VALUES (:name, :source, :target, :startTime, :endTime, :state, " +
michael@0 112 ":currBytes, :maxBytes, :preferredAction, :autoResume)");
michael@0 113 for each (let dl in DownloadData) {
michael@0 114 for (let prop in dl)
michael@0 115 stmt.params[prop] = dl[prop];
michael@0 116
michael@0 117 stmt.execute();
michael@0 118 }
michael@0 119 stmt.finalize();
michael@0 120 }
michael@0 121
michael@0 122 /**
michael@0 123 * Returns an instance to the download manager window
michael@0 124 *
michael@0 125 * @return an instance of nsIDOMWindow
michael@0 126 */
michael@0 127 function getDMWindow()
michael@0 128 {
michael@0 129 return Cc["@mozilla.org/appshell/window-mediator;1"].
michael@0 130 getService(Ci.nsIWindowMediator).
michael@0 131 getMostRecentWindow("Download:Manager");
michael@0 132 }
michael@0 133
michael@0 134 /**
michael@0 135 * Establishes a clean state to run a test in by removing everything from the
michael@0 136 * database and ensuring that the download manager's window is not open.
michael@0 137 */
michael@0 138 function setCleanState()
michael@0 139 {
michael@0 140 let dm = Cc["@mozilla.org/download-manager;1"].
michael@0 141 getService(Ci.nsIDownloadManager);
michael@0 142
michael@0 143 // Clean the dm
michael@0 144 dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
michael@0 145
michael@0 146 let win = getDMWindow();
michael@0 147 if (win) win.close();
michael@0 148 }
michael@0 149
michael@0 150 /**
michael@0 151 * Clears history invoking callback when done.
michael@0 152 */
michael@0 153 function waitForClearHistory(aCallback) {
michael@0 154 Components.utils.import("resource://gre/modules/PlacesUtils.jsm");
michael@0 155 Services.obs.addObserver(function observeClearHistory(aSubject, aTopic) {
michael@0 156 Services.obs.removeObserver(observeClearHistory, aTopic);
michael@0 157 aCallback();
michael@0 158 }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
michael@0 159 PlacesUtils.bhistory.removeAllPages();
michael@0 160 }

mercurial