michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Bug 874061: test for how the browser and web consoles display messages coming michael@0: // from private windows. See bug for description of expected behavior. michael@0: michael@0: function test() michael@0: { michael@0: const TEST_URI = "data:text/html;charset=utf8,
hello world! bug 874061" + michael@0: ""; michael@0: let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] michael@0: .getService(Ci.nsIConsoleAPIStorage); michael@0: let privateWindow, privateBrowser, privateTab, privateContent; michael@0: let hud, expectedMessages, nonPrivateMessage; michael@0: michael@0: // This test is slightly more involved: it opens the web console twice, michael@0: // a new private window once, and the browser console twice. We can get michael@0: // a timeout with debug builds on slower machines. michael@0: requestLongerTimeout(2); michael@0: start(); michael@0: michael@0: function start() michael@0: { michael@0: gBrowser.selectedTab = gBrowser.addTab("data:text/html;charset=utf8," + michael@0: "
hello world! I am not private!"); michael@0: gBrowser.selectedBrowser.addEventListener("load", onLoadTab, true); michael@0: } michael@0: michael@0: function onLoadTab() michael@0: { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onLoadTab, true); michael@0: info("onLoadTab()"); michael@0: michael@0: // Make sure we have a clean state to start with. michael@0: Services.console.reset(); michael@0: ConsoleAPIStorage.clearEvents(); michael@0: michael@0: // Add a non-private message to the browser console. michael@0: content.console.log("bug874061-not-private"); michael@0: michael@0: nonPrivateMessage = { michael@0: name: "console message from a non-private window", michael@0: text: "bug874061-not-private", michael@0: category: CATEGORY_WEBDEV, michael@0: severity: SEVERITY_LOG, michael@0: }; michael@0: michael@0: privateWindow = OpenBrowserWindow({ private: true }); michael@0: ok(privateWindow, "new private window"); michael@0: ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private"); michael@0: michael@0: whenDelayedStartupFinished(privateWindow, onPrivateWindowReady); michael@0: } michael@0: michael@0: function onPrivateWindowReady() michael@0: { michael@0: info("private browser window opened"); michael@0: privateBrowser = privateWindow.gBrowser; michael@0: michael@0: privateTab = privateBrowser.selectedTab = privateBrowser.addTab(TEST_URI); michael@0: privateBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: info("private tab opened"); michael@0: privateBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: privateContent = privateBrowser.selectedBrowser.contentWindow; michael@0: ok(PrivateBrowsingUtils.isWindowPrivate(privateContent), "tab window is private"); michael@0: openConsole(privateTab, consoleOpened); michael@0: }, true); michael@0: } michael@0: michael@0: function addMessages() michael@0: { michael@0: let button = privateContent.document.querySelector("button"); michael@0: ok(button, "button in page"); michael@0: EventUtils.synthesizeMouse(button, 2, 2, {}, privateContent); michael@0: } michael@0: michael@0: function consoleOpened(aHud) michael@0: { michael@0: hud = aHud; michael@0: ok(hud, "web console opened"); michael@0: michael@0: addMessages(); michael@0: expectedMessages = [ michael@0: { michael@0: name: "script error", michael@0: text: "fooBazBaz is not defined", michael@0: category: CATEGORY_JS, michael@0: severity: SEVERITY_ERROR, michael@0: }, michael@0: { michael@0: name: "console message", michael@0: text: "foobar bug 874061", michael@0: category: CATEGORY_WEBDEV, michael@0: severity: SEVERITY_LOG, michael@0: }, michael@0: ]; michael@0: michael@0: // Make sure messages are displayed in the web console as they happen, even michael@0: // if this is a private tab. michael@0: waitForMessages({ michael@0: webconsole: hud, michael@0: messages: expectedMessages, michael@0: }).then(testCachedMessages); michael@0: } michael@0: michael@0: function testCachedMessages() michael@0: { michael@0: info("testCachedMessages()"); michael@0: closeConsole(privateTab, () => { michael@0: info("web console closed"); michael@0: openConsole(privateTab, consoleReopened); michael@0: }); michael@0: } michael@0: michael@0: function consoleReopened(aHud) michael@0: { michael@0: hud = aHud; michael@0: ok(hud, "web console reopened"); michael@0: michael@0: // Make sure that cached messages are displayed in the web console, even michael@0: // if this is a private tab. michael@0: waitForMessages({ michael@0: webconsole: hud, michael@0: messages: expectedMessages, michael@0: }).then(testBrowserConsole); michael@0: } michael@0: michael@0: function testBrowserConsole() michael@0: { michael@0: info("testBrowserConsole()"); michael@0: closeConsole(privateTab, () => { michael@0: info("web console closed"); michael@0: privateWindow.HUDService.toggleBrowserConsole().then(onBrowserConsoleOpen); michael@0: }); michael@0: } michael@0: michael@0: // Make sure that the cached messages from private tabs are not displayed in michael@0: // the browser console. michael@0: function checkNoPrivateMessages() michael@0: { michael@0: let text = hud.outputNode.textContent; michael@0: is(text.indexOf("fooBazBaz"), -1, "no exception displayed"); michael@0: is(text.indexOf("bug 874061"), -1, "no console message displayed"); michael@0: } michael@0: michael@0: function onBrowserConsoleOpen(aHud) michael@0: { michael@0: hud = aHud; michael@0: ok(hud, "browser console opened"); michael@0: michael@0: checkNoPrivateMessages(); michael@0: addMessages(); michael@0: expectedMessages.push(nonPrivateMessage); michael@0: michael@0: // Make sure that live messages are displayed in the browser console, even michael@0: // from private tabs. michael@0: waitForMessages({ michael@0: webconsole: hud, michael@0: messages: expectedMessages, michael@0: }).then(testPrivateWindowClose); michael@0: } michael@0: michael@0: function testPrivateWindowClose() michael@0: { michael@0: info("close the private window and check if the private messages are removed"); michael@0: hud.jsterm.once("private-messages-cleared", () => { michael@0: isnot(hud.outputNode.textContent.indexOf("bug874061-not-private"), -1, michael@0: "non-private messages are still shown after private window closed"); michael@0: checkNoPrivateMessages(); michael@0: michael@0: info("close the browser console"); michael@0: privateWindow.HUDService.toggleBrowserConsole().then(() => { michael@0: info("reopen the browser console"); michael@0: executeSoon(() => michael@0: HUDService.toggleBrowserConsole().then(onBrowserConsoleReopen)); michael@0: }); michael@0: }); michael@0: privateWindow.BrowserTryToCloseWindow(); michael@0: } michael@0: michael@0: function onBrowserConsoleReopen(aHud) michael@0: { michael@0: hud = aHud; michael@0: ok(hud, "browser console reopened"); michael@0: michael@0: // Make sure that the non-private message is still shown after reopen. michael@0: waitForMessages({ michael@0: webconsole: hud, michael@0: messages: [nonPrivateMessage], michael@0: }).then(() => { michael@0: // Make sure that no private message is displayed after closing the private michael@0: // window and reopening the Browser Console. michael@0: checkNoPrivateMessages(); michael@0: executeSoon(finishTest); michael@0: }); michael@0: } michael@0: }