toolkit/devtools/webconsole/test/test_cached_messages.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/webconsole/test/test_cached_messages.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,230 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html lang="en">
     1.6 +<head>
     1.7 +  <meta charset="utf8">
     1.8 +  <title>Test for cached messages</title>
     1.9 +  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.10 +  <script type="text/javascript;version=1.8" src="common.js"></script>
    1.11 +  <!-- Any copyright is dedicated to the Public Domain.
    1.12 +     - http://creativecommons.org/publicdomain/zero/1.0/ -->
    1.13 +</head>
    1.14 +<body>
    1.15 +<p>Test for cached messages</p>
    1.16 +
    1.17 +<script class="testbody" type="application/javascript;version=1.8">
    1.18 +let expectedConsoleCalls = [];
    1.19 +let expectedPageErrors = [];
    1.20 +
    1.21 +function doPageErrors()
    1.22 +{
    1.23 +  Services.console.reset();
    1.24 +
    1.25 +  expectedPageErrors = [
    1.26 +    {
    1.27 +      _type: "PageError",
    1.28 +      errorMessage: /fooColor/,
    1.29 +      sourceName: /.+/,
    1.30 +      category: "CSS Parser",
    1.31 +      timeStamp: /^\d+$/,
    1.32 +      error: false,
    1.33 +      warning: true,
    1.34 +      exception: false,
    1.35 +      strict: false,
    1.36 +    },
    1.37 +    {
    1.38 +      _type: "PageError",
    1.39 +      errorMessage: /doTheImpossible/,
    1.40 +      sourceName: /.+/,
    1.41 +      category: "chrome javascript",
    1.42 +      timeStamp: /^\d+$/,
    1.43 +      error: false,
    1.44 +      warning: false,
    1.45 +      exception: true,
    1.46 +      strict: false,
    1.47 +    },
    1.48 +  ];
    1.49 +
    1.50 +  let container = document.createElement("script");
    1.51 +  document.body.appendChild(container);
    1.52 +  container.textContent = "document.body.style.color = 'fooColor';";
    1.53 +  document.body.removeChild(container);
    1.54 +
    1.55 +  SimpleTest.expectUncaughtException();
    1.56 +
    1.57 +  container = document.createElement("script");
    1.58 +  document.body.appendChild(container);
    1.59 +  container.textContent = "document.doTheImpossible();";
    1.60 +  document.body.removeChild(container);
    1.61 +}
    1.62 +
    1.63 +function doConsoleCalls()
    1.64 +{
    1.65 +  ConsoleAPIStorage.clearEvents();
    1.66 +
    1.67 +  top.console.log("foobarBaz-log", undefined);
    1.68 +  top.console.info("foobarBaz-info", null);
    1.69 +  top.console.warn("foobarBaz-warn", document.body);
    1.70 +
    1.71 +  expectedConsoleCalls = [
    1.72 +    {
    1.73 +      _type: "ConsoleAPI",
    1.74 +      level: "log",
    1.75 +      filename: /test_cached_messages/,
    1.76 +      functionName: "doConsoleCalls",
    1.77 +      timeStamp: /^\d+$/,
    1.78 +      arguments: ["foobarBaz-log", { type: "undefined" }],
    1.79 +    },
    1.80 +    {
    1.81 +      _type: "ConsoleAPI",
    1.82 +      level: "info",
    1.83 +      filename: /test_cached_messages/,
    1.84 +      functionName: "doConsoleCalls",
    1.85 +      timeStamp: /^\d+$/,
    1.86 +      arguments: ["foobarBaz-info", { type: "null" }],
    1.87 +    },
    1.88 +    {
    1.89 +      _type: "ConsoleAPI",
    1.90 +      level: "warn",
    1.91 +      filename: /test_cached_messages/,
    1.92 +      functionName: "doConsoleCalls",
    1.93 +      timeStamp: /^\d+$/,
    1.94 +      arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }],
    1.95 +    },
    1.96 +  ];
    1.97 +}
    1.98 +</script>
    1.99 +
   1.100 +<script class="testbody" type="text/javascript;version=1.8">
   1.101 +SimpleTest.waitForExplicitFinish();
   1.102 +
   1.103 +let consoleAPIListener, consoleServiceListener;
   1.104 +let consoleAPICalls = 0;
   1.105 +let pageErrors = 0;
   1.106 +
   1.107 +let handlers = {
   1.108 +  onConsoleAPICall: function onConsoleAPICall(aMessage)
   1.109 +  {
   1.110 +    for (let msg of expectedConsoleCalls) {
   1.111 +      if (msg.functionName == aMessage.functionName &&
   1.112 +          msg.filename.test(aMessage.filename)) {
   1.113 +        consoleAPICalls++;
   1.114 +        break;
   1.115 +      }
   1.116 +    }
   1.117 +    if (consoleAPICalls == expectedConsoleCalls.length) {
   1.118 +      checkConsoleAPICache();
   1.119 +    }
   1.120 +  },
   1.121 +
   1.122 +  onConsoleServiceMessage: function onConsoleServiceMessage(aMessage)
   1.123 +  {
   1.124 +    if (!(aMessage instanceof Ci.nsIScriptError)) {
   1.125 +      return;
   1.126 +    }
   1.127 +    for (let msg of expectedPageErrors) {
   1.128 +      if (msg.category == aMessage.category &&
   1.129 +          msg.errorMessage.test(aMessage.errorMessage)) {
   1.130 +        pageErrors++;
   1.131 +        break;
   1.132 +      }
   1.133 +    }
   1.134 +    if (pageErrors == expectedPageErrors.length) {
   1.135 +      testPageErrors();
   1.136 +    }
   1.137 +  },
   1.138 +};
   1.139 +
   1.140 +function startTest()
   1.141 +{
   1.142 +  removeEventListener("load", startTest);
   1.143 +
   1.144 +  consoleAPIListener = new ConsoleAPIListener(top, handlers);
   1.145 +  consoleAPIListener.init();
   1.146 +
   1.147 +  doConsoleCalls();
   1.148 +}
   1.149 +
   1.150 +function checkConsoleAPICache()
   1.151 +{
   1.152 +  consoleAPIListener.destroy();
   1.153 +  consoleAPIListener = null;
   1.154 +  attachConsole(["ConsoleAPI"], onAttach1);
   1.155 +}
   1.156 +
   1.157 +function onAttach1(aState, aResponse)
   1.158 +{
   1.159 +  aState.client.getCachedMessages(["ConsoleAPI"],
   1.160 +                                  onCachedConsoleAPI.bind(null, aState));
   1.161 +}
   1.162 +
   1.163 +function onCachedConsoleAPI(aState, aResponse)
   1.164 +{
   1.165 +  let msgs = aResponse.messages;
   1.166 +  info("cached console messages: " + msgs.length);
   1.167 +
   1.168 +  ok(msgs.length >= expectedConsoleCalls.length,
   1.169 +     "number of cached console messages");
   1.170 +
   1.171 +  for (let msg of msgs) {
   1.172 +    for (let expected of expectedConsoleCalls) {
   1.173 +      if (expected.functionName == msg.functionName &&
   1.174 +          expected.filename.test(msg.filename)) {
   1.175 +        expectedConsoleCalls.splice(expectedConsoleCalls.indexOf(expected));
   1.176 +        checkConsoleAPICall(msg, expected);
   1.177 +        break;
   1.178 +      }
   1.179 +    }
   1.180 +  }
   1.181 +
   1.182 +  is(expectedConsoleCalls.length, 0, "all expected messages have been found");
   1.183 +
   1.184 +  closeDebugger(aState, function() {
   1.185 +    consoleServiceListener = new ConsoleServiceListener(null, handlers);
   1.186 +    consoleServiceListener.init();
   1.187 +    doPageErrors();
   1.188 +  });
   1.189 +}
   1.190 +
   1.191 +function testPageErrors()
   1.192 +{
   1.193 +  consoleServiceListener.destroy();
   1.194 +  consoleServiceListener = null;
   1.195 +  attachConsole(["PageError"], onAttach2);
   1.196 +}
   1.197 +
   1.198 +function onAttach2(aState, aResponse)
   1.199 +{
   1.200 +  aState.client.getCachedMessages(["PageError"],
   1.201 +                                  onCachedPageErrors.bind(null, aState));
   1.202 +}
   1.203 +
   1.204 +function onCachedPageErrors(aState, aResponse)
   1.205 +{
   1.206 +  let msgs = aResponse.messages;
   1.207 +  info("cached page errors: " + msgs.length);
   1.208 +
   1.209 +  ok(msgs.length >= expectedPageErrors.length,
   1.210 +     "number of cached page errors");
   1.211 +
   1.212 +  for (let msg of msgs) {
   1.213 +    for (let expected of expectedPageErrors) {
   1.214 +      if (expected.category == msg.category &&
   1.215 +          expected.errorMessage.test(msg.errorMessage)) {
   1.216 +        expectedPageErrors.splice(expectedPageErrors.indexOf(expected));
   1.217 +        checkObject(msg, expected);
   1.218 +        break;
   1.219 +      }
   1.220 +    }
   1.221 +  }
   1.222 +
   1.223 +  is(expectedPageErrors.length, 0, "all expected messages have been found");
   1.224 +
   1.225 +  closeDebugger(aState, function() {
   1.226 +    SimpleTest.finish();
   1.227 +  });
   1.228 +}
   1.229 +
   1.230 +addEventListener("load", startTest);
   1.231 +</script>
   1.232 +</body>
   1.233 +</html>

mercurial