toolkit/mozapps/downloads/tests/chrome/utils.js

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

mercurial