Bug 1438945 - Part 5: SharedWorker browser test. r=asuth
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 19 Nov 2018 15:18:21 -0800
changeset 503567 81c5bdf85bd781ba80d4bfc52aa66c778b248702
parent 503566 5de8d2303240d72cbca56a1fd1d50f575ec3b2de
child 503568 d87a2d39f76f7e0ec05dcfb6387c2abda16ad545
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs1438945
milestone65.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 1438945 - Part 5: SharedWorker browser test. r=asuth
devtools/client/webconsole/test/mochitest/browser.ini
dom/serviceworkers/test/console_monitor.js
dom/serviceworkers/test/mochitest.ini
dom/serviceworkers/test/test_fetch_integrity.html
dom/workers/test/browser.ini
dom/workers/test/browser_consoleSharedWorkers.js
dom/workers/test/mochitest.ini
dom/workers/test/test_consoleSharedWorkers.html
--- a/devtools/client/webconsole/test/mochitest/browser.ini
+++ b/devtools/client/webconsole/test/mochitest/browser.ini
@@ -267,16 +267,17 @@ tags = mcb
 [browser_webconsole_close_sidebar.js]
 skip-if = true # Bug 1405250
 [browser_webconsole_console_api_iframe.js]
 [browser_webconsole_console_dir.js]
 [browser_webconsole_console_dir_uninspectable.js]
 [browser_webconsole_console_error_expand_object.js]
 [browser_webconsole_console_group.js]
 [browser_webconsole_console_logging_workers_api.js]
+skip-if = e10s # SharedWorkers console events are not received on the current process because they could run on any process.
 [browser_webconsole_console_table.js]
 [browser_webconsole_console_trace_duplicates.js]
 [browser_webconsole_context_menu_copy_entire_message.js]
 subsuite = clipboard
 skip-if = (os == 'linux' && bits == 32 && debug) # bug 1328915, disable linux32 debug devtools for timeouts
 [browser_webconsole_context_menu_copy_link_location.js]
 subsuite = clipboard
 skip-if = (os == 'linux' && bits == 32 && debug) || (os == 'linux') # bug 1328915, disable linux32 debug devtools for timeouts, bug 1473120
new file mode 100644
--- /dev/null
+++ b/dom/serviceworkers/test/console_monitor.js
@@ -0,0 +1,45 @@
+ChromeUtils.import("resource://gre/modules/Services.jsm");
+
+let consoleListener;
+
+function ConsoleListener() {
+  Services.console.registerListener(this);
+}
+
+ConsoleListener.prototype  = {
+  callbacks: [],
+
+  observe: (aMsg) => {
+    if (!(aMsg instanceof Ci.nsIScriptError)) {
+      return;
+    }
+
+    let msg = {
+      errorMessage  : aMsg.errorMessage,
+      sourceName    : aMsg.sourceName,
+      sourceLine    : aMsg.sourceLine,
+      lineNumber    : aMsg.lineNumber,
+      columnNumber  : aMsg.columnNumber,
+      category      : aMsg.category,
+      windowID      : aMsg.outerWindowID,
+      innerWindowID : aMsg.innerWindowID,
+      isScriptError : true,
+      isWarning     : ((aMsg.flags & Ci.nsIScriptError.warningFlag) === 1),
+      isException   : ((aMsg.flags & Ci.nsIScriptError.exceptionFlag) === 1),
+      isStrict      : ((aMsg.flags & Ci.nsIScriptError.strictFlag) === 1),
+    };
+
+    sendAsyncMessage("monitor", msg);
+  }
+}
+
+addMessageListener("load", function (e) {
+  consoleListener = new ConsoleListener();
+  sendAsyncMessage("ready", {});
+});
+
+addMessageListener("unload", function (e) {
+  Services.console.unregisterListener(consoleListener);
+  consoleListener = null;
+  sendAsyncMessage("unloaded", {});
+});
--- a/dom/serviceworkers/test/mochitest.ini
+++ b/dom/serviceworkers/test/mochitest.ini
@@ -243,16 +243,17 @@ skip-if = debug # Bug 1262224
 [test_eval_allowed.html]
 [test_eventsource_intercept.html]
 [test_event_listener_leaks.html]
 [test_fetch_event.html]
 skip-if = (debug && e10s) # Bug 1262224
 [test_fetch_event_with_thirdpartypref.html]
 skip-if = (debug && e10s) # Bug 1262224
 [test_fetch_integrity.html]
+support-files = console_monitor.js
 [test_file_blob_response.html]
 [test_file_blob_upload.html]
 [test_force_refresh.html]
 [test_gzip_redirect.html]
 [test_hsts_upgrade_intercept.html]
 [test_https_fetch.html]
 [test_https_fetch_cloned_response.html]
 [test_https_origin_after_redirect.html]
--- a/dom/serviceworkers/test/test_fetch_integrity.html
+++ b/dom/serviceworkers/test/test_fetch_integrity.html
@@ -12,16 +12,81 @@
 <div id="content" style="display: none"></div>
 <script src="utils.js"></script>
 <script type="text/javascript">
 "use strict";
 
 let security_localizer =
   stringBundleService.createBundle("chrome://global/locale/security/security.properties");
 
+let consoleScript;
+let monitorCallbacks = [];
+
+function registerConsoleMonitor() {
+  return new Promise(resolve => {
+    var url = SimpleTest.getTestFileURL("console_monitor.js");
+    consoleScript = SpecialPowers.loadChromeScript(url);
+
+    consoleScript.addMessageListener("ready", resolve);
+    consoleScript.addMessageListener("monitor", function(msg) {
+      for (let i = 0; i < monitorCallbacks.length;) {
+         if (monitorCallbacks[i](msg)) {
+           ++i;
+         } else {
+           monitorCallbacks.splice(i, 1);
+         }
+      }
+    });
+    consoleScript.sendAsyncMessage("load", {});
+  });
+}
+
+function unregisterConsoleMonitor() {
+  return new Promise(resolve => {
+    consoleScript.addMessageListener("unloaded", () => {
+      consoleScript.destroy();
+      resolve();
+    });
+    consoleScript.sendAsyncMessage("unload", {});
+  });
+}
+
+function registerConsoleMonitorCallback(callback) {
+  monitorCallbacks.push(callback);
+}
+
+function waitForMessages() {
+  let messages = [];
+
+  // process repeated paired arguments of: msgId, args
+  for (let i = 0; i < arguments.length; i += 3) {
+    let msgId = arguments[i];
+    let args = arguments[i + 1];
+    messages.push(security_localizer.formatStringFromName(msgId, args, args.length));
+  }
+
+  return new Promise(resolve => {
+    registerConsoleMonitorCallback(msg => {
+      for (let i = 0; i < messages.length; ++i) {
+        if (messages[i] == msg.errorMessage) {
+          messages.splice(i, 1);
+          break;
+        }
+      }
+
+      if (messages.length == 0) {
+        resolve();
+        return false;
+      }
+
+      return true;
+    });
+  });
+}
+
 function expect_security_console_message(/* msgId, args, ... */) {
   let expectations = [];
   // process repeated paired arguments of: msgId, args
   for (let i = 0; i < arguments.length; i += 3) {
     let msgId = arguments[i];
     let args = arguments[i + 1];
     let filename = arguments[i + 2];
     expectations.push({
@@ -87,17 +152,19 @@ add_task(async function test_integrity_s
 
   await registration.unregister();
   client_win.close();
 });
 
 add_task(async function test_integrity_sharedWorker() {
   var filename = make_absolute_url("sharedWorker_fetch.js");
 
-  info("Attch main window to a SharedWorker.");
+  await registerConsoleMonitor();
+
+  info("Attach main window to a SharedWorker.");
   let sharedWorker = new SharedWorker(filename);
   let waitForConnected = new Promise((resolve) => {
     sharedWorker.port.onmessage = function (e) {
       if (e.data == "Connected") {
         resolve();
       } else {
         reject();
       }
@@ -114,25 +181,26 @@ add_task(async function test_integrity_s
         resolve();
       } else {
         reject();
       }
     }
   });
   await waitForBothConnected;
 
-  let expectedMessage = expect_security_console_message(
+  let expectedMessage = waitForMessages(
     "MalformedIntegrityHash",
     ["abc"],
     filename,
     "NoValidMetadata",
     [""],
     filename,
   );
-  let expectedMessage2 = expect_security_console_message(
+
+  let expectedMessage2 = waitForMessages(
     "MalformedIntegrityHash",
     ["abc"],
     filename,
     "NoValidMetadata",
     [""],
     filename,
   );
 
@@ -146,17 +214,19 @@ add_task(async function test_integrity_s
         resolve();
       } else {
         reject();
       }
     }
   });
   await waitForSRIFailed;
 
-  await wait_for_expected_message(expectedMessage);
+  await expectedMessage;
+  await expectedMessage2;
 
-  await wait_for_expected_message(expectedMessage2);
   client_win.close();
+
+  await unregisterConsoleMonitor();
 });
 
 </script>
 </body>
 </html>
--- a/dom/workers/test/browser.ini
+++ b/dom/workers/test/browser.ini
@@ -4,10 +4,12 @@ support-files =
   bug1047663_worker.sjs
   frame_script.js
   head.js
   !/dom/base/test/file_empty.html
 
 [browser_bug1047663.js]
 [browser_bug1104623.js]
 run-if = buildapp == 'browser'
+[browser_consoleSharedWorkers.js]
+support-files = sharedWorker_console.js empty.html
 [browser_fileURL.js]
 support-files = empty.html empty_worker.js
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/browser_consoleSharedWorkers.js
@@ -0,0 +1,58 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+ChromeUtils.import("resource://gre/modules/Services.jsm");
+
+add_task(async function test() {
+  const testURL = getRootDirectory(gTestPath) + "empty.html";
+  let tab = BrowserTestUtils.addTab(gBrowser, testURL);
+  gBrowser.selectedTab = tab;
+
+  await BrowserTestUtils.browserLoaded(gBrowser.getBrowserForTab(tab));
+
+  let promise = new Promise(resolve => {
+    function consoleListener() {
+      Services.obs.addObserver(this, "console-api-log-event");
+      Services.obs.addObserver(this, "console-api-profiler");
+    }
+
+    var order = 0;
+    consoleListener.prototype  = {
+      observe: (aSubject, aTopic, aData) => {
+        ok(true, "Something has been received");
+
+        if (aTopic == "console-api-profiler") {
+          var obj = aSubject.wrappedJSObject;
+          is (obj.arguments[0], "Hello profiling from a SharedWorker!", "A message from a SharedWorker \\o/");
+          is (order++, 0, "First a profiler message.");
+
+          Services.obs.removeObserver(cl, "console-api-profiler");
+          return;
+        }
+
+        if (aTopic == "console-api-log-event") {
+          var obj = aSubject.wrappedJSObject;
+          is (obj.arguments[0], "Hello world from a SharedWorker!", "A message from a SharedWorker \\o/");
+          is (obj.ID, "chrome://mochitests/content/browser/dom/workers/test/sharedWorker_console.js", "The ID is SharedWorker");
+          is (obj.innerID, "SharedWorker", "The ID is SharedWorker");
+          is (order++, 1, "Then a log message.");
+
+          Services.obs.removeObserver(cl, "console-api-log-event");
+          resolve();
+          return;
+        }
+      }
+    }
+
+    var cl = new consoleListener();
+  });
+
+  await ContentTask.spawn(tab.linkedBrowser, {}, async function() {
+    new content.SharedWorker('sharedWorker_console.js');
+  });
+
+  await promise;
+
+  await BrowserTestUtils.removeTab(tab);
+});
--- a/dom/workers/test/mochitest.ini
+++ b/dom/workers/test/mochitest.ini
@@ -49,17 +49,16 @@ support-files =
   onLine_worker.js
   onLine_worker_child.js
   onLine_worker_head.js
   promise_worker.js
   recursion_worker.js
   recursiveOnerror_worker.js
   redirect_to_foreign.sjs
   rvals_worker.js
-  sharedWorker_console.js
   sharedWorker_sharedWorker.js
   sharedWorker_thirdparty_frame.html
   simpleThread_worker.js
   suspend_window.html
   suspend_worker.js
   terminate_worker.js
   test_csp.html^headers^
   test_csp.js
@@ -123,17 +122,16 @@ support-files =
 [test_bug1132395.html]
 skip-if = true # bug 1176225
 [test_bug1132924.html]
 [test_chromeWorker.html]
 [test_clearTimeouts.html]
 [test_console.html]
 [test_consoleAndBlobs.html]
 [test_consoleReplaceable.html]
-[test_consoleSharedWorkers.html]
 [test_contentWorker.html]
 [test_csp.html]
 [test_dataURLWorker.html]
 [test_errorPropagation.html]
 [test_errorwarning.html]
 [test_eventDispatch.html]
 [test_fibonacci.html]
 [test_importScripts.html]
deleted file mode 100644
--- a/dom/workers/test/test_consoleSharedWorkers.html
+++ /dev/null
@@ -1,56 +0,0 @@
-<!--
-  Any copyright is dedicated to the Public Domain.
-  http://creativecommons.org/publicdomain/zero/1.0/
--->
-<!DOCTYPE HTML>
-<html>
-  <head>
-    <title>Test for console API in SharedWorker</title>
-    <script src="/tests/SimpleTest/SimpleTest.js">
-    </script>
-    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
-  </head>
-  <body>
-    <script type="text/javascript">
-
-  function consoleListener() {
-    SpecialPowers.addObserver(this, "console-api-log-event");
-    SpecialPowers.addObserver(this, "console-api-profiler");
-  }
-
-  var order = 0;
-  consoleListener.prototype  = {
-    observe: function(aSubject, aTopic, aData) {
-      ok(true, "Something has been received");
-
-      if (aTopic == "console-api-profiler") {
-        var obj = aSubject.wrappedJSObject;
-        is (obj.arguments[0], "Hello profiling from a SharedWorker!", "A message from a SharedWorker \\o/");
-        is (order++, 0, "First a profiler message.");
-
-        SpecialPowers.removeObserver(this, "console-api-profiler");
-        return;
-      }
-
-      if (aTopic == "console-api-log-event") {
-        var obj = aSubject.wrappedJSObject;
-        is (obj.arguments[0], "Hello world from a SharedWorker!", "A message from a SharedWorker \\o/");
-        is (obj.ID, "http://mochi.test:8888/tests/dom/workers/test/sharedWorker_console.js", "The ID is SharedWorker");
-        is (obj.innerID, "SharedWorker", "The ID is SharedWorker");
-        is (order++, 1, "Then a log message.");
-
-        SpecialPowers.removeObserver(this, "console-api-log-event");
-        SimpleTest.finish();
-        return;
-      }
-    }
-  }
-
-  var cl = new consoleListener();
-  new SharedWorker('sharedWorker_console.js');
-
-  SimpleTest.waitForExplicitFinish();
-
-    </script>
-  </body>
-</html>