Backed out changeset 06f41c22f3a6 (bug 1888460) for causing linux xpcshell failures...
[gecko.git] / dom / workers / test / onLine_worker_head.js
blob8c6d601aa53ee75d5fe846eb9bdbf9eb48c67897
1 /*
2  * Any copyright is dedicated to the Public Domain.
3  * http://creativecommons.org/licenses/publicdomain/
4  */
6 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
8 function info(text) {
9   dump("Test for Bug 925437: worker: " + text + "\n");
12 function ok(test, message) {
13   postMessage({ type: "ok", test, message });
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  * @param nameTemplate The string identifying the hanlder. '%1' in that
21  *                     string will be replaced with the event name.
22  * @param eventName 'online' or 'offline'
23  * @param expectedState value of gState at the moment the handler is called.
24  *                       The handler increases gState by one before checking
25  *                       if it matches expectedState.
26  */
27 function makeHandler(nameTemplate, eventName, expectedState, prefix, custom) {
28   prefix += ": ";
29   return function (e) {
30     var name = nameTemplate.replace(/%1/, eventName);
31     ok(e.constructor == Event, prefix + "event should be an Event");
32     ok(e.type == eventName, prefix + "event type should be " + eventName);
33     ok(!e.bubbles, prefix + "event should not bubble");
34     ok(!e.cancelable, prefix + "event should not be cancelable");
35     ok(
36       e.target == self,
37       prefix + "the event target should be the worker scope"
38     );
39     ok(
40       eventName == "online" ? navigator.onLine : !navigator.onLine,
41       prefix +
42         "navigator.onLine " +
43         navigator.onLine +
44         " should reflect event " +
45         eventName
46     );
48     if (custom) {
49       custom();
50     }
51   };