Bug 1523562 [wpt PR 15120] - service worker: WPT: modernize and add debugging to claim-fetch.https.html., a=testonly

Automatic update from web-platform-tests
service worker: WPT: modernize and add debugging to claim-fetch.https.html.

This test is flaky in Chrome. One of the fetches fails but we only get
output "failed to fetch" from Chrome. Make the test easier to read using
async/await and add more debugging info.

Bug: 924448
Change-Id: Ib9a0537772b1160e627517ff3ed1cef820778705
Reviewed-on: https://chromium-review.googlesource.com/c/1441654
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: Kenichi Ishibashi <bashi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626891}

--

wpt-commits: 2e71724144098b754b1839e0d8cdfdcbe13bd7dd
wpt-pr: 15120
This commit is contained in:
Matt Falkenhagen 2019-02-01 13:41:16 +00:00 коммит произвёл James Graham
Родитель 447b214c4c
Коммит 275e88f6d9
1 изменённых файлов: 66 добавлений и 52 удалений

Просмотреть файл

@ -7,66 +7,80 @@
<body>
<script>
promise_test(function(t) {
var frame;
var resource = 'simple.txt';
async function try_fetch(fetch_func, path) {
let response;
try {
response = await fetch_func(path);
} catch (err) {
throw (`fetch() threw: ${err}`);
}
var worker;
var scope = 'resources/';
var script = 'resources/claim-worker.js';
let response_text;
try {
response_text = await response.text();
} catch (err) {
throw (`text() threw: ${err}`);
}
return Promise.resolve()
// Create the test iframe.
.then(() => with_iframe('resources/blank.html'))
.then(f => frame = f)
return response_text;
}
// Check the controller and test with fetch.
.then(() => assert_equals(frame.contentWindow.navigator.controller,
undefined,
'Should have no controller.'))
.then(() => frame.contentWindow.fetch(resource))
.then(response => response.text())
.then(response_text => assert_equals(response_text,
'a simple text file\n',
'fetch() should not be intercepted.'))
promise_test(async function(t) {
let frame;
const scope = 'resources/';
const script = 'resources/claim-worker.js';
t.add_cleanup(async () => {
if (frame)
frame.remove();
return service_worker_unregister(t, scope);
});
// Register a service worker.
.then(() => service_worker_unregister_and_register(t, script, scope))
.then(r => {
t.add_cleanup(() => service_worker_unregister(t, scope));
const resource = 'simple.txt';
worker = r.installing;
// Create the test frame.
await service_worker_unregister(t, scope);
frame = await with_iframe('resources/blank.html');
return wait_for_state(t, worker, 'activated');
})
// Let the service worker claim the iframe.
.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.
assert_equals(frame.contentWindow.navigator.controller, undefined,
'Should have no controller.');
let response;
try {
response = await try_fetch(frame.contentWindow.fetch, resource);
} catch (err) {
assert_unreached(`uncontrolled fetch failed: ${err}`);
}
assert_equals(response, 'a simple text file\n',
'fetch() should not be intercepted.');
// Check the controller and test with fetch.
.then(() => frame.contentWindow.navigator.serviceWorker.getRegistration(scope))
.then(r => assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
r.active,
'Test iframe should be claimed.'))
.then(() => frame.contentWindow.fetch(resource))
.then(response => response.text())
.then(response_text => assert_equals(response_text,
'Intercepted!',
'fetch() should be intercepted.'))
// Register a service worker.
const registration = await navigator.serviceWorker.register(script, {scope});
const worker = registration.installing;
await wait_for_state(t, worker, 'activated');
// Cleanup this testcase.
.then(() => frame.remove())
}, 'fetch() should be intercepted after the client is claimed.')
// Tell the service worker to claim the iframe.
const saw_message = new Promise((resolve) => {
const channel = new MessageChannel();
channel.port1.onmessage = t.step_func((event) => {
resolve(event.data);
});
worker.postMessage({port: channel.port2}, [channel.port2]);
});
const data = await saw_message;
assert_equals(data, 'PASS', 'Worker call to claim() should fulfill.');
// Check the controller and test with fetch.
const controller = frame.contentWindow.navigator.serviceWorker.controller;
assert_true(controller instanceof frame.contentWindow.ServiceWorker,
'iframe should be controlled.');
try {
response = await try_fetch(frame.contentWindow.fetch, resource);
} catch (err) {
assert_unreached(`controlled fetch failed: ${err}`);
}
assert_equals(response, 'Intercepted!',
'fetch() should be intercepted.');
}, 'fetch() should be intercepted after the client is claimed.');
</script>
</body>