michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: function info(text) { michael@0: dump("Test for Bug 925437: worker: " + text + "\n"); michael@0: } michael@0: michael@0: function ok(test, message) { michael@0: postMessage({ type: 'ok', test: test, message: message }); michael@0: } michael@0: michael@0: /** michael@0: * Returns a handler function for an online/offline event. The returned handler michael@0: * ensures the passed event object has expected properties and that the handler michael@0: * is called at the right moment (according to the gState variable). michael@0: * @param nameTemplate The string identifying the hanlder. '%1' in that michael@0: * string will be replaced with the event name. michael@0: * @param eventName 'online' or 'offline' michael@0: * @param expectedState value of gState at the moment the handler is called. michael@0: * The handler increases gState by one before checking michael@0: * if it matches expectedState. michael@0: */ michael@0: function makeHandler(nameTemplate, eventName, expectedState, prefix, custom) { michael@0: prefix += ": "; michael@0: return function(e) { michael@0: var name = nameTemplate.replace(/%1/, eventName); michael@0: ok(e.constructor == Event, prefix + "event should be an Event"); michael@0: ok(e.type == eventName, prefix + "event type should be " + eventName); michael@0: ok(!e.bubbles, prefix + "event should not bubble"); michael@0: ok(!e.cancelable, prefix + "event should not be cancelable"); michael@0: ok(e.target == self, prefix + "the event target should be the worker scope"); michael@0: ok(eventName == 'online' ? navigator.onLine : !navigator.onLine, prefix + "navigator.onLine " + navigator.onLine + " should reflect event " + eventName); michael@0: michael@0: if (custom) { michael@0: custom(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: var lastTest = false; michael@0: michael@0: function lastTestTest() { michael@0: if (lastTest) { michael@0: postMessage({ type: 'finished' }); michael@0: close(); michael@0: } michael@0: } michael@0: michael@0: for (var event of ["online", "offline"]) { michael@0: addEventListener(event, michael@0: makeHandler( michael@0: "addEventListener('%1', ..., false)", michael@0: event, 1, "Child Worker", lastTestTest michael@0: ), michael@0: false); michael@0: } michael@0: michael@0: onmessage = function(e) { michael@0: if (e.data.type === 'lastTest') { michael@0: lastTest = true; michael@0: } else if (e.data.type === 'navigatorState') { michael@0: ok(e.data.state === navigator.onLine, "Child and parent navigator state should match"); michael@0: } michael@0: } michael@0: michael@0: postMessage({ type: 'ready' });