toolkit/mozapps/update/tests/unit_aus_update/downloadInterruptedByOfflineRetry.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/update/tests/unit_aus_update/downloadInterruptedByOfflineRetry.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     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 +// Needs to be in sync w/ nsUpdateService.js
    1.10 +const NETWORK_ERROR_OFFLINE = 111;
    1.11 +
    1.12 +function run_test() {
    1.13 +  setupTestCommon();
    1.14 +
    1.15 +  logTestInfo("testing when an update check fails because the network is " +
    1.16 +              "offline that we check again when the network comes online " +
    1.17 +              "(Bug 794211).");
    1.18 +
    1.19 +  setUpdateURLOverride();
    1.20 +  Services.prefs.setBoolPref(PREF_APP_UPDATE_AUTO, false);
    1.21 +
    1.22 +  overrideXHR(null);
    1.23 +  overrideUpdatePrompt(updatePrompt);
    1.24 +  standardInit();
    1.25 +
    1.26 +  do_execute_soon(run_test_pt1);
    1.27 +}
    1.28 +
    1.29 +function run_test_pt1() {
    1.30 +  gResponseBody = null;
    1.31 +  gCheckFunc = check_test_pt1;
    1.32 +  gXHRCallback = xhr_pt1;
    1.33 +  gUpdateChecker.checkForUpdates(updateCheckListener, true);
    1.34 +}
    1.35 +
    1.36 +function xhr_pt1() {
    1.37 +  gXHR.status = AUS_Cr.NS_ERROR_OFFLINE;
    1.38 +  gXHR.onerror({ target: gXHR });
    1.39 +}
    1.40 +
    1.41 +function check_test_pt1(request, update) {
    1.42 +  do_check_eq(gStatusCode, AUS_Cr.NS_ERROR_OFFLINE);
    1.43 +  do_check_eq(update.errorCode, NETWORK_ERROR_OFFLINE);
    1.44 +
    1.45 +  // Forward the error to AUS, which should register the online observer
    1.46 +  gAUS.onError(request, update);
    1.47 +
    1.48 +  // Trigger another check by notifying the offline status observer
    1.49 +  gXHRCallback = xhr_pt2;
    1.50 +  Services.obs.notifyObservers(gAUS, "network:offline-status-changed", "online");
    1.51 +}
    1.52 +
    1.53 +var updatePrompt = {
    1.54 +  showUpdateAvailable: function(update) {
    1.55 +    check_test_pt2(update);
    1.56 +  }
    1.57 +};
    1.58 +
    1.59 +function xhr_pt2() {
    1.60 +  var patches = getLocalPatchString();
    1.61 +  var updates = getLocalUpdateString(patches);
    1.62 +  var responseBody = getLocalUpdatesXMLString(updates);
    1.63 +
    1.64 +  gXHR.status = 200;
    1.65 +  gXHR.responseText = responseBody;
    1.66 +  try {
    1.67 +    var parser = AUS_Cc["@mozilla.org/xmlextras/domparser;1"].
    1.68 +                 createInstance(AUS_Ci.nsIDOMParser);
    1.69 +    gXHR.responseXML = parser.parseFromString(responseBody, "application/xml");
    1.70 +  } catch (e) {
    1.71 +  }
    1.72 +  gXHR.onload({ target: gXHR });
    1.73 +}
    1.74 +
    1.75 +function check_test_pt2(update) {
    1.76 +  // We just verify that there are updates to know the check succeeded.
    1.77 +  do_check_neq(update, null);
    1.78 +  do_check_eq(update.name, "App Update Test");
    1.79 +
    1.80 +  doTestFinish();
    1.81 +}

mercurial