Bug 1432846 - Add test for self updating service worker. r=bkelly

This commit is contained in:
Catalin Badea 2018-02-15 14:04:55 +02:00
parent 2443202da8
commit c2e89ca6b0
3 changed files with 115 additions and 0 deletions

View File

@ -232,6 +232,7 @@ support-files =
bug1290951_worker_imported.sjs
sw_storage_not_allow.js
update_worker.sjs
self_update_worker.sjs
[test_bug1151916.html]
[test_bug1240436.html]
@ -348,3 +349,4 @@ tags = openwindow
[test_bad_script_cache.html]
[test_file_upload.html]
support-files = script_file_upload.js sw_file_upload.js server_file_upload.sjs
[test_self_update_worker.html]

View File

@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const WORKER_BODY = `
onactivate = function(event) {
let promise = clients.matchAll({includeUncontrolled: true}).then(function(clients) {
for (i = 0; i < clients.length; i++) {
clients[i].postMessage({version: version});
}
}).then(function() {
return self.registration.update();
});
event.waitUntil(promise);
};
`;
function handleRequest(request, response) {
let count = getState("count");
if (count === "") {
count = 1;
} else {
count = parseInt(count);
}
let worker = "var version = " + count + ";\n";
worker = worker + WORKER_BODY;
// This header is necessary for making this script able to be loaded.
response.setHeader("Content-Type", "application/javascript");
// If this is the first request, return the first source.
response.write(worker);
setState("count", "" + (count + 1));
}

View File

@ -0,0 +1,76 @@
<!DOCTYPE HTML>
<html>
<!--
Test that a self updating service worker can't keep running forever when the
script changes.
-->
<head>
<title>Test for Bug 1432846</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/SpawnTask.js"></script>
<script src="error_reporting_helpers.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>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1432846">Mozilla Bug 1432846</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<script src="utils.js"></script>
<script class="testbody" type="text/javascript">
add_task(function setupPrefs() {
return SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
]});
});
function activateDummyWorker() {
return navigator.serviceWorker.register("empty.js",
{ scope: "./empty?random=" + Date.now() })
.then(function(registration) {
var worker = registration.installing;
return waitForState(worker, 'activated', registration).then(function() {
ok(true, "got dummy!");
return registration.unregister();
});
});
}
add_task(async function test_update() {
navigator.serviceWorker.onmessage = function(event) {
ok (event.data.version < 3, "Service worker updated too many times." + event.data.version);
}
await SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.idle_timeout", 0],
["dom.serviceWorkers.update_delay", 30000],
["dom.serviceWorkers.idle_extended_timeout", 299999]]});
var worker;
let registration = await navigator.serviceWorker.register(
"self_update_worker.sjs",
{ scope: "./test_self_update_worker.html?random=" + Date.now()})
.then(function(registration) {
worker = registration.installing;
return waitForState(worker, 'activated', registration);
});
// We need to wait a reasonable time to give the self updating worker a chance
// to change to a newer version. Register and activate an empty worker 5 times.
for (i = 0; i < 5; i++) {
await activateDummyWorker();
}
await registration.unregister();
await SpecialPowers.popPrefEnv();
await SpecialPowers.popPrefEnv();
});
</script>
</body>
</html>