|
| 1 | +<!DOCTYPE html> |
| 2 | +<title>Service Worker: about:blank replacement handling</title> |
| 3 | +<meta name=timeout content=long> |
| 4 | +<script src="/resources/testharness.js"></script> |
| 5 | +<script src="/resources/testharnessreport.js"></script> |
| 6 | +<script src="/common/get-host-info.sub.js"></script> |
| 7 | +<script src="resources/test-helpers.sub.js"></script> |
| 8 | +<body> |
| 9 | +<script> |
| 10 | +// This test attempts to verify various initial about:blank document |
| 11 | +// creation is accurately reflected via the Clients API. The goal is |
| 12 | +// for Clients API to reflect what the browser actually does and not |
| 13 | +// to make special cases for the API. |
| 14 | +// |
| 15 | +// If your browser does not create an about:blank document in certain |
| 16 | +// cases then please just mark the test expected fail for now. The |
| 17 | +// reuse of globals from about:blank documents to the final load document |
| 18 | +// has particularly bad interop at the moment. Hopefully we can evolve |
| 19 | +// tests like this to eventually align browsers. |
| 20 | + |
| 21 | +const worker = 'resources/about-blank-replacement-worker.js'; |
| 22 | + |
| 23 | +// Helper routine that creates an iframe that internally has some kind |
| 24 | +// of nested window. The nested window could be another iframe or |
| 25 | +// it could be a popup window. |
| 26 | +function createFrameWithNestedWindow(url) { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + let frame = document.createElement('iframe'); |
| 29 | + frame.src = url; |
| 30 | + document.body.appendChild(frame); |
| 31 | + |
| 32 | + window.addEventListener('message', function onMsg(evt) { |
| 33 | + if (evt.data.type !== 'NESTED_LOADED') { |
| 34 | + return; |
| 35 | + } |
| 36 | + window.removeEventListener('message', onMsg); |
| 37 | + if (evt.data.result && evt.data.result.startsWith('failure:')) { |
| 38 | + reject(evt.data.result); |
| 39 | + return; |
| 40 | + } |
| 41 | + resolve(frame); |
| 42 | + }); |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +// Helper routine to request the given worker find the client with |
| 47 | +// the specified URL using the clients.matchAll() API. |
| 48 | +function getClientIdByURL(worker, url) { |
| 49 | + return new Promise(resolve => { |
| 50 | + navigator.serviceWorker.addEventListener('message', function onMsg(evt) { |
| 51 | + if (evt.data.type !== 'GET_CLIENT_ID') { |
| 52 | + return; |
| 53 | + } |
| 54 | + navigator.serviceWorker.removeEventListener('message', onMsg); |
| 55 | + resolve(evt.data.result); |
| 56 | + }); |
| 57 | + worker.postMessage({ type: 'GET_CLIENT_ID', url: url.toString() }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +async function doAsyncTest(t, scope, extraSearchParams) { |
| 62 | + let reg = await service_worker_unregister_and_register(t, worker, scope); |
| 63 | + await wait_for_state(t, reg.installing, 'activated'); |
| 64 | + |
| 65 | + // Load the scope as a frame. We expect this in turn to have a nested |
| 66 | + // iframe. The service worker will intercept the load of the nested |
| 67 | + // iframe and populate its body with the client ID of the initial |
| 68 | + // about:blank document it sees via clients.matchAll(). |
| 69 | + let frame = await createFrameWithNestedWindow(scope); |
| 70 | + let initialResult = frame.contentWindow.nested().document.body.textContent; |
| 71 | + assert_false(initialResult.startsWith('failure:'), `result: ${initialResult}`); |
| 72 | + |
| 73 | + // Next, ask the service worker to find the final client ID for the fully |
| 74 | + // loaded nested frame. |
| 75 | + let nestedURL = new URL(scope, window.location); |
| 76 | + nestedURL.searchParams.set('nested', true); |
| 77 | + extraSearchParams = extraSearchParams || {}; |
| 78 | + for (let p in extraSearchParams) { |
| 79 | + nestedURL.searchParams.set(p, extraSearchParams[p]); |
| 80 | + } |
| 81 | + let finalResult = await getClientIdByURL(reg.active, nestedURL); |
| 82 | + assert_false(finalResult.startsWith('failure:'), `result: ${finalResult}`); |
| 83 | + |
| 84 | + // The initial about:blank client and the final loaded client should have |
| 85 | + // the same ID value. |
| 86 | + assert_equals(initialResult, finalResult, 'client ID values should match'); |
| 87 | + |
| 88 | + frame.remove(); |
| 89 | + await service_worker_unregister_and_done(t, scope); |
| 90 | +} |
| 91 | + |
| 92 | +promise_test(async function(t) { |
| 93 | + // Execute a test where the nested frame is simply loaded normally. |
| 94 | + await doAsyncTest(t, 'resources/about-blank-replacement-frame.py'); |
| 95 | +}, 'Initial about:blank is controlled, exposed to clients.matchAll(), and ' + |
| 96 | + 'matches final Client.'); |
| 97 | + |
| 98 | +promise_test(async function(t) { |
| 99 | + // Execute a test where the nested frame is modified immediately by |
| 100 | + // its parent. In this case we add a message listener so the service |
| 101 | + // worker can ping the client to verify its existence. This ping-pong |
| 102 | + // check is performed during the initial load and when verifying the |
| 103 | + // final loaded client. |
| 104 | + await doAsyncTest(t, 'resources/about-blank-replacement-ping-frame.py', |
| 105 | + { 'ping': true }); |
| 106 | +}, 'Initial about:blank modified by parent is controlled, exposed to ' + |
| 107 | + 'clients.matchAll(), and matches final Client.'); |
| 108 | + |
| 109 | +promise_test(async function(t) { |
| 110 | + // Execute a test where the nested window is a popup window instead of |
| 111 | + // an iframe. This should behave the same as the simple iframe case. |
| 112 | + await doAsyncTest(t, 'resources/about-blank-replacement-popup-frame.py'); |
| 113 | +}, 'Popup initial about:blank is controlled, exposed to clients.matchAll(), and ' + |
| 114 | + 'matches final Client.'); |
| 115 | + |
| 116 | +promise_test(async function(t) { |
| 117 | + const scope = 'resources/about-blank-replacement-uncontrolled-nested-frame.html'; |
| 118 | + |
| 119 | + let reg = await service_worker_unregister_and_register(t, worker, scope); |
| 120 | + await wait_for_state(t, reg.installing, 'activated'); |
| 121 | + |
| 122 | + // Load the scope as a frame. We expect this in turn to have a nested |
| 123 | + // iframe. Unlike the other tests in this file the nested iframe URL |
| 124 | + // is not covered by a service worker scope. It should end up as |
| 125 | + // uncontrolled even though its initial about:blank is controlled. |
| 126 | + let frame = await createFrameWithNestedWindow(scope); |
| 127 | + let nested = frame.contentWindow.nested(); |
| 128 | + let initialResult = nested.document.body.textContent; |
| 129 | + |
| 130 | + // The nested iframe should not have been intercepted by the service |
| 131 | + // worker. The empty.html nested frame has "hello world" for its body. |
| 132 | + assert_equals(initialResult.trim(), 'hello world', `result: ${initialResult}`); |
| 133 | + |
| 134 | + assert_not_equals(frame.contentWindow.navigator.serviceWorker.controller, null, |
| 135 | + 'outer frame should be controlled'); |
| 136 | + |
| 137 | + assert_equals(nested.navigator.serviceWorker.controller, null, |
| 138 | + 'nested frame should not be controlled'); |
| 139 | + |
| 140 | + frame.remove(); |
| 141 | + await service_worker_unregister_and_done(t, scope); |
| 142 | +}, 'Initial about:blank is controlled, exposed to clients.matchAll(), and ' + |
| 143 | + 'final Client is not controlled by a service worker.'); |
| 144 | + |
| 145 | +</script> |
| 146 | +</body> |
0 commit comments