|
1 /* |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/licenses/publicdomain/ |
|
4 */ |
|
5 |
|
6 /* |
|
7 * Any copyright is dedicated to the Public Domain. |
|
8 * http://creativecommons.org/licenses/publicdomain/ |
|
9 */ |
|
10 |
|
11 function info(text) { |
|
12 dump("Test for Bug 925437: worker: " + text + "\n"); |
|
13 } |
|
14 |
|
15 function ok(test, message) { |
|
16 postMessage({ type: 'ok', test: test, message: message }); |
|
17 } |
|
18 |
|
19 /** |
|
20 * Returns a handler function for an online/offline event. The returned handler |
|
21 * ensures the passed event object has expected properties and that the handler |
|
22 * is called at the right moment (according to the gState variable). |
|
23 * @param nameTemplate The string identifying the hanlder. '%1' in that |
|
24 * string will be replaced with the event name. |
|
25 * @param eventName 'online' or 'offline' |
|
26 * @param expectedState value of gState at the moment the handler is called. |
|
27 * The handler increases gState by one before checking |
|
28 * if it matches expectedState. |
|
29 */ |
|
30 function makeHandler(nameTemplate, eventName, expectedState, prefix, custom) { |
|
31 prefix += ": "; |
|
32 return function(e) { |
|
33 var name = nameTemplate.replace(/%1/, eventName); |
|
34 ok(e.constructor == Event, prefix + "event should be an Event"); |
|
35 ok(e.type == eventName, prefix + "event type should be " + eventName); |
|
36 ok(!e.bubbles, prefix + "event should not bubble"); |
|
37 ok(!e.cancelable, prefix + "event should not be cancelable"); |
|
38 ok(e.target == self, prefix + "the event target should be the worker scope"); |
|
39 ok(eventName == 'online' ? navigator.onLine : !navigator.onLine, prefix + "navigator.onLine " + navigator.onLine + " should reflect event " + eventName); |
|
40 |
|
41 if (custom) { |
|
42 custom(); |
|
43 } |
|
44 } |
|
45 } |
|
46 |
|
47 |
|
48 |
|
49 var lastTest = false; |
|
50 |
|
51 function lastTestTest() { |
|
52 if (lastTest) { |
|
53 postMessage({ type: 'finished' }); |
|
54 close(); |
|
55 } |
|
56 } |
|
57 |
|
58 for (var event of ["online", "offline"]) { |
|
59 addEventListener(event, |
|
60 makeHandler( |
|
61 "addEventListener('%1', ..., false)", |
|
62 event, 1, "Child Worker", lastTestTest |
|
63 ), |
|
64 false); |
|
65 } |
|
66 |
|
67 onmessage = function(e) { |
|
68 if (e.data.type === 'lastTest') { |
|
69 lastTest = true; |
|
70 } else if (e.data.type === 'navigatorState') { |
|
71 ok(e.data.state === navigator.onLine, "Child and parent navigator state should match"); |
|
72 } |
|
73 } |
|
74 |
|
75 postMessage({ type: 'ready' }); |