dom/workers/test/onLine_worker_child.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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

mercurial