Commit 39c398ad authored by Hiroki Nakagawa's avatar Hiroki Nakagawa Committed by Commit Bot

Worklet: Modernize service worker interception tests for worklets using async/await

This is just a cleanup CL, and there're no functional changes.

Bug: n/a
Change-Id: I60820045469eed4a0903b37af9e4f8927bcc884d
Reviewed-on: https://chromium-review.googlesource.com/c/1335047Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607892}
parent 0a54a94c
function openWindow(url) {
function openWindow(t, url) {
return new Promise(resolve => {
let win = window.open(url, '_blank');
add_result_callback(() => win.close());
const win = window.open(url, '_blank');
t.add_cleanup(() => win.close());
window.onmessage = e => {
assert_equals(e.data, 'LOADED');
resolve(win);
......@@ -22,31 +22,27 @@ function runServiceWorkerInterceptionTests(worklet_type) {
// [Current document] registers a service worker for Window's URL.
// --(open)--> [Window] should be controlled by the service worker.
// --(addModule)--> [Worklet] should be served by the service worker.
promise_test(t => {
promise_test(async t => {
const kWindowURL = 'resources/addmodule-window.html';
const kServiceWorkerScriptURL = 'resources/service-worker.js';
// This doesn't contain the 'resources/' prefix because this will be
// imported from a html file under resources/.
const kWorkletScriptURL = 'non-existent-worklet-script.js';
return service_worker_unregister_and_register(
t, kServiceWorkerScriptURL, kWindowURL)
.then(r => {
add_result_callback(() => r.unregister());
return wait_for_state(t, r.installing, 'activated');
})
.then(() => openWindow(kWindowURL))
.then(win => {
const registration = await service_worker_unregister_and_register(
t, kServiceWorkerScriptURL, kWindowURL);
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');
const win = await openWindow(t, kWindowURL);
assert_not_equals(win.navigator.serviceWorker.controller, null,
'The document should be controlled.');
const promise = new Promise(r => window.onmessage = r);
// The worklet script on kWorkletScriptURL doesn't exist but the
// service worker serves it, so the addModule() should succeed.
win.postMessage({ type: worklet_type,
script_url: kWorkletScriptURL }, '*');
return promise;
})
.then(msg_event => assert_equals(msg_event.data, 'RESOLVED'));
// The worklet script on kWorkletScriptURL doesn't exist but the service
// worker serves it, so the addModule() should succeed.
win.postMessage({ type: worklet_type, script_url: kWorkletScriptURL }, '*');
const msgEvent = await new Promise(resolve => window.onmessage = resolve);
assert_equals(msgEvent.data, 'RESOLVED');
}, 'addModule() on a controlled document should be intercepted by a ' +
'service worker.');
......@@ -56,35 +52,29 @@ function runServiceWorkerInterceptionTests(worklet_type) {
// [Current document] registers a service worker for Worklet's URL.
// --(open)--> [Window] should not be controlled by the service worker.
// --(addModule)--> [Worklet] should not be served by the service worker.
promise_test(t => {
promise_test(async t => {
const kWindowURL = 'resources/addmodule-window.html';
const kServiceWorkerScriptURL = 'resources/service-worker.js';
// This doesn't contain the 'resources/' prefix because this will be
// imported from a html file under resources/.
const kWorkletScriptURL = 'non-existent-worklet-script.js';
return service_worker_unregister_and_register(
t, kServiceWorkerScriptURL, 'resources/' + kWorkletScriptURL)
.then(r => {
add_result_callback(() => r.unregister());
return wait_for_state(t, r.installing, 'activated');
})
.then(() => openWindow(kWindowURL))
.then(win => {
const registration = await service_worker_unregister_and_register(
t, kServiceWorkerScriptURL, 'resources/' + kWorkletScriptURL);
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');
const win = await openWindow(t, kWindowURL);
assert_equals(win.navigator.serviceWorker.controller, null,
'The document should not be controlled.');
const promise = new Promise(r => window.onmessage = r);
// The worklet script on kWorkletScriptURL doesn't exist and the
// service worker doesn't serve it, so the addModule() should
// fail.
win.postMessage({ type: worklet_type,
script_url: kWorkletScriptURL }, '*');
return promise;
})
.then(msg_event => assert_equals(msg_event.data, 'REJECTED'));
}, 'addModule() on a non-controlled document should not be intercepted ' +
'by a service worker even if the script is under the service worker ' +
'scope.');
// The worklet script on kWorkletScriptURL doesn't exist and the service
// worker doesn't serve it, so the addModule() should fail.
win.postMessage({ type: worklet_type, script_url: kWorkletScriptURL }, '*');
const msgEvent = await new Promise(resolve => window.onmessage = resolve);
assert_equals(msgEvent.data, 'REJECTED');
}, 'addModule() on a non-controlled document should not be intercepted by ' +
'a service worker even if the script is under the service worker scope.');
// Tests that static import should be served by the owner document's service
// worker.
......@@ -93,31 +83,26 @@ function runServiceWorkerInterceptionTests(worklet_type) {
// --(open)--> [Window] should be controlled by the service worker.
// --(addModule)--> [Worklet] should be served by the service worker.
// --(static import)--> [Script] should be served by the service worker.
promise_test(t => {
promise_test(async t => {
const kWindowURL = 'resources/addmodule-window.html';
const kServiceWorkerScriptURL = 'resources/service-worker.js';
// This doesn't contain the 'resources/' prefix because this will be
// imported from a html file under resources/.
const kWorkletScriptURL = 'import-non-existent-worklet-script.js';
return service_worker_unregister_and_register(
t, kServiceWorkerScriptURL, kWindowURL)
.then(r => {
add_result_callback(() => r.unregister());
return wait_for_state(t, r.installing, 'activated');
})
.then(() => openWindow(kWindowURL))
.then(win => {
const registration = await service_worker_unregister_and_register(
t, kServiceWorkerScriptURL, kWindowURL);
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');
const win = await openWindow(t, kWindowURL);
assert_not_equals(win.navigator.serviceWorker.controller, null,
'The document should be controlled.');
const promise = new Promise(r => window.onmessage = r);
// A script statically imported by the worklet doesn't exist but
// the service worker serves it, so the addModule() should
// succeed.
win.postMessage({ type: worklet_type,
script_url: kWorkletScriptURL }, '*');
return promise;
})
.then(msg_event => assert_equals(msg_event.data, 'RESOLVED'));
// A script statically imported by the worklet doesn't exist but the service
// worker serves it, so the addModule() should succeed.
win.postMessage({ type: worklet_type, script_url: kWorkletScriptURL }, '*');
const msgEvent = await new Promise(resolve => window.onmessage = resolve);
assert_equals(msgEvent.data, 'RESOLVED');
}, 'Static import should be intercepted by a service worker.');
}
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