1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/downloads/tests/chrome/utils.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * Provides utility functions for the download manager chrome tests. 1.10 + */ 1.11 + 1.12 +const Cc = Components.classes; 1.13 +const Ci = Components.interfaces; 1.14 +const Cr = Components.results; 1.15 + 1.16 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.17 + 1.18 +/** 1.19 + * Returns the toolkit implementation of the download manager UI service. 1.20 + * If the toolkit implementation of the service can't be found (e.g. because 1.21 + * SeaMonkey doesn't package that version but an own implementation that calls 1.22 + * different UI), then returns false (see bug 483781). 1.23 + * 1.24 + * @returns toolkit's nsIDownloadManagerUI implementation or false if not found 1.25 + */ 1.26 +function getDMUI() 1.27 +{ 1.28 + try { 1.29 + // This method throws an exception if the old Download Manager is disabled. 1.30 + Services.downloads.activeDownloadCount; 1.31 + } catch (ex) { 1.32 + return false; 1.33 + } 1.34 + if (Components.classesByID["{7dfdf0d1-aff6-4a34-bad1-d0fe74601642}"]) 1.35 + return Components.classesByID["{7dfdf0d1-aff6-4a34-bad1-d0fe74601642}"]. 1.36 + getService(Ci.nsIDownloadManagerUI); 1.37 + return false; 1.38 +} 1.39 + 1.40 +/** 1.41 + * Adds a live download to the download manager. 1.42 + * 1.43 + * @param [optional] aName 1.44 + * The name that will be assigned to the downloaded file. 1.45 + * @returns an instance of nsIDownload. 1.46 + */ 1.47 +function addDownload(aName) { 1.48 + function createURI(aObj) { 1.49 + let ios = Cc["@mozilla.org/network/io-service;1"]. 1.50 + getService(Ci.nsIIOService); 1.51 + return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) : 1.52 + ios.newURI(aObj, null, null); 1.53 + } 1.54 + 1.55 + function randomString() { 1.56 + let chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; 1.57 + let string_length = 8; 1.58 + let randomstring = ""; 1.59 + for (let i=0; i<string_length; i++) { 1.60 + let rnum = Math.floor(Math.random() * chars.length); 1.61 + randomstring += chars.substring(rnum, rnum+1); 1.62 + } 1.63 + return randomstring; 1.64 + } 1.65 + 1.66 + let dm = Cc["@mozilla.org/download-manager;1"]. 1.67 + getService(Ci.nsIDownloadManager); 1.68 + const nsIWBP = Ci.nsIWebBrowserPersist; 1.69 + let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] 1.70 + .createInstance(Ci.nsIWebBrowserPersist); 1.71 + persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES | 1.72 + nsIWBP.PERSIST_FLAGS_BYPASS_CACHE | 1.73 + nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; 1.74 + let dirSvc = Cc["@mozilla.org/file/directory_service;1"]. 1.75 + getService(Ci.nsIProperties); 1.76 + let dmFile = dirSvc.get("TmpD", Ci.nsIFile); 1.77 + dmFile.append(aName || ("dm-test-file-" + randomString())); 1.78 + if (dmFile.exists()) 1.79 + throw "Download file already exists"; 1.80 + 1.81 + let dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD, 1.82 + createURI("http://example.com/httpd.js"), 1.83 + createURI(dmFile), null, null, 1.84 + Math.round(Date.now() * 1000), null, persist, false); 1.85 + 1.86 + let privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor) 1.87 + .getInterface(Ci.nsIWebNavigation) 1.88 + .QueryInterface(Ci.nsILoadContext); 1.89 + 1.90 + persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener); 1.91 + persist.saveURI(dl.source, null, null, null, null, dl.targetFile, privacyContext); 1.92 + 1.93 + return dl; 1.94 + } 1.95 + 1.96 +/** 1.97 + * Used to populate the dm with dummy download data. 1.98 + * 1.99 + * @param aDownloadData 1.100 + * An array that contains the dummy download data to be added to the DM. 1.101 + * Expected fields are: 1.102 + * name, source, target, startTime, endTime, state, currBytes, 1.103 + * maxBytes, preferredAction, and autoResume 1.104 + */ 1.105 +function populateDM(DownloadData) 1.106 +{ 1.107 + let dm = Cc["@mozilla.org/download-manager;1"]. 1.108 + getService(Ci.nsIDownloadManager); 1.109 + let db = dm.DBConnection; 1.110 + 1.111 + let stmt = db.createStatement( 1.112 + "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " + 1.113 + "state, currBytes, maxBytes, preferredAction, autoResume) " + 1.114 + "VALUES (:name, :source, :target, :startTime, :endTime, :state, " + 1.115 + ":currBytes, :maxBytes, :preferredAction, :autoResume)"); 1.116 + for each (let dl in DownloadData) { 1.117 + for (let prop in dl) 1.118 + stmt.params[prop] = dl[prop]; 1.119 + 1.120 + stmt.execute(); 1.121 + } 1.122 + stmt.finalize(); 1.123 +} 1.124 + 1.125 +/** 1.126 + * Returns an instance to the download manager window 1.127 + * 1.128 + * @return an instance of nsIDOMWindow 1.129 + */ 1.130 +function getDMWindow() 1.131 +{ 1.132 + return Cc["@mozilla.org/appshell/window-mediator;1"]. 1.133 + getService(Ci.nsIWindowMediator). 1.134 + getMostRecentWindow("Download:Manager"); 1.135 +} 1.136 + 1.137 +/** 1.138 + * Establishes a clean state to run a test in by removing everything from the 1.139 + * database and ensuring that the download manager's window is not open. 1.140 + */ 1.141 +function setCleanState() 1.142 +{ 1.143 + let dm = Cc["@mozilla.org/download-manager;1"]. 1.144 + getService(Ci.nsIDownloadManager); 1.145 + 1.146 + // Clean the dm 1.147 + dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads"); 1.148 + 1.149 + let win = getDMWindow(); 1.150 + if (win) win.close(); 1.151 +} 1.152 + 1.153 +/** 1.154 + * Clears history invoking callback when done. 1.155 + */ 1.156 +function waitForClearHistory(aCallback) { 1.157 + Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); 1.158 + Services.obs.addObserver(function observeClearHistory(aSubject, aTopic) { 1.159 + Services.obs.removeObserver(observeClearHistory, aTopic); 1.160 + aCallback(); 1.161 + }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false); 1.162 + PlacesUtils.bhistory.removeAllPages(); 1.163 +}