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 @@ ...@@ -5,50 +5,38 @@
<script src="/common/get-host-info.sub.js"></script> <script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script> <script src="resources/test-helpers.sub.js"></script>
<script> <script>
promise_test(t => { promise_test(async t => {
var script = 'resources/postmessage-to-client-worker.js'; const script = 'resources/postmessage-to-client-worker.js';
var scope = 'resources/blank.html'; const scope = 'resources/blank.html';
var w;
return service_worker_unregister_and_register(t, script, scope) const registration =
.then(registration => { await service_worker_unregister_and_register(t, script, scope);
t.add_cleanup(() => registration.unregister()); t.add_cleanup(() => registration.unregister());
return wait_for_state(t, registration.installing, 'activated'); await wait_for_state(t, registration.installing, 'activated');
}) const frame = await with_iframe(scope);
.then(() => with_iframe(scope)) t.add_cleanup(() => frame.remove());
.then(frame => { const w = frame.contentWindow;
t.add_cleanup(() => frame.remove());
return new Promise(resolve => { w.navigator.serviceWorker.controller.postMessage('ping');
w = frame.contentWindow; let e = await new Promise(r => w.navigator.serviceWorker.onmessage = r);
w.navigator.serviceWorker.onmessage = resolve;
w.navigator.serviceWorker.controller.postMessage('ping'); assert_equals(e.constructor, w.MessageEvent,
}); 'message events should use MessageEvent interface.');
}) assert_equals(e.type, 'message', 'type should be "message".');
.then(e => { assert_false(e.bubbles, 'message events should not bubble.');
var message = e.data; assert_false(e.cancelable, 'message events should not be cancelable.');
assert_equals(e.constructor, w.MessageEvent, assert_equals(e.origin, location.origin,
'message events should use MessageEvent interface.'); 'origin of message should be origin of Service Worker.');
assert_equals(e.type, 'message', 'type should be "message".'); assert_equals(e.lastEventId, '',
assert_equals(e.bubbles, false, 'message events should not bubble.'); 'lastEventId should be an empty string.');
assert_equals(e.cancelable, false, assert_equals(e.source.constructor, w.ServiceWorker,
'message events should not be cancelable.'); 'source should use ServiceWorker interface.');
assert_equals( assert_equals(e.source, w.navigator.serviceWorker.controller,
e.origin, location.origin, 'source should be the service worker that sent the message.');
'origin of message should be origin of Service Worker.'); assert_equals(e.ports.length, 0, 'ports should be an empty array.');
assert_equals(e.lastEventId, '', assert_equals(e.data, 'Sending message via clients');
'lastEventId should be an empty string.');
assert_equals(e.source.constructor, w.ServiceWorker, e = await new Promise(r => w.navigator.serviceWorker.onmessage = r);
'source should use ServiceWorker interface.'); assert_equals(e.data, 'quit');
assert_equals( }, 'postMessage from ServiceWorker to Client.');
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.');
</script> </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