browser/devtools/shared/test/browser_toolbar_webconsole_errors_count.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/shared/test/browser_toolbar_webconsole_errors_count.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,247 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Tests that the developer toolbar errors count works properly.
     1.8 +
     1.9 +function test() {
    1.10 +  const TEST_URI = "http://example.com/browser/browser/devtools/shared/test/" +
    1.11 +                   "browser_toolbar_webconsole_errors_count.html";
    1.12 +
    1.13 +  let gDevTools = Cu.import("resource:///modules/devtools/gDevTools.jsm",
    1.14 +                             {}).gDevTools;
    1.15 +
    1.16 +  let webconsole = document.getElementById("developer-toolbar-toolbox-button");
    1.17 +  let tab1, tab2;
    1.18 +
    1.19 +  Services.prefs.setBoolPref("javascript.options.strict", true);
    1.20 +
    1.21 +  registerCleanupFunction(() => {
    1.22 +    Services.prefs.clearUserPref("javascript.options.strict");
    1.23 +  });
    1.24 +
    1.25 +  ignoreAllUncaughtExceptions();
    1.26 +  addTab(TEST_URI, openToolbar);
    1.27 +
    1.28 +  function openToolbar(browser, tab) {
    1.29 +    tab1 = tab;
    1.30 +    ignoreAllUncaughtExceptions(false);
    1.31 +
    1.32 +    expectUncaughtException();
    1.33 +
    1.34 +    if (!DeveloperToolbar.visible) {
    1.35 +      DeveloperToolbar.show(true, onOpenToolbar);
    1.36 +    }
    1.37 +    else {
    1.38 +      onOpenToolbar();
    1.39 +    }
    1.40 +  }
    1.41 +
    1.42 +  function onOpenToolbar() {
    1.43 +    ok(DeveloperToolbar.visible, "DeveloperToolbar is visible");
    1.44 +
    1.45 +    waitForButtonUpdate({
    1.46 +      name: "web console button shows page errors",
    1.47 +      errors: 3,
    1.48 +      warnings: 0,
    1.49 +      callback: addErrors,
    1.50 +    });
    1.51 +  }
    1.52 +
    1.53 +  function addErrors() {
    1.54 +    expectUncaughtException();
    1.55 +
    1.56 +    waitForFocus(function() {
    1.57 +      let button = content.document.querySelector("button");
    1.58 +      executeSoon(function() {
    1.59 +        EventUtils.synthesizeMouse(button, 3, 2, {}, content);
    1.60 +      });
    1.61 +    }, content);
    1.62 +
    1.63 +    waitForButtonUpdate({
    1.64 +      name: "button shows one more error after click in page",
    1.65 +      errors: 4,
    1.66 +      warnings: 1,
    1.67 +      callback: () => {
    1.68 +        ignoreAllUncaughtExceptions();
    1.69 +        addTab(TEST_URI, onOpenSecondTab);
    1.70 +      },
    1.71 +    });
    1.72 +  }
    1.73 +
    1.74 +  function onOpenSecondTab(browser, tab) {
    1.75 +    tab2 = tab;
    1.76 +
    1.77 +    ignoreAllUncaughtExceptions(false);
    1.78 +    expectUncaughtException();
    1.79 +
    1.80 +    waitForButtonUpdate({
    1.81 +      name: "button shows correct number of errors after new tab is open",
    1.82 +      errors: 3,
    1.83 +      warnings: 0,
    1.84 +      callback: switchToTab1,
    1.85 +    });
    1.86 +  }
    1.87 +
    1.88 +  function switchToTab1() {
    1.89 +    gBrowser.selectedTab = tab1;
    1.90 +    waitForButtonUpdate({
    1.91 +      name: "button shows the page errors from tab 1",
    1.92 +      errors: 4,
    1.93 +      warnings: 1,
    1.94 +      callback: openWebConsole.bind(null, tab1, onWebConsoleOpen),
    1.95 +    });
    1.96 +  }
    1.97 +
    1.98 +  function onWebConsoleOpen(hud) {
    1.99 +    dump("lolz!!\n");
   1.100 +    waitForValue({
   1.101 +      name: "web console shows the page errors",
   1.102 +      validator: function() {
   1.103 +        return hud.outputNode.querySelectorAll(".message[category=exception][severity=error]").length;
   1.104 +      },
   1.105 +      value: 4,
   1.106 +      success: checkConsoleOutput.bind(null, hud),
   1.107 +      failure: () => {
   1.108 +        finish();
   1.109 +      },
   1.110 +    });
   1.111 +  }
   1.112 +
   1.113 +  function checkConsoleOutput(hud) {
   1.114 +    let msgs = ["foobarBug762996a", "foobarBug762996b", "foobarBug762996load",
   1.115 +                "foobarBug762996click", "foobarBug762996consoleLog",
   1.116 +                "foobarBug762996css", "fooBug788445"];
   1.117 +    msgs.forEach(function(msg) {
   1.118 +      isnot(hud.outputNode.textContent.indexOf(msg), -1,
   1.119 +            msg + " found in the Web Console output");
   1.120 +    });
   1.121 +
   1.122 +    hud.jsterm.clearOutput();
   1.123 +
   1.124 +    is(hud.outputNode.textContent.indexOf("foobarBug762996color"), -1,
   1.125 +       "clearOutput() worked");
   1.126 +
   1.127 +    expectUncaughtException();
   1.128 +    let button = content.document.querySelector("button");
   1.129 +    EventUtils.synthesizeMouse(button, 2, 2, {}, content);
   1.130 +
   1.131 +    waitForButtonUpdate({
   1.132 +      name: "button shows one more error after another click in page",
   1.133 +      errors: 5,
   1.134 +      warnings: 1, // warnings are not repeated by the js engine
   1.135 +      callback: () => waitForValue(waitForNewError),
   1.136 +    });
   1.137 +
   1.138 +    let waitForNewError = {
   1.139 +      name: "the Web Console displays the new error",
   1.140 +      validator: function() {
   1.141 +        return hud.outputNode.textContent.indexOf("foobarBug762996click") > -1;
   1.142 +      },
   1.143 +      success: doClearConsoleButton.bind(null, hud),
   1.144 +      failure: finish,
   1.145 +    };
   1.146 +  }
   1.147 +
   1.148 +  function doClearConsoleButton(hud) {
   1.149 +    let clearButton = hud.ui.rootElement
   1.150 +                      .querySelector(".webconsole-clear-console-button");
   1.151 +    EventUtils.synthesizeMouse(clearButton, 2, 2, {}, hud.iframeWindow);
   1.152 +
   1.153 +    is(hud.outputNode.textContent.indexOf("foobarBug762996click"), -1,
   1.154 +       "clear console button worked");
   1.155 +    is(getErrorsCount(), 0, "page errors counter has been reset");
   1.156 +    let tooltip = getTooltipValues();
   1.157 +    is(tooltip[1], 0, "page warnings counter has been reset");
   1.158 +
   1.159 +    doPageReload(hud);
   1.160 +  }
   1.161 +
   1.162 +  function doPageReload(hud) {
   1.163 +    tab1.linkedBrowser.addEventListener("load", onReload, true);
   1.164 +
   1.165 +    ignoreAllUncaughtExceptions();
   1.166 +    content.location.reload();
   1.167 +
   1.168 +    function onReload() {
   1.169 +      tab1.linkedBrowser.removeEventListener("load", onReload, true);
   1.170 +      ignoreAllUncaughtExceptions(false);
   1.171 +      expectUncaughtException();
   1.172 +
   1.173 +      waitForButtonUpdate({
   1.174 +        name: "the Web Console button count has been reset after page reload",
   1.175 +        errors: 3,
   1.176 +        warnings: 0,
   1.177 +        callback: waitForValue.bind(null, waitForConsoleOutputAfterReload),
   1.178 +      });
   1.179 +    }
   1.180 +
   1.181 +    let waitForConsoleOutputAfterReload = {
   1.182 +      name: "the Web Console displays the correct number of errors after reload",
   1.183 +      validator: function() {
   1.184 +        return hud.outputNode.querySelectorAll(".message[category=exception][severity=error]").length;
   1.185 +      },
   1.186 +      value: 3,
   1.187 +      success: function() {
   1.188 +        isnot(hud.outputNode.textContent.indexOf("foobarBug762996load"), -1,
   1.189 +              "foobarBug762996load found in console output after page reload");
   1.190 +        testEnd();
   1.191 +      },
   1.192 +      failure: testEnd,
   1.193 +    };
   1.194 +  }
   1.195 +
   1.196 +  function testEnd() {
   1.197 +    document.getElementById("developer-toolbar-closebutton").doCommand();
   1.198 +    let target1 = TargetFactory.forTab(tab1);
   1.199 +    gDevTools.closeToolbox(target1).then(() => {
   1.200 +      gBrowser.removeTab(tab1);
   1.201 +      gBrowser.removeTab(tab2);
   1.202 +      finish();
   1.203 +    });
   1.204 +  }
   1.205 +
   1.206 +  // Utility functions
   1.207 +
   1.208 +  function getErrorsCount() {
   1.209 +    let count = webconsole.getAttribute("error-count");
   1.210 +    return count ? count : "0";
   1.211 +  }
   1.212 +
   1.213 +  function getTooltipValues() {
   1.214 +    let matches = webconsole.getAttribute("tooltiptext")
   1.215 +                  .match(/(\d+) errors?, (\d+) warnings?/);
   1.216 +    return matches ? [matches[1], matches[2]] : [0, 0];
   1.217 +  }
   1.218 +
   1.219 +  function waitForButtonUpdate(options) {
   1.220 +    function check() {
   1.221 +      let errors = getErrorsCount();
   1.222 +      let tooltip = getTooltipValues();
   1.223 +      let result = errors == options.errors && tooltip[1] == options.warnings;
   1.224 +      if (result) {
   1.225 +        ok(true, options.name);
   1.226 +        is(errors, tooltip[0], "button error-count is the same as in the tooltip");
   1.227 +
   1.228 +        // Get out of the toolbar event execution loop.
   1.229 +        executeSoon(options.callback);
   1.230 +      }
   1.231 +      return result;
   1.232 +    }
   1.233 +
   1.234 +    if (!check()) {
   1.235 +      info("wait for: " + options.name);
   1.236 +      DeveloperToolbar.on("errors-counter-updated", function onUpdate(event) {
   1.237 +        if (check()) {
   1.238 +          DeveloperToolbar.off(event, onUpdate);
   1.239 +        }
   1.240 +      });
   1.241 +    }
   1.242 +  }
   1.243 +
   1.244 +  function openWebConsole(tab, callback)
   1.245 +  {
   1.246 +    let target = TargetFactory.forTab(tab);
   1.247 +    gDevTools.showToolbox(target, "webconsole").then((toolbox) =>
   1.248 +      callback(toolbox.getCurrentPanel().hud));
   1.249 +  }
   1.250 +}

mercurial