Bug 1251238 - Part 8: Add a test to make sure that timestamps track time as expected and modify the test since we notify the chrome listener when updating. r=bkelly
authorTom Tung <shes050117@gmail.com>
Thu, 30 Mar 2017 17:25:03 +0800
changeset 353096 d485179f4a271d35981b4ea7b4c92cd8f99d2972
parent 353095 8f3b554401e8b030221981cbb218c085c34601da
child 353097 b43d7c9d5a5ac683657c329a1e98c84c32ff9d65
push id31656
push userihsiao@mozilla.com
push dateFri, 14 Apr 2017 09:10:41 +0000
treeherdermozilla-central@cda24082bff8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1251238
milestone55.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 1251238 - Part 8: Add a test to make sure that timestamps track time as expected and modify the test since we notify the chrome listener when updating. r=bkelly
dom/workers/test/serviceworkers/chrome.ini
dom/workers/test/serviceworkers/test_devtools_track_serviceworker_time.html
dom/workers/test/serviceworkers/test_serviceworkerregistrationinfo.xul
--- a/dom/workers/test/serviceworkers/chrome.ini
+++ b/dom/workers/test/serviceworkers/chrome.ini
@@ -8,12 +8,13 @@ support-files =
   serviceworker.html
   serviceworkerinfo_iframe.html
   serviceworkermanager_iframe.html
   serviceworkerregistrationinfo_iframe.html
   worker.js
   worker2.js
 
 [test_devtools_serviceworker_interception.html]
+[test_devtools_track_serviceworker_time.html]
 [test_privateBrowsing.html]
 [test_serviceworkerinfo.xul]
 [test_serviceworkermanager.xul]
 [test_serviceworkerregistrationinfo.xul]
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/test_devtools_track_serviceworker_time.html
@@ -0,0 +1,218 @@
+<html>
+<head>
+  <title>Bug 1251238 - track service worker install time</title>
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
+</head>
+<iframe id="iframe"></iframe>
+<body>
+
+<script type="text/javascript">
+
+const State = {
+  BYTECHECK: -1,
+  INSTALLING: 0,
+  INSTALLED: 1,
+  ACTIVATING: 2,
+  ACTIVATED: 3,
+  REDUNDANT: 4
+};
+
+let { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+let swm = Cc["@mozilla.org/serviceworkers/manager;1"].
+          getService(Ci.nsIServiceWorkerManager);
+
+let EXAMPLE_URL = "https://example.com/chrome/dom/workers/test/serviceworkers/";
+
+let swrlistener = null;
+let registrationInfo = null;
+
+// Use it to keep the sw after unregistration.
+let astrayServiceWorkerInfo = null;
+
+let expectedResults = [
+  {
+    // Speacial state for verifying update since we will do the byte-check
+    // first.
+    state: State.BYTECHECK, installedTimeRecorded: false,
+    activatedTimeRecorded: false, redundantTimeRecorded: false
+  },
+  {
+    state: State.INSTALLING, installedTimeRecorded: false,
+    activatedTimeRecorded: false, redundantTimeRecorded: false
+  },
+  {
+    state: State.INSTALLED, installedTimeRecorded: true,
+    activatedTimeRecorded: false, redundantTimeRecorded: false
+  },
+  {
+    state: State.ACTIVATING, installedTimeRecorded: true,
+    activatedTimeRecorded: false, redundantTimeRecorded: false
+  },
+  {
+    state: State.ACTIVATED, installedTimeRecorded: true,
+    activatedTimeRecorded: true, redundantTimeRecorded: false
+  },
+  {
+    state: State.REDUNDANT, installedTimeRecorded: true,
+    activatedTimeRecorded: true, redundantTimeRecorded: true
+  }
+];
+
+function waitForRegister(aScope, aCallback) {
+  return new Promise(function (aResolve) {
+    let listener = {
+      onRegister: function (aRegistration) {
+        if (aRegistration.scope !== aScope) {
+          return;
+        }
+        swm.removeListener(listener);
+        registrationInfo = aRegistration;
+        aResolve();
+      }
+    };
+    swm.addListener(listener);
+  });
+}
+
+function waitForUnregister(aScope) {
+  return new Promise(function (aResolve) {
+    let listener = {
+      onUnregister: function (aRegistration) {
+        if (aRegistration.scope !== aScope) {
+          return;
+        }
+        swm.removeListener(listener);
+        aResolve();
+      }
+    };
+    swm.addListener(listener);
+  });
+}
+
+function register() {
+  info("Register a ServiceWorker in the iframe");
+
+  let iframe = document.querySelector("iframe");
+  iframe.src = EXAMPLE_URL + "serviceworkerinfo_iframe.html";
+
+  let promise = new Promise(function(aResolve) {
+    iframe.onload = aResolve;
+  });
+
+  return promise.then(function() {
+    iframe.contentWindow.postMessage("register", "*");
+    return waitForRegister(EXAMPLE_URL);
+  })
+}
+
+function verifyServiceWorkTime(aSWRInfo) {
+  let expectedResult = expectedResults.shift();
+  ok(!!expectedResult, "We should be able to get test from expectedResults");
+
+  info("Check the ServiceWorker time in its state is " + expectedResult.state);
+
+  // Get serviceWorkerInfo from swrInfo or get the astray one which we hold.
+  let swInfo = aSWRInfo.installingWorker ||
+               aSWRInfo.waitingWorker ||
+               aSWRInfo.activeWorker ||
+               astrayServiceWorkerInfo;
+
+  ok(!!aSWRInfo.lastUpdateTime,
+     "We should do the byte-check and update the update timeStamp");
+
+  if (!swInfo) {
+    is(expectedResult.state, State.BYTECHECK,
+       "We shouldn't get sw when we are notified for frist time updating");
+    return;
+  }
+
+  ok(!!swInfo);
+
+  is(expectedResult.state, swInfo.state,
+     "The service worker's state should be " + swInfo.state + ", but got " +
+     expectedResult.state);
+
+  is(expectedResult.installedTimeRecorded, !!swInfo.installedTime,
+     "InstalledTime should be recorded when their state is greater than " +
+     "INSTALLING");
+
+  is(expectedResult.activatedTimeRecorded, !!swInfo.activatedTime,
+     "ActivatedTime should be recorded when their state is greater than " +
+     "ACTIVATING");
+
+  is(expectedResult.redundantTimeRecorded, !!swInfo.redundantTime,
+     "RedundantTime should be recorded when their state is REDUNDANT");
+
+  // We need to hold sw to avoid losing it since we'll unregister the swr later.
+  if (expectedResult.state === State.ACTIVATED) {
+    astrayServiceWorkerInfo = aSWRInfo.activeWorker;
+  }
+}
+
+function testServiceWorkerInfo() {
+  info("Listen onChange event and verify service worker's information");
+
+  let promise_resolve;
+  let promise = new Promise(aResolve => promise_resolve = aResolve);
+
+  let counter = 0;
+  swrlistener = {
+    onChange: () => {
+      verifyServiceWorkTime(registrationInfo)
+      counter++;
+
+      // Resolve the promise after sw is activated.
+      if (counter >= State.ACTIVATED) {
+        promise_resolve();
+      }
+    }
+  };
+
+  registrationInfo.addListener(swrlistener);
+
+  return promise;
+}
+
+function unregister() {
+  info("Unregister the ServiceWorker");
+
+  let iframe = document.querySelector("iframe");
+  iframe.contentWindow.postMessage("unregister", "*");
+  return waitForUnregister(EXAMPLE_URL);
+}
+
+function cleanAll() {
+  return new Promise((aResolve, aReject) => {
+    is(expectedResults.length, 0, "All the tests should be tested");
+
+    registrationInfo.removeListener(swrlistener);
+
+    swm = null;
+    swrlistener = null;
+    registrationInfo = null;
+    astrayServiceWorkerInfo = null;
+    aResolve();
+  })
+}
+
+function runTest() {
+  return Promise.resolve()
+    .then(register)
+    .then(testServiceWorkerInfo)
+    .then(unregister)
+    .catch(aError => ok(false, "Some test failed with error " + aError))
+    .then(cleanAll)
+    .then(SimpleTest.finish);
+}
+
+SimpleTest.waitForExplicitFinish();
+SpecialPowers.pushPrefEnv({"set": [
+  ["dom.serviceWorkers.exemptFromPerDomainMax", true],
+  ["dom.serviceWorkers.enabled", true],
+  ["dom.serviceWorkers.testing.enabled", true]
+]}, runTest);
+
+</script>
+</body>
+</html>
--- a/dom/workers/test/serviceworkers/test_serviceworkerregistrationinfo.xul
+++ b/dom/workers/test/serviceworkers/test_serviceworkerregistrationinfo.xul
@@ -41,26 +41,72 @@
                "listeners when its state changes.");
           promise = waitForRegister(EXAMPLE_URL, function (registration) {
             is(registration.scriptSpec, "");
             ok(registration.installingWorker === null);
             ok(registration.waitingWorker === null);
             ok(registration.activeWorker === null);
 
             return waitForServiceWorkerRegistrationChange(registration, function  () {
-              is(registration.scriptSpec, EXAMPLE_URL + "worker.js");
-              ok(registration.installingWorker !== null);
-              is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker.js");
+              // Got change event for updating (byte-check)
+              ok(registration.installingWorker === null);
               ok(registration.waitingWorker === null);
               ok(registration.activeWorker === null);
 
+              return waitForServiceWorkerRegistrationChange(registration, function  () {
+                is(registration.scriptSpec, EXAMPLE_URL + "worker.js");
+                ok(registration.installingWorker !== null);
+                is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker.js");
+                ok(registration.waitingWorker === null);
+                ok(registration.activeWorker === null);
+
+                return waitForServiceWorkerRegistrationChange(registration, function () {
+                  ok(registration.installingWorker === null);
+                  ok(registration.waitingWorker !== null);
+                  ok(registration.activeWorker === null);
+
+                  return waitForServiceWorkerRegistrationChange(registration, function () {
+                    // Activating
+                    ok(registration.installingWorker === null);
+                    ok(registration.waitingWorker === null);
+                    ok(registration.activeWorker !== null);
+
+                    return waitForServiceWorkerRegistrationChange(registration, function () {
+                      // Activated
+                      ok(registration.installingWorker === null);
+                      ok(registration.waitingWorker === null);
+                      ok(registration.activeWorker !== null);
+
+                      return registration;
+                    });
+                  });
+                });
+              });
+            });
+          });
+          iframe.contentWindow.postMessage("register", "*");
+          let registration = yield promise;
+
+          promise = waitForServiceWorkerRegistrationChange(registration, function () {
+            // Got change event for updating (byte-check)
+            ok(registration.installingWorker === null);
+            ok(registration.waitingWorker === null);
+            ok(registration.activeWorker !== null);
+
+            return waitForServiceWorkerRegistrationChange(registration, function () {
+              is(registration.scriptSpec, EXAMPLE_URL + "worker2.js");
+              ok(registration.installingWorker !== null);
+              is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker2.js");
+              ok(registration.waitingWorker === null);
+              ok(registration.activeWorker !== null);
+
               return waitForServiceWorkerRegistrationChange(registration, function () {
                 ok(registration.installingWorker === null);
                 ok(registration.waitingWorker !== null);
-                ok(registration.activeWorker === null);
+                ok(registration.activeWorker !== null);
 
                 return waitForServiceWorkerRegistrationChange(registration, function () {
                   // Activating
                   ok(registration.installingWorker === null);
                   ok(registration.waitingWorker === null);
                   ok(registration.activeWorker !== null);
 
                   return waitForServiceWorkerRegistrationChange(registration, function () {
@@ -71,48 +117,16 @@
 
                     return registration;
                   });
                 });
               });
             });
           });
           iframe.contentWindow.postMessage("register", "*");
-          let registration = yield promise;
-
-          promise = waitForServiceWorkerRegistrationChange(registration, function () {
-            is(registration.scriptSpec, EXAMPLE_URL + "worker2.js");
-            ok(registration.installingWorker !== null);
-            is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker2.js");
-            ok(registration.waitingWorker === null);
-            ok(registration.activeWorker !== null);
-
-            return waitForServiceWorkerRegistrationChange(registration, function () {
-              ok(registration.installingWorker === null);
-              ok(registration.waitingWorker !== null);
-              ok(registration.activeWorker !== null);
-
-              return waitForServiceWorkerRegistrationChange(registration, function () {
-                // Activating
-                ok(registration.installingWorker === null);
-                ok(registration.waitingWorker === null);
-                ok(registration.activeWorker !== null);
-
-                return waitForServiceWorkerRegistrationChange(registration, function () {
-                  // Activated
-                  ok(registration.installingWorker === null);
-                  ok(registration.waitingWorker === null);
-                  ok(registration.activeWorker !== null);
-
-                  return registration;
-                });
-              });
-            });
-          });
-          iframe.contentWindow.postMessage("register", "*");
           yield promise;
 
           iframe.contentWindow.postMessage("unregister", "*");
           yield waitForUnregister(EXAMPLE_URL);
 
           SimpleTest.finish();
         });
       });