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