summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_shared_workers.js
blob: 54c14e5524d8f024d30a272d38f33d9c01ed6e79 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// This test attemps to verify that:
// - SharedWorkers can be created and successfully spawned by web extensions
//   when web-extensions run in their own child process.
add_task(async function test_spawn_shared_worker() {
  if (!WebExtensionPolicy.useRemoteWebExtensions) {
    // Ensure RemoteWorkerService has been initialized in the main
    // process.
    Services.obs.notifyObservers(null, "profile-after-change");
  }

  const background = async function () {
    const worker = new SharedWorker("worker.js");
    await new Promise(resolve => {
      worker.port.onmessage = resolve;
      worker.port.postMessage("bgpage->worker");
    });
    browser.test.sendMessage("test-shared-worker:done");
  };

  const extension = ExtensionTestUtils.loadExtension({
    background,
    files: {
      "worker.js": function () {
        self.onconnect = evt => {
          const port = evt.ports[0];
          port.onmessage = () => port.postMessage("worker-reply");
        };
      },
    },
  });

  await extension.startup();
  await extension.awaitMessage("test-shared-worker:done");
  await extension.unload();
});