Skip to content

Commit

Permalink
[WPT] Eligibility: dedicated and shared workers (web-platform-tests#3…
Browse files Browse the repository at this point in the history
…1228)

When there are dedicated workers/shared workers,

Chrome: The page is not BFCached.
Firefox: The page is BFCached.
Safari: The page is BFCached for dedicated workers.
        Shared workers are not supported.

Bug: 1146955
Change-Id: Ia901715436e1a0c64c68ad5b537e99f685825489
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3220944
Reviewed-by: Ben Kelly <[email protected]>
Reviewed-by: Fergal Daly <[email protected]>
Reviewed-by: Rakina Zata Amni <[email protected]>
Commit-Queue: Hiroshige Hayashizaki <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968294}

Co-authored-by: Hiroshige Hayashizaki <[email protected]>
  • Loading branch information
2 people authored and mattwoodrow committed Feb 15, 2022
1 parent cb862e4 commit 6e4a5b0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="../resources/helper.sub.js"></script>
<script>
// Check whether the page is BFCached when there are dedicated workers that are
// already loaded.
runBfcacheTest({
funcBeforeNavigation: async () => {
globalThis.worker = new Worker('../resources/echo-worker.js');
// Make sure the worker starts before navigation.
await WorkerHelper.pingWorker(globalThis.worker);
},
funcAfterAssertion: async (pageA) => {
// Confirm that the worker is still there.
assert_equals(
await pageA.execute_script(() => WorkerHelper.pingWorker(globalThis.worker)),
'PASS',
'Worker should still work after restored from BFCache');
}
}, 'Eligibility: dedicated workers');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="../resources/helper.sub.js"></script>
<script>
// Check whether the page is BFCached when there are shared workers that are
// already loaded.
runBfcacheTest({
funcBeforeNavigation: async () => {
globalThis.worker = new SharedWorker('../resources/echo-worker.js');
// Make sure the worker starts before navigation.
await WorkerHelper.pingWorker(globalThis.worker);
},
funcAfterAssertion: async (pageA) => {
// Confirm that the worker is still there.
assert_equals(
await pageA.execute_script(() => WorkerHelper.pingWorker(globalThis.worker)),
'PASS',
'SharedWorker should still work after restored from BFCache');
}
}, 'Eligibility: shared workers');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// On receiving a message from the parent Document, send back a message to the
// parent Document. This is used to wait for worker initialization and test
// that this worker is alive and working.

// For dedicated workers.
self.addEventListener('message', event => {
postMessage(event.data);
});

// For shared workers.
onconnect = e => {
const port = e.ports[0];
port.onmessage = event => {
port.postMessage(event.data);
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE HTML>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="event-recorder.js" type="module"></script>
<script src="worker-helper.js" type="module"></script>
<script type="module">
const params = new URLSearchParams(window.location.search);
const uuid = params.get('uuid');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Worker-related helper file to be used from executor.html.

// The class `WorkerHelper` is exposed to `globalThis` because this should be
// used via `eval()`.
globalThis.WorkerHelper = class {
static pingWorker(worker) {
return new Promise((resolve, reject) => {
const message = 'message ' + Math.random();
const onmessage = e => {
if (e.data === message) {
resolve('PASS');
} else {
reject('pingWorker: expected ' + message + ' but got ' + e.data);
}
};
worker.onerror = reject;
if (worker instanceof Worker) {
worker.addEventListener('message', onmessage, {once: true});
worker.postMessage(message);
} else if (worker instanceof SharedWorker) {
worker.port.onmessage = onmessage;
worker.port.postMessage(message);
} else {
reject('Unexpected worker type');
}
});
}
};

0 comments on commit 6e4a5b0

Please sign in to comment.