Bug 1432846 - Add test for self updating service worker. r=bkelly
authorCatalin Badea <catalin.badea392@gmail.com>
Thu, 15 Feb 2018 14:04:55 +0200
changeset 461318 8b2b977347eb5e1579759925af366ed8bb337a04
parent 461317 d994e314879426eaae8871e949d656241a8871f1
child 461319 9e64627b95da8580c01d9951eb4237e0a6122ee6
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1432846
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1432846 - Add test for self updating service worker. r=bkelly
dom/serviceworkers/test/mochitest.ini
dom/serviceworkers/test/self_update_worker.sjs
dom/serviceworkers/test/test_self_update_worker.html
--- a/dom/serviceworkers/test/mochitest.ini
+++ b/dom/serviceworkers/test/mochitest.ini
@@ -227,16 +227,17 @@ support-files =
   nofetch_handler_worker.js
   service_worker.js
   service_worker_client.html
   utils.js
   bug1290951_worker_main.sjs
   bug1290951_worker_imported.sjs
   sw_storage_not_allow.js
   update_worker.sjs
+  self_update_worker.sjs
 
 [test_bug1151916.html]
 [test_bug1240436.html]
 [test_bug1408734.html]
 [test_claim.html]
 [test_claim_oninstall.html]
 [test_controller.html]
 [test_cookie_fetch.html]
@@ -343,8 +344,9 @@ tags = openwindow
 [test_workerupdatefoundevent.html]
 [test_xslt.html]
 [test_async_waituntil.html]
 [test_worker_reference_gc_timeout.html]
 [test_nofetch_handler.html]
 [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]
new file mode 100644
--- /dev/null
+++ b/dom/serviceworkers/test/self_update_worker.sjs
@@ -0,0 +1,43 @@
+/* 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) {
+  if (request.queryString == 'clearcounter') {
+    setState('count', "1");
+    response.write("ok");
+    return;
+  }
+
+  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));
+}
+
new file mode 100644
--- /dev/null
+++ b/dom/serviceworkers/test/test_self_update_worker.html
@@ -0,0 +1,79 @@
+<!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]]});
+
+  // clear version counter
+  await fetch("self_update_worker.sjs?clearcounter");
+
+  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>