tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bug336682.js (3032B)


      1 /*
      2 * Helper functions for online/offline events tests.
      3 *
      4 * Any copyright is dedicated to the Public Domain.
      5 * http://creativecommons.org/licenses/publicdomain/
      6 */
      7 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
      8 
      9 var gState = 0;
     10 /**
     11 * After all the on/offline handlers run,
     12 * gState is expected to be equal to MAX_STATE.
     13 */
     14 var MAX_STATE;
     15 
     16 function trace(text) {
     17  var t = text.replace(/&/g, "&" + "amp;").replace(/</g, "&" + "lt;") + "<br>";
     18  //document.getElementById("display").innerHTML += t;
     19 }
     20 
     21 /**
     22 * Returns a handler function for an online/offline event. The returned handler
     23 * ensures the passed event object has expected properties and that the handler
     24 * is called at the right moment (according to the gState variable).
     25 *
     26 * @param nameTemplate The string identifying the hanlder. '%1' in that
     27 *                     string will be replaced with the event name.
     28 * @param eventName 'online' or 'offline'
     29 * @param expectedStates an array listing the possible values of gState at the
     30 *                       moment the handler is called. The handler increases
     31 *                       gState by one before checking if it's listed in
     32 *                       expectedStates.
     33 */
     34 function makeHandler(nameTemplate, eventName, expectedStates) {
     35  return function (e) {
     36    var name = nameTemplate.replace(/%1/, eventName);
     37    ++gState;
     38    trace(name + ": gState=" + gState);
     39    ok(
     40      expectedStates.includes(gState),
     41      "handlers called in the right order: " +
     42        name +
     43        " is called, " +
     44        "gState=" +
     45        gState +
     46        ", expectedStates=" +
     47        expectedStates
     48    );
     49    ok(e.constructor == Event, "event should be an Event");
     50    ok(e.type == eventName, "event type should be " + eventName);
     51    ok(!e.bubbles, "event should not bubble");
     52    ok(!e.cancelable, "event should not be cancelable");
     53    ok(e.target == window, "target should be the window");
     54  };
     55 }
     56 
     57 function doTest() {
     58  var iosvc = SpecialPowers.Cc["@mozilla.org/network/io-service;1"].getService(
     59    SpecialPowers.Ci.nsIIOService
     60  );
     61  iosvc.manageOfflineStatus = false;
     62  iosvc.offline = false;
     63  ok(
     64    navigator.onLine,
     65    "navigator.onLine should be true, since we've just " +
     66      "set nsIIOService.offline to false"
     67  );
     68 
     69  gState = 0;
     70 
     71  trace("setting iosvc.offline = true");
     72  iosvc.offline = true;
     73  trace("done setting iosvc.offline = true");
     74  ok(
     75    !navigator.onLine,
     76    "navigator.onLine should be false when iosvc.offline == true"
     77  );
     78  ok(
     79    gState == window.MAX_STATE,
     80    "offline event: all registered handlers should have been invoked, " +
     81      "actual: " +
     82      gState
     83  );
     84 
     85  gState = 0;
     86  trace("setting iosvc.offline = false");
     87  iosvc.offline = false;
     88  trace("done setting iosvc.offline = false");
     89  ok(
     90    navigator.onLine,
     91    "navigator.onLine should be true when iosvc.offline == false"
     92  );
     93  ok(
     94    gState == window.MAX_STATE,
     95    "online event: all registered handlers should have been invoked, " +
     96      "actual: " +
     97      gState
     98  );
     99 }