tor-browser

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

test_consoleapi.html (5808B)


      1 <!DOCTYPE HTML>
      2 <html lang="en">
      3 <head>
      4  <meta charset="utf8">
      5  <title>Test for the Console API</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 the Console API</p>
     13 
     14 <script class="testbody" type="text/javascript">
     15 "use strict";
     16 
     17 SimpleTest.waitForExplicitFinish();
     18 
     19 let expectedConsoleCalls = [];
     20 
     21 function doConsoleCalls()
     22 {
     23  const longString = (new Array(DevToolsServer.LONG_STRING_LENGTH + 2)).join("a");
     24 
     25  top.console.log("foobarBaz-log", undefined);
     26 
     27  top.console.log("Float from not a number: %f", "foo");
     28  top.console.log("Float from string: %f", "1.2");
     29  top.console.log("Float from number: %f", 1.3);
     30 
     31  top.console.info("foobarBaz-info", null);
     32  top.console.warn("foobarBaz-warn", top.document.documentElement);
     33  top.console.debug(null);
     34  top.console.trace();
     35  top.console.dir(top.document, top.location);
     36  top.console.log("foo", longString);
     37  top.console.table([Symbol("apples"), Symbol("bananas")]);
     38 
     39  const sandbox = new Cu.Sandbox(null, { invisibleToDebugger: true });
     40  const sandboxObj = sandbox.eval("new Object");
     41  top.console.log(sandboxObj);
     42 
     43  function fromAsmJS() {
     44      top.console.error("foobarBaz-asmjs-error", undefined);
     45  }
     46 
     47  (function(global, foreign) {
     48    "use asm";
     49    function inAsmJS2() { foreign.fromAsmJS() }
     50    function inAsmJS1() { inAsmJS2() }
     51    return inAsmJS1
     52  })(null, { fromAsmJS })();
     53 
     54  expectedConsoleCalls = [
     55    {
     56      level: "log",
     57      filename: /test_consoleapi/,
     58      timeStamp: FRACTIONAL_NUMBER_REGEX,
     59      arguments: ["foobarBaz-log", { type: "undefined" }],
     60    },
     61    {
     62      level: "log",
     63      arguments: ["Float from not a number: NaN"],
     64    },
     65    {
     66      level: "log",
     67      arguments: ["Float from string: 1.200000"],
     68    },
     69    {
     70      level: "log",
     71      arguments: ["Float from number: 1.300000"],
     72    },
     73    {
     74      level: "info",
     75      filename: /test_consoleapi/,
     76      timeStamp: FRACTIONAL_NUMBER_REGEX,
     77      arguments: ["foobarBaz-info", { type: "null" }],
     78    },
     79    {
     80      level: "warn",
     81      filename: /test_consoleapi/,
     82      timeStamp: FRACTIONAL_NUMBER_REGEX,
     83      arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }],
     84    },
     85    {
     86      level: "debug",
     87      filename: /test_consoleapi/,
     88      timeStamp: FRACTIONAL_NUMBER_REGEX,
     89      arguments: [{ type: "null" }],
     90    },
     91    {
     92      level: "trace",
     93      filename: /test_consoleapi/,
     94      timeStamp: FRACTIONAL_NUMBER_REGEX,
     95      stacktrace: [
     96        {
     97          filename: /test_consoleapi/,
     98          functionName: "doConsoleCalls",
     99        },
    100        {
    101          filename: /test_consoleapi/,
    102          functionName: "onAttach",
    103        },
    104      ],
    105    },
    106    {
    107      level: "dir",
    108      filename: /test_consoleapi/,
    109      timeStamp: FRACTIONAL_NUMBER_REGEX,
    110      arguments: [
    111        {
    112          type: "object",
    113          actor: /[a-z]/,
    114          class: "HTMLDocument",
    115        },
    116        {
    117          type: "object",
    118          actor: /[a-z]/,
    119          class: "Location",
    120        }
    121      ],
    122    },
    123    {
    124      level: "log",
    125      filename: /test_consoleapi/,
    126      timeStamp: FRACTIONAL_NUMBER_REGEX,
    127      arguments: [
    128        "foo",
    129        {
    130          type: "longString",
    131          initial: longString.substring(0,
    132            DevToolsServer.LONG_STRING_INITIAL_LENGTH),
    133          length: longString.length,
    134          actor: /[a-z]/,
    135        },
    136      ],
    137    },
    138    {
    139      level: "table",
    140      filename: /test_consoleapi/,
    141      timeStamp: FRACTIONAL_NUMBER_REGEX,
    142      arguments: [
    143        {
    144          type: "object",
    145          actor: /[a-z]/,
    146          class: "Array",
    147        },
    148      ],
    149    },
    150    {
    151      level: "log",
    152      filename: /test_consoleapi/,
    153      timeStamp: FRACTIONAL_NUMBER_REGEX,
    154      arguments: [
    155        {
    156          type: "object",
    157          actor: /[a-z]/,
    158          class: "InvisibleToDebugger: Object",
    159        },
    160      ],
    161    },
    162    {
    163      level: "error",
    164      filename: /test_consoleapi/,
    165      timeStamp: FRACTIONAL_NUMBER_REGEX,
    166      arguments: ["foobarBaz-asmjs-error", { type: "undefined" }],
    167 
    168      stacktrace: [
    169        {
    170          filename: /test_consoleapi/,
    171          functionName: "fromAsmJS",
    172        },
    173        {
    174          filename: /test_consoleapi/,
    175          functionName: "inAsmJS2",
    176        },
    177        {
    178          filename: /test_consoleapi/,
    179          functionName: "inAsmJS1",
    180        },
    181        {
    182          filename: /test_consoleapi/,
    183          functionName: "doConsoleCalls",
    184        },
    185        {
    186          filename: /test_consoleapi/,
    187          functionName: "onAttach",
    188        },
    189      ],
    190    },
    191  ];
    192 }
    193 
    194 async function startTest()
    195 {
    196  removeEventListener("load", startTest);
    197 
    198  const {state, response} = await attachConsoleToTab(["ConsoleAPI"]);
    199  onAttach(state, response);
    200 }
    201 
    202 function onAttach(state)
    203 {
    204  onConsoleAPICall = onConsoleAPICall.bind(null, state);
    205  state.webConsoleFront.on("consoleAPICall", onConsoleAPICall);
    206  doConsoleCalls(state.actor);
    207 }
    208 
    209 let consoleCalls = [];
    210 
    211 function onConsoleAPICall(state, packet)
    212 {
    213  info("received message level: " + packet.message.level);
    214 
    215  consoleCalls.push(packet.message);
    216  if (consoleCalls.length != expectedConsoleCalls.length) {
    217    return;
    218  }
    219 
    220  state.webConsoleFront.off("consoleAPICall", onConsoleAPICall);
    221 
    222  expectedConsoleCalls.forEach(function(message, index) {
    223    info("checking received console call #" + index);
    224    checkConsoleAPICall(consoleCalls[index], expectedConsoleCalls[index]);
    225  });
    226 
    227 
    228  consoleCalls = [];
    229 
    230  closeDebugger(state, function() {
    231    SimpleTest.finish();
    232  });
    233 }
    234 
    235 addEventListener("load", startTest);
    236 </script>
    237 </body>
    238 </html>