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