tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

common.js (3232B)


      1 /* import-globals-from ../../../../testing/mochitest/tests/SimpleTest/SimpleTest.js */
      2 
      3 // This would be a bit nicer with `self`, but Worklet doesn't have that, so
      4 // `globalThis` it is, see https://github.com/whatwg/html/issues/7696
      5 function workerReply(port) {
      6  port.postMessage({
      7    testTrialInterfaceExposed: !!globalThis.TestTrialInterface,
      8  });
      9 }
     10 
     11 if (
     12  globalThis.SharedWorkerGlobalScope &&
     13  globalThis instanceof globalThis.SharedWorkerGlobalScope
     14 ) {
     15  globalThis.addEventListener("connect", function (e) {
     16    const port = e.ports[0];
     17    workerReply(port);
     18  });
     19 } else if (
     20  globalThis.WorkerGlobalScope &&
     21  globalThis instanceof globalThis.WorkerGlobalScope
     22 ) {
     23  workerReply(globalThis);
     24 } else if (
     25  globalThis.WorkletGlobalScope &&
     26  globalThis instanceof globalThis.WorkletGlobalScope
     27 ) {
     28  class Processor extends AudioWorkletProcessor {
     29    constructor() {
     30      super();
     31      this.port.start();
     32      workerReply(this.port);
     33    }
     34 
     35    process() {
     36      // Do nothing, output silence
     37      return true;
     38    }
     39  }
     40  registerProcessor("test-processor", Processor);
     41 }
     42 
     43 function assertTestTrialActive(shouldBeActive) {
     44  add_task(async function () {
     45    info("Main thread test: " + document.URL);
     46    is(
     47      !!navigator.testTrialGatedAttribute,
     48      shouldBeActive,
     49      "Should match active status for Navigator.testTrialControlledAttribute"
     50    );
     51    is(
     52      !!self.TestTrialInterface,
     53      shouldBeActive,
     54      "Should match active status for TestTrialInterface"
     55    );
     56    if (shouldBeActive) {
     57      ok(
     58        new self.TestTrialInterface(),
     59        "Should be able to construct interface"
     60      );
     61    }
     62 
     63    function promiseWorkerWorkletMessage(target, context) {
     64      info(`promiseWorkerWorkletMessage(${context})`);
     65      return new Promise(resolve => {
     66        target.addEventListener(
     67          "message",
     68          function (e) {
     69            is(
     70              e.data.testTrialInterfaceExposed,
     71              shouldBeActive,
     72              "Should work as expected in " + context
     73            );
     74            info(`got ${context} message`);
     75            resolve();
     76          },
     77          { once: true }
     78        );
     79      });
     80    }
     81 
     82    {
     83      info("Worker test");
     84      const worker = new Worker("common.js");
     85      await promiseWorkerWorkletMessage(worker, "worker");
     86      worker.terminate();
     87    }
     88 
     89    {
     90      info("SharedWorker test");
     91      // We want a unique worker per page since the trial state depends on the
     92      // creator document.
     93      const worker = new SharedWorker("common.js", document.URL);
     94      const promise = promiseWorkerWorkletMessage(worker.port, "shared worker");
     95      worker.port.start();
     96      await promise;
     97    }
     98 
     99    {
    100      info("AudioWorklet test");
    101      const audioContext = new AudioContext();
    102      await audioContext.audioWorklet.addModule("common.js");
    103      audioContext.resume();
    104      const workletNode = new AudioWorkletNode(audioContext, "test-processor");
    105      const promise = promiseWorkerWorkletMessage(workletNode.port, "worklet");
    106      workletNode.port.start();
    107      await promise;
    108      await audioContext.close();
    109    }
    110 
    111    // FIXME(emilio): Add more tests.
    112    //  * Stuff hanging off Window or Document (bug 1757935).
    113  });
    114 }