1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/webconsole/test/browser_console_private_browsing.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 + 1.9 +// Bug 874061: test for how the browser and web consoles display messages coming 1.10 +// from private windows. See bug for description of expected behavior. 1.11 + 1.12 +function test() 1.13 +{ 1.14 + const TEST_URI = "data:text/html;charset=utf8,<p>hello world! bug 874061" + 1.15 + "<button onclick='console.log(\"foobar bug 874061\");" + 1.16 + "fooBazBaz.yummy()'>click</button>"; 1.17 + let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] 1.18 + .getService(Ci.nsIConsoleAPIStorage); 1.19 + let privateWindow, privateBrowser, privateTab, privateContent; 1.20 + let hud, expectedMessages, nonPrivateMessage; 1.21 + 1.22 + // This test is slightly more involved: it opens the web console twice, 1.23 + // a new private window once, and the browser console twice. We can get 1.24 + // a timeout with debug builds on slower machines. 1.25 + requestLongerTimeout(2); 1.26 + start(); 1.27 + 1.28 + function start() 1.29 + { 1.30 + gBrowser.selectedTab = gBrowser.addTab("data:text/html;charset=utf8," + 1.31 + "<p>hello world! I am not private!"); 1.32 + gBrowser.selectedBrowser.addEventListener("load", onLoadTab, true); 1.33 + } 1.34 + 1.35 + function onLoadTab() 1.36 + { 1.37 + gBrowser.selectedBrowser.removeEventListener("load", onLoadTab, true); 1.38 + info("onLoadTab()"); 1.39 + 1.40 + // Make sure we have a clean state to start with. 1.41 + Services.console.reset(); 1.42 + ConsoleAPIStorage.clearEvents(); 1.43 + 1.44 + // Add a non-private message to the browser console. 1.45 + content.console.log("bug874061-not-private"); 1.46 + 1.47 + nonPrivateMessage = { 1.48 + name: "console message from a non-private window", 1.49 + text: "bug874061-not-private", 1.50 + category: CATEGORY_WEBDEV, 1.51 + severity: SEVERITY_LOG, 1.52 + }; 1.53 + 1.54 + privateWindow = OpenBrowserWindow({ private: true }); 1.55 + ok(privateWindow, "new private window"); 1.56 + ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private"); 1.57 + 1.58 + whenDelayedStartupFinished(privateWindow, onPrivateWindowReady); 1.59 + } 1.60 + 1.61 + function onPrivateWindowReady() 1.62 + { 1.63 + info("private browser window opened"); 1.64 + privateBrowser = privateWindow.gBrowser; 1.65 + 1.66 + privateTab = privateBrowser.selectedTab = privateBrowser.addTab(TEST_URI); 1.67 + privateBrowser.selectedBrowser.addEventListener("load", function onLoad() { 1.68 + info("private tab opened"); 1.69 + privateBrowser.selectedBrowser.removeEventListener("load", onLoad, true); 1.70 + privateContent = privateBrowser.selectedBrowser.contentWindow; 1.71 + ok(PrivateBrowsingUtils.isWindowPrivate(privateContent), "tab window is private"); 1.72 + openConsole(privateTab, consoleOpened); 1.73 + }, true); 1.74 + } 1.75 + 1.76 + function addMessages() 1.77 + { 1.78 + let button = privateContent.document.querySelector("button"); 1.79 + ok(button, "button in page"); 1.80 + EventUtils.synthesizeMouse(button, 2, 2, {}, privateContent); 1.81 + } 1.82 + 1.83 + function consoleOpened(aHud) 1.84 + { 1.85 + hud = aHud; 1.86 + ok(hud, "web console opened"); 1.87 + 1.88 + addMessages(); 1.89 + expectedMessages = [ 1.90 + { 1.91 + name: "script error", 1.92 + text: "fooBazBaz is not defined", 1.93 + category: CATEGORY_JS, 1.94 + severity: SEVERITY_ERROR, 1.95 + }, 1.96 + { 1.97 + name: "console message", 1.98 + text: "foobar bug 874061", 1.99 + category: CATEGORY_WEBDEV, 1.100 + severity: SEVERITY_LOG, 1.101 + }, 1.102 + ]; 1.103 + 1.104 + // Make sure messages are displayed in the web console as they happen, even 1.105 + // if this is a private tab. 1.106 + waitForMessages({ 1.107 + webconsole: hud, 1.108 + messages: expectedMessages, 1.109 + }).then(testCachedMessages); 1.110 + } 1.111 + 1.112 + function testCachedMessages() 1.113 + { 1.114 + info("testCachedMessages()"); 1.115 + closeConsole(privateTab, () => { 1.116 + info("web console closed"); 1.117 + openConsole(privateTab, consoleReopened); 1.118 + }); 1.119 + } 1.120 + 1.121 + function consoleReopened(aHud) 1.122 + { 1.123 + hud = aHud; 1.124 + ok(hud, "web console reopened"); 1.125 + 1.126 + // Make sure that cached messages are displayed in the web console, even 1.127 + // if this is a private tab. 1.128 + waitForMessages({ 1.129 + webconsole: hud, 1.130 + messages: expectedMessages, 1.131 + }).then(testBrowserConsole); 1.132 + } 1.133 + 1.134 + function testBrowserConsole() 1.135 + { 1.136 + info("testBrowserConsole()"); 1.137 + closeConsole(privateTab, () => { 1.138 + info("web console closed"); 1.139 + privateWindow.HUDService.toggleBrowserConsole().then(onBrowserConsoleOpen); 1.140 + }); 1.141 + } 1.142 + 1.143 + // Make sure that the cached messages from private tabs are not displayed in 1.144 + // the browser console. 1.145 + function checkNoPrivateMessages() 1.146 + { 1.147 + let text = hud.outputNode.textContent; 1.148 + is(text.indexOf("fooBazBaz"), -1, "no exception displayed"); 1.149 + is(text.indexOf("bug 874061"), -1, "no console message displayed"); 1.150 + } 1.151 + 1.152 + function onBrowserConsoleOpen(aHud) 1.153 + { 1.154 + hud = aHud; 1.155 + ok(hud, "browser console opened"); 1.156 + 1.157 + checkNoPrivateMessages(); 1.158 + addMessages(); 1.159 + expectedMessages.push(nonPrivateMessage); 1.160 + 1.161 + // Make sure that live messages are displayed in the browser console, even 1.162 + // from private tabs. 1.163 + waitForMessages({ 1.164 + webconsole: hud, 1.165 + messages: expectedMessages, 1.166 + }).then(testPrivateWindowClose); 1.167 + } 1.168 + 1.169 + function testPrivateWindowClose() 1.170 + { 1.171 + info("close the private window and check if the private messages are removed"); 1.172 + hud.jsterm.once("private-messages-cleared", () => { 1.173 + isnot(hud.outputNode.textContent.indexOf("bug874061-not-private"), -1, 1.174 + "non-private messages are still shown after private window closed"); 1.175 + checkNoPrivateMessages(); 1.176 + 1.177 + info("close the browser console"); 1.178 + privateWindow.HUDService.toggleBrowserConsole().then(() => { 1.179 + info("reopen the browser console"); 1.180 + executeSoon(() => 1.181 + HUDService.toggleBrowserConsole().then(onBrowserConsoleReopen)); 1.182 + }); 1.183 + }); 1.184 + privateWindow.BrowserTryToCloseWindow(); 1.185 + } 1.186 + 1.187 + function onBrowserConsoleReopen(aHud) 1.188 + { 1.189 + hud = aHud; 1.190 + ok(hud, "browser console reopened"); 1.191 + 1.192 + // Make sure that the non-private message is still shown after reopen. 1.193 + waitForMessages({ 1.194 + webconsole: hud, 1.195 + messages: [nonPrivateMessage], 1.196 + }).then(() => { 1.197 + // Make sure that no private message is displayed after closing the private 1.198 + // window and reopening the Browser Console. 1.199 + checkNoPrivateMessages(); 1.200 + executeSoon(finishTest); 1.201 + }); 1.202 + } 1.203 +}