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 onload error code statusText Tests */
42 // 2152398849, 2152398862, 2152398864, 2152398867, 2152398868, 2152398878,
43 // 2152398890, 2152398919, 2152398920, default (404)
45 const DIR_DATA = "data"
46 const URL_PREFIX = "http://localhost:4444/" + DIR_DATA + "/";
48 const PREF_APP_UPDATE_URL_OVERRIDE = "app.update.url.override";
50 const URI_UPDATES_PROPERTIES = "chrome://mozapps/locale/update/updates.properties";
51 const gUpdateBundle = AUS_Cc["@mozilla.org/intl/stringbundle;1"]
52 .getService(AUS_Ci.nsIStringBundleService)
53 .createBundle(URI_UPDATES_PROPERTIES);
57 var gExpectedStatusCode;
58 var gExpectedStatusText;
65 overrideXHR(callHandleEvent);
66 do_timeout(0, "run_test_pt1()");
73 // Returns human readable status text from the updates.properties bundle
74 function getStatusText(aErrCode) {
76 return gUpdateBundle.GetStringFromName("checker_error-" + aErrCode);
83 // Callback function used by the custom XMLHttpRequest implemetation to
84 // call the nsIDOMEventListener's handleEvent method for onload.
85 function callHandleEvent() {
86 gXHR.status = gExpectedStatusCode;
87 var e = { target: gXHR };
88 gXHR.onload.handleEvent(e);
91 // Helper functions for testing nsIUpdateCheckListener onload error statusTexts
92 function run_test_helper(aUpdateXML, aMsg, aExpectedStatusCode,
93 aExpectedStatusText, aNextRunFunc) {
96 gCheckFunc = check_test_helper;
97 gNextRunFunc = aNextRunFunc;
98 gExpectedStatusCode = aExpectedStatusCode;
99 gExpectedStatusText = aExpectedStatusText;
100 var url = URL_PREFIX + aUpdateXML;
101 dump("Testing: " + aMsg + " - " + url + "\n");
102 gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
103 gUpdateChecker.checkForUpdates(updateCheckListener, true);
106 function check_test_helper() {
107 do_check_eq(gStatusCode, gExpectedStatusCode);
108 do_check_eq(gStatusText, gExpectedStatusText);
113 * The following tests use a custom XMLHttpRequest to return the status codes
116 // failed (unknown reason)
117 function run_test_pt1() {
118 run_test_helper("aus-0051_general-1.xml", "failed (unknown reason)",
119 2152398849, getStatusText("2152398849"), run_test_pt2);
122 // connection timed out
123 function run_test_pt2() {
124 run_test_helper("aus-0051_general-2.xml", "connection timed out",
125 2152398862, getStatusText("2152398862"), run_test_pt3);
129 function run_test_pt3() {
130 run_test_helper("aus-0051_general-3.xml", "network offline",
131 2152398864, getStatusText("2152398864"), run_test_pt4);
135 function run_test_pt4() {
136 run_test_helper("aus-0051_general-4.xml", "port not allowed",
137 2152398867, getStatusText("2152398867"), run_test_pt5);
140 // no data was received
141 function run_test_pt5() {
142 run_test_helper("aus-0051_general-5.xml", "no data was received",
143 2152398868, getStatusText("2152398868"), run_test_pt6);
146 // update server not found
147 function run_test_pt6() {
148 run_test_helper("aus-0051_general-6.xml", "update server not found",
149 2152398878, getStatusText("2152398878"), run_test_pt7);
152 // proxy server not found
153 function run_test_pt7() {
154 run_test_helper("aus-0051_general-7.xml", "proxy server not found",
155 2152398890, getStatusText("2152398890"), run_test_pt8);
158 // data transfer interrupted
159 function run_test_pt8() {
160 run_test_helper("aus-0051_general-8.xml", "data transfer interrupted",
161 2152398919, getStatusText("2152398919"), run_test_pt9);
164 // proxy server connection refused
165 function run_test_pt9() {
166 run_test_helper("aus-0051_general-9.xml", "proxy server connection refused",
167 2152398920, getStatusText("2152398920"), run_test_pt10);
170 // server certificate expired
171 function run_test_pt10() {
172 run_test_helper("aus-0051_general-10.xml", "server certificate expired",
173 2153390069, getStatusText("2153390069"), run_test_pt11);
176 // default onload error message (error code 1152398920 is not defined)
177 function run_test_pt11() {
178 run_test_helper("aus-0051_general-11.xml", "default onload error message",
179 1152398920, getStatusText("404"), end_test);
182 // Update check listener
183 const updateCheckListener = {
184 onProgress: function(request, position, totalSize) {
187 onCheckComplete: function(request, updates, updateCount) {
188 dump("onCheckComplete request.status = " + request.status + "\n\n");
189 // Use a timeout to allow the XHR to complete
190 do_timeout(0, "gCheckFunc()");
193 onError: function(request, update) {
194 gStatusCode = request.status;
195 gStatusText = update.statusText;
196 dump("onError: request.status = " + gStatusCode + ", update.statusText = " + gStatusText + "\n\n");
197 // Use a timeout to allow the XHR to complete
198 do_timeout(0, "gCheckFunc()");
201 QueryInterface: function(aIID) {
202 if (!aIID.equals(AUS_Ci.nsIUpdateCheckListener) &&
203 !aIID.equals(AUS_Ci.nsISupports))
204 throw AUS_Cr.NS_ERROR_NO_INTERFACE;