summaryrefslogtreecommitdiffstats
path: root/dom/origin-trials/tests/mochitest/common.js
blob: a38a975ea7de2a58727ef3d0d877feb37b488335 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* import-globals-from ../../../../testing/mochitest/tests/SimpleTest/SimpleTest.js */

// This would be a bit nicer with `self`, but Worklet doesn't have that, so
// `globalThis` it is, see https://github.com/whatwg/html/issues/7696
function workerReply(port) {
  port.postMessage({
    testTrialInterfaceExposed: !!globalThis.TestTrialInterface,
  });
}

if (
  globalThis.SharedWorkerGlobalScope &&
  globalThis instanceof globalThis.SharedWorkerGlobalScope
) {
  globalThis.addEventListener("connect", function (e) {
    const port = e.ports[0];
    workerReply(port);
  });
} else if (
  globalThis.WorkerGlobalScope &&
  globalThis instanceof globalThis.WorkerGlobalScope
) {
  workerReply(globalThis);
} else if (
  globalThis.WorkletGlobalScope &&
  globalThis instanceof globalThis.WorkletGlobalScope
) {
  class Processor extends AudioWorkletProcessor {
    constructor() {
      super();
      this.port.start();
      workerReply(this.port);
    }

    process(inputs, outputs, parameters) {
      // Do nothing, output silence
      return true;
    }
  }
  registerProcessor("test-processor", Processor);
}

function assertTestTrialActive(shouldBeActive) {
  add_task(async function () {
    info("Main thread test: " + document.URL);
    is(
      !!navigator.testTrialGatedAttribute,
      shouldBeActive,
      "Should match active status for Navigator.testTrialControlledAttribute"
    );
    is(
      !!self.TestTrialInterface,
      shouldBeActive,
      "Should match active status for TestTrialInterface"
    );
    if (shouldBeActive) {
      ok(
        new self.TestTrialInterface(),
        "Should be able to construct interface"
      );
    }

    function promiseWorkerWorkletMessage(target, context) {
      info(`promiseWorkerWorkletMessage(${context})`);
      return new Promise(resolve => {
        target.addEventListener(
          "message",
          function (e) {
            is(
              e.data.testTrialInterfaceExposed,
              shouldBeActive,
              "Should work as expected in " + context
            );
            info(`got ${context} message`);
            resolve();
          },
          { once: true }
        );
      });
    }

    {
      info("Worker test");
      const worker = new Worker("common.js");
      await promiseWorkerWorkletMessage(worker, "worker");
      worker.terminate();
    }

    {
      info("SharedWorker test");
      // We want a unique worker per page since the trial state depends on the
      // creator document.
      const worker = new SharedWorker("common.js", document.URL);
      const promise = promiseWorkerWorkletMessage(worker.port, "shared worker");
      worker.port.start();
      await promise;
    }

    {
      info("AudioWorklet test");
      const audioContext = new AudioContext();
      await audioContext.audioWorklet.addModule("common.js");
      audioContext.resume();
      const workletNode = new AudioWorkletNode(audioContext, "test-processor");
      const promise = promiseWorkerWorkletMessage(workletNode.port, "worklet");
      workletNode.port.start();
      await promise;
      await audioContext.close();
    }

    // FIXME(emilio): Add more tests.
    //  * Stuff hanging off Window or Document (bug 1757935).
  });
}