dom/workers/test/onLine_worker_child.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/licenses/publicdomain/
     4  */
     6 /*
     7  * Any copyright is dedicated to the Public Domain.
     8  * http://creativecommons.org/licenses/publicdomain/
     9  */
    11 function info(text) {
    12   dump("Test for Bug 925437: worker: " + text + "\n");
    13 }
    15 function ok(test, message) {
    16   postMessage({ type: 'ok', test: test, message: message });
    17 }
    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);
    41     if (custom) {
    42       custom();
    43     }
    44   }
    45 }
    49 var lastTest = false;
    51 function lastTestTest() {
    52   if (lastTest) {
    53     postMessage({ type: 'finished' });
    54     close();
    55   }
    56 }
    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 }
    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 }
    75 postMessage({ type: 'ready' });

mercurial