summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/modules/content/WorkerDriver.js
blob: 365d00f3638cbe02cf34287754f559d66a4f9ae1 (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

export async function runTestInWorker(script, base, listener) {
  return new Promise(function(resolve) {
    const globalHeadUrl = new URL(
      "/tests/dom/quota/test/modules/worker/head.js",
      base
    );

    const worker = new Worker(globalHeadUrl.href);

    worker.onmessage = function(event) {
      const data = event.data;

      switch (data.op) {
        case "ok":
          listener.onOk(data.value, data.message);
          break;

        case "is":
          listener.onIs(data.a, data.b, data.message);
          break;

        case "info":
          listener.onInfo(data.message);
          break;

        case "finish":
          resolve();
          break;

        case "failure":
          listener.onOk(false, "Worker had a failure: " + data.message);
          resolve();
          break;
      }
    };

    worker.onerror = function(event) {
      listener.onOk(false, "Worker had an error: " + event.data);
      resolve();
    };

    const scriptUrl = new URL(script, base);

    const localHeadUrl = new URL("head.js", scriptUrl);

    worker.postMessage([localHeadUrl.href, scriptUrl.href]);
  });
}