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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
<!doctype html>
<meta name="timeout" content="long">
<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 src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script>
// Calling Clients.claim() on the service worker when a controlled page is in
// BFCache should evict the page from BFCache, as per
// https://github.com/w3c/ServiceWorker/issues/1038#issuecomment-291028845.
promise_test(async t => {
const pageA = new RemoteContext(token());
const pageB = new RemoteContext(token());
const urlA = location.origin + executorPath + pageA.context_id;
const urlB = originCrossSite + executorPath + pageB.context_id;
window.open(urlA, '_blank', 'noopener');
await pageA.execute_script(waitForPageShow);
// Register a service worker after `pageA` is loaded to make `pageA`
// uncontrolled at this time.
const workerUrl =
'resources/service-worker.js?pipe=header(Service-Worker-Allowed,../)';
const registration =
await service_worker_unregister_and_register(t, workerUrl, './');
t.add_cleanup(_ => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');
// Navigate to `urlB`.
await pageA.execute_script(
(url) => {
prepareNavigation(() => { location.href = url; });
},
[urlB]);
await pageB.execute_script(waitForPageShow);
// Call Clients.claim() on the service worker when `pageA` is in BFCache.
const controllerChanged = new Promise(
resolve => navigator.serviceWorker.oncontrollerchange = resolve);
await claim(t, registration.active);
await controllerChanged;
// `pageA` doesn't appear in matchAll().
const clients1 = await (await fetch('/get-clients-matchall')).json();
assert_true(clients1.indexOf(urlA) < 0,
'1: matchAll() before back navigation');
// Back navigate and check that the page was evicted from BFCache.
await pageB.execute_script(
() => {
prepareNavigation(() => { history.back(); });
}
);
await pageA.execute_script(waitForPageShow);
await assert_not_bfcached(pageA);
// After back navigation, `pageA` appear in matchAll(), because it was newly
// loaded and controlled by the service worker.
const clients2 = await (await fetch('/get-clients-matchall')).json();
const controlled2 = await pageA.execute_script(
() => (navigator.serviceWorker.controller !== null));
assert_true(clients2.indexOf(urlA) >= 0,
'2: matchAll() just after back navigation');
assert_true(controlled2,
'2: pageA should be controlled just after back navigation');
}, 'Clients.claim() evicts pages that would be affected from BFCache');
</script>
|