dom/events/test/test_bug336682.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/events/test/test_bug336682.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +/*
     1.5 + * Helper functions for online/offline events tests.
     1.6 + * 
     1.7 + * Any copyright is dedicated to the Public Domain.
     1.8 + * http://creativecommons.org/licenses/publicdomain/
     1.9 + */
    1.10 +var gState = 0;
    1.11 +/**
    1.12 + * After all the on/offline handlers run, 
    1.13 + * gState is expected to be equal to MAX_STATE.
    1.14 + */
    1.15 +var MAX_STATE;
    1.16 +
    1.17 +function trace(text) {
    1.18 +  var t = text.replace(/&/g, "&" + "amp;").replace(/</g, "&" + "lt;") + "<br>";
    1.19 +  //document.getElementById("display").innerHTML += t;
    1.20 +}
    1.21 +
    1.22 +// window.ononline and window.onclick shouldn't work
    1.23 +// Right now, <body ononline=...> sets window.ononline (bug 380618)
    1.24 +// When these start passing, be sure to uncomment the code inside if(0) below.
    1.25 +todo(typeof window.ononline == "undefined",
    1.26 +     "window.ononline should be undefined at this point");
    1.27 +todo(typeof window.onoffline == "undefined",
    1.28 +     "window.onoffline should be undefined at this point");
    1.29 +
    1.30 +if (0) {
    1.31 +  window.ononline = function() {
    1.32 +    ok(false, "window.ononline shouldn't be called");
    1.33 +  }
    1.34 +  window.onoffline = function() {
    1.35 +    ok(false, "window.onclick shouldn't be called");
    1.36 +  }
    1.37 +}
    1.38 +
    1.39 +/**
    1.40 + * Returns a handler function for an online/offline event. The returned handler
    1.41 + * ensures the passed event object has expected properties and that the handler
    1.42 + * is called at the right moment (according to the gState variable).
    1.43 + * @param nameTemplate The string identifying the hanlder. '%1' in that
    1.44 + *                     string will be replaced with the event name.
    1.45 + * @param eventName 'online' or 'offline'
    1.46 + * @param expectedStates an array listing the possible values of gState at the
    1.47 + *                       moment the handler is called. The handler increases
    1.48 + *                       gState by one before checking if it's listed in
    1.49 + *                       expectedStates.
    1.50 + */
    1.51 +function makeHandler(nameTemplate, eventName, expectedStates) {
    1.52 +  return function(e) {
    1.53 +    var name = nameTemplate.replace(/%1/, eventName);
    1.54 +    ++gState;
    1.55 +    trace(name + ": gState=" + gState);
    1.56 +    ok(expectedStates.indexOf(gState) != -1,
    1.57 +       "handlers called in the right order: " + name + " is called, " + 
    1.58 +       "gState=" + gState + ", expectedStates=" + expectedStates);
    1.59 +    ok(e.constructor == Event, "event should be an Event");
    1.60 +    ok(e.type == eventName, "event type should be " + eventName);
    1.61 +    ok(e.bubbles, "event should bubble");
    1.62 +    ok(!e.cancelable, "event should not be cancelable");
    1.63 +    ok(e.target == (document instanceof HTMLDocument
    1.64 +                    ? document.body : document.documentElement),
    1.65 +       "the event target should be the body element");
    1.66 +  }
    1.67 +}
    1.68 +
    1.69 +function doTest() {
    1.70 +  var iosvc = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
    1.71 +                           .getService(SpecialPowers.Ci.nsIIOService2);
    1.72 +  iosvc.manageOfflineStatus = false;
    1.73 +  iosvc.offline = false;
    1.74 +  ok(navigator.onLine, "navigator.onLine should be true, since we've just " +
    1.75 +                       "set nsIIOService.offline to false");
    1.76 +
    1.77 +  gState = 0;
    1.78 +
    1.79 +  trace("setting iosvc.offline = true");
    1.80 +  iosvc.offline = true;
    1.81 +  trace("done setting iosvc.offline = true");
    1.82 +  ok(!navigator.onLine,
    1.83 +     "navigator.onLine should be false when iosvc.offline == true");
    1.84 +  ok(gState == window.MAX_STATE,
    1.85 +     "offline event: all registered handlers should have been invoked, " +
    1.86 +     "actual: " + gState);
    1.87 +
    1.88 +  gState = 0;
    1.89 +  trace("setting iosvc.offline = false");
    1.90 +  iosvc.offline = false;
    1.91 +  trace("done setting iosvc.offline = false");
    1.92 +  ok(navigator.onLine,
    1.93 +     "navigator.onLine should be true when iosvc.offline == false");
    1.94 +  ok(gState == window.MAX_STATE,
    1.95 +     "online event: all registered handlers should have been invoked, " +
    1.96 +     "actual: " + gState);
    1.97 +}

mercurial