Bug 1153292 - part6: add tests for pending state service workers and fix existing tests;r=janx draft
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 12 Aug 2016 16:20:19 +0200
changeset 400183 18cfb827448ad27d79cd646eb7ec98aea4430ba1
parent 400182 02fab3ec1601229cf77800b3e7e21f76899cf1d2
child 528147 b83d6a0fc606cc2dfd5590ea1595261e3f264188
push id26083
push userjdescottes@mozilla.com
push dateFri, 12 Aug 2016 17:20:18 +0000
reviewersjanx
bugs1153292
milestone51.0a1
Bug 1153292 - part6: add tests for pending state service workers and fix existing tests;r=janx MozReview-Commit-ID: CKJomtoLMg1
devtools/client/aboutdebugging/components/workers/panel.js
devtools/client/aboutdebugging/test/browser.ini
devtools/client/aboutdebugging/test/browser_service_workers_push.js
devtools/client/aboutdebugging/test/browser_service_workers_push_service.js
devtools/client/aboutdebugging/test/browser_service_workers_start.js
devtools/client/aboutdebugging/test/browser_service_workers_status.js
devtools/client/aboutdebugging/test/browser_service_workers_unregister.js
devtools/client/aboutdebugging/test/service-workers/delay-sw.html
devtools/client/aboutdebugging/test/service-workers/delay-sw.js
--- a/devtools/client/aboutdebugging/components/workers/panel.js
+++ b/devtools/client/aboutdebugging/components/workers/panel.js
@@ -113,44 +113,16 @@ module.exports = createClass({
       // XXX: Filter out the service worker registrations for which we couldn't
       // find the scriptSpec.
       workers.service = workers.service.filter(reg => !!reg.url);
 
       this.setState({ workers });
     });
   },
 
-  _updateServiceWorker(form) {
-    let workers = this.getInitialState().workers;
-    let registrationFound = false;
-    for (let registration of workers.service) {
-      if (registration.scope === form.scope) {
-        // XXX: Race, sometimes a ServiceWorkerRegistrationInfo doesn't
-        // have a scriptSpec, but its associated WorkerDebugger does.
-        if (!registration.url) {
-          registration.name = registration.url = form.url;
-        }
-        registration.workerActor = form.actor;
-        registrationFound = true;
-        break;
-      }
-    }
-    if (!registrationFound) {
-      let serviceWorker = {
-        icon: WorkerIcon,
-        name: form.url,
-        url: form.url,
-        scope: form.scope,
-        registrationActor: null,
-        workerActor: form.actor
-      };
-      workers.service.push(serviceWorker);
-    }
-  },
-
   render() {
     let { client, id } = this.props;
     let { workers } = this.state;
 
     let isWindowPrivate = PrivateBrowsingUtils.isContentWindowPrivate(window);
     let isPrivateBrowsingMode = PrivateBrowsingUtils.permanentPrivateBrowsing;
     let isServiceWorkerDisabled = !Services.prefs
                                     .getBoolPref("dom.serviceWorkers.enabled");
--- a/devtools/client/aboutdebugging/test/browser.ini
+++ b/devtools/client/aboutdebugging/test/browser.ini
@@ -4,16 +4,18 @@ subsuite = devtools
 support-files =
   head.js
   addons/unpacked/bootstrap.js
   addons/unpacked/install.rdf
   addons/bad/manifest.json
   addons/bug1273184.xpi
   addons/test-devtools-webextension/*
   addons/test-devtools-webextension-nobg/*
+  service-workers/delay-sw.html
+  service-workers/delay-sw.js
   service-workers/empty-sw.html
   service-workers/empty-sw.js
   service-workers/push-sw.html
   service-workers/push-sw.js
   !/devtools/client/framework/test/shared-head.js
 
 [browser_addons_debug_bootstrapped.js]
 [browser_addons_debug_webextension.js]
@@ -25,12 +27,13 @@ support-files =
 [browser_addons_reload.js]
 [browser_addons_toggle_debug.js]
 [browser_page_not_found.js]
 [browser_service_workers.js]
 [browser_service_workers_not_compatible.js]
 [browser_service_workers_push.js]
 [browser_service_workers_push_service.js]
 [browser_service_workers_start.js]
+[browser_service_workers_status.js]
 [browser_service_workers_timeout.js]
 skip-if = true # Bug 1232931
 [browser_service_workers_unregister.js]
 [browser_tabs.js]
--- a/devtools/client/aboutdebugging/test/browser_service_workers_push.js
+++ b/devtools/client/aboutdebugging/test/browser_service_workers_push.js
@@ -61,17 +61,26 @@ add_task(function* () {
     "the service worker.");
   yield waitForServiceWorkerRegistered(swTab);
   ok(true, "Service worker registration resolved");
 
   // Retrieve the Push button for the worker.
   let names = [...document.querySelectorAll("#service-workers .target-name")];
   let name = names.filter(element => element.textContent === SERVICE_WORKER)[0];
   ok(name, "Found the service worker in the list");
+
   let targetElement = name.parentNode.parentNode;
+
+  let state = targetElement.querySelector(".target-status");
+  if (state.textContent !== "Running") {
+    // Wait for the PUSH button to be available, when the service worker reaches the
+    // ACTIVATED state.
+    yield waitForMutation(serviceWorkersElement, { childList: true, subtree: true });
+  }
+
   let pushBtn = targetElement.querySelector(".push-button");
   ok(pushBtn, "Found its push button");
 
   info("Wait for the service worker to claim the test window before " +
     "proceeding.");
   yield onClaimed;
 
   info("Click on the Push button and wait for the service worker to receive " +
--- a/devtools/client/aboutdebugging/test/browser_service_workers_push_service.js
+++ b/devtools/client/aboutdebugging/test/browser_service_workers_push_service.js
@@ -71,17 +71,26 @@ add_task(function* () {
 
   // Check that the service worker appears in the UI.
   assertHasTarget(true, document, "service-workers", SERVICE_WORKER);
 
   // Wait for the service worker details to update.
   let names = [...document.querySelectorAll("#service-workers .target-name")];
   let name = names.filter(element => element.textContent === SERVICE_WORKER)[0];
   ok(name, "Found the service worker in the list");
+
   let targetContainer = name.parentNode.parentNode;
+
+  let state = targetContainer.querySelector(".target-status");
+  if (state.textContent === "Inactive") {
+    // Wait for the PUSH button to be available, when the service worker reaches the
+    // ACTIVATED state.
+    yield waitForMutation(serviceWorkersElement, { childList: true, subtree: true });
+  }
+
   let targetDetailsElement = targetContainer.querySelector(".target-details");
   yield waitForMutation(targetDetailsElement, { childList: true });
 
   // Retrieve the push subscription endpoint URL, and verify it looks good.
   let pushURL = targetContainer.querySelector(".service-worker-push-url");
   ok(pushURL, "Found the push service URL in the service worker details");
   is(pushURL.textContent, FAKE_ENDPOINT, "The push service URL looks correct");
 
--- a/devtools/client/aboutdebugging/test/browser_service_workers_start.js
+++ b/devtools/client/aboutdebugging/test/browser_service_workers_start.js
@@ -48,16 +48,23 @@ add_task(function* () {
   ok(true, "Service worker registration resolved");
 
   // Retrieve the Target element corresponding to the service worker.
   let names = [...document.querySelectorAll("#service-workers .target-name")];
   let name = names.filter(element => element.textContent === SERVICE_WORKER)[0];
   ok(name, "Found the service worker in the list");
   let targetElement = name.parentNode.parentNode;
 
+  let state = targetElement.querySelector(".target-status");
+  if (state.textContent !== "Running") {
+    // Wait for the PUSH button to be available, when the service worker reaches the
+    // ACTIVATED state.
+    yield waitForMutation(serviceWorkersElement, { childList: true, subtree: true });
+  }
+
   // The service worker may already be killed with the low 1s timeout
   if (!targetElement.querySelector(".start-button")) {
     // Check that there is a Debug button but not a Start button.
     ok(targetElement.querySelector(".debug-button"), "Found its debug button");
 
     // Wait for the service worker to be killed due to inactivity.
     yield waitForMutation(targetElement, { childList: true });
   } else {
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging/test/browser_service_workers_status.js
@@ -0,0 +1,72 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+// Service workers can't be loaded from chrome://,
+// but http:// is ok with dom.serviceWorkers.testing.enabled turned on.
+const SERVICE_WORKER = URL_ROOT + "service-workers/delay-sw.js";
+const TAB_URL = URL_ROOT + "service-workers/delay-sw.html";
+const SW_TIMEOUT = 2000;
+
+add_task(function* () {
+  yield new Promise(done => {
+    let options = {"set": [
+      ["dom.serviceWorkers.enabled", true],
+      ["dom.serviceWorkers.testing.enabled", true],
+      ["dom.serviceWorkers.idle_timeout", SW_TIMEOUT],
+      ["dom.serviceWorkers.idle_extended_timeout", SW_TIMEOUT],
+    ]};
+    SpecialPowers.pushPrefEnv(options, done);
+  });
+
+  let { tab, document } = yield openAboutDebugging("workers");
+
+  // Listen for mutations in the service-workers list.
+  let serviceWorkersElement = getServiceWorkerList(document);
+  let onMutation = waitForMutation(serviceWorkersElement, { childList: true });
+
+  let swTab = yield addTab(TAB_URL);
+
+  info("Make the test page notify us when the service worker sends a message.");
+
+  // Wait for the service-workers list to update.
+  yield onMutation;
+
+  // Check that the service worker appears in the UI
+  let names = [...document.querySelectorAll("#service-workers .target-name")];
+  let name = names.filter(element => element.textContent === SERVICE_WORKER)[0];
+  ok(name, "Found the service worker in the list");
+
+  let targetElement = name.parentNode.parentNode;
+  let status = targetElement.querySelector(".target-status");
+  is(status.textContent, "Inactive", "Service worker is currently inactive");
+
+  yield waitForMutation(serviceWorkersElement, { childList: true, subtree: true });
+  is(status.textContent, "Running", "Service worker is currently running");
+
+  yield waitForMutation(serviceWorkersElement, { attributes: true, subtree: true });
+  is(status.textContent, "Stopped", "Service worker is currently stopped");
+
+  // Finally, unregister the service worker itself
+  let aboutDebuggingUpdate = waitForMutation(serviceWorkersElement,
+    { childList: true });
+
+  try {
+    yield unregisterServiceWorker(swTab);
+    ok(true, "Service worker registration unregistered");
+  } catch (e) {
+    ok(false, "SW not unregistered; " + e);
+  }
+
+  yield aboutDebuggingUpdate;
+
+  // Check that the service worker disappeared from the UI
+  names = [...document.querySelectorAll("#service-workers .target-name")];
+  names = names.map(element => element.textContent);
+  ok(!names.includes(SERVICE_WORKER),
+    "The service worker url is no longer in the list: " + names);
+
+  yield removeTab(swTab);
+  yield closeAboutDebugging(tab);
+});
--- a/devtools/client/aboutdebugging/test/browser_service_workers_unregister.js
+++ b/devtools/client/aboutdebugging/test/browser_service_workers_unregister.js
@@ -31,16 +31,20 @@ add_task(function* () {
   let onMutation = waitForMutation(serviceWorkersElement, { childList: true });
 
   // Open a tab that registers an empty service worker.
   let swTab = yield addTab(TAB_URL);
 
   // Wait for the service workers-list to update.
   yield onMutation;
 
+  // Wait for the unregister link to be available, when the service worker reaches the
+  // ACTIVATED state.
+  yield waitForMutation(serviceWorkersElement, { childList: true, subtree: true });
+
   // Check that the service worker appears in the UI.
   assertHasTarget(true, document, "service-workers", SERVICE_WORKER);
 
   info("Ensure that the registration resolved before trying to interact with " +
     "the service worker.");
   yield waitForServiceWorkerRegistered(swTab);
   ok(true, "Service worker registration resolved");
 
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging/test/service-workers/delay-sw.html
@@ -0,0 +1,22 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="UTF-8">
+  <title>Service worker test</title>
+</head>
+<body>
+<script type="text/javascript">
+"use strict";
+
+var sw = navigator.serviceWorker.register("delay-sw.js");
+sw.then(
+  function () {
+    dump("SW registered\n");
+  },
+  function (e) {
+    dump("SW not registered: " + e + "\n");
+  }
+);
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging/test/service-workers/delay-sw.js
@@ -0,0 +1,17 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/* eslint-env worker */
+
+"use strict";
+
+function wait(ms) {
+  return new Promise(resolve => {
+    setTimeout(resolve, ms);
+  });
+}
+
+// Don't wait for the next page load to become the active service worker.
+self.addEventListener("install", function (event) {
+  event.waitUntil(wait(1000));
+});