Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | // Bug 874061: test for how the browser and web consoles display messages coming |
michael@0 | 7 | // from private windows. See bug for description of expected behavior. |
michael@0 | 8 | |
michael@0 | 9 | function test() |
michael@0 | 10 | { |
michael@0 | 11 | const TEST_URI = "data:text/html;charset=utf8,<p>hello world! bug 874061" + |
michael@0 | 12 | "<button onclick='console.log(\"foobar bug 874061\");" + |
michael@0 | 13 | "fooBazBaz.yummy()'>click</button>"; |
michael@0 | 14 | let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"] |
michael@0 | 15 | .getService(Ci.nsIConsoleAPIStorage); |
michael@0 | 16 | let privateWindow, privateBrowser, privateTab, privateContent; |
michael@0 | 17 | let hud, expectedMessages, nonPrivateMessage; |
michael@0 | 18 | |
michael@0 | 19 | // This test is slightly more involved: it opens the web console twice, |
michael@0 | 20 | // a new private window once, and the browser console twice. We can get |
michael@0 | 21 | // a timeout with debug builds on slower machines. |
michael@0 | 22 | requestLongerTimeout(2); |
michael@0 | 23 | start(); |
michael@0 | 24 | |
michael@0 | 25 | function start() |
michael@0 | 26 | { |
michael@0 | 27 | gBrowser.selectedTab = gBrowser.addTab("data:text/html;charset=utf8," + |
michael@0 | 28 | "<p>hello world! I am not private!"); |
michael@0 | 29 | gBrowser.selectedBrowser.addEventListener("load", onLoadTab, true); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | function onLoadTab() |
michael@0 | 33 | { |
michael@0 | 34 | gBrowser.selectedBrowser.removeEventListener("load", onLoadTab, true); |
michael@0 | 35 | info("onLoadTab()"); |
michael@0 | 36 | |
michael@0 | 37 | // Make sure we have a clean state to start with. |
michael@0 | 38 | Services.console.reset(); |
michael@0 | 39 | ConsoleAPIStorage.clearEvents(); |
michael@0 | 40 | |
michael@0 | 41 | // Add a non-private message to the browser console. |
michael@0 | 42 | content.console.log("bug874061-not-private"); |
michael@0 | 43 | |
michael@0 | 44 | nonPrivateMessage = { |
michael@0 | 45 | name: "console message from a non-private window", |
michael@0 | 46 | text: "bug874061-not-private", |
michael@0 | 47 | category: CATEGORY_WEBDEV, |
michael@0 | 48 | severity: SEVERITY_LOG, |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | privateWindow = OpenBrowserWindow({ private: true }); |
michael@0 | 52 | ok(privateWindow, "new private window"); |
michael@0 | 53 | ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private"); |
michael@0 | 54 | |
michael@0 | 55 | whenDelayedStartupFinished(privateWindow, onPrivateWindowReady); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | function onPrivateWindowReady() |
michael@0 | 59 | { |
michael@0 | 60 | info("private browser window opened"); |
michael@0 | 61 | privateBrowser = privateWindow.gBrowser; |
michael@0 | 62 | |
michael@0 | 63 | privateTab = privateBrowser.selectedTab = privateBrowser.addTab(TEST_URI); |
michael@0 | 64 | privateBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
michael@0 | 65 | info("private tab opened"); |
michael@0 | 66 | privateBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
michael@0 | 67 | privateContent = privateBrowser.selectedBrowser.contentWindow; |
michael@0 | 68 | ok(PrivateBrowsingUtils.isWindowPrivate(privateContent), "tab window is private"); |
michael@0 | 69 | openConsole(privateTab, consoleOpened); |
michael@0 | 70 | }, true); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | function addMessages() |
michael@0 | 74 | { |
michael@0 | 75 | let button = privateContent.document.querySelector("button"); |
michael@0 | 76 | ok(button, "button in page"); |
michael@0 | 77 | EventUtils.synthesizeMouse(button, 2, 2, {}, privateContent); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | function consoleOpened(aHud) |
michael@0 | 81 | { |
michael@0 | 82 | hud = aHud; |
michael@0 | 83 | ok(hud, "web console opened"); |
michael@0 | 84 | |
michael@0 | 85 | addMessages(); |
michael@0 | 86 | expectedMessages = [ |
michael@0 | 87 | { |
michael@0 | 88 | name: "script error", |
michael@0 | 89 | text: "fooBazBaz is not defined", |
michael@0 | 90 | category: CATEGORY_JS, |
michael@0 | 91 | severity: SEVERITY_ERROR, |
michael@0 | 92 | }, |
michael@0 | 93 | { |
michael@0 | 94 | name: "console message", |
michael@0 | 95 | text: "foobar bug 874061", |
michael@0 | 96 | category: CATEGORY_WEBDEV, |
michael@0 | 97 | severity: SEVERITY_LOG, |
michael@0 | 98 | }, |
michael@0 | 99 | ]; |
michael@0 | 100 | |
michael@0 | 101 | // Make sure messages are displayed in the web console as they happen, even |
michael@0 | 102 | // if this is a private tab. |
michael@0 | 103 | waitForMessages({ |
michael@0 | 104 | webconsole: hud, |
michael@0 | 105 | messages: expectedMessages, |
michael@0 | 106 | }).then(testCachedMessages); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | function testCachedMessages() |
michael@0 | 110 | { |
michael@0 | 111 | info("testCachedMessages()"); |
michael@0 | 112 | closeConsole(privateTab, () => { |
michael@0 | 113 | info("web console closed"); |
michael@0 | 114 | openConsole(privateTab, consoleReopened); |
michael@0 | 115 | }); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | function consoleReopened(aHud) |
michael@0 | 119 | { |
michael@0 | 120 | hud = aHud; |
michael@0 | 121 | ok(hud, "web console reopened"); |
michael@0 | 122 | |
michael@0 | 123 | // Make sure that cached messages are displayed in the web console, even |
michael@0 | 124 | // if this is a private tab. |
michael@0 | 125 | waitForMessages({ |
michael@0 | 126 | webconsole: hud, |
michael@0 | 127 | messages: expectedMessages, |
michael@0 | 128 | }).then(testBrowserConsole); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | function testBrowserConsole() |
michael@0 | 132 | { |
michael@0 | 133 | info("testBrowserConsole()"); |
michael@0 | 134 | closeConsole(privateTab, () => { |
michael@0 | 135 | info("web console closed"); |
michael@0 | 136 | privateWindow.HUDService.toggleBrowserConsole().then(onBrowserConsoleOpen); |
michael@0 | 137 | }); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | // Make sure that the cached messages from private tabs are not displayed in |
michael@0 | 141 | // the browser console. |
michael@0 | 142 | function checkNoPrivateMessages() |
michael@0 | 143 | { |
michael@0 | 144 | let text = hud.outputNode.textContent; |
michael@0 | 145 | is(text.indexOf("fooBazBaz"), -1, "no exception displayed"); |
michael@0 | 146 | is(text.indexOf("bug 874061"), -1, "no console message displayed"); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | function onBrowserConsoleOpen(aHud) |
michael@0 | 150 | { |
michael@0 | 151 | hud = aHud; |
michael@0 | 152 | ok(hud, "browser console opened"); |
michael@0 | 153 | |
michael@0 | 154 | checkNoPrivateMessages(); |
michael@0 | 155 | addMessages(); |
michael@0 | 156 | expectedMessages.push(nonPrivateMessage); |
michael@0 | 157 | |
michael@0 | 158 | // Make sure that live messages are displayed in the browser console, even |
michael@0 | 159 | // from private tabs. |
michael@0 | 160 | waitForMessages({ |
michael@0 | 161 | webconsole: hud, |
michael@0 | 162 | messages: expectedMessages, |
michael@0 | 163 | }).then(testPrivateWindowClose); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | function testPrivateWindowClose() |
michael@0 | 167 | { |
michael@0 | 168 | info("close the private window and check if the private messages are removed"); |
michael@0 | 169 | hud.jsterm.once("private-messages-cleared", () => { |
michael@0 | 170 | isnot(hud.outputNode.textContent.indexOf("bug874061-not-private"), -1, |
michael@0 | 171 | "non-private messages are still shown after private window closed"); |
michael@0 | 172 | checkNoPrivateMessages(); |
michael@0 | 173 | |
michael@0 | 174 | info("close the browser console"); |
michael@0 | 175 | privateWindow.HUDService.toggleBrowserConsole().then(() => { |
michael@0 | 176 | info("reopen the browser console"); |
michael@0 | 177 | executeSoon(() => |
michael@0 | 178 | HUDService.toggleBrowserConsole().then(onBrowserConsoleReopen)); |
michael@0 | 179 | }); |
michael@0 | 180 | }); |
michael@0 | 181 | privateWindow.BrowserTryToCloseWindow(); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | function onBrowserConsoleReopen(aHud) |
michael@0 | 185 | { |
michael@0 | 186 | hud = aHud; |
michael@0 | 187 | ok(hud, "browser console reopened"); |
michael@0 | 188 | |
michael@0 | 189 | // Make sure that the non-private message is still shown after reopen. |
michael@0 | 190 | waitForMessages({ |
michael@0 | 191 | webconsole: hud, |
michael@0 | 192 | messages: [nonPrivateMessage], |
michael@0 | 193 | }).then(() => { |
michael@0 | 194 | // Make sure that no private message is displayed after closing the private |
michael@0 | 195 | // window and reopening the Browser Console. |
michael@0 | 196 | checkNoPrivateMessages(); |
michael@0 | 197 | executeSoon(finishTest); |
michael@0 | 198 | }); |
michael@0 | 199 | } |
michael@0 | 200 | } |