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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // Tests that the developer toolbar errors count works properly. |
michael@0 | 5 | |
michael@0 | 6 | function test() { |
michael@0 | 7 | const TEST_URI = "http://example.com/browser/browser/devtools/shared/test/" + |
michael@0 | 8 | "browser_toolbar_webconsole_errors_count.html"; |
michael@0 | 9 | |
michael@0 | 10 | let gDevTools = Cu.import("resource:///modules/devtools/gDevTools.jsm", |
michael@0 | 11 | {}).gDevTools; |
michael@0 | 12 | |
michael@0 | 13 | let webconsole = document.getElementById("developer-toolbar-toolbox-button"); |
michael@0 | 14 | let tab1, tab2; |
michael@0 | 15 | |
michael@0 | 16 | Services.prefs.setBoolPref("javascript.options.strict", true); |
michael@0 | 17 | |
michael@0 | 18 | registerCleanupFunction(() => { |
michael@0 | 19 | Services.prefs.clearUserPref("javascript.options.strict"); |
michael@0 | 20 | }); |
michael@0 | 21 | |
michael@0 | 22 | ignoreAllUncaughtExceptions(); |
michael@0 | 23 | addTab(TEST_URI, openToolbar); |
michael@0 | 24 | |
michael@0 | 25 | function openToolbar(browser, tab) { |
michael@0 | 26 | tab1 = tab; |
michael@0 | 27 | ignoreAllUncaughtExceptions(false); |
michael@0 | 28 | |
michael@0 | 29 | expectUncaughtException(); |
michael@0 | 30 | |
michael@0 | 31 | if (!DeveloperToolbar.visible) { |
michael@0 | 32 | DeveloperToolbar.show(true, onOpenToolbar); |
michael@0 | 33 | } |
michael@0 | 34 | else { |
michael@0 | 35 | onOpenToolbar(); |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | function onOpenToolbar() { |
michael@0 | 40 | ok(DeveloperToolbar.visible, "DeveloperToolbar is visible"); |
michael@0 | 41 | |
michael@0 | 42 | waitForButtonUpdate({ |
michael@0 | 43 | name: "web console button shows page errors", |
michael@0 | 44 | errors: 3, |
michael@0 | 45 | warnings: 0, |
michael@0 | 46 | callback: addErrors, |
michael@0 | 47 | }); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | function addErrors() { |
michael@0 | 51 | expectUncaughtException(); |
michael@0 | 52 | |
michael@0 | 53 | waitForFocus(function() { |
michael@0 | 54 | let button = content.document.querySelector("button"); |
michael@0 | 55 | executeSoon(function() { |
michael@0 | 56 | EventUtils.synthesizeMouse(button, 3, 2, {}, content); |
michael@0 | 57 | }); |
michael@0 | 58 | }, content); |
michael@0 | 59 | |
michael@0 | 60 | waitForButtonUpdate({ |
michael@0 | 61 | name: "button shows one more error after click in page", |
michael@0 | 62 | errors: 4, |
michael@0 | 63 | warnings: 1, |
michael@0 | 64 | callback: () => { |
michael@0 | 65 | ignoreAllUncaughtExceptions(); |
michael@0 | 66 | addTab(TEST_URI, onOpenSecondTab); |
michael@0 | 67 | }, |
michael@0 | 68 | }); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function onOpenSecondTab(browser, tab) { |
michael@0 | 72 | tab2 = tab; |
michael@0 | 73 | |
michael@0 | 74 | ignoreAllUncaughtExceptions(false); |
michael@0 | 75 | expectUncaughtException(); |
michael@0 | 76 | |
michael@0 | 77 | waitForButtonUpdate({ |
michael@0 | 78 | name: "button shows correct number of errors after new tab is open", |
michael@0 | 79 | errors: 3, |
michael@0 | 80 | warnings: 0, |
michael@0 | 81 | callback: switchToTab1, |
michael@0 | 82 | }); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | function switchToTab1() { |
michael@0 | 86 | gBrowser.selectedTab = tab1; |
michael@0 | 87 | waitForButtonUpdate({ |
michael@0 | 88 | name: "button shows the page errors from tab 1", |
michael@0 | 89 | errors: 4, |
michael@0 | 90 | warnings: 1, |
michael@0 | 91 | callback: openWebConsole.bind(null, tab1, onWebConsoleOpen), |
michael@0 | 92 | }); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function onWebConsoleOpen(hud) { |
michael@0 | 96 | dump("lolz!!\n"); |
michael@0 | 97 | waitForValue({ |
michael@0 | 98 | name: "web console shows the page errors", |
michael@0 | 99 | validator: function() { |
michael@0 | 100 | return hud.outputNode.querySelectorAll(".message[category=exception][severity=error]").length; |
michael@0 | 101 | }, |
michael@0 | 102 | value: 4, |
michael@0 | 103 | success: checkConsoleOutput.bind(null, hud), |
michael@0 | 104 | failure: () => { |
michael@0 | 105 | finish(); |
michael@0 | 106 | }, |
michael@0 | 107 | }); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | function checkConsoleOutput(hud) { |
michael@0 | 111 | let msgs = ["foobarBug762996a", "foobarBug762996b", "foobarBug762996load", |
michael@0 | 112 | "foobarBug762996click", "foobarBug762996consoleLog", |
michael@0 | 113 | "foobarBug762996css", "fooBug788445"]; |
michael@0 | 114 | msgs.forEach(function(msg) { |
michael@0 | 115 | isnot(hud.outputNode.textContent.indexOf(msg), -1, |
michael@0 | 116 | msg + " found in the Web Console output"); |
michael@0 | 117 | }); |
michael@0 | 118 | |
michael@0 | 119 | hud.jsterm.clearOutput(); |
michael@0 | 120 | |
michael@0 | 121 | is(hud.outputNode.textContent.indexOf("foobarBug762996color"), -1, |
michael@0 | 122 | "clearOutput() worked"); |
michael@0 | 123 | |
michael@0 | 124 | expectUncaughtException(); |
michael@0 | 125 | let button = content.document.querySelector("button"); |
michael@0 | 126 | EventUtils.synthesizeMouse(button, 2, 2, {}, content); |
michael@0 | 127 | |
michael@0 | 128 | waitForButtonUpdate({ |
michael@0 | 129 | name: "button shows one more error after another click in page", |
michael@0 | 130 | errors: 5, |
michael@0 | 131 | warnings: 1, // warnings are not repeated by the js engine |
michael@0 | 132 | callback: () => waitForValue(waitForNewError), |
michael@0 | 133 | }); |
michael@0 | 134 | |
michael@0 | 135 | let waitForNewError = { |
michael@0 | 136 | name: "the Web Console displays the new error", |
michael@0 | 137 | validator: function() { |
michael@0 | 138 | return hud.outputNode.textContent.indexOf("foobarBug762996click") > -1; |
michael@0 | 139 | }, |
michael@0 | 140 | success: doClearConsoleButton.bind(null, hud), |
michael@0 | 141 | failure: finish, |
michael@0 | 142 | }; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | function doClearConsoleButton(hud) { |
michael@0 | 146 | let clearButton = hud.ui.rootElement |
michael@0 | 147 | .querySelector(".webconsole-clear-console-button"); |
michael@0 | 148 | EventUtils.synthesizeMouse(clearButton, 2, 2, {}, hud.iframeWindow); |
michael@0 | 149 | |
michael@0 | 150 | is(hud.outputNode.textContent.indexOf("foobarBug762996click"), -1, |
michael@0 | 151 | "clear console button worked"); |
michael@0 | 152 | is(getErrorsCount(), 0, "page errors counter has been reset"); |
michael@0 | 153 | let tooltip = getTooltipValues(); |
michael@0 | 154 | is(tooltip[1], 0, "page warnings counter has been reset"); |
michael@0 | 155 | |
michael@0 | 156 | doPageReload(hud); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | function doPageReload(hud) { |
michael@0 | 160 | tab1.linkedBrowser.addEventListener("load", onReload, true); |
michael@0 | 161 | |
michael@0 | 162 | ignoreAllUncaughtExceptions(); |
michael@0 | 163 | content.location.reload(); |
michael@0 | 164 | |
michael@0 | 165 | function onReload() { |
michael@0 | 166 | tab1.linkedBrowser.removeEventListener("load", onReload, true); |
michael@0 | 167 | ignoreAllUncaughtExceptions(false); |
michael@0 | 168 | expectUncaughtException(); |
michael@0 | 169 | |
michael@0 | 170 | waitForButtonUpdate({ |
michael@0 | 171 | name: "the Web Console button count has been reset after page reload", |
michael@0 | 172 | errors: 3, |
michael@0 | 173 | warnings: 0, |
michael@0 | 174 | callback: waitForValue.bind(null, waitForConsoleOutputAfterReload), |
michael@0 | 175 | }); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | let waitForConsoleOutputAfterReload = { |
michael@0 | 179 | name: "the Web Console displays the correct number of errors after reload", |
michael@0 | 180 | validator: function() { |
michael@0 | 181 | return hud.outputNode.querySelectorAll(".message[category=exception][severity=error]").length; |
michael@0 | 182 | }, |
michael@0 | 183 | value: 3, |
michael@0 | 184 | success: function() { |
michael@0 | 185 | isnot(hud.outputNode.textContent.indexOf("foobarBug762996load"), -1, |
michael@0 | 186 | "foobarBug762996load found in console output after page reload"); |
michael@0 | 187 | testEnd(); |
michael@0 | 188 | }, |
michael@0 | 189 | failure: testEnd, |
michael@0 | 190 | }; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | function testEnd() { |
michael@0 | 194 | document.getElementById("developer-toolbar-closebutton").doCommand(); |
michael@0 | 195 | let target1 = TargetFactory.forTab(tab1); |
michael@0 | 196 | gDevTools.closeToolbox(target1).then(() => { |
michael@0 | 197 | gBrowser.removeTab(tab1); |
michael@0 | 198 | gBrowser.removeTab(tab2); |
michael@0 | 199 | finish(); |
michael@0 | 200 | }); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | // Utility functions |
michael@0 | 204 | |
michael@0 | 205 | function getErrorsCount() { |
michael@0 | 206 | let count = webconsole.getAttribute("error-count"); |
michael@0 | 207 | return count ? count : "0"; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | function getTooltipValues() { |
michael@0 | 211 | let matches = webconsole.getAttribute("tooltiptext") |
michael@0 | 212 | .match(/(\d+) errors?, (\d+) warnings?/); |
michael@0 | 213 | return matches ? [matches[1], matches[2]] : [0, 0]; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | function waitForButtonUpdate(options) { |
michael@0 | 217 | function check() { |
michael@0 | 218 | let errors = getErrorsCount(); |
michael@0 | 219 | let tooltip = getTooltipValues(); |
michael@0 | 220 | let result = errors == options.errors && tooltip[1] == options.warnings; |
michael@0 | 221 | if (result) { |
michael@0 | 222 | ok(true, options.name); |
michael@0 | 223 | is(errors, tooltip[0], "button error-count is the same as in the tooltip"); |
michael@0 | 224 | |
michael@0 | 225 | // Get out of the toolbar event execution loop. |
michael@0 | 226 | executeSoon(options.callback); |
michael@0 | 227 | } |
michael@0 | 228 | return result; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | if (!check()) { |
michael@0 | 232 | info("wait for: " + options.name); |
michael@0 | 233 | DeveloperToolbar.on("errors-counter-updated", function onUpdate(event) { |
michael@0 | 234 | if (check()) { |
michael@0 | 235 | DeveloperToolbar.off(event, onUpdate); |
michael@0 | 236 | } |
michael@0 | 237 | }); |
michael@0 | 238 | } |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | function openWebConsole(tab, callback) |
michael@0 | 242 | { |
michael@0 | 243 | let target = TargetFactory.forTab(tab); |
michael@0 | 244 | gDevTools.showToolbox(target, "webconsole").then((toolbox) => |
michael@0 | 245 | callback(toolbox.getCurrentPanel().hud)); |
michael@0 | 246 | } |
michael@0 | 247 | } |