Bug 1139561 - Various small ServiceWorker test fixes. r=baku
authorNikhil Marathe <nsm.nikhil@gmail.com>
Tue, 03 Mar 2015 16:00:02 -0800
changeset 232062 16b7f5cec223aa792cc0df92dc0d9a106959f846
parent 232061 977e7f1f392714eac911b668a0b524ecf3305332
child 232063 ffc53dc185b9285e5145b1f9def8813439d98646
push id28368
push userkwierso@gmail.com
push dateThu, 05 Mar 2015 23:18:02 +0000
treeherdermozilla-central@1fe0dd36816b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1139561
milestone39.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 1139561 - Various small ServiceWorker test fixes. r=baku Replace setTimeout for test_unregister with detailed checks for active worker before opening iframe check for null reg in unregister tests Removed current dir scope so potential fetch event slowdown does not timeout tests use navigator.serviceWorker to receive messages Remove leftover getRegistration() test
dom/workers/test/serviceworkers/simpleregister/index.html
dom/workers/test/serviceworkers/test_scopes.html
dom/workers/test/serviceworkers/test_unregister.html
dom/workers/test/serviceworkers/test_workerUnregister.html
dom/workers/test/serviceworkers/unregister/index.html
dom/workers/test/serviceworkers/unregister/unregister.html
dom/workers/test/serviceworkers/workerUpdate/update.html
--- a/dom/workers/test/serviceworkers/simpleregister/index.html
+++ b/dom/workers/test/serviceworkers/simpleregister/index.html
@@ -31,25 +31,16 @@
             .then(function(a) {
             window.parent.postMessage({ type: "check", status: a instanceof ServiceWorkerRegistration,
                                         msg: "getRegistration returns a ServiceWorkerRegistration" }, "*");
             a.onupdatefound = function(e) {
               eventReceived();
             }
           });
 
-          navigator.serviceWorker.getRegistration('http://mochi.test:8888/with_star/*')
-            .then(function(a) {
-            window.parent.postMessage({ type: "check", status: true,
-                                        msg: "getRegistration returns a ServiceWorkerRegistration" }, "*");
-            a.onupdatefound = function(e) {
-              eventReceived();
-            }
-          });
-
           navigator.serviceWorker.getRegistration('http://www.something_else.net/')
             .then(function(a) {
             window.parent.postMessage({ type: "check", status: false,
                                         msg: "getRegistration should throw for security error!" }, "*");
           }, function(a) {
             window.parent.postMessage({ type: "check", status: true,
                                         msg: "getRegistration should throw for security error!" }, "*");
           });
--- a/dom/workers/test/serviceworkers/test_scopes.html
+++ b/dom/workers/test/serviceworkers/test_scopes.html
@@ -11,17 +11,16 @@
 </head>
 <body>
 <p id="display"></p>
 <div id="content" style="display: none"></div>
 <pre id="test"></pre>
 <script class="testbody" type="text/javascript">
 
   var scriptsAndScopes = [
-    [ "worker.js", "." ],
     [ "worker.js", "./sub/dir/"],
     [ "worker.js", "./sub/dir" ],
     [ "worker.js", "./sub/dir.html" ],
     [ "worker.js", "./sub/dir/a" ],
     [ "worker.js", "./sub" ],
     [ "worker.js", "./star*" ], // '*' has no special meaning
   ];
 
@@ -48,25 +47,34 @@
     return new Promise(function(resolve, reject) {
       var getScope = navigator.serviceWorker.getScopeForUrl.bind(navigator.serviceWorker);
       var base = new URL(".", document.baseURI);
 
       function p(s) {
         return base + s;
       }
 
-      ok(getScope(p("index.html")) === p(""), "Scope should match");
-      ok(getScope(p("sua.html")) === p(""), "Scope should match");
+      function fail(fn) {
+        try {
+          getScope(p("index.html"));
+          ok(false, "No registration");
+        } catch(e) {
+          ok(true, "No registration");
+        }
+      }
+
       ok(getScope(p("sub.html")) === p("sub"), "Scope should match");
       ok(getScope(p("sub/dir.html")) === p("sub/dir.html"), "Scope should match");
       ok(getScope(p("sub/dir")) === p("sub/dir"), "Scope should match");
       ok(getScope(p("sub/dir/foo")) === p("sub/dir/"), "Scope should match");
       ok(getScope(p("sub/dir/afoo")) === p("sub/dir/a"), "Scope should match");
       ok(getScope(p("star*wars")) === p("star*"), "Scope should match");
-      ok(getScope(p("star/a.html")) === p(""), "Scope should match");
+      fail("index.html");
+      fail("sua.html");
+      fail("star/a.html");
       resolve();
     });
   }
 
   function runTest() {
     registerWorkers()
       .then(testScopes)
       .then(unregisterWorkers)
--- a/dom/workers/test/serviceworkers/test_unregister.html
+++ b/dom/workers/test/serviceworkers/test_unregister.html
@@ -11,18 +11,41 @@
 </head>
 <body>
 <p id="display"></p>
 <div id="content" style="display: none"></div>
 <pre id="test"></pre>
 <script class="testbody" type="text/javascript">
 
   function simpleRegister() {
-        info("simpleRegister() just before calling register");
-    return navigator.serviceWorker.register("worker.js", { scope: "unregister/" });
+    return navigator.serviceWorker.register("worker.js", { scope: "unregister/" }).then(function(swr) {
+      if (swr.installing) {
+        return new Promise(function(resolve, reject) {
+          swr.installing.onstatechange = function(e) {
+            if (swr.waiting) {
+              swr.waiting.onstatechange = function(e) {
+                if (swr.active) {
+                  resolve();
+                } else if (swr.waiting && swr.waiting.state == "redundant") {
+                  reject("Should not go into redundant");
+                }
+              }
+            } else {
+              if (swr.active) {
+                resolve();
+              } else {
+                reject("No waiting and no active!");
+              }
+            }
+          }
+        });
+      } else {
+        return Promise.reject("Installing should be non-null");
+      }
+    });
   }
 
   function testControlled() {
     var testPromise = new Promise(function(res, rej) {
       window.onmessage = function(e) {
         if (!("controlled" in e.data)) {
           ok(false, "Something went wrong.");
           rej();
@@ -44,16 +67,21 @@
     return testPromise.then(function() {
       div.removeChild(ifr);
     });
   }
 
   function unregister() {
     return navigator.serviceWorker.getRegistration("unregister/")
            .then(function(reg) {
+             if (!reg) {
+               info("Registration already removed");
+               return;
+             }
+
              info("getRegistration() succeeded " + reg.scope);
              return reg.unregister().then(function(v) {
                ok(v, "Unregister should resolve to true");
              }, function(e) {
                ok(false, "Unregister failed with " + e.name);
              });
            });
   }
--- a/dom/workers/test/serviceworkers/test_workerUnregister.html
+++ b/dom/workers/test/serviceworkers/test_workerUnregister.html
@@ -9,17 +9,41 @@
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <div id="container"></div>
 <script class="testbody" type="text/javascript">
 
   function simpleRegister() {
-    return navigator.serviceWorker.register("worker_unregister.js", { scope: "unregister/" });
+    return navigator.serviceWorker.register("worker_unregister.js", { scope: "unregister/" }).then(function(swr) {
+      if (swr.installing) {
+        return new Promise(function(resolve, reject) {
+          swr.installing.onstatechange = function(e) {
+            if (swr.waiting) {
+              swr.waiting.onstatechange = function(e) {
+                if (swr.active) {
+                  resolve();
+                } else if (swr.waiting && swr.waiting.state == "redundant") {
+                  reject("Should not go into redundant");
+                }
+              }
+            } else {
+              if (swr.active) {
+                resolve();
+              } else {
+                reject("No waiting and no active!");
+              }
+            }
+          }
+        });
+      } else {
+        return Promise.reject("Installing should be non-null");
+      }
+    });
   }
 
   function waitForMessages(sw) {
     var p = new Promise(function(resolve, reject) {
       window.onmessage = function(e) {
         if (e.data === "DONE") {
           ok(true, "The worker has unregistered itself");
         } else if (e.data === "ERROR") {
--- a/dom/workers/test/serviceworkers/unregister/index.html
+++ b/dom/workers/test/serviceworkers/unregister/index.html
@@ -14,28 +14,13 @@
 <div id="content" style="display: none"></div>
 <pre id="test"></pre>
 <script class="testbody" type="text/javascript">
 
   if (!parent) {
     info("unregister/index.html should not to be launched directly!");
   }
 
-  SimpleTest.requestFlakyTimeout("Unfortunately we have no way to test for a page being uncontrolled except waiting for ready to not resolve");
-  var tId = setTimeout(function() {
-    parent.postMessage({ controlled: false }, "*");
-    tId = null;
-  }, 2000);
-
-  navigator.serviceWorker.ready.then(function() {
-    if (tId == null) {
-      parent.postMessage("FAIL!!!", "*");
-      return;
-    }
-
-    clearTimeout(tId);
-    parent.postMessage({ controlled: true }, "*");
-  });
-
+  parent.postMessage({ controlled: !!navigator.serviceWorker.controller }, "*");
 </script>
 </pre>
 </body>
 </html>
--- a/dom/workers/test/serviceworkers/unregister/unregister.html
+++ b/dom/workers/test/serviceworkers/unregister/unregister.html
@@ -7,16 +7,16 @@
 <head>
   <title>Test worker::unregister</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <script type="text/javascript">
 
-  onmessage = function(e) { parent.postMessage(e.data, "*"); }
+  navigator.serviceWorker.onmessage = function(e) { parent.postMessage(e.data, "*"); }
   navigator.serviceWorker.controller.postMessage("GO");
 
 </script>
 </pre>
 </body>
 </html>
 
--- a/dom/workers/test/serviceworkers/workerUpdate/update.html
+++ b/dom/workers/test/serviceworkers/workerUpdate/update.html
@@ -7,17 +7,17 @@
 <head>
   <title>Test worker::update</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <script type="text/javascript">
 
-  onmessage = function(e) { parent.postMessage(e.data, "*"); }
+  navigator.serviceWorker.onmessage = function(e) { parent.postMessage(e.data, "*"); }
   navigator.serviceWorker.ready.then(function() {
     navigator.serviceWorker.controller.postMessage("GO");
   });
 
 </script>
 </pre>
 </body>
 </html>