Bug 1159378 - Part 3: Refactor the logic of test_periodic_update.html into a helper script; r=nsm
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 04 May 2015 10:18:12 -0400
changeset 274266 482c23ebcf041df32b15c8e04e7d71b8b9377d9f
parent 274265 c532540057ddc282cb886502df2f31940acfc857
child 274267 ca507947df16e61c1afe2dad7f4451d0084d958b
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnsm
bugs1159378
milestone40.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 1159378 - Part 3: Refactor the logic of test_periodic_update.html into a helper script; r=nsm This helper script will allow us to run the same test across both HTTP and HTTPS origins. This patch also uses postMessage instead of a direct function call on the parent window, because in the HTTPS case, that window will be cross-origin. It also moves the code to trigger the update and get the corresponding updatefound event to wait_for_update.html, since getting the service worker registration cross-origin is not possible.
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/periodic.sjs
dom/workers/test/serviceworkers/periodic/frame.html
dom/workers/test/serviceworkers/periodic/register.html
dom/workers/test/serviceworkers/periodic/unregister.html
dom/workers/test/serviceworkers/periodic/wait_for_update.html
dom/workers/test/serviceworkers/periodic_update_test.js
dom/workers/test/serviceworkers/test_periodic_update.html
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -67,19 +67,21 @@ support-files =
   importscript_worker.js
   client_focus_worker.js
   bug1151916_worker.js
   bug1151916_driver.html
   worker_updatefoundevent.js
   worker_updatefoundevent2.js
   updatefoundevent.html
   empty.js
+  periodic_update_test.js
   periodic.sjs
   periodic/frame.html
   periodic/register.html
+  periodic/wait_for_update.html
   periodic/unregister.html
 
 [test_unregister.html]
 [test_installation_simple.html]
 [test_fetch_event.html]
 [test_https_fetch.html]
 [test_https_fetch_cloned_response.html]
 [test_https_synth_fetch_from_cached_sw.html]
--- a/dom/workers/test/serviceworkers/periodic.sjs
+++ b/dom/workers/test/serviceworkers/periodic.sjs
@@ -1,22 +1,24 @@
 function handleRequest(request, response) {
+  var stateName = request.scheme + 'periodiccounter';
   if (request.queryString == 'clearcounter') {
-    setState('periodiccounter', '');
+    setState(stateName, '');
     return;
   }
-  if (!getState('periodiccounter')) {
-    setState('periodiccounter', '1');
+  
+  if (!getState(stateName)) {
+    setState(stateName, '1');
   } else {
     // Make sure that we pass a string value to setState!
-    setState('periodiccounter', "" + (parseInt(getState('periodiccounter')) + 1));
+    setState(stateName, "" + (parseInt(getState(stateName)) + 1));
   }
   response.setHeader("Content-Type", "application/javascript", false);
-  response.write(getScript());
+  response.write(getScript(stateName));
 }
 
-function getScript() {
+function getScript(stateName) {
   return "onfetch = function(e) {" +
            "if (e.request.url.indexOf('get-sw-version') > -1) {" +
-             "e.respondWith(new Response('" + getState('periodiccounter') + "'));" +
+             "e.respondWith(new Response('" + getState(stateName) + "'));" +
            "}" +
          "};";
 }
--- a/dom/workers/test/serviceworkers/periodic/frame.html
+++ b/dom/workers/test/serviceworkers/periodic/frame.html
@@ -1,8 +1,8 @@
 <!DOCTYPE html>
 <script>
   fetch("get-sw-version").then(function(r) {
     return r.text();
   }).then(function(body) {
-    parent.callback(body);
+    parent.postMessage({status: "callback", data: body}, "*");
   });
 </script>
--- a/dom/workers/test/serviceworkers/periodic/register.html
+++ b/dom/workers/test/serviceworkers/periodic/register.html
@@ -1,9 +1,9 @@
 <!DOCTYPE html>
 <script>
   function done() {
-    parent.callback();
+    parent.postMessage({status: "callback", data: "done"}, "*");
   }
 
   navigator.serviceWorker.ready.then(done);
   navigator.serviceWorker.register("../periodic.sjs", {scope: "."});
 </script>
--- a/dom/workers/test/serviceworkers/periodic/unregister.html
+++ b/dom/workers/test/serviceworkers/periodic/unregister.html
@@ -1,16 +1,16 @@
 <!DOCTYPE html>
 <script>
   fetch("../periodic.sjs?clearcounter").then(function() {
     return navigator.serviceWorker.getRegistration(".");
   }).then(function(registration) {
     registration.unregister().then(function(success) {
       if (success) {
-        parent.callback();
+        parent.postMessage({status: "callback", data: "done"}, "*");
       } else {
         dump("Unregister failed\n");
       }
     }, function(e) {
       dump("Unregistering the SW failed with " + e + "\n");
     });
   });
 </script>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/periodic/wait_for_update.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<script>
+  navigator.serviceWorker.getRegistration(".").then(function(reg) {
+    reg.onupdatefound = function() {
+      reg.onupdatefound = null;
+      var sw = reg.installing;
+      sw.onstatechange = function() {
+        sw.onstatechange = null;
+        var success = !reg.waiting && reg.active;
+        parent.postMessage({status: "callback", data: "done"}, "*");
+      };
+    };
+    SpecialPowers.startPeriodicServiceWorkerUpdates();
+  });
+</script>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/periodic_update_test.js
@@ -0,0 +1,74 @@
+var oldSWVersion, newSWVersion;
+// This will be set by the test to the base directory for the test files.
+var gPrefix;
+
+function start() {
+  const Cc = SpecialPowers.Cc;
+  const Ci = SpecialPowers.Ci;
+
+  function testVersion() {
+    // Verify that the service worker has been correctly updated.
+    testFrame(gPrefix + "periodic/frame.html").then(function(body) {
+      newSWVersion = parseInt(body);
+      is(newSWVersion, 2, "Expected correct new version");
+      ok(newSWVersion > oldSWVersion,
+         "The SW should be successfully updated, old: " + oldSWVersion +
+         ", new: " + newSWVersion);
+      unregisterSW().then(function() {
+        SimpleTest.finish();
+      });
+    });
+  }
+
+  registerSW().then(function() {
+    return testFrame(gPrefix + "periodic/frame.html").then(function(body) {
+      oldSWVersion = parseInt(body);
+      is(oldSWVersion, 1, "Expected correct old version");
+    });
+  }).then(function() {
+    return testFrame(gPrefix + "periodic/wait_for_update.html");
+  }).then(function() {
+    return testVersion();
+  });
+}
+
+function testFrame(src) {
+  return new Promise(function(resolve, reject) {
+    var iframe = document.createElement("iframe");
+    iframe.src = src;
+    window.onmessage = function(e) {
+      if (e.data.status == "callback") {
+        window.onmessage = null;
+        var result = e.data.data;
+        iframe.src = "about:blank";
+        document.body.removeChild(iframe);
+        iframe = null;
+        SpecialPowers.exactGC(window, function() {
+          resolve(result);
+        });
+      }
+    };
+    document.body.appendChild(iframe);
+  });
+}
+
+function registerSW() {
+  return testFrame(gPrefix + "periodic/register.html");
+}
+
+function unregisterSW() {
+  return testFrame(gPrefix + "periodic/unregister.html");
+}
+
+function runTheTest() {
+  SimpleTest.waitForExplicitFinish();
+
+  SpecialPowers.pushPrefEnv({"set": [
+    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
+    ["dom.serviceWorkers.enabled", true],
+    ["dom.serviceWorkers.testing.enabled", true],
+    ["dom.serviceWorkers.periodic-updates.enabled", true],
+  ]}, function() {
+    start();
+  });
+}
--- a/dom/workers/test/serviceworkers/test_periodic_update.html
+++ b/dom/workers/test/serviceworkers/test_periodic_update.html
@@ -2,97 +2,25 @@
   Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/
 -->
 <!DOCTYPE HTML>
 <html>
 <head>
   <title>Bug 1112469 - Test the periodic update of service workers</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="periodic_update_test.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <p id="display"></p>
 <div id="content" style="display: none"></div>
 <pre id="test"></pre>
 <script class="testbody" type="text/javascript">
 
-  var oldSWVersion, newSWVersion;
-
-  function start() {
-    const Cc = SpecialPowers.Cc;
-    const Ci = SpecialPowers.Ci;
-
-    function testVersion(sw) {
-      // Verify that the service worker has been correctly updated.
-      testFrame("periodic/frame.html").then(function(body) {
-        newSWVersion = parseInt(body);
-        is(newSWVersion, 2, "Expected correct new version");
-        ok(newSWVersion > oldSWVersion,
-           "The SW should be successfully updated, old: " + oldSWVersion +
-           ", new: " + newSWVersion);
-        unregisterSW().then(function() {
-          SimpleTest.finish();
-        });
-      });
-    }
+gPrefix = "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/";
+runTheTest();
 
-    registerSW().then(function() {
-      return testFrame("periodic/frame.html").then(function(body) {
-        oldSWVersion = parseInt(body);
-        is(oldSWVersion, 1, "Expected correct old version");
-      });
-    }).then(function() {
-      return navigator.serviceWorker.getRegistration("periodic/foo");
-    }).then(function(reg) {
-      reg.onupdatefound = function() {
-        reg.onupdatefound = null;
-        var sw = reg.installing;
-        sw.onstatechange = function() {
-          sw.onstatechange = null;
-          ok(!reg.waiting && reg.active, "New worker must get activated immediately");
-          testVersion(reg.active);
-        };
-      };
-    }).then(function() {
-      SpecialPowers.startPeriodicServiceWorkerUpdates();
-    });
-  }
-
-  function testFrame(src) {
-    return new Promise(function(resolve, reject) {
-      var iframe = document.createElement("iframe");
-      iframe.src = src;
-      window.callback = function(result) {
-        iframe.src = "about:blank";
-        document.body.removeChild(iframe);
-        iframe = null;
-        SpecialPowers.exactGC(window, function() {
-          resolve(result);
-        });
-      };
-      document.body.appendChild(iframe);
-    });
-  }
-
-  function registerSW() {
-    return testFrame("periodic/register.html");
-  }
-
-  function unregisterSW() {
-    return testFrame("periodic/unregister.html");
-  }
-
-  SimpleTest.waitForExplicitFinish();
-
-  SpecialPowers.pushPrefEnv({"set": [
-    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
-    ["dom.serviceWorkers.enabled", true],
-    ["dom.serviceWorkers.testing.enabled", true],
-    ["dom.serviceWorkers.periodic-updates.enabled", true],
-  ]}, function() {
-    start();
-  });
 </script>
 </pre>
 </body>
 </html>