Bug 1290116 P7 Fixed activation races in push service worker tests. r=kitcambridge

This commit is contained in:
Ben Kelly 2016-08-17 20:03:21 -07:00
parent b555cf7d21
commit 11f187d4ff
13 changed files with 38 additions and 5 deletions

View File

@ -47,6 +47,7 @@ http://creativecommons.org/licenses/publicdomain/
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
yield waitForActive(registration);
});
var controlledFrame;

View File

@ -45,6 +45,7 @@ http://creativecommons.org/licenses/publicdomain/
var url = "error_worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
yield waitForActive(registration);
});
var controlledFrame;

View File

@ -10,6 +10,7 @@ http://creativecommons.org/licenses/publicdomain/
<head>
<title>Test for Bug 1038811</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/dom/push/test/test_utils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
@ -29,7 +30,10 @@ http://creativecommons.org/licenses/publicdomain/
function start() {
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."})
.then((swr) => registration = swr);
.then((swr) => {
registration = swr;
return waitForActive(registration);
});
}
function unregister() {

View File

@ -30,7 +30,10 @@ http://creativecommons.org/licenses/publicdomain/
function start() {
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."})
.then((swr) => registration = swr);
.then((swr) => {
registration = swr
return waitForActive(registration);
});
}
function unregister() {

View File

@ -32,7 +32,8 @@ http://creativecommons.org/licenses/publicdomain/
}
function registerServiceWorker(scope) {
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: scope});
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: scope})
.then(swr => waitForActive(swr));
}
function unregister(swr) {

View File

@ -36,6 +36,7 @@ http://creativecommons.org/licenses/publicdomain/
var url = "worker.js" + "?" + Math.random();
registration = yield navigator.serviceWorker.register(url, {scope: "."});
yield waitForActive(registration);
});
add_task(function* denySubscribe() {

View File

@ -51,6 +51,7 @@ http://creativecommons.org/licenses/publicdomain/
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
yield waitForActive(registration);
});
var controlledFrame;

View File

@ -70,6 +70,7 @@ http://creativecommons.org/licenses/publicdomain/
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
yield waitForActive(registration);
});
var controlledFrame;

View File

@ -43,7 +43,7 @@
}
function waitForActiveServiceWorker(ctx) {
return navigator.serviceWorker.ready.then(function(result) {
return waitForActive(ctx.registration).then(function(result) {
ok(ctx.registration.active, "Service Worker is active");
return ctx;
});

View File

@ -32,6 +32,7 @@ http://creativecommons.org/licenses/publicdomain/
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
yield waitForActive(registration);
});
var controlledFrame;

View File

@ -29,7 +29,8 @@ http://creativecommons.org/licenses/publicdomain/
}
function registerServiceWorker() {
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."});
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."})
.then(swr => waitForActive(swr));
}
function unregister(swr) {

View File

@ -36,6 +36,7 @@ http://creativecommons.org/licenses/publicdomain/
registration = yield navigator.serviceWorker.register(
generateURL(), {scope: "."});
yield waitForActive(registration);
});
var pushSubscription;
@ -63,6 +64,7 @@ http://creativecommons.org/licenses/publicdomain/
registration = yield navigator.serviceWorker.register(
generateURL(), {scope: "."});
yield waitForActive(registration);
var pushSubscription = yield registration.pushManager.getSubscription();
ok(!pushSubscription,
"Unregistering a service worker should drop its subscription");

View File

@ -227,3 +227,19 @@ function sendRequestToWorker(request) {
});
});
}
function waitForActive(swr) {
let sw = swr.installing || swr.waiting || swr.active;
return new Promise(resolve => {
if (sw.state === 'activated') {
resolve(swr);
return;
}
sw.addEventListener('statechange', function onStateChange(evt) {
if (sw.state === 'activated') {
sw.removeEventListener('statechange', onStateChange);
resolve(swr);
}
});
});
}