michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Tests that the developer toolbar errors count works properly. michael@0: michael@0: function test() { michael@0: const TEST_URI = "http://example.com/browser/browser/devtools/shared/test/" + michael@0: "browser_toolbar_webconsole_errors_count.html"; michael@0: michael@0: let gDevTools = Cu.import("resource:///modules/devtools/gDevTools.jsm", michael@0: {}).gDevTools; michael@0: michael@0: let webconsole = document.getElementById("developer-toolbar-toolbox-button"); michael@0: let tab1, tab2; michael@0: michael@0: Services.prefs.setBoolPref("javascript.options.strict", true); michael@0: michael@0: registerCleanupFunction(() => { michael@0: Services.prefs.clearUserPref("javascript.options.strict"); michael@0: }); michael@0: michael@0: ignoreAllUncaughtExceptions(); michael@0: addTab(TEST_URI, openToolbar); michael@0: michael@0: function openToolbar(browser, tab) { michael@0: tab1 = tab; michael@0: ignoreAllUncaughtExceptions(false); michael@0: michael@0: expectUncaughtException(); michael@0: michael@0: if (!DeveloperToolbar.visible) { michael@0: DeveloperToolbar.show(true, onOpenToolbar); michael@0: } michael@0: else { michael@0: onOpenToolbar(); michael@0: } michael@0: } michael@0: michael@0: function onOpenToolbar() { michael@0: ok(DeveloperToolbar.visible, "DeveloperToolbar is visible"); michael@0: michael@0: waitForButtonUpdate({ michael@0: name: "web console button shows page errors", michael@0: errors: 3, michael@0: warnings: 0, michael@0: callback: addErrors, michael@0: }); michael@0: } michael@0: michael@0: function addErrors() { michael@0: expectUncaughtException(); michael@0: michael@0: waitForFocus(function() { michael@0: let button = content.document.querySelector("button"); michael@0: executeSoon(function() { michael@0: EventUtils.synthesizeMouse(button, 3, 2, {}, content); michael@0: }); michael@0: }, content); michael@0: michael@0: waitForButtonUpdate({ michael@0: name: "button shows one more error after click in page", michael@0: errors: 4, michael@0: warnings: 1, michael@0: callback: () => { michael@0: ignoreAllUncaughtExceptions(); michael@0: addTab(TEST_URI, onOpenSecondTab); michael@0: }, michael@0: }); michael@0: } michael@0: michael@0: function onOpenSecondTab(browser, tab) { michael@0: tab2 = tab; michael@0: michael@0: ignoreAllUncaughtExceptions(false); michael@0: expectUncaughtException(); michael@0: michael@0: waitForButtonUpdate({ michael@0: name: "button shows correct number of errors after new tab is open", michael@0: errors: 3, michael@0: warnings: 0, michael@0: callback: switchToTab1, michael@0: }); michael@0: } michael@0: michael@0: function switchToTab1() { michael@0: gBrowser.selectedTab = tab1; michael@0: waitForButtonUpdate({ michael@0: name: "button shows the page errors from tab 1", michael@0: errors: 4, michael@0: warnings: 1, michael@0: callback: openWebConsole.bind(null, tab1, onWebConsoleOpen), michael@0: }); michael@0: } michael@0: michael@0: function onWebConsoleOpen(hud) { michael@0: dump("lolz!!\n"); michael@0: waitForValue({ michael@0: name: "web console shows the page errors", michael@0: validator: function() { michael@0: return hud.outputNode.querySelectorAll(".message[category=exception][severity=error]").length; michael@0: }, michael@0: value: 4, michael@0: success: checkConsoleOutput.bind(null, hud), michael@0: failure: () => { michael@0: finish(); michael@0: }, michael@0: }); michael@0: } michael@0: michael@0: function checkConsoleOutput(hud) { michael@0: let msgs = ["foobarBug762996a", "foobarBug762996b", "foobarBug762996load", michael@0: "foobarBug762996click", "foobarBug762996consoleLog", michael@0: "foobarBug762996css", "fooBug788445"]; michael@0: msgs.forEach(function(msg) { michael@0: isnot(hud.outputNode.textContent.indexOf(msg), -1, michael@0: msg + " found in the Web Console output"); michael@0: }); michael@0: michael@0: hud.jsterm.clearOutput(); michael@0: michael@0: is(hud.outputNode.textContent.indexOf("foobarBug762996color"), -1, michael@0: "clearOutput() worked"); michael@0: michael@0: expectUncaughtException(); michael@0: let button = content.document.querySelector("button"); michael@0: EventUtils.synthesizeMouse(button, 2, 2, {}, content); michael@0: michael@0: waitForButtonUpdate({ michael@0: name: "button shows one more error after another click in page", michael@0: errors: 5, michael@0: warnings: 1, // warnings are not repeated by the js engine michael@0: callback: () => waitForValue(waitForNewError), michael@0: }); michael@0: michael@0: let waitForNewError = { michael@0: name: "the Web Console displays the new error", michael@0: validator: function() { michael@0: return hud.outputNode.textContent.indexOf("foobarBug762996click") > -1; michael@0: }, michael@0: success: doClearConsoleButton.bind(null, hud), michael@0: failure: finish, michael@0: }; michael@0: } michael@0: michael@0: function doClearConsoleButton(hud) { michael@0: let clearButton = hud.ui.rootElement michael@0: .querySelector(".webconsole-clear-console-button"); michael@0: EventUtils.synthesizeMouse(clearButton, 2, 2, {}, hud.iframeWindow); michael@0: michael@0: is(hud.outputNode.textContent.indexOf("foobarBug762996click"), -1, michael@0: "clear console button worked"); michael@0: is(getErrorsCount(), 0, "page errors counter has been reset"); michael@0: let tooltip = getTooltipValues(); michael@0: is(tooltip[1], 0, "page warnings counter has been reset"); michael@0: michael@0: doPageReload(hud); michael@0: } michael@0: michael@0: function doPageReload(hud) { michael@0: tab1.linkedBrowser.addEventListener("load", onReload, true); michael@0: michael@0: ignoreAllUncaughtExceptions(); michael@0: content.location.reload(); michael@0: michael@0: function onReload() { michael@0: tab1.linkedBrowser.removeEventListener("load", onReload, true); michael@0: ignoreAllUncaughtExceptions(false); michael@0: expectUncaughtException(); michael@0: michael@0: waitForButtonUpdate({ michael@0: name: "the Web Console button count has been reset after page reload", michael@0: errors: 3, michael@0: warnings: 0, michael@0: callback: waitForValue.bind(null, waitForConsoleOutputAfterReload), michael@0: }); michael@0: } michael@0: michael@0: let waitForConsoleOutputAfterReload = { michael@0: name: "the Web Console displays the correct number of errors after reload", michael@0: validator: function() { michael@0: return hud.outputNode.querySelectorAll(".message[category=exception][severity=error]").length; michael@0: }, michael@0: value: 3, michael@0: success: function() { michael@0: isnot(hud.outputNode.textContent.indexOf("foobarBug762996load"), -1, michael@0: "foobarBug762996load found in console output after page reload"); michael@0: testEnd(); michael@0: }, michael@0: failure: testEnd, michael@0: }; michael@0: } michael@0: michael@0: function testEnd() { michael@0: document.getElementById("developer-toolbar-closebutton").doCommand(); michael@0: let target1 = TargetFactory.forTab(tab1); michael@0: gDevTools.closeToolbox(target1).then(() => { michael@0: gBrowser.removeTab(tab1); michael@0: gBrowser.removeTab(tab2); michael@0: finish(); michael@0: }); michael@0: } michael@0: michael@0: // Utility functions michael@0: michael@0: function getErrorsCount() { michael@0: let count = webconsole.getAttribute("error-count"); michael@0: return count ? count : "0"; michael@0: } michael@0: michael@0: function getTooltipValues() { michael@0: let matches = webconsole.getAttribute("tooltiptext") michael@0: .match(/(\d+) errors?, (\d+) warnings?/); michael@0: return matches ? [matches[1], matches[2]] : [0, 0]; michael@0: } michael@0: michael@0: function waitForButtonUpdate(options) { michael@0: function check() { michael@0: let errors = getErrorsCount(); michael@0: let tooltip = getTooltipValues(); michael@0: let result = errors == options.errors && tooltip[1] == options.warnings; michael@0: if (result) { michael@0: ok(true, options.name); michael@0: is(errors, tooltip[0], "button error-count is the same as in the tooltip"); michael@0: michael@0: // Get out of the toolbar event execution loop. michael@0: executeSoon(options.callback); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: if (!check()) { michael@0: info("wait for: " + options.name); michael@0: DeveloperToolbar.on("errors-counter-updated", function onUpdate(event) { michael@0: if (check()) { michael@0: DeveloperToolbar.off(event, onUpdate); michael@0: } michael@0: }); michael@0: } michael@0: } michael@0: michael@0: function openWebConsole(tab, callback) michael@0: { michael@0: let target = TargetFactory.forTab(tab); michael@0: gDevTools.showToolbox(target, "webconsole").then((toolbox) => michael@0: callback(toolbox.getCurrentPanel().hud)); michael@0: } michael@0: }