Bug 1879312 [wpt PR 44471] - [SAA] Implementing SharedWorker support (Part 4: Add...
[gecko.git] / testing / web-platform / tests / storage-access-api / storage-access-beyond-cookies.SharedWorker.tentative.sub.https.window.js
blob2d4e3503b955b5b4be646425c98e869407d69e95
1 // META: script=/resources/testdriver.js
2 // META: script=/resources/testdriver-vendor.js
4 'use strict';
6 // Here's the set-up for this test:
7 // Step 1 (top-frame) Set up fallback failure listener for if the handle cannot be used.
8 // Step 2 (top-frame) Set up relay worker to expect "Same-origin handle access".
9 // Step 3 (top-frame) Set cookies and embed an iframe that's cross-site with top-frame.
10 // Step 4 (sub-frame) Try to use storage access API to access shared worker.
11 // Step 5 (sub-frame) Embed an iframe that's same-origin with top-frame.
12 // Step 6 (sub-sub-frame) Try to use storage access API to access first-party shared worker.
13 // Step 7 (sub-sub-frame) Send "HasAccess for SharedWorker" message to top-frame.
14 // Step 8 (top-frame) Set up cookie worker to expect it's already opened.
15 // TODO(crbug.com/1484966): Verify access to cookies in shared workers.
17 async_test(t => {
18   // Step 1
19   window.addEventListener("message", t.step_func(e => {
20     assert_equals(e.data, "HasAccess for SharedWorker", "Storage Access API should be accessible and return first-party data");
21   }));
23   // Step 2
24   const id = Date.now();
25   const relay_worker = new SharedWorker("/storage-access-api/resources/shared-worker-relay.js", {name: id, sameSiteCookies: 'none'});
26   relay_worker.port.onmessage = t.step_func(e => {
27     assert_equals(e.data, "Same-origin handle access", "Relay worker should divert messages here");
28     // Step 8
29     const cookie_worker = new SharedWorker("/storage-access-api/resources/shared-worker-cookies.py", {name: id, sameSiteCookies: 'none'});
30     cookie_worker.port.onmessage = t.step_func(e => {
31       assert_equals(e.data, "ReadOnLoad:None,ReadOnFetch:None,ConnectionsMade:2", "Worker should already have been opened and only see SameSite=None cookies");
32       t.done();
33     });
34   });
36   // Step 3
37   const cookie_set_window = window.open("/storage-access-api/resources/set_cookies.py");
38   cookie_set_window.onload =  t.step_func(_ => {
39     let iframe = document.createElement("iframe");
40     iframe.src = "https://{{hosts[alt][]}}:{{ports[https][0]}}/storage-access-api/resources/storage-access-beyond-cookies-iframe.sub.html?type=SharedWorker&id="+id;
41     document.body.appendChild(iframe);
42   });
43 }, "Verify StorageAccessAPIBeyondCookies for Shared Worker");