Bug 1292325 - New console frontend: Add a waitForMessages function for end-to-end testing. r=linclark
authorBrian Grinstead <briangrinstead@gmail.com>
Thu, 18 Aug 2016 16:04:33 -0700
changeset 309890 e7ab4bc97bd0ead33c58a5a08427de48df29eea1
parent 309889 3d6b0fecc1fc6003ef74d6522d54c2f7a21a4348
child 309891 bb03d58f1435fbfadde5c0a069131223d0ea696c
push id20341
push userbgrinstead@mozilla.com
push dateThu, 18 Aug 2016 23:06:20 +0000
treeherderfx-team@7bf7d0d47b79 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslinclark
bugs1292325
milestone51.0a1
Bug 1292325 - New console frontend: Add a waitForMessages function for end-to-end testing. r=linclark MozReview-Commit-ID: H908h06zfNJ
devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_init.js
devtools/client/webconsole/webconsole.js
--- a/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_init.js
+++ b/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_init.js
@@ -4,20 +4,65 @@
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 const TEST_URI = "http://example.com/browser/devtools/client/webconsole/new-console-output/test/mochitest/test-console.html";
 
 add_task(function* () {
   let toolbox = yield openNewTabAndToolbox(TEST_URI, "webconsole");
-  let ui = toolbox.getCurrentPanel().hud.ui;
+  let hud = toolbox.getCurrentPanel().hud;
+  let {ui} = hud;
 
   ok(ui.jsterm, "jsterm exists");
   ok(ui.newConsoleOutput, "newConsoleOutput exists");
 
-  // @TODO: fix proptype errors and actually emit 'new-messages' event
-  // let receivedLog = ui.once("new-messages");
-  // yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function() {
-  //   content.wrappedJSObject.doLogs();
-  // });
-  // yield receivedLog;
+  // @TODO: fix proptype errors
+  let receievedMessages = waitForMessages({
+    hud,
+    messages: [{
+      text: '0',
+    }, {
+      text: '1',
+    }, {
+      text: '2',
+    }],
+  });
+
+  yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function() {
+    content.wrappedJSObject.doLogs(3);
+  });
+
+  yield receievedMessages;
 });
+
+/**
+ * Wait for messages in the web console output, resolving once they are receieved.
+ *
+ * @param object options
+ *        - hud: the webconsole
+ *        - messages: Array[Object]. An array of messages to match. Current supported options:
+ *            - text: Exact text match in .message-body
+ */
+function waitForMessages({ hud, messages }) {
+  return new Promise(resolve => {
+    let numMatched = 0;
+    let receivedLog = hud.ui.on("new-messages", function messagesReceieved(e, newMessage) {
+      for (let message of messages) {
+        if (message.matched) {
+          continue;
+        }
+
+        if (newMessage.node.querySelector(".message-body").textContent == message.text) {
+          numMatched++;
+          message.matched = true;
+          info("Matched a message with text: " + message.text + ", still waiting for " + (messages.length - numMatched) + " messages");
+        }
+
+        if (numMatched === messages.length) {
+          hud.ui.off("new-messages", messagesReceieved);
+          resolve();
+          return;
+        }
+      }
+    });
+  });
+}
\ No newline at end of file
--- a/devtools/client/webconsole/webconsole.js
+++ b/devtools/client/webconsole/webconsole.js
@@ -3218,16 +3218,31 @@ WebConsoleConnectionProxy.prototype = {
 
     let msgs = ["PageError", "ConsoleAPI"];
     this.webConsoleClient.getCachedMessages(msgs, this._onCachedMessages);
 
     this.webConsoleFrame._onUpdateListeners();
   },
 
   /**
+   * Dispatch a message add on the new frontend and emit an event for tests.
+   */
+  dispatchMessageAdd: function(packet) {
+    this.webConsoleFrame.newConsoleOutput.dispatchMessageAdd(packet);
+
+    // Return the last message in the DOM as the message that was just dispatched. This may not
+    // always be true in the case of filtered messages, but it's close enough for our tests.
+    let messageNodes = this.webConsoleFrame.experimentalOutputNode.querySelectorAll(".message");
+    this.webConsoleFrame.emit("new-messages", {
+      response: packet,
+      node: messageNodes[messageNodes.length - 1],
+    });
+  },
+
+  /**
    * The "cachedMessages" response handler.
    *
    * @private
    * @param object response
    *        The JSON response object received from the server.
    */
   _onCachedMessages: function (response) {
     if (response.error) {
@@ -3244,17 +3259,17 @@ WebConsoleConnectionProxy.prototype = {
     }
 
     let messages =
       response.messages.concat(...this.webConsoleClient.getNetworkEvents());
     messages.sort((a, b) => a.timeStamp - b.timeStamp);
 
     if (this.webConsoleFrame.NEW_CONSOLE_OUTPUT_ENABLED) {
       for (let packet of messages) {
-        this.webConsoleFrame.newConsoleOutput.dispatchMessageAdd(packet);
+        this.dispatchMessageAdd(packet);
       }
     } else {
       this.webConsoleFrame.displayCachedMessages(messages);
       if (!this._hasNativeConsoleAPI) {
         this.webConsoleFrame.logWarningAboutReplacedAPI();
       }
     }
 
@@ -3270,17 +3285,17 @@ WebConsoleConnectionProxy.prototype = {
    * @param string type
    *        Message type.
    * @param object packet
    *        The message received from the server.
    */
   _onPageError: function (type, packet) {
     if (this.webConsoleFrame && packet.from == this._consoleActor) {
       if (this.webConsoleFrame.NEW_CONSOLE_OUTPUT_ENABLED) {
-        this.webConsoleFrame.newConsoleOutput.dispatchMessageAdd(packet);
+        this.dispatchMessageAdd(packet);
         return;
       }
       this.webConsoleFrame.handlePageError(packet.pageError);
     }
   },
 
   /**
    * The "logMessage" message type handler. We redirect any message to the UI
@@ -3306,17 +3321,17 @@ WebConsoleConnectionProxy.prototype = {
    * @param string type
    *        Message type.
    * @param object packet
    *        The message received from the server.
    */
   _onConsoleAPICall: function (type, packet) {
     if (this.webConsoleFrame && packet.from == this._consoleActor) {
       if (this.webConsoleFrame.NEW_CONSOLE_OUTPUT_ENABLED) {
-        this.webConsoleFrame.newConsoleOutput.dispatchMessageAdd(packet);
+        this.dispatchMessageAdd(packet);
       } else {
         this.webConsoleFrame.handleConsoleAPICall(packet.message);
       }
     }
   },
 
   /**
    * The "networkEvent" message type handler. We redirect any message to