2 Any copyright is dedicated to the Public Domain.
3 http://creativecommons.org/publicdomain/zero/1.0/
8 <title>Test for SharedWorker
</title>
9 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css">
11 <script class=
"testbody" type=
"text/javascript">
15 const windowRelativeURL =
"multi_sharedWorker_frame_nobfcache.html";
16 const storedData =
"0123456789abcdefghijklmnopqrstuvwxyz";
18 bc = new BroadcastChannel(
"bugSharedWorkerLiftetime");
19 bc.onmessage = (event) =
> {
21 var command = msg.command;
22 if (command ==
"finished") {
26 } else if (command ==
"debug") {
28 } else if (command ==
"fromWorker" || command ==
"loaded") {
29 sendToGenerator(msg.workerMessage);
32 bc2 = new BroadcastChannel(
"navigate");
33 bc2.onmessage = (event) =
> {
34 if (event.data.command ==
"loaded") {
39 function postToWorker(workerMessage) {
40 bc.postMessage({command:
"postToWorker", workerMessage});
43 let testGenerator = (function*() {
44 SimpleTest.waitForExplicitFinish();
47 window.open(windowRelativeURL,
"testWin",
"noopener");
50 // Retrieve data from worker
51 postToWorker({ command:
"retrieve" });
53 let msg = yield undefined;
55 // Verify there is no data stored
56 is(msg.type,
"result",
"Got a result message");
57 is(msg.data, undefined,
"No data stored yet");
59 // Store data, and retrieve it
60 postToWorker({ command:
"store", data: storedData });
61 postToWorker({ command:
"retrieve" });
63 msg = yield undefined;
64 // Verify there is data stored
65 is(msg.type,
"result",
"Got a result message");
66 is(msg.data, storedData,
"Got stored data");
69 info(
"Navigating to a different page");
70 // Current subpage should not go into bfcache because of the Cache-Control
71 // headers we have set.
72 bc.postMessage({command:
"navigate", location:
"navigate.html"});
75 info(
"Navigating to " + windowRelativeURL);
76 bc2.postMessage({command:
"navigate", location: windowRelativeURL });
79 postToWorker({ command:
"retrieve" });
81 msg = yield undefined;
82 is(msg.type,
"result",
"Got a result message");
83 is(msg.data, undefined,
"No data stored");
85 postToWorker({ command:
"store", data: storedData });
86 postToWorker({ command:
"retrieve" });
88 msg = yield undefined;
89 is(msg.type,
"result",
"Got a result message");
90 is(msg.data, storedData,
"Got stored data");
92 bc.postMessage({command:
"finish"});
95 let sendToGenerator = testGenerator.next.bind(testGenerator);
99 SimpleTest.waitForExplicitFinish();
101 // Bug
1746646: Make mochitests work with TCP enabled (cookieBehavior =
5)
102 // Acquire storage access permission here so that the BroadcastChannel used to
103 // communicate with the opened windows works in xorigin tests. Otherwise,
104 // the iframe containing this page is isolated from first-party storage access,
105 // which isolates BroadcastChannel communication.
106 SpecialPowers.wrap(document).notifyUserGestureActivation();
107 SpecialPowers.pushPrefEnv({
108 set: [[
"privacy.partition.always_partition_third_party_non_cookie_storage", false]],
110 SpecialPowers.pushPermissions([{'type': 'storageAccessAPI', 'allow':
1, 'context': document}], () =
>{
111 SpecialPowers.wrap(document).requestStorageAccess().then(() =
> {
114 SpecialPowers.removePermission(
"3rdPartyStorage^http://mochi.test:8888",
"http://mochi.xorigin-test:8888");