Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 2 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /service-workers/service-worker/controlled-dedicatedworker-postMessage.https.html - WPT Dashboard Interop Dashboard
<html>
<head>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
</head>
<body>
<script>
// Scope only the dedicated worker scripts used by this test (scope matching is
// a prefix match). A broad scope such as 'resources' would make this worker
// intercept navigations made by other tests that run later in the same browser
// session.
const SCOPE = 'resources/controlled-worker-';
let registration;
promise_test(async (test) => {
registration = await service_worker_unregister_and_register(
test, "postMessage-client-worker.js", SCOPE);
const worker = registration.installing || registration.waiting || registration.active;
await wait_for_state(test, worker, "activated");
}, "Register service worker");
promise_test(async (test) => {
const worker = new Worker('resources/controlled-worker-postMessage.js');
const event = await new Promise((resolve, reject) => {
test.step_timeout(() => reject("test timed out"), 3000);
worker.onmessage = resolve;
});
assert_equals(event.data, 0);
}, "Verify dedicated worker gets messages if setting event listener early");
promise_test(async (test) => {
const worker = new Worker('resources/controlled-worker-late-postMessage.js?repeatMessages');
const event = await new Promise((resolve, reject) => {
test.step_timeout(() => reject("test timed out"), 3000);
worker.onmessage = resolve;
});
assert_not_equals(event.data, "No message received");
assert_true(event.data > 0);
}, "Verify dedicated worker does not get all messages if not setting event listener early");
// Declared last so it runs after the tests above; leaves no registration
// behind for subsequent tests in the same browser session.
promise_test(async (test) => {
await registration.unregister();
}, "Unregister service worker");
</script>
</body>
</html>