blob: c2c254ca65c36aea942a8a77bd630f2a66dcf982 [file] [log] [blame]
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
promise_test(function(t) {
return runTest(t, 'resources/claim-worker-fetch-iframe.html');
}, 'fetch() in Worker should be intercepted after the client is claimed.');
promise_test(function(t) {
return runTest(t, 'resources/claim-nested-worker-fetch-iframe.html');
}, 'fetch() in nested Worker should be intercepted after the client is claimed.');
var frame;
var registration;
function runTest(t, iframe_url) {
var resource = 'simple.txt';
var worker;
var scope = 'resources/';
var script = 'resources/claim-worker.js';
const test_result = Promise.resolve()
// Create the test iframe with a dedicated worker.
.then(() => with_iframe(iframe_url))
.then(f => frame = f)
// Check the controller and test with fetch in the worker.
.then(() => assert_equals(frame.contentWindow.navigator.controller,
undefined,
'Should have no controller.'))
.then(() => frame.contentWindow.fetch_in_worker(resource))
.then(response_text => assert_equals(response_text,
'a simple text file\n',
'fetch() should not be intercepted.'))
// Register a service worker.
.then(() => service_worker_unregister_and_register(t, script, scope))
.then(r => worker = r.installing)
.then(() => wait_for_state(t, worker, 'activated'))
// Let the service worker claim the iframe and the worker.
.then(() => {
var channel = new MessageChannel();
var saw_message = new Promise(function(resolve) {
channel.port1.onmessage = t.step_func(function(e) {
assert_equals(e.data, 'PASS',
'Worker call to claim() should fulfill.');
resolve();
});
});
worker.postMessage({port: channel.port2}, [channel.port2]);
return saw_message;
})
// Check the controller and test with fetch in the worker.
.then(() => frame.contentWindow.navigator.serviceWorker.getRegistration(scope))
.then(r => registration = r)
.then(() => assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
registration.active,
'Test iframe should be claimed.'))
// TODO(horo): Check the worker's navigator.seviceWorker.controller.
.then(() => frame.contentWindow.fetch_in_worker(resource))
.then(response_text =>
assert_equals(response_text,
'Intercepted!',
'fetch() in the worker should be intercepted.'));
// Cleanup this testcase.
return Promise.resolve()
.then(cleanup, cleanup)
.then(() => { return test_result; });
}
const cleanup = async () => {
if (registration)
await registration.unregister();
if (frame)
frame.remove();
}
</script>
</body>