--- 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");
}