Commit 269bf004 authored by Phillis Tang's avatar Phillis Tang Committed by Commit Bot

web_app: fix flaky badging browsertest

Fix WebAppBadgingBrowserTest to wait for service workers to be
active before testing sending messages to sub app service worker.

Bug: 1085547
Change-Id: Ie24b192b4d2f5654f300d935e11a50637df52ea4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2213021
Commit-Queue: Phillis Tang <phillis@chromium.org>
Reviewed-by: default avatarChase Phillips <cmp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#771706}
parent 4f5895dd
......@@ -14,6 +14,24 @@
const crossSiteUrl = new URLSearchParams(location.search).get('url');
crossFrame.src = crossSiteUrl;
// Copy of //content/test/data/service_worker/create_service_worker.html.
// Simulates navigator.serviceWorker.ready, which can't be used since this
// document may not be in-scope.
async function whenReady(registration) {
if (registration.active)
return;
// If there's no .active, .waiting will activate before .installing.
const nextActiveWorker = registration.waiting || registration.installing;
return new Promise((resolve, reject) => {
nextActiveWorker.addEventListener('statechange', event => {
if (nextActiveWorker.state == 'activated')
resolve();
if (nextActiveWorker.state == 'redundant')
reject('worker became redundant');
});
});
}
// BrowserTests should use EvalJs() to call registerServiceWorker().
// EvalJs() provides this function's return value, which enables the
// BrowserTest to verify successful service worker registration.
......@@ -22,7 +40,13 @@
// "registerServiceWorker(script, scope);"));
async function registerServiceWorker(script, scope) {
try {
await navigator.serviceWorker.register(script, { scope });
const init = scope ? {scope} : {};
const registration =
await navigator.serviceWorker.register(script, init);
// wait for serviceworker to be active, otherwise
// navigator.serviceWorker.getRegistration might get serviceworker of
// parent scope
await whenReady(registration);
return 'OK';
} catch (error) {
return `EXCEPTION: ${error}`;
......
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