Bug 1425965 P5 Make SimpleTest.monitorConsole() and SpecialPowers.registerConsoleListener() observe dom/console events. r=baku
authorBen Kelly <ben@wanderview.com>
Fri, 12 Jan 2018 12:16:05 -0500
changeset 450870 241b9e419ca4a960144320842839f4ae39be6493
parent 450869 4027039552fea211647109c3d45b4215d759bd52
child 450871 9972008ac10510753a6adda9af0f21b33ee861e8
push id8543
push userryanvm@gmail.com
push dateTue, 16 Jan 2018 14:33:22 +0000
treeherdermozilla-beta@a6525ed16a32 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1425965
milestone59.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 1425965 P5 Make SimpleTest.monitorConsole() and SpecialPowers.registerConsoleListener() observe dom/console events. r=baku
testing/specialpowers/content/specialpowersAPI.js
--- a/testing/specialpowers/content/specialpowersAPI.js
+++ b/testing/specialpowers/content/specialpowersAPI.js
@@ -308,57 +308,74 @@ SpecialPowersHandler.prototype = {
 // tidy, XPCOM-hiding way.  Messages that are nsIScriptError objects
 // have their properties exposed in detail.  It also auto-unregisters
 // itself when it receives a "sentinel" message.
 function SPConsoleListener(callback) {
   this.callback = callback;
 }
 
 SPConsoleListener.prototype = {
-  observe(msg) {
+  // Overload the observe method for both nsIConsoleListener and nsIObserver.
+  // The topic will be null for nsIConsoleListener.
+  observe(msg, topic) {
     let m = { message: msg.message,
               errorMessage: null,
               sourceName: null,
               sourceLine: null,
               lineNumber: null,
               columnNumber: null,
               category: null,
               windowID: null,
               isScriptError: false,
+              isConsoleEvent: false,
               isWarning: false,
               isException: false,
               isStrict: false };
     if (msg instanceof Ci.nsIScriptError) {
       m.errorMessage  = msg.errorMessage;
       m.sourceName    = msg.sourceName;
       m.sourceLine    = msg.sourceLine;
       m.lineNumber    = msg.lineNumber;
       m.columnNumber  = msg.columnNumber;
       m.category      = msg.category;
       m.windowID      = msg.outerWindowID;
       m.innerWindowID = msg.innerWindowID;
       m.isScriptError = true;
       m.isWarning     = ((msg.flags & Ci.nsIScriptError.warningFlag) === 1);
       m.isException   = ((msg.flags & Ci.nsIScriptError.exceptionFlag) === 1);
       m.isStrict      = ((msg.flags & Ci.nsIScriptError.strictFlag) === 1);
+    } else if (topic === "console-api-log-event") {
+      // This is a dom/console event.
+      let unwrapped = msg.wrappedJSObject;
+      m.errorMessage   = unwrapped.arguments[0];
+      m.sourceName     = unwrapped.filename;
+      m.lineNumber     = unwrapped.lineNumber;
+      m.columnNumber   = unwrapped.columnNumber;
+      m.windowID       = unwrapped.ID;
+      m.innerWindowID  = unwrapped.innerID;
+      m.isConsoleEvent = true;
+      m.isWarning      = unwrapped.level === "warning";
     }
 
     Object.freeze(m);
 
     // Run in a separate runnable since console listeners aren't
     // supposed to touch content and this one might.
     Services.tm.dispatchToMainThread(() => {
       this.callback.call(undefined, m);
     });
 
-    if (!m.isScriptError && m.message === "SENTINEL")
+    if (!m.isScriptError && !m.isConsoleEvent && m.message === "SENTINEL") {
+      Services.obs.removeObserver(this, "console-api-log-event");
       Services.console.unregisterListener(this);
+    }
   },
 
-  QueryInterface: XPCOMUtils.generateQI([Ci.nsIConsoleListener])
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIConsoleListener,
+                                         Ci.nsIObserver])
 };
 
 function wrapCallback(cb) {
   return function SpecialPowersCallbackWrapper() {
     var args = Array.prototype.map.call(arguments, wrapIfUnwrapped);
     return cb.apply(this, args);
   };
 }
@@ -1433,16 +1450,19 @@ SpecialPowersAPI.prototype = {
   // call to postConsoleSentinel; when the callback receives the
   // sentinel it will unregister itself (_after_ calling the
   // callback).  SimpleTest.expectConsoleMessages does this for you.
   // If you register more than one console listener, a call to
   // postConsoleSentinel will zap all of them.
   registerConsoleListener(callback) {
     let listener = new SPConsoleListener(callback);
     Services.console.registerListener(listener);
+
+    // listen for dom/console events as well
+    Services.obs.addObserver(listener, "console-api-log-event");
   },
   postConsoleSentinel() {
     Services.console.logStringMessage("SENTINEL");
   },
   resetConsole() {
     Services.console.reset();
   },