1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Application Update Service.
16 * The Initial Developer of the Original Code is
17 * Robert Strong <robert.bugzilla@gmail.com>.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Mozilla Foundation <http://www.mozilla.org/>. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK *****
39 const NS_APP_USER_PROFILE_50_DIR = "ProfD";
40 const NS_APP_PROFILE_DIR_STARTUP = "ProfDS";
42 // const Cc, Ci, and Cr are defined in netwerk/test/httpserver/httpd.js so we
43 // need to define unique ones.
44 const AUS_Cc = Components.classes;
45 const AUS_Ci = Components.interfaces;
46 const AUS_Cr = Components.results;
49 var gUpdateChecker = null;
51 var gTestserver = null;
53 var gXHRCallback = null;
55 /* Initializes the most commonly used global vars used by tests */
57 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1.0", "2.0");
58 gPrefs = AUS_Cc["@mozilla.org/preferences;1"]
59 .getService(AUS_Ci.nsIPrefBranch);
61 // Enable Update logging (see below)
62 gPrefs.setBoolPref("app.update.log.all", true);
63 // Lessens the noise in the logs when using Update Service logging (see below)
64 gPrefs.setBoolPref("app.update.enabled", false);
65 gPrefs.setBoolPref("extensions.blocklist.enabled", false);
66 gPrefs.setBoolPref("extensions.update.enabled", false);
67 gPrefs.setBoolPref("browser.search.update", false);
68 gPrefs.setBoolPref("browser.microsummary.updateGenerators", false);
70 gAUS = AUS_Cc["@mozilla.org/updates/update-service;1"]
71 .getService(AUS_Ci.nsIApplicationUpdateService);
72 gUpdateChecker = AUS_Cc["@mozilla.org/updates/update-checker;1"]
73 .createInstance(AUS_Ci.nsIUpdateChecker);
74 // Uncomment the following two lines to log Update Service logging to the
77 var os = AUS_Cc["@mozilla.org/observer-service;1"]
78 .getService(AUS_Ci.nsIObserverService);
79 os.notifyObservers(null, "profile-after-change", null);
84 * Toggles network offline.
86 * Be sure to toggle back to online before the test finishes to prevent the
87 * following from being printed to the test's log file.
88 * WARNING: NS_ENSURE_TRUE(thread) failed: file c:/moz/mozilla-central/mozilla/netwerk/base/src/nsSocketTransportService2.cpp, line 115
89 * WARNING: unable to post SHUTDOWN message
91 function toggleOffline(aOffline) {
92 const ioService = AUS_Cc["@mozilla.org/network/io-service;1"]
93 .getService(AUS_Ci.nsIIOService);
96 ioService.manageOfflineStatus = !aOffline;
100 if (ioService.offline != aOffline)
101 ioService.offline = aOffline;
105 * Sets up the bare bones XMLHttpRequest implementation below.
108 * The callback function that will call the nsIDomEventListener's
109 * handleEvent method.
111 * Example of the callback function
113 * function callHandleEvent() {
114 * gXHR.status = gExpectedStatus;
115 * var e = { target: gXHR };
116 * gXHR.onload.handleEvent(e);
119 function overrideXHR(callback) {
120 gXHRCallback = callback;
122 var registrar = Components.manager.QueryInterface(AUS_Ci.nsIComponentRegistrar);
123 registrar.registerFactory(gXHR.classID, gXHR.classDescription,
124 gXHR.contractID, gXHR);
128 * Bare bones XMLHttpRequest implementation for testing onprogress, onerror,
129 * and onload nsIDomEventListener handleEvent.
134 overrideMimeType: function(mimetype) { },
135 setRequestHeader: function(header, value) { },
137 channel: { set notificationCallbacks(val) { } },
140 open: function (method, url) { gXHR._method = method; gXHR._url = url; },
141 send: function(body) {
142 do_timeout(0, "gXHRCallback()"); // Use a timeout so the XHR completes
145 set onprogress(val) { gXHR._onprogress = val; },
146 get onprogress() { return gXHR._onprogress; },
148 set onerror(val) { gXHR._onerror = val; },
149 get onerror() { return gXHR._onerror; },
151 set onload(val) { gXHR._onload = val; },
152 get onload() { return gXHR._onload; },
153 flags: AUS_Ci.nsIClassInfo.SINGLETON,
154 implementationLanguage: AUS_Ci.nsIProgrammingLanguage.JAVASCRIPT,
155 getHelperForLanguage: function(language) null,
156 getInterfaces: function(count) {
157 var interfaces = [AUS_Ci.nsIXMLHttpRequest, AUS_Ci.nsIJSXMLHttpRequest];
158 count.value = interfaces.length;
161 classDescription: "XMLHttpRequest",
162 contractID: "@mozilla.org/xmlextras/xmlhttprequest;1",
163 classID: Components.ID("{c9b37f43-4278-4304-a5e0-600991ab08cb}"),
164 createInstance: function (outer, aIID) {
166 throw AUS_Cr.NS_ERROR_NO_AGGREGATION;
167 return gXHR.QueryInterface(aIID);
169 QueryInterface: function(aIID) {
170 if (aIID.equals(AUS_Ci.nsIXMLHttpRequest) ||
171 aIID.equals(AUS_Ci.nsIJSXMLHttpRequest) ||
172 aIID.equals(AUS_Ci.nsIClassInfo) ||
173 aIID.equals(AUS_Ci.nsISupports))
175 throw AUS_Cr.NS_ERROR_NO_INTERFACE;
180 * Removes the updates directory and the active-update.xml file if they exist.
181 * This prevents some tests from failing due to files being left behind when
182 * the tests are interrupted.
184 function remove_dirs_and_files () {
185 var fileLocator = AUS_Cc["@mozilla.org/file/directory_service;1"]
186 .getService(AUS_Ci.nsIProperties);
187 var dir = fileLocator.get("XCurProcD", AUS_Ci.nsIFile);
189 var file = dir.clone();
190 file.append("active-update.xml");
195 file.append("updates.xml");
200 file.append("updates");
201 file.append("last-update.log");
210 file.append("updates");
212 file.append("update.mar");
221 file.append("updates");
223 file.append("update.status");
231 // This fails sporadically on Mac OS X so wrap it in a try catch
232 dir.append("updates");
242 * Helper for starting the http server used by the tests
243 * @param aRelativeDirName
244 * The directory name to register relative to
245 * toolkit/mozapps/update/test/unit/
247 function start_httpserver(aRelativeDirName) {
248 do_import_script("netwerk/test/httpserver/httpd.js");
249 gTestserver = new nsHttpServer();
250 gTestserver.registerDirectory("/data/", do_get_file("toolkit/mozapps/update/test/unit/" + aRelativeDirName));
251 gTestserver.start(4444);
254 /* Helper for stopping the http server used by the tests */
255 function stop_httpserver() {
260 * Creates an nsIXULAppInfo
262 * The ID of the test application
264 * A name for the test application
266 * The version of the application
267 * @param platformVersion
268 * The gecko version of the application
270 function createAppInfo(id, name, version, platformVersion)
272 const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
273 const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
279 appBuildID: "2007010101",
280 platformVersion: platformVersion,
281 platformBuildID: "2007010101",
283 logConsoleErrors: true,
285 XPCOMABI: "noarch-spidermonkey",
287 QueryInterface: function QueryInterface(iid) {
288 if (iid.equals(AUS_Ci.nsIXULAppInfo) ||
289 iid.equals(AUS_Ci.nsIXULRuntime) ||
290 iid.equals(AUS_Ci.nsISupports))
293 throw AUS_Cr.NS_ERROR_NO_INTERFACE;
297 var XULAppInfoFactory = {
298 createInstance: function (outer, iid) {
300 throw AUS_Cr.NS_ERROR_NO_AGGREGATION;
301 return XULAppInfo.QueryInterface(iid);
305 var registrar = Components.manager.QueryInterface(AUS_Ci.nsIComponentRegistrar);
306 registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
307 XULAPPINFO_CONTRACTID, XULAppInfoFactory);
310 // Use a custom profile dir to keep the bin dir clean... not necessary but nice
311 var gDirSvc = AUS_Cc["@mozilla.org/file/directory_service;1"].
312 getService(AUS_Ci.nsIProperties);
313 var gTestRoot = gDirSvc.get("CurProcD", AUS_Ci.nsILocalFile);
314 gTestRoot = gTestRoot.parent.parent;
315 gTestRoot.append("_tests");
316 gTestRoot.append("xpcshell-simple");
317 gTestRoot.append("test_update");
318 gTestRoot.normalize();
320 // Create and register a profile directory.
321 var gProfD = gTestRoot.clone();
322 gProfD.append("profile");
325 gProfD.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, 0755);
328 getFile: function(prop, persistent) {
329 persistent.value = true;
330 if (prop == NS_APP_USER_PROFILE_50_DIR ||
331 prop == NS_APP_PROFILE_DIR_STARTUP)
332 return gProfD.clone();
335 QueryInterface: function(iid) {
336 if (iid.equals(AUS_Ci.nsIDirectoryServiceProvider) ||
337 iid.equals(AUS_Ci.nsISupports)) {
340 throw AUS_Cr.NS_ERROR_NO_INTERFACE;
343 gDirSvc.QueryInterface(AUS_Ci.nsIDirectoryService).registerProvider(dirProvider);
345 remove_dirs_and_files();