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