michael@0: /* michael@0: * Helper functions for online/offline events tests. michael@0: * michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: var gState = 0; michael@0: /** michael@0: * After all the on/offline handlers run, michael@0: * gState is expected to be equal to MAX_STATE. michael@0: */ michael@0: var MAX_STATE; michael@0: michael@0: function trace(text) { michael@0: var t = text.replace(/&/g, "&" + "amp;").replace(/"; michael@0: //document.getElementById("display").innerHTML += t; michael@0: } michael@0: michael@0: // window.ononline and window.onclick shouldn't work michael@0: // Right now, sets window.ononline (bug 380618) michael@0: // When these start passing, be sure to uncomment the code inside if(0) below. michael@0: todo(typeof window.ononline == "undefined", michael@0: "window.ononline should be undefined at this point"); michael@0: todo(typeof window.onoffline == "undefined", michael@0: "window.onoffline should be undefined at this point"); michael@0: michael@0: if (0) { michael@0: window.ononline = function() { michael@0: ok(false, "window.ononline shouldn't be called"); michael@0: } michael@0: window.onoffline = function() { michael@0: ok(false, "window.onclick shouldn't be called"); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns a handler function for an online/offline event. The returned handler michael@0: * ensures the passed event object has expected properties and that the handler michael@0: * is called at the right moment (according to the gState variable). michael@0: * @param nameTemplate The string identifying the hanlder. '%1' in that michael@0: * string will be replaced with the event name. michael@0: * @param eventName 'online' or 'offline' michael@0: * @param expectedStates an array listing the possible values of gState at the michael@0: * moment the handler is called. The handler increases michael@0: * gState by one before checking if it's listed in michael@0: * expectedStates. michael@0: */ michael@0: function makeHandler(nameTemplate, eventName, expectedStates) { michael@0: return function(e) { michael@0: var name = nameTemplate.replace(/%1/, eventName); michael@0: ++gState; michael@0: trace(name + ": gState=" + gState); michael@0: ok(expectedStates.indexOf(gState) != -1, michael@0: "handlers called in the right order: " + name + " is called, " + michael@0: "gState=" + gState + ", expectedStates=" + expectedStates); michael@0: ok(e.constructor == Event, "event should be an Event"); michael@0: ok(e.type == eventName, "event type should be " + eventName); michael@0: ok(e.bubbles, "event should bubble"); michael@0: ok(!e.cancelable, "event should not be cancelable"); michael@0: ok(e.target == (document instanceof HTMLDocument michael@0: ? document.body : document.documentElement), michael@0: "the event target should be the body element"); michael@0: } michael@0: } michael@0: michael@0: function doTest() { michael@0: var iosvc = SpecialPowers.Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(SpecialPowers.Ci.nsIIOService2); michael@0: iosvc.manageOfflineStatus = false; michael@0: iosvc.offline = false; michael@0: ok(navigator.onLine, "navigator.onLine should be true, since we've just " + michael@0: "set nsIIOService.offline to false"); michael@0: michael@0: gState = 0; michael@0: michael@0: trace("setting iosvc.offline = true"); michael@0: iosvc.offline = true; michael@0: trace("done setting iosvc.offline = true"); michael@0: ok(!navigator.onLine, michael@0: "navigator.onLine should be false when iosvc.offline == true"); michael@0: ok(gState == window.MAX_STATE, michael@0: "offline event: all registered handlers should have been invoked, " + michael@0: "actual: " + gState); michael@0: michael@0: gState = 0; michael@0: trace("setting iosvc.offline = false"); michael@0: iosvc.offline = false; michael@0: trace("done setting iosvc.offline = false"); michael@0: ok(navigator.onLine, michael@0: "navigator.onLine should be true when iosvc.offline == false"); michael@0: ok(gState == window.MAX_STATE, michael@0: "online event: all registered handlers should have been invoked, " + michael@0: "actual: " + gState); michael@0: }