tor-browser

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

test_logpoint-03.js (2482B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Check that logpoints generate console errors if the logpoint statement is invalid.
      8 */
      9 
     10 const Resources = require("resource://devtools/server/actors/resources/index.js");
     11 
     12 add_task(
     13  threadFrontTest(async ({ threadActor, threadFront, debuggee }) => {
     14    let lastMessage, lastExpression;
     15    const { targetActor } = threadActor;
     16    // Only Workers are evaluating through the WebConsoleActor.
     17    // Tabs will be evaluating directly via the frame object.
     18    targetActor._consoleActor = {
     19      evaluateJS(expression) {
     20        lastExpression = expression;
     21      },
     22    };
     23 
     24    // And then listen for resource RDP event.
     25    // Bug 1646677: But we should probably migrate this test to ResourceCommand so that
     26    // we don't have to hack the server side via Resource.watchResources call.
     27    targetActor.on(
     28      "resources-available-array",
     29      ([[resourceType, resources]]) => {
     30        if (resourceType == Resources.TYPES.CONSOLE_MESSAGE) {
     31          lastMessage = resources[0];
     32        }
     33      }
     34    );
     35 
     36    // But both tabs and processes will be going through the ConsoleMessages module
     37    // We force watching for console message first,
     38    await Resources.watchResources(targetActor, [
     39      Resources.TYPES.CONSOLE_MESSAGE,
     40    ]);
     41 
     42    const packet = await executeOnNextTickAndWaitForPause(
     43      () => evalCode(debuggee),
     44      threadFront
     45    );
     46 
     47    const source = await getSourceById(threadFront, packet.frame.where.actor);
     48 
     49    // Set a logpoint which should throw an error message.
     50    await threadFront.setBreakpoint(
     51      {
     52        sourceUrl: source.url,
     53        line: 3,
     54      },
     55      { logValue: "c" }
     56    );
     57 
     58    // Execute the rest of the code.
     59    await threadFront.resume();
     60 
     61    // NOTE: logpoints evaluated in a worker have a lastExpression
     62    if (lastMessage) {
     63      Assert.equal(lastMessage.level, "logPointError");
     64      Assert.equal(lastMessage.arguments[0], "c is not defined");
     65      Assert.ok(/\d+\.\d+/.test(lastMessage.timeStamp));
     66    } else {
     67      Assert.equal(lastExpression.text, "console.log(...[c])");
     68      Assert.equal(lastExpression.lineNumber, 3);
     69    }
     70  })
     71 );
     72 
     73 function evalCode(debuggee) {
     74  /* eslint-disable */
     75  Cu.evalInSandbox(
     76    "debugger;\n" + // 1
     77      "var a = 'three';\n" + // 2
     78      "var b = 2;\n", // 3
     79    debuggee,
     80    "1.8",
     81    "test.js",
     82    1
     83  );
     84  /* eslint-enable */
     85 }