Bug 1557244 - Remove `waitForControlled(win)` testing utility r=asuth

`postMessage('claim')` followed by `waitForControlled(win)` was used instead of
calling `clients.claim()` in a Service Worker's `activate` event handler; this
invocation of the event handler was not reliable due to resurrection
(resurrected active workers won't receive an `activate` event). This is no
longer necessary with resurrection removed.

Differential Revision: https://phabricator.services.mozilla.com/D38687

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Perry Jiang 2019-07-26 18:40:12 +00:00
Родитель bdba1dc9d2
Коммит df5283bd56
11 изменённых файлов: 28 добавлений и 72 удалений

Просмотреть файл

@ -33,3 +33,7 @@ addEventListener("message", function(event) {
event.waitUntil(clients.claim());
}
});
addEventListener("activate", function(event) {
event.waitUntil(clients.claim());
});

Просмотреть файл

@ -4,8 +4,6 @@ addEventListener("fetch", event => {
event.respondWith(fetch(event.request));
});
addEventListener("message", function(event) {
if (event.data === "claim") {
event.waitUntil(clients.claim());
}
addEventListener("activate", function(event) {
event.waitUntil(clients.claim());
});

Просмотреть файл

@ -28,8 +28,6 @@ addEventListener("fetch", function(event) {
);
});
addEventListener("message", function(event) {
if (event.data === "claim") {
event.waitUntil(clients.claim());
}
addEventListener("activate", function(event) {
event.waitUntil(clients.claim());
});

Просмотреть файл

@ -28,15 +28,10 @@ add_task(() => {
// test for bug 1408734
add_task(async () => {
// register a service worker
let registration = await navigator.serviceWorker.register("fetch.js", {scope: "./"});
let sw = registration.installing || registration.active;
let registration = await navigator.serviceWorker.register("fetch.js",
{scope: "./"});
// wait for service worker be activated
await waitForState(sw, 'activated');
// wait for control changed
sw.postMessage('claim');
await waitForControlled(window);
await waitForState(registration.installing, "activated");
// get the ServiceWorkerRegistration we just register through GetRegistration
registration = await navigator.serviceWorker.getRegistration("./");

Просмотреть файл

@ -91,13 +91,7 @@ add_task(function setupPrefs() {
add_task(async function test_bypassServiceWorker() {
const swURL = "fetch.js";
let registration = await navigator.serviceWorker.register(swURL);
let sw =
registration.active || registration.waiting || registration.installing;
await waitForState(sw, 'activated');
sw.postMessage("claim");
await waitForControlled(window);
await waitForState(registration.installing, 'activated');
try {
await testBypassSW();

Просмотреть файл

@ -196,18 +196,14 @@ add_task(async function get_client_storage_error() {
["dom.securecontext.whitelist", "mochi.test"]
]});
let registration, sw;
let registration;
// consume the expected rejection so it doesn't get thrown at us.
await navigator.serviceWorker.register("sw_storage_not_allow.js",
{ scope: "test_error_reporting.html" })
.then(reg => { registration = reg; })
.then(() => registration.installing ||
registration.waiting ||
registration.active)
.then(worker => { sw = worker; })
.then(() => waitForState(sw, 'activated'))
.then(() => sw.postMessage('claim'))
.then(() => waitForControlled(window))
.then(reg => {
registration = reg;
return waitForState(registration.installing, "activated");
})
// Get the client's ID in the stage 1
.then(() => fetch("getClient-stage1"))
.then(() => notAllowStorageAccess())
@ -229,18 +225,14 @@ add_task(async function get_clients_storage_error() {
let expectedMessage =
expect_console_message("ServiceWorkerGetClientStorageError", []);
let registration, sw;
let registration;
// consume the expected rejection so it doesn't get thrown at us.
await navigator.serviceWorker.register("sw_storage_not_allow.js",
{ scope: "test_error_reporting.html" })
.then(reg => { registration = reg; })
.then(() => registration.installing ||
registration.waiting ||
registration.active)
.then(worker => { sw = worker; })
.then(() => waitForState(sw, 'activated'))
.then(() => sw.postMessage('claim'))
.then(() => waitForControlled(window))
.then(reg => {
registration = reg;
return waitForState(registration.installing, "activated");
})
.then(() => notAllowStorageAccess())
.then(() => fetch("getClients"))
.catch(e => ok(false, "fail due to:" + e));

Просмотреть файл

@ -114,10 +114,7 @@ add_task(async function test_integrity_serviceWorker() {
let registration = await navigator.serviceWorker.register("fetch.js",
{ scope: "./" });
let worker = registration.installing || registration.active;
await waitForState(worker, 'activated');
worker.postMessage('claim');
await waitForControlled(window);
await waitForState(registration.installing, "activated");
info("Test for mNavigationInterceptions.")
// The client_win will reload to another URL after opening filename2.

Просмотреть файл

@ -21,11 +21,7 @@ async function onOpened(message) {
let registration = await navigator.serviceWorker.register('sw_file_upload.js',
{scope: "." });
let worker = registration.installing || registration.active;
await waitForState(worker, 'activated');
worker.postMessage('claim');
await waitForControlled(window);
await waitForState(registration.installing, 'activated');
let res = await fetch('server_file_upload.sjs', {
method: 'POST',

Просмотреть файл

@ -41,11 +41,7 @@ add_task(async function grace_timeout_termination_with_interrupted_intercept() {
let registration = await navigator.serviceWorker.register(
"unresolved_fetch_worker.js", { scope: "./"} );
let worker = registration.installing || registration.active;
await waitForState(worker, 'activated');
worker.postMessage('claim');
await waitForControlled(window);
await waitForState(registration.installing, "activated");
ok(navigator.serviceWorker.controller, "Controlled"); // double check!
// We want to make sure the SW is active and processing the fetch before we

Просмотреть файл

@ -13,8 +13,6 @@ onfetch = function(event) {
event.respondWith((keepPromiseAlive = new Promise(function(res, rej) {})));
};
onmessage = function(event) {
if (event.data === "claim") {
event.waitUntil(clients.claim());
}
};
addEventListener("activate", function(event) {
event.waitUntil(clients.claim());
});

Просмотреть файл

@ -13,18 +13,6 @@ function waitForState(worker, state, context) {
});
}
function waitForControlled(win) {
return new Promise(resolve => {
if (win.navigator.serviceWorker.controller) {
return resolve();
}
win.navigator.serviceWorker.addEventListener("controllerchange", resolve, {
once: true,
});
});
}
/**
* Helper for browser tests to issue register calls from the content global and
* wait for the SW to progress to the active state, as most tests desire.