Bug 1382968 - Make WebExtension debugging test better test console behavior. r=rpl draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 08 Aug 2017 22:30:56 +0200
changeset 643441 d53bd03fa7ba7f4f2d7eb49873a72fd9c2c51a11
parent 643440 40ac077a4f6d9303ce637ecee72963620a81e212
child 644352 ea0f590b996e3e9f4dee91c6b82eef2140bf6b89
push id73100
push userbmo:poirot.alex@gmail.com
push dateWed, 09 Aug 2017 18:59:59 +0000
reviewersrpl
bugs1382968
milestone57.0a1
Bug 1382968 - Make WebExtension debugging test better test console behavior. r=rpl MozReview-Commit-ID: Ch8DHw7nTws
devtools/client/aboutdebugging/test/browser_addons_debug_webextension.js
--- a/devtools/client/aboutdebugging/test/browser_addons_debug_webextension.js
+++ b/devtools/client/aboutdebugging/test/browser_addons_debug_webextension.js
@@ -18,56 +18,58 @@ const {
  * - when the debug button is clicked on a webextension, the opened toolbox
  *   has a working webconsole with the background page as default target;
  */
 add_task(function* testWebExtensionsToolboxWebConsole() {
   let {
     tab, document, debugBtn,
   } = yield setupTestAboutDebuggingWebExtension(ADDON_NAME, ADDON_MANIFEST_PATH);
 
-  // Wait for a notification sent by a script evaluated the test addon via
-  // the web console.
-  let onCustomMessage = new Promise(done => {
-    Services.obs.addObserver(function listener(message, topic) {
-      let apiMessage = message.wrappedJSObject;
-      if (apiMessage.addonId != ADDON_ID) {
-        return;
-      }
-      Services.obs.removeObserver(listener, "console-api-log-event");
-      done(apiMessage.arguments);
-    }, "console-api-log-event");
-  });
-
   // Be careful, this JS function is going to be executed in the addon toolbox,
   // which lives in another process. So do not try to use any scope variable!
   let env = Cc["@mozilla.org/process/environment;1"]
               .getService(Ci.nsIEnvironment);
   let testScript = function () {
     /* eslint-disable no-undef */
+    function findMessages(hud, text, selector = ".message") {
+      const messages = hud.ui.outputNode.querySelectorAll(selector);
+      const elements = Array.prototype.filter.call(
+        messages,
+        (el) => el.textContent.includes(text)
+      );
+      return elements;
+    }
+
+    async function waitFor(condition) {
+      while (!condition()) {
+        await new Promise(done => window.setTimeout(done, 1000));
+      }
+    }
+
     toolbox.selectTool("webconsole")
-      .then(console => {
-        let { jsterm } = console.hud;
-        return jsterm.execute("myWebExtensionAddonFunction()");
+      .then(async console => {
+        let { hud } = console;
+        let { jsterm } = hud;
+        let onMessage = waitFor(() => {
+          return findMessages(hud, "Background page function called").length > 0;
+        });
+        await jsterm.execute("myWebExtensionAddonFunction()");
+        await onMessage;
+        await toolbox.destroy();
       })
-      .then(() => toolbox.destroy());
+      .catch(e => dump("Exception from browser toolbox process: " + e + "\n"));
     /* eslint-enable no-undef */
   };
   env.set("MOZ_TOOLBOX_TEST_SCRIPT", "new " + testScript);
   registerCleanupFunction(() => {
     env.set("MOZ_TOOLBOX_TEST_SCRIPT", "");
   });
 
   let onToolboxClose = BrowserToolboxProcess.once("close");
 
   debugBtn.click();
 
-  let args = yield onCustomMessage;
-  ok(true, "Received console message from the background page function as expected");
-  is(args[0], "Background page function called", "Got the expected console message");
-  is(args[1] && args[1].name, ADDON_NAME,
-     "Got the expected manifest from WebExtension API");
-
   yield onToolboxClose;
   ok(true, "Addon toolbox closed");
 
   yield uninstallAddon({document, id: ADDON_ID, name: ADDON_NAME});
   yield closeAboutDebugging(tab);
 });