Commit 5973da0d authored by Hiroki Nakagawa's avatar Hiroki Nakagawa Committed by Commit Bot

ServiceWorker: Modernize WPTs for Client.postMessage using async/await etc

This is a preparation for adding more tests for postMessage to worker clients.

Bug: n/a
Change-Id: I9e20b04fd86ad27faaecd7d592cac6dfa5ae16b1
Reviewed-on: https://chromium-review.googlesource.com/c/1317067
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605276}
parent ef055860
......@@ -5,50 +5,38 @@
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
promise_test(t => {
var script = 'resources/postmessage-to-client-worker.js';
var scope = 'resources/blank.html';
var w;
promise_test(async t => {
const script = 'resources/postmessage-to-client-worker.js';
const scope = 'resources/blank.html';
return service_worker_unregister_and_register(t, script, scope)
.then(registration => {
t.add_cleanup(() => registration.unregister());
return wait_for_state(t, registration.installing, 'activated');
})
.then(() => with_iframe(scope))
.then(frame => {
t.add_cleanup(() => frame.remove());
const registration =
await service_worker_unregister_and_register(t, script, scope);
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');
const frame = await with_iframe(scope);
t.add_cleanup(() => frame.remove());
const w = frame.contentWindow;
return new Promise(resolve => {
w = frame.contentWindow;
w.navigator.serviceWorker.onmessage = resolve;
w.navigator.serviceWorker.controller.postMessage('ping');
});
})
.then(e => {
var message = e.data;
assert_equals(e.constructor, w.MessageEvent,
'message events should use MessageEvent interface.');
assert_equals(e.type, 'message', 'type should be "message".');
assert_equals(e.bubbles, false, 'message events should not bubble.');
assert_equals(e.cancelable, false,
'message events should not be cancelable.');
assert_equals(
e.origin, location.origin,
'origin of message should be origin of Service Worker.');
assert_equals(e.lastEventId, '',
'lastEventId should be an empty string.');
assert_equals(e.source.constructor, w.ServiceWorker,
'source should use ServiceWorker interface.');
assert_equals(
e.source, w.navigator.serviceWorker.controller,
'source should be the service worker that sent the message.');
assert_equals(e.ports.length, 0, 'ports should be an empty array.');
assert_equals(message, 'Sending message via clients');
return new Promise(resolve => {
w.navigator.serviceWorker.onmessage = resolve;
});
})
.then(e => { assert_equals(e.data, 'quit'); });
}, 'postMessage from ServiceWorker to Client.');
w.navigator.serviceWorker.controller.postMessage('ping');
let e = await new Promise(r => w.navigator.serviceWorker.onmessage = r);
assert_equals(e.constructor, w.MessageEvent,
'message events should use MessageEvent interface.');
assert_equals(e.type, 'message', 'type should be "message".');
assert_false(e.bubbles, 'message events should not bubble.');
assert_false(e.cancelable, 'message events should not be cancelable.');
assert_equals(e.origin, location.origin,
'origin of message should be origin of Service Worker.');
assert_equals(e.lastEventId, '',
'lastEventId should be an empty string.');
assert_equals(e.source.constructor, w.ServiceWorker,
'source should use ServiceWorker interface.');
assert_equals(e.source, w.navigator.serviceWorker.controller,
'source should be the service worker that sent the message.');
assert_equals(e.ports.length, 0, 'ports should be an empty array.');
assert_equals(e.data, 'Sending message via clients');
e = await new Promise(r => w.navigator.serviceWorker.onmessage = r);
assert_equals(e.data, 'quit');
}, 'postMessage from ServiceWorker to Client.');
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment