Bug 1455078: Refactor test to run in code in page r=ochameau,asuth
authorYaron Tausky <ytausky@mozilla.com>
Thu, 16 Aug 2018 16:18:30 +0000
changeset 487005 3c2f920f1a2d73d710f18360dc9cd723121d3b42
parent 487004 a4b1382784fada28d5fa9b0d0b08607ee1ca82a1
child 487006 056aaefaffac83e68417a62a048b046ab5e20f58
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau, asuth
bugs1455078
milestone63.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 1455078: Refactor test to run in code in page r=ochameau,asuth This test runs code in the frame script, which has system privileges. Even though it trips the security tests as it is, it's not clear whether that should be the case or not. By running the code in the page itself, the expected behavior is governed by the service workers spec and is thus well defined. Differential Revision: https://phabricator.services.mozilla.com/D3323
devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing.html
devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing_frame_script.js
--- a/devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing.html
+++ b/devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing.html
@@ -1,10 +1,81 @@
 <!DOCTYPE html>
 <html>
   <head>
     <title>browser_toolbox_options_enable_serviceworkers_testing.html</title>
     <meta charset="UTF-8">
   </head>
   <body>
     <h1>SW-test</h1>
+    <script>
+      function register() {
+        return Promise.resolve().then(function() {
+          // While ServiceWorkerContainer.register() returns a promise, it's
+          // still wrapped with a .then() because navigator.serviceWorker is not
+          // defined in insecure contexts unless service worker testing is
+          // enabled, so dereferencing it would throw a ReferenceError (which
+          // is then caught in the .catch() clause).
+          return window.navigator.serviceWorker.register("serviceworker.js");
+        }).then(registration => {
+          return {success: true};
+        }).catch(error => {
+          return {success: false};
+        });
+      }
+
+      function unregister() {
+        return Promise.resolve().then(function() {
+          return window.navigator.serviceWorker.getRegistration();
+        }).then(registration => {
+          return registration.unregister().then(result => {
+            return {success: !!result};
+          });
+        }).catch(_ => {
+          return {success: false};
+        });
+      }
+
+      function iframeRegisterAndUnregister() {
+        var frame = window.document.createElement("iframe");
+        var promise = new Promise(function(resolve, reject) {
+          frame.addEventListener("load", function() {
+            Promise.resolve().then(_ => {
+              return frame.contentWindow.navigator.serviceWorker.register("serviceworker.js");
+            }).then(swr => {
+              return swr.unregister();
+            }).then(_ => {
+              frame.remove();
+              resolve({success: true});
+            }).catch(error => {
+              resolve({success: false});
+            });
+          }, {once: true});
+        });
+        frame.src = "browser_toolbox_options_enabled_serviceworkers_testing.html";
+        window.document.body.appendChild(frame);
+        return promise;
+      }
+
+      window.addEventListener("message", function(event) {
+        var response;
+        switch (event.data) {
+          case "devtools:sw-test:register": {
+            response = register();
+            break;
+          }
+          case "devtools:sw-test:unregister": {
+            response = unregister();
+            break;
+          }
+          case "devtools:sw-test:iframe:register-and-unregister": {
+            response = iframeRegisterAndUnregister();
+            break;
+          }
+        }
+        response.then(data => {
+          event.ports[0].postMessage(data);
+          event.ports[0].close();
+        });
+      });
+    </script>
   </body>
 </html>
--- a/devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing_frame_script.js
+++ b/devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing_frame_script.js
@@ -4,44 +4,28 @@
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 // A helper frame-script for devtools/client/framework service worker tests.
 
 /* eslint-env mozilla/frame-script */
 
 "use strict";
 
-addMessageListener("devtools:sw-test:register", function(msg) {
-  content.navigator.serviceWorker.register("serviceworker.js")
-    .then(swr => {
-      sendAsyncMessage("devtools:sw-test:register", {success: true});
-    }, error => {
-      sendAsyncMessage("devtools:sw-test:register", {success: false});
-    });
-});
-
-addMessageListener("devtools:sw-test:unregister", function(msg) {
-  content.navigator.serviceWorker.getRegistration().then(swr => {
-    swr.unregister().then(result => {
-      sendAsyncMessage("devtools:sw-test:unregister",
-                       {success: !!result});
-    });
+function addMessageForwarder(name) {
+  addMessageListener(name, function(_) {
+    var channel = new MessageChannel();
+    content.postMessage(name, "*", [channel.port2]);
+    channel.port1.onmessage = function(msg) {
+      sendAsyncMessage(name, msg.data);
+      channel.port1.close();
+    };
   });
-});
+}
 
-addMessageListener("devtools:sw-test:iframe:register-and-unregister", function(msg) {
-  var frame = content.document.createElement("iframe");
-  frame.addEventListener("load", function() {
-    frame.contentWindow.navigator.serviceWorker.register("serviceworker.js")
-      .then(swr => {
-        return swr.unregister();
-      }).then(_ => {
-        frame.remove();
-        sendAsyncMessage("devtools:sw-test:iframe:register-and-unregister",
-                         {success: true});
-      }).catch(error => {
-        sendAsyncMessage("devtools:sw-test:iframe:register-and-unregister",
-                         {success: false});
-      });
-  }, {once: true});
-  frame.src = "browser_toolbox_options_enabled_serviceworkers_testing.html";
-  content.document.body.appendChild(frame);
-});
+const messages = [
+  "devtools:sw-test:register",
+  "devtools:sw-test:unregister",
+  "devtools:sw-test:iframe:register-and-unregister"
+];
+
+for (var msg of messages) {
+  addMessageForwarder(msg);
+}