Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Helper functions for online/offline events tests. |
michael@0 | 3 | * |
michael@0 | 4 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 5 | * http://creativecommons.org/licenses/publicdomain/ |
michael@0 | 6 | */ |
michael@0 | 7 | var gState = 0; |
michael@0 | 8 | /** |
michael@0 | 9 | * After all the on/offline handlers run, |
michael@0 | 10 | * gState is expected to be equal to MAX_STATE. |
michael@0 | 11 | */ |
michael@0 | 12 | var MAX_STATE; |
michael@0 | 13 | |
michael@0 | 14 | function trace(text) { |
michael@0 | 15 | var t = text.replace(/&/g, "&" + "amp;").replace(/</g, "&" + "lt;") + "<br>"; |
michael@0 | 16 | //document.getElementById("display").innerHTML += t; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | // window.ononline and window.onclick shouldn't work |
michael@0 | 20 | // Right now, <body ononline=...> sets window.ononline (bug 380618) |
michael@0 | 21 | // When these start passing, be sure to uncomment the code inside if(0) below. |
michael@0 | 22 | todo(typeof window.ononline == "undefined", |
michael@0 | 23 | "window.ononline should be undefined at this point"); |
michael@0 | 24 | todo(typeof window.onoffline == "undefined", |
michael@0 | 25 | "window.onoffline should be undefined at this point"); |
michael@0 | 26 | |
michael@0 | 27 | if (0) { |
michael@0 | 28 | window.ononline = function() { |
michael@0 | 29 | ok(false, "window.ononline shouldn't be called"); |
michael@0 | 30 | } |
michael@0 | 31 | window.onoffline = function() { |
michael@0 | 32 | ok(false, "window.onclick shouldn't be called"); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * Returns a handler function for an online/offline event. The returned handler |
michael@0 | 38 | * ensures the passed event object has expected properties and that the handler |
michael@0 | 39 | * is called at the right moment (according to the gState variable). |
michael@0 | 40 | * @param nameTemplate The string identifying the hanlder. '%1' in that |
michael@0 | 41 | * string will be replaced with the event name. |
michael@0 | 42 | * @param eventName 'online' or 'offline' |
michael@0 | 43 | * @param expectedStates an array listing the possible values of gState at the |
michael@0 | 44 | * moment the handler is called. The handler increases |
michael@0 | 45 | * gState by one before checking if it's listed in |
michael@0 | 46 | * expectedStates. |
michael@0 | 47 | */ |
michael@0 | 48 | function makeHandler(nameTemplate, eventName, expectedStates) { |
michael@0 | 49 | return function(e) { |
michael@0 | 50 | var name = nameTemplate.replace(/%1/, eventName); |
michael@0 | 51 | ++gState; |
michael@0 | 52 | trace(name + ": gState=" + gState); |
michael@0 | 53 | ok(expectedStates.indexOf(gState) != -1, |
michael@0 | 54 | "handlers called in the right order: " + name + " is called, " + |
michael@0 | 55 | "gState=" + gState + ", expectedStates=" + expectedStates); |
michael@0 | 56 | ok(e.constructor == Event, "event should be an Event"); |
michael@0 | 57 | ok(e.type == eventName, "event type should be " + eventName); |
michael@0 | 58 | ok(e.bubbles, "event should bubble"); |
michael@0 | 59 | ok(!e.cancelable, "event should not be cancelable"); |
michael@0 | 60 | ok(e.target == (document instanceof HTMLDocument |
michael@0 | 61 | ? document.body : document.documentElement), |
michael@0 | 62 | "the event target should be the body element"); |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | function doTest() { |
michael@0 | 67 | var iosvc = SpecialPowers.Cc["@mozilla.org/network/io-service;1"] |
michael@0 | 68 | .getService(SpecialPowers.Ci.nsIIOService2); |
michael@0 | 69 | iosvc.manageOfflineStatus = false; |
michael@0 | 70 | iosvc.offline = false; |
michael@0 | 71 | ok(navigator.onLine, "navigator.onLine should be true, since we've just " + |
michael@0 | 72 | "set nsIIOService.offline to false"); |
michael@0 | 73 | |
michael@0 | 74 | gState = 0; |
michael@0 | 75 | |
michael@0 | 76 | trace("setting iosvc.offline = true"); |
michael@0 | 77 | iosvc.offline = true; |
michael@0 | 78 | trace("done setting iosvc.offline = true"); |
michael@0 | 79 | ok(!navigator.onLine, |
michael@0 | 80 | "navigator.onLine should be false when iosvc.offline == true"); |
michael@0 | 81 | ok(gState == window.MAX_STATE, |
michael@0 | 82 | "offline event: all registered handlers should have been invoked, " + |
michael@0 | 83 | "actual: " + gState); |
michael@0 | 84 | |
michael@0 | 85 | gState = 0; |
michael@0 | 86 | trace("setting iosvc.offline = false"); |
michael@0 | 87 | iosvc.offline = false; |
michael@0 | 88 | trace("done setting iosvc.offline = false"); |
michael@0 | 89 | ok(navigator.onLine, |
michael@0 | 90 | "navigator.onLine should be true when iosvc.offline == false"); |
michael@0 | 91 | ok(gState == window.MAX_STATE, |
michael@0 | 92 | "online event: all registered handlers should have been invoked, " + |
michael@0 | 93 | "actual: " + gState); |
michael@0 | 94 | } |