Bug 1403188 - Enable browser_console_webconsole_private_browsing.js in new console frontend; r=jdescottes. draft
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Tue, 13 Feb 2018 14:39:26 +0100
changeset 768966 7108df5408e126112dadc177a528642dcb4b8580
parent 768965 97160a734959af73cc97af0bf8d198e301ebedae
push id103011
push userbmo:nchevobbe@mozilla.com
push dateSat, 17 Mar 2018 13:35:31 +0000
reviewersjdescottes
bugs1403188
milestone61.0a1
Bug 1403188 - Enable browser_console_webconsole_private_browsing.js in new console frontend; r=jdescottes. MozReview-Commit-ID: EvFyvzpCUH7
devtools/client/webconsole/new-console-output/test/mochitest/browser.ini
devtools/client/webconsole/new-console-output/test/mochitest/browser_console_webconsole_private_browsing.js
devtools/client/webconsole/new-console-output/test/mochitest/head.js
--- a/devtools/client/webconsole/new-console-output/test/mochitest/browser.ini
+++ b/devtools/client/webconsole/new-console-output/test/mochitest/browser.ini
@@ -176,18 +176,16 @@ skip-if = true # Bug 1437843
 [browser_console_filters.js]
 [browser_console_nsiconsolemessage.js]
 [browser_console_open_or_focus.js]
 [browser_console_restore.js]
 [browser_console_webconsole_console_api_calls.js]
 [browser_console_webconsole_ctrlw_close_tab.js]
 [browser_console_webconsole_iframe_messages.js]
 [browser_console_webconsole_private_browsing.js]
-skip-if = true #	Bug 1403188
-# old console skip-if = e10s # Bug 1042253 - webconsole e10s tests
 [browser_jsterm_accessibility.js]
 [browser_jsterm_add_edited_input_to_history.js]
 [browser_jsterm_autocomplete_array_no_index.js]
 [browser_jsterm_autocomplete_cached_results.js]
 [browser_jsterm_autocomplete_crossdomain_iframe.js]
 [browser_jsterm_autocomplete_escape_key.js]
 [browser_jsterm_autocomplete_extraneous_closing_brackets.js]
 [browser_jsterm_autocomplete_helpers.js]
--- a/devtools/client/webconsole/new-console-output/test/mochitest/browser_console_webconsole_private_browsing.js
+++ b/devtools/client/webconsole/new-console-output/test/mochitest/browser_console_webconsole_private_browsing.js
@@ -5,191 +5,111 @@
 
 /* import-globals-from head.js */
 
 // Bug 874061: test for how the browser and web consoles display messages coming
 // from private windows. See bug for description of expected behavior.
 
 "use strict";
 
-function test() {
-  const TEST_URI = "data:text/html;charset=utf8,<p>hello world! bug 874061" +
-                   "<button onclick='console.log(\"foobar bug 874061\");" +
-                   "fooBazBaz.yummy()'>click</button>";
-  let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
-                            .getService(Ci.nsIConsoleAPIStorage);
-  let privateWindow, privateBrowser, privateTab, privateContent;
-  let hud, expectedMessages, nonPrivateMessage;
-
-  // This test is slightly more involved: it opens the web console twice,
-  // a new private window once, and the browser console twice. We can get
-  // a timeout with debug builds on slower machines.
-  requestLongerTimeout(2);
-  start();
+const NON_PRIVATE_MESSAGE = "This is not a private message";
+const PRIVATE_MESSAGE = "This is a private message";
+const PRIVATE_UNDEFINED_FN = "privateException";
+const PRIVATE_EXCEPTION = `${PRIVATE_UNDEFINED_FN} is not defined`;
 
-  function start() {
-    gBrowser.selectedTab =
-      BrowserTestUtils.addTab(gBrowser, "data:text/html;charset=utf8," +
-                                        "<p>hello world! I am not private!");
-    gBrowser.selectedBrowser.addEventListener("load", onLoadTab, true);
-  }
-
-  function onLoadTab() {
-    gBrowser.selectedBrowser.removeEventListener("load", onLoadTab, true);
-    info("onLoadTab()");
+const NON_PRIVATE_TEST_URI = "data:text/html;charset=utf8,Not private";
+const PRIVATE_TEST_URI = `data:text/html;charset=utf8,Test console in private windows
+  <script>
+    function logMessages() {
+      /* Wrap the exception so we don't throw in ContentTask. */
+      setTimeout(() => {
+        console.log("${PRIVATE_MESSAGE}");
+        ${PRIVATE_UNDEFINED_FN}();
+      }, 10);
+    }
+  </script>`;
 
-    // Make sure we have a clean state to start with.
-    Services.console.reset();
-    ConsoleAPIStorage.clearEvents();
-
-    // Add a non-private message to the browser console.
-    ContentTask.spawn(gBrowser.selectedBrowser, null, async function() {
-      content.console.log("bug874061-not-private");
-    });
-
-    nonPrivateMessage = {
-      name: "console message from a non-private window",
-      text: "bug874061-not-private",
-      category: CATEGORY_WEBDEV,
-      severity: SEVERITY_LOG,
-    };
+add_task(async function() {
+  await addTab(NON_PRIVATE_TEST_URI);
 
-    privateWindow = OpenBrowserWindow({ private: true });
-    ok(privateWindow, "new private window");
-    ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window's private");
-
-    whenDelayedStartupFinished(privateWindow, onPrivateWindowReady);
-  }
-
-  function onPrivateWindowReady() {
-    info("private browser window opened");
-    privateBrowser = privateWindow.gBrowser;
+  const privateWindow = await openNewBrowserWindow({ private: true });
+  ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private");
+  const privateBrowser = privateWindow.gBrowser;
+  privateBrowser.selectedTab = privateBrowser.addTab(PRIVATE_TEST_URI);
+  const privateTab = privateBrowser.selectedTab;
 
-    privateTab = privateBrowser.selectedTab = privateBrowser.addTab(TEST_URI);
-    privateBrowser.selectedBrowser.addEventListener("load", function onLoad() {
-      info("private tab opened");
-      privateBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
-      privateContent = privateBrowser.selectedBrowser.contentWindow;
-      ok(PrivateBrowsingUtils.isBrowserPrivate(privateBrowser.selectedBrowser),
-         "tab window is private");
-      openConsole(privateTab).then(consoleOpened);
-    }, true);
-  }
+  info("private tab opened");
+  ok(PrivateBrowsingUtils.isBrowserPrivate(privateBrowser.selectedBrowser),
+    "tab window is private");
+
+  let hud = await openConsole(privateTab);
+  ok(hud, "web console opened");
 
-  function addMessages() {
-    let button = privateContent.document.querySelector("button");
-    ok(button, "button in page");
-    EventUtils.synthesizeMouse(button, 2, 2, {}, privateContent);
-  }
+  const onLogMessage = waitForMessage(hud, PRIVATE_MESSAGE);
+  const onErrorMessage = waitForMessage(hud, PRIVATE_EXCEPTION, ".error");
+  logPrivateMessages(privateBrowser.selectedBrowser);
 
-  function consoleOpened(injectedHud) {
-    hud = injectedHud;
-    ok(hud, "web console opened");
+  await onLogMessage;
+  await onErrorMessage;
+  ok(true, "Messages are displayed as expected");
 
-    addMessages();
-    expectedMessages = [
-      {
-        name: "script error",
-        text: "fooBazBaz is not defined",
-        category: CATEGORY_JS,
-        severity: SEVERITY_ERROR,
-      },
-      {
-        name: "console message",
-        text: "foobar bug 874061",
-        category: CATEGORY_WEBDEV,
-        severity: SEVERITY_LOG,
-      },
-    ];
+  info("test cached messages");
+  await closeConsole(privateTab);
+  info("web console closed");
+  hud = await openConsole(privateTab);
+  ok(hud, "web console reopened");
+
+  await waitFor(() => findMessage(hud, PRIVATE_MESSAGE));
+  await waitFor(() => findMessage(hud, PRIVATE_EXCEPTION, ".message.error"));
+  ok(true, "Messages are still displayed after closing and reopening the console");
 
-    // Make sure messages are displayed in the web console as they happen, even
-    // if this is a private tab.
-    waitForMessages({
-      webconsole: hud,
-      messages: expectedMessages,
-    }).then(testCachedMessages);
-  }
+  info("Test browser console");
+  await closeConsole(privateTab);
+  info("web console closed");
+  hud = await HUDService.toggleBrowserConsole();
 
-  function testCachedMessages() {
-    info("testCachedMessages()");
-    closeConsole(privateTab).then(() => {
-      info("web console closed");
-      openConsole(privateTab).then(consoleReopened);
-    });
-  }
-
-  function consoleReopened(injectedHud) {
-    hud = injectedHud;
-    ok(hud, "web console reopened");
+  // Make sure that the cached messages from private tabs are not displayed in the
+  // browser console.
+  assertNoPrivateMessages(hud);
 
-    // Make sure that cached messages are displayed in the web console, even
-    // if this is a private tab.
-    waitForMessages({
-      webconsole: hud,
-      messages: expectedMessages,
-    }).then(testBrowserConsole);
-  }
+  // Add a non-private message to the console.
+  const onBrowserConsoleNonPrivateMessage = waitForMessage(hud, NON_PRIVATE_MESSAGE);
+  ContentTask.spawn(gBrowser.selectedBrowser, NON_PRIVATE_MESSAGE, function(msg) {
+    content.console.log(msg);
+  });
+  await onBrowserConsoleNonPrivateMessage;
 
-  function testBrowserConsole() {
-    info("testBrowserConsole()");
-    closeConsole(privateTab).then(() => {
-      info("web console closed");
-      HUDService.toggleBrowserConsole().then(onBrowserConsoleOpen);
-    });
-  }
+  const onBrowserConsolePrivateLogMessage = waitForMessage(hud, PRIVATE_MESSAGE);
+  const onBrowserConsolePrivateErrorMessage =
+    waitForMessage(hud, PRIVATE_EXCEPTION, ".error");
+  logPrivateMessages(privateBrowser.selectedBrowser);
 
-  // Make sure that the cached messages from private tabs are not displayed in
-  // the browser console.
-  function checkNoPrivateMessages() {
-    let text = hud.outputNode.textContent;
-    is(text.indexOf("fooBazBaz"), -1, "no exception displayed");
-    is(text.indexOf("bug 874061"), -1, "no console message displayed");
-  }
+  await onBrowserConsolePrivateLogMessage;
+  await onBrowserConsolePrivateErrorMessage;
+  ok(true, "Messages are displayed as expected");
 
-  function onBrowserConsoleOpen(injectedHud) {
-    hud = injectedHud;
-    ok(hud, "browser console opened");
-
-    checkNoPrivateMessages();
-    addMessages();
-    expectedMessages.push(nonPrivateMessage);
+  info("close the private window and check if private messages are removed");
+  const onPrivateMessagesCleared = hud.jsterm.once("private-messages-cleared");
+  privateWindow.BrowserTryToCloseWindow();
+  await onPrivateMessagesCleared;
 
-    // Make sure that live messages are displayed in the browser console, even
-    // from private tabs.
-    waitForMessages({
-      webconsole: hud,
-      messages: expectedMessages,
-    }).then(testPrivateWindowClose);
-  }
+  ok(findMessage(hud, NON_PRIVATE_MESSAGE),
+    "non-private messages are still shown after private window closed");
+  assertNoPrivateMessages(hud);
 
-  function testPrivateWindowClose() {
-    info("close the private window and check if private messages are removed");
-    hud.jsterm.once("private-messages-cleared", () => {
-      isnot(hud.outputNode.textContent.indexOf("bug874061-not-private"), -1,
-            "non-private messages are still shown after private window closed");
-      checkNoPrivateMessages();
+  info("close the browser console");
+  await HUDService.toggleBrowserConsole();
 
-      info("close the browser console");
-      HUDService.toggleBrowserConsole().then(() => {
-        info("reopen the browser console");
-        executeSoon(() =>
-          HUDService.toggleBrowserConsole().then(onBrowserConsoleReopen));
-      });
-    });
-    privateWindow.BrowserTryToCloseWindow();
-  }
+  info("reopen the browser console");
+  hud = await HUDService.toggleBrowserConsole();
+  ok(hud, "browser console reopened");
+
+  assertNoPrivateMessages(hud);
+});
 
-  function onBrowserConsoleReopen(injectedHud) {
-    hud = injectedHud;
-    ok(hud, "browser console reopened");
+function logPrivateMessages(browser) {
+  ContentTask.spawn(browser, null, () => content.wrappedJSObject.logMessages());
+}
 
-    // Make sure that the non-private message is still shown after reopen.
-    waitForMessages({
-      webconsole: hud,
-      messages: [nonPrivateMessage],
-    }).then(() => {
-      // Make sure that no private message is displayed after closing the
-      // private window and reopening the Browser Console.
-      checkNoPrivateMessages();
-      executeSoon(finishTest);
-    });
-  }
+function assertNoPrivateMessages(hud) {
+  is(findMessage(hud, PRIVATE_MESSAGE), null, "no console message displayed");
+  is(findMessage(hud, PRIVATE_EXCEPTION), null, "no exception displayed");
 }
--- a/devtools/client/webconsole/new-console-output/test/mochitest/head.js
+++ b/devtools/client/webconsole/new-console-output/test/mochitest/head.js
@@ -520,21 +520,22 @@ function simulateLinkClick(element, clic
   });
   return Promise.race([onOpenLink, onTimeout]);
 }
 
 /**
  * Open a new browser window and return a promise that resolves when the new window has
  * fired the "browser-delayed-startup-finished" event.
  *
+ * @param {Object} options: An options object that will be passed to OpenBrowserWindow.
  * @returns Promise
  *          A Promise that resolves when the window is ready.
  */
-function openNewBrowserWindow() {
-  let win = OpenBrowserWindow();
+function openNewBrowserWindow(options) {
+  let win = OpenBrowserWindow(options);
   return new Promise(resolve => {
     Services.obs.addObserver(function observer(subject, topic) {
       if (win == subject) {
         Services.obs.removeObserver(observer, topic);
         resolve(win);
       }
     }, "browser-delayed-startup-finished");
   });