summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/fetch/worker_wrapper.js
blob: c2f0ec00e8ed069ad2b7d816060ce776230ee3c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
importScripts("utils.js");
var client;
var context;

function ok(a, msg) {
  client.postMessage({type: 'status', status: !!a,
                      msg: a + ": " + msg, context: context});
}

function is(a, b, msg) {
  client.postMessage({type: 'status', status: a === b,
                      msg: a + " === " + b + ": " + msg, context: context});
}

addEventListener('message', function workerWrapperOnMessage(e) {
  removeEventListener('message', workerWrapperOnMessage);
  var data = e.data;

  function loadTest() {
    var done = function() {
      client.postMessage({ type: 'finish', context: context });
    }

    try {
      importScripts(data.script);
      // runTest() is provided by the test.
      runTest().then(done, done);
    } catch(e) {
      client.postMessage({
        type: 'status',
        status: false,
        msg: 'worker failed to import ' + data.script + "; error: " + e.message,
        context: context
      });
      done();
    }
  }

  if ("ServiceWorker" in self) {
    self.clients.matchAll().then(function(clients) {
      for (var i = 0; i < clients.length; ++i) {
        if (clients[i].url.indexOf("message_receiver.html") > -1) {
          client = clients[i];
          break;
        }
      }
      if (!client) {
        dump("We couldn't find the message_receiver window, the test will fail\n");
      }
      context = "ServiceWorker";
      loadTest();
    });
  } else {
    client = self;
    context = "Worker";
    loadTest();
  }
});