dom/workers/test/onLine_worker_child.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/onLine_worker_child.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/*
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/licenses/publicdomain/
     1.7 + */
     1.8 +
     1.9 +/*
    1.10 + * Any copyright is dedicated to the Public Domain.
    1.11 + * http://creativecommons.org/licenses/publicdomain/
    1.12 + */
    1.13 +
    1.14 +function info(text) {
    1.15 +  dump("Test for Bug 925437: worker: " + text + "\n");
    1.16 +}
    1.17 +
    1.18 +function ok(test, message) {
    1.19 +  postMessage({ type: 'ok', test: test, message: message });
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 + * Returns a handler function for an online/offline event. The returned handler
    1.24 + * ensures the passed event object has expected properties and that the handler
    1.25 + * is called at the right moment (according to the gState variable).
    1.26 + * @param nameTemplate The string identifying the hanlder. '%1' in that
    1.27 + *                     string will be replaced with the event name.
    1.28 + * @param eventName 'online' or 'offline'
    1.29 + * @param expectedState value of gState at the moment the handler is called.
    1.30 + *                       The handler increases gState by one before checking
    1.31 + *                       if it matches expectedState.
    1.32 + */
    1.33 +function makeHandler(nameTemplate, eventName, expectedState, prefix, custom) {
    1.34 +  prefix += ": ";
    1.35 +  return function(e) {
    1.36 +    var name = nameTemplate.replace(/%1/, eventName);
    1.37 +    ok(e.constructor == Event, prefix + "event should be an Event");
    1.38 +    ok(e.type == eventName, prefix + "event type should be " + eventName);
    1.39 +    ok(!e.bubbles, prefix + "event should not bubble");
    1.40 +    ok(!e.cancelable, prefix + "event should not be cancelable");
    1.41 +    ok(e.target == self, prefix + "the event target should be the worker scope");
    1.42 +    ok(eventName == 'online' ? navigator.onLine : !navigator.onLine, prefix + "navigator.onLine " + navigator.onLine + " should reflect event " + eventName);
    1.43 +
    1.44 +    if (custom) {
    1.45 +      custom();
    1.46 +    }
    1.47 +  }
    1.48 +}
    1.49 +
    1.50 +
    1.51 +
    1.52 +var lastTest = false;
    1.53 +
    1.54 +function lastTestTest() {
    1.55 +  if (lastTest) {
    1.56 +    postMessage({ type: 'finished' });
    1.57 +    close();
    1.58 +  }
    1.59 +}
    1.60 +
    1.61 +for (var event of ["online", "offline"]) {
    1.62 +  addEventListener(event,
    1.63 +                   makeHandler(
    1.64 +                     "addEventListener('%1', ..., false)",
    1.65 +                     event, 1, "Child Worker", lastTestTest
    1.66 +                   ),
    1.67 +                   false);
    1.68 +}
    1.69 +
    1.70 +onmessage = function(e) {
    1.71 +  if (e.data.type === 'lastTest') {
    1.72 +    lastTest = true;
    1.73 +  } else if (e.data.type === 'navigatorState') {
    1.74 +    ok(e.data.state === navigator.onLine, "Child and parent navigator state should match");
    1.75 +  }
    1.76 +}
    1.77 +
    1.78 +postMessage({ type: 'ready' });

mercurial