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 /* General nsIUpdateCheckListener onerror error code and statusText Tests */
42 // 200, 403, 404, 500, 2152398861, 2152398918, default (200)
44 const DIR_DATA = "data"
45 const URL_PREFIX = "http://localhost:4444/" + DIR_DATA + "/";
47 const PREF_APP_UPDATE_URL_OVERRIDE = "app.update.url.override";
49 const URI_UPDATES_PROPERTIES = "chrome://mozapps/locale/update/updates.properties";
50 const gUpdateBundle = AUS_Cc["@mozilla.org/intl/stringbundle;1"]
51 .getService(AUS_Ci.nsIStringBundleService)
52 .createBundle(URI_UPDATES_PROPERTIES);
56 var gExpectedStatusText;
63 do_timeout(0, "run_test_pt1()");
71 // Returns human readable status text from the updates.properties bundle
72 function getStatusText(aErrCode) {
74 return gUpdateBundle.GetStringFromName("checker_error-" + aErrCode);
81 // Custom error httpd handler used to return error codes we can't simulate
82 function httpdErrorHandler(metadata, response) {
83 response.setStatusLine(metadata.httpVersion, gExpectedStatus, "Error");
86 // Helper functions for testing nsIUpdateCheckListener onerror error statusText
87 function run_test_helper(aUpdateXML, aMsg, aExpectedStatus,
88 aExpectedStatusText, aNextRunFunc) {
91 gCheckFunc = check_test_helper;
92 gNextRunFunc = aNextRunFunc;
93 gExpectedStatus = aExpectedStatus;
94 gExpectedStatusText = aExpectedStatusText;
95 var url = URL_PREFIX + aUpdateXML;
96 dump("Testing: " + aMsg + " - " + url + "\n");
97 gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
98 gUpdateChecker.checkForUpdates(updateCheckListener, true);
101 function check_test_helper() {
102 do_check_eq(gStatus, gExpectedStatus);
103 do_check_eq(gStatusText, gExpectedStatusText);
108 * The following tests do not use the http server
111 // network is offline
112 function run_test_pt1() {
114 run_test_helper("aus-0050_general-1.xml", "network is offline",
115 0, getStatusText("2152398918"), run_test_pt2);
118 // connection refused
119 function run_test_pt2() {
120 toggleOffline(false);
121 run_test_helper("aus-0050_general-2.xml", "connection refused",
122 0, getStatusText("2152398861"), run_test_pt3);
126 * The following tests use the codes returned by the http server
130 function run_test_pt3() {
131 start_httpserver(DIR_DATA);
132 run_test_helper("aus-0050_general-3.xml", "file malformed",
133 200, getStatusText("200"), run_test_pt4);
137 function run_test_pt4() {
138 run_test_helper("aus-0050_general-4.xml", "file not found",
139 404, getStatusText("404"), run_test_pt5);
142 // internal server error
143 function run_test_pt5() {
144 gTestserver.registerContentType("sjs", "sjs");
145 run_test_helper("aus-0050_general-5.sjs", "internal server error",
146 500, getStatusText("500"), run_test_pt6);
150 * The following tests use a custom error handler to return codes from the http
155 function run_test_pt6() {
156 gTestserver.registerErrorHandler(404, httpdErrorHandler);
157 run_test_helper("aus-0050_general-6.xml", "access denied",
158 403, getStatusText("403"), run_test_pt7);
161 // default onerror error message (error code 399 is not defined)
162 function run_test_pt7() {
163 run_test_helper("aus-0050_general-7.xml", "default onerror error message",
164 399, getStatusText("404"), end_test);
168 // Update check listener
169 const updateCheckListener = {
170 onProgress: function(request, position, totalSize) {
173 onCheckComplete: function(request, updates, updateCount) {
174 dump("onCheckComplete request.status = " + request.status + "\n\n");
175 // Use a timeout to allow the XHR to complete
176 do_timeout(0, "gCheckFunc()");
179 onError: function(request, update) {
180 gStatus = request.status;
181 gStatusText = update.statusText;
182 dump("onError: request.status = " + gStatus + ", update.statusText = " + gStatusText + "\n\n");
183 // Use a timeout to allow the XHR to complete
184 do_timeout(0, "gCheckFunc()");
187 QueryInterface: function(aIID) {
188 if (!aIID.equals(AUS_Ci.nsIUpdateCheckListener) &&
189 !aIID.equals(AUS_Ci.nsISupports))
190 throw AUS_Cr.NS_ERROR_NO_INTERFACE;