tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_cached_messages.html (6093B)


      1 <!DOCTYPE HTML>
      2 <html lang="en">
      3 <head>
      4  <meta charset="utf8">
      5  <title>Test for cached messages</title>
      6  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
      7  <script type="text/javascript" src="common.js"></script>
      8  <!-- Any copyright is dedicated to the Public Domain.
      9     - http://creativecommons.org/publicdomain/zero/1.0/ -->
     10 </head>
     11 <body>
     12 <p>Test for cached messages</p>
     13 
     14 <script class="testbody" type="application/javascript">
     15 "use strict";
     16 
     17 const { MESSAGE_CATEGORY } = require("devtools/shared/constants");
     18 
     19 const previousEnabled = window.docShell.cssErrorReportingEnabled;
     20 window.docShell.cssErrorReportingEnabled = true;
     21 
     22 SimpleTest.registerCleanupFunction(() => {
     23  window.docShell.cssErrorReportingEnabled = previousEnabled;
     24 });
     25 
     26 var ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
     27                          .getService(Ci.nsIConsoleAPIStorage);
     28 let expectedConsoleCalls = [];
     29 let expectedPageErrors = [];
     30 
     31 function doPageErrors() {
     32  Services.console.reset();
     33 
     34  expectedPageErrors = [
     35    {
     36      errorMessage: /fooColor/,
     37      sourceName: /.+/,
     38      category: MESSAGE_CATEGORY.CSS_PARSER,
     39      timeStamp: FRACTIONAL_NUMBER_REGEX,
     40      error: false,
     41      warning: true,
     42    },
     43    {
     44      errorMessage: /doTheImpossible/,
     45      sourceName: /.+/,
     46      category: "chrome javascript",
     47      timeStamp: FRACTIONAL_NUMBER_REGEX,
     48      error: true,
     49      warning: false,
     50    },
     51  ];
     52 
     53  let container = document.createElement("script");
     54  document.body.appendChild(container);
     55  container.textContent = "document.body.style.color = 'fooColor';";
     56  document.body.removeChild(container);
     57 
     58  SimpleTest.expectUncaughtException();
     59 
     60  container = document.createElement("script");
     61  document.body.appendChild(container);
     62  container.textContent = "document.doTheImpossible();";
     63  document.body.removeChild(container);
     64 }
     65 
     66 function doConsoleCalls() {
     67  ConsoleAPIStorage.clearEvents();
     68 
     69  top.console.log("foobarBaz-log", undefined);
     70  top.console.info("foobarBaz-info", null);
     71  top.console.warn("foobarBaz-warn", document.body);
     72 
     73  expectedConsoleCalls = [
     74    {
     75      level: "log",
     76      filename: /test_cached_messages/,
     77      timeStamp: FRACTIONAL_NUMBER_REGEX,
     78      arguments: ["foobarBaz-log", { type: "undefined" }],
     79    },
     80    {
     81      level: "info",
     82      filename: /test_cached_messages/,
     83      timeStamp: FRACTIONAL_NUMBER_REGEX,
     84      arguments: ["foobarBaz-info", { type: "null" }],
     85    },
     86    {
     87      level: "warn",
     88      filename: /test_cached_messages/,
     89      timeStamp: FRACTIONAL_NUMBER_REGEX,
     90      arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }],
     91    },
     92  ];
     93 }
     94 </script>
     95 
     96 <script class="testbody" type="text/javascript">
     97 "use strict";
     98 
     99 SimpleTest.waitForExplicitFinish();
    100 var {ConsoleServiceListener} =
    101  require("devtools/server/actors/webconsole/listeners/console-service");
    102 var {ConsoleAPIListener} =
    103  require("devtools/server/actors/webconsole/listeners/console-api");
    104 
    105 let consoleAPIListener, consoleServiceListener;
    106 let consoleAPICalls = 0;
    107 let pageErrors = 0;
    108 
    109 async function onConsoleAPICall(message) {
    110  for (const msg of expectedConsoleCalls) {
    111    if (msg.functionName == message.functionName &&
    112        msg.filename.test(message.filename)) {
    113      consoleAPICalls++;
    114      break;
    115    }
    116  }
    117  if (consoleAPICalls == expectedConsoleCalls.length) {
    118    await checkConsoleAPICache();
    119  }
    120 }
    121 
    122 async function onConsoleServiceMessage(message) {
    123  if (!(message instanceof Ci.nsIScriptError)) {
    124    return;
    125  }
    126  for (const msg of expectedPageErrors) {
    127    if (msg.category == message.category &&
    128        msg.errorMessage.test(message.errorMessage)) {
    129      pageErrors++;
    130      break;
    131    }
    132  }
    133  if (pageErrors == expectedPageErrors.length) {
    134    await testPageErrors();
    135  }
    136 }
    137 
    138 function startTest() {
    139  removeEventListener("load", startTest);
    140 
    141  consoleAPIListener = new ConsoleAPIListener(top, onConsoleAPICall);
    142  consoleAPIListener.init();
    143 
    144  doConsoleCalls();
    145 }
    146 
    147 async function checkConsoleAPICache() {
    148  consoleAPIListener.destroy();
    149  consoleAPIListener = null;
    150  const {state} = await attachConsole(["ConsoleAPI"]);
    151  const response = await state.webConsoleFront.getCachedMessages(["ConsoleAPI"]);
    152  onCachedConsoleAPI(state, response);
    153 }
    154 
    155 function onCachedConsoleAPI(state, response) {
    156  const msgs = response.messages;
    157  info("cached console messages: " + msgs.length);
    158 
    159  ok(msgs.length >= expectedConsoleCalls.length,
    160     "number of cached console messages");
    161 
    162  for (const {message} of msgs) {
    163    for (const expected of expectedConsoleCalls) {
    164      if (expected.filename.test(message.filename)) {
    165        expectedConsoleCalls.splice(expectedConsoleCalls.indexOf(expected));
    166        checkConsoleAPICall(message, expected);
    167        break;
    168      }
    169    }
    170  }
    171 
    172  is(expectedConsoleCalls.length, 0, "all expected messages have been found");
    173 
    174  closeDebugger(state, function() {
    175    consoleServiceListener = new ConsoleServiceListener(null, onConsoleServiceMessage);
    176    consoleServiceListener.init();
    177    doPageErrors();
    178  });
    179 }
    180 
    181 async function testPageErrors() {
    182  consoleServiceListener.destroy();
    183  consoleServiceListener = null;
    184  const {state} = await attachConsole(["PageError"]);
    185  const response = await state.webConsoleFront.getCachedMessages(["PageError"]);
    186  onCachedPageErrors(state, response);
    187 }
    188 
    189 function onCachedPageErrors(state, response) {
    190  const msgs = response.messages;
    191  info("cached page errors: " + msgs.length);
    192 
    193  ok(msgs.length >= expectedPageErrors.length,
    194     "number of cached page errors");
    195 
    196  for (const {pageError} of msgs) {
    197    for (const expected of expectedPageErrors) {
    198      if (expected.category == pageError.category &&
    199          expected.errorMessage.test(pageError.errorMessage)) {
    200        expectedPageErrors.splice(expectedPageErrors.indexOf(expected));
    201        checkObject(pageError, expected);
    202        break;
    203      }
    204    }
    205  }
    206 
    207  is(expectedPageErrors.length, 0, "all expected messages have been found");
    208 
    209  closeDebugger(state, function() {
    210    SimpleTest.finish();
    211  });
    212 }
    213 
    214 addEventListener("load", startTest);
    215 </script>
    216 </body>
    217 </html>