Bug 1172870 - Part 3 - Fix openWindow mochitest to work on e10s. r=smaug a=rkothari
authorCatalin Badea <catalin.badea392@gmail.com>
Thu, 12 Nov 2015 04:02:34 +0200
changeset 303695 70132ea00d70
parent 303694 be8391c58a83
child 303696 fcbbc5261056
push id5392
push userraliiev@mozilla.com
push dateMon, 14 Dec 2015 20:08:23 +0000
treeherdermozilla-beta@16ce8562a975 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, rkothari
bugs1172870
milestone44.0a2
Bug 1172870 - Part 3 - Fix openWindow mochitest to work on e10s. r=smaug a=rkothari Conflicts: dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/openWindow_worker.js
dom/workers/test/serviceworkers/open_window/client.html
dom/workers/test/serviceworkers/test_openWindow.html
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -192,16 +192,17 @@ support-files =
   sw_clients/file_blob_upload_frame.html
   redirect_post.sjs
   xslt_worker.js
   xslt/*
   unresolved_fetch_worker.js
   header_checker.sjs
   openWindow_worker.js
   redirect.sjs
+  open_window/client.html
 
 [test_app_protocol.html]
 skip-if = release_build
 [test_bug1151916.html]
 [test_claim.html]
 [test_claim_fetch.html]
 [test_claim_oninstall.html]
 [test_close.html]
@@ -289,11 +290,11 @@ skip-if = toolkit == "android" || toolki
 [test_file_blob_upload.html]
 [test_unresolved_fetch_interception.html]
 [test_hsts_upgrade_intercept.html]
 skip-if = e10s # Bug 1214305
 [test_csp_upgrade-insecure_intercept.html]
 skip-if = e10s # Bug 1214305
 [test_serviceworker_header.html]
 [test_openWindow.html]
-skip-if = toolkit == "android" || toolkit == "gonk" || e10s
+skip-if = toolkit == "android" || toolkit == "gonk"
 [test_imagecache.html]
 [test_imagecache_max_age.html]
--- a/dom/workers/test/serviceworkers/openWindow_worker.js
+++ b/dom/workers/test/serviceworkers/openWindow_worker.js
@@ -1,12 +1,21 @@
 // the worker won't shut down between events because we increased
 // the timeout values.
 var client;
+var window_count = 0;
+var expected_window_count = 6;
+var resolve_got_all_windows = null;
+var got_all_windows = new Promise(function(res, rej) {
+  resolve_got_all_windows = res;
+});
 
+// |expected_window_count| needs to be updated for every new call that's
+// expected to actually open a new window regardless of what |clients.openWindow|
+// returns.
 function testForUrl(url, throwType, clientProperties, resultsArray) {
   return clients.openWindow(url)
     .then(function(e) {
       if (throwType != null) {
         resultsArray.push({
           result: false,
           message: "openWindow should throw " + throwType
         });
@@ -38,53 +47,69 @@ function testForUrl(url, throwType, clie
           result: err.toString().indexOf(throwType) >= 0,
           message: "openWindow should throw: " + err
         });
       }
     })
 }
 
 onmessage = function(event) {
-  client = event.source;
+  if (event.data == "testNoPopup") {
+    client = event.source;
 
-  var results = [];
-  var promises = [];
-  promises.push(testForUrl("about:blank", "TypeError", null, results));
-  promises.push(testForUrl("http://example.com", "InvalidAccessError", null, results));
-  promises.push(testForUrl("_._*`InvalidURL", "InvalidAccessError", null, results));
-  Promise.all(promises).then(function(e) {
-    client.postMessage(results);
-  });
+    var results = [];
+    var promises = [];
+    promises.push(testForUrl("about:blank", "TypeError", null, results));
+    promises.push(testForUrl("http://example.com", "InvalidAccessError", null, results));
+    promises.push(testForUrl("_._*`InvalidURL", "InvalidAccessError", null, results));
+    Promise.all(promises).then(function(e) {
+      client.postMessage(results);
+    });
+  }
+  if (event.data == "NEW_WINDOW") {
+    window_count += 1;
+    if (window_count == expected_window_count) {
+      resolve_got_all_windows();
+    }
+  }
+
+  if (event.data == "CHECK_NUMBER_OF_WINDOWS") {
+    got_all_windows.then(function() {
+      return clients.matchAll();
+    }).then(function(cl) {
+      event.source.postMessage({result: cl.length == expected_window_count,
+                                message: "The number of windows is correct."});
+      for (i = 0; i < cl.length; i++) {
+        cl[i].postMessage("CLOSE");
+      }
+    });
+  }
 }
 
 onnotificationclick = function(e) {
   var results = [];
   var promises = [];
 
+  var redirect = "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/redirect.sjs?"
+  var redirect_xorigin = "http://example.com/tests/dom/workers/test/serviceworkers/redirect.sjs?"
+  var same_origin = "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/open_window/client.html"
+  var different_origin = "http://example.com/tests/dom/workers/test/serviceworkers/open_window/client.html"
+
+
   promises.push(testForUrl("about:blank", "TypeError", null, results));
-  promises.push(testForUrl("http://example.com", null, null, results));
-  promises.push(testForUrl("http://mochi.test:8888/same_origin.html", null,
-                           {url: "http://mochi.test:8888/same_origin.html"}, results));
+  promises.push(testForUrl(different_origin, null, null, results));
+  promises.push(testForUrl(same_origin, null, {url: same_origin}, results));
 
   // redirect tests
-  var redirect = "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/redirect.sjs?"
-  var baseURL = "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/"
-  promises.push(testForUrl(redirect + "same_origin_redirect.html", null,
-			   {url: baseURL + "same_origin_redirect.html"}, results));
-  promises.push(testForUrl(redirect + "http://example.com/redirect_to_other_origin.html", null,
+  promises.push(testForUrl(redirect + "open_window/client.html", null,
+			   {url: same_origin}, results));
+  promises.push(testForUrl(redirect + different_origin, null, null, results));
+
+  promises.push(testForUrl(redirect_xorigin + "open_window/client.html", null,
 			   null, results));
-
-  var redirect_xorigin = "http://example.com/tests/dom/workers/test/serviceworkers/redirect.sjs?"
-  promises.push(testForUrl(redirect_xorigin + "xorigin_redirect.html", null,
-			   null, results));
-  promises.push(testForUrl(redirect_xorigin + "http://mochi.test:8888/xorigin_to_same_origin.html", null,
-			   {url: "http://mochi.test:8888/xorigin_to_same_origin.html"}, results));
+  promises.push(testForUrl(redirect_xorigin + same_origin, null,
+			   {url: same_origin}, results));
 
   Promise.all(promises).then(function(e) {
     client.postMessage(results);
   });
 }
 
-onfetch = function(e) {
-  if (e.request.url.indexOf(same_origin) >= 0) {
-    e.respondWith(new Response("same_origin_window"));
-  }
-}
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/open_window/client.html
@@ -0,0 +1,48 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1172870 - page opened by ServiceWorkerClients.OpenWindow</title>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test"></pre>
+<script class="testbody" type="text/javascript">
+
+  window.onload = function() {
+    if (window.location == "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/open_window/client.html") {
+      navigator.serviceWorker.ready.then(function(result) {
+        navigator.serviceWorker.onmessage = function(event) {
+          if (event.data !== "CLOSE") {
+            dump("ERROR: unexepected reply from the service worker.\n");
+          }
+          if (parent) {
+            parent.postMessage("CLOSE", "*");
+          }
+          window.close();
+        }
+        navigator.serviceWorker.controller.postMessage("NEW_WINDOW");
+      })
+    } else {
+      window.onmessage = function(event) {
+        if (event.data !== "CLOSE") {
+            dump("ERROR: unexepected reply from the iframe.\n");
+        }
+        window.close();
+      }
+
+      var iframe = document.createElement('iframe');
+      iframe.src = "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/open_window/client.html";
+      document.body.appendChild(iframe);
+    }
+  }
+
+</script>
+</pre>
+</body>
+</html>
+
--- a/dom/workers/test/serviceworkers/test_openWindow.html
+++ b/dom/workers/test/serviceworkers/test_openWindow.html
@@ -60,39 +60,44 @@ https://bugzilla.mozilla.org/show_bug.cg
 
   function testPopupAllowed(ctx) {
     var p = setupMessageHandler(ctx);
     ctx.registration.showNotification("testPopup");
 
     return p;
   }
 
+  function checkNumberOfWindows(ctx) {
+    return new Promise(function(res, rej) {
+      navigator.serviceWorker.onmessage = function(event) {
+        navigator.serviceWorker.onmessage = null;
+        ok(event.data.result, event.data.message);
+        res(ctx);
+      }
+      ctx.registration.active.postMessage("CHECK_NUMBER_OF_WINDOWS");
+    });
+  }
+
   function clear(ctx) {
     MockServices.unregister();
 
-    var browser = SpecialPowers.Services.wm.getMostRecentWindow("navigator:browser").gBrowser;
-
-    ok(browser.tabs.length == 7, "Total number of tabs is correct.");
-    while (browser.tabs.length > 1) {
-      browser.removeTab(browser.tabs[1]);
-    }
-
     return ctx.registration.unregister().then(function(result) {
       ctx.registration = null;
       ok(result, "Unregister was successful.");
     });
   }
 
   function runTest() {
     setup({})
       .then(waitForActiveServiceWorker)
       // Permission to allow popups persists for some time after a notification
       // click event, so the order here is important.
       .then(testPopupNotAllowed)
       .then(testPopupAllowed)
+      .then(checkNumberOfWindows)
       .then(clear)
       .catch(function(e) {
         ok(false, "Some test failed with error " + e);
       }).then(SimpleTest.finish);
   }
 
   SimpleTest.waitForExplicitFinish();
   SpecialPowers.pushPrefEnv({"set": [