tor-browser

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

browser_dbg-javascript-tracer-next-interaction.js (4971B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 // Tests tracing only on next user interaction
      6 
      7 "use strict";
      8 
      9 add_task(async function testTracingOnNextInteraction() {
     10  // Cover tracing only on next user interaction
     11  const jsCode = `function foo() {}; window.addEventListener("mousedown", function onmousedown(){}, { capture: true }); window.onclick = function onclick() {};`;
     12  const dbg = await initDebuggerWithAbsoluteURL(
     13    "data:text/html," +
     14      encodeURIComponent(`<script>${jsCode}</script><body></body>`)
     15  );
     16 
     17  // This test covers the Web Console, whereas it is no longer the default output
     18  await toggleJsTracerMenuItem(dbg, "#jstracer-menu-item-console");
     19 
     20  await toggleJsTracerMenuItem(dbg, "#jstracer-menu-item-next-interaction");
     21 
     22  await toggleJsTracer(dbg.toolbox);
     23 
     24  const traceButton = dbg.toolbox.doc.getElementById("command-button-jstracer");
     25  // Wait for the trace button to be highlighted
     26  await waitFor(() => {
     27    return traceButton.classList.contains("pending");
     28  });
     29  ok(
     30    traceButton.classList.contains("pending"),
     31    "The tracer button is also highlighted as pending until the user interaction is triggered"
     32  );
     33 
     34  invokeInTab("foo");
     35 
     36  // Let a change to have the tracer to regress and log foo call
     37  await wait(500);
     38 
     39  is(
     40    (await findConsoleMessages(dbg.toolbox, "λ foo")).length,
     41    0,
     42    "The tracer did not log the function call before trigerring the click event"
     43  );
     44 
     45  // We intentionally turn off this a11y check, because the following click
     46  // is send on an empty <body> to to test the click event tracer performance,
     47  // and not to activate any control, therefore this check can be ignored.
     48  AccessibilityUtils.setEnv({
     49    mustHaveAccessibleRule: false,
     50  });
     51  await BrowserTestUtils.synthesizeMouseAtCenter(
     52    "body",
     53    {},
     54    gBrowser.selectedBrowser
     55  );
     56  AccessibilityUtils.resetEnv();
     57 
     58  await hasConsoleMessage(dbg, "Started tracing to Web Console");
     59 
     60  await hasConsoleMessage(dbg, "λ onmousedown");
     61  await hasConsoleMessage(dbg, "λ onclick");
     62 
     63  is(
     64    traceButton.getAttribute("aria-pressed"),
     65    "true",
     66    "The tracer button is still highlighted as active"
     67  );
     68  ok(
     69    !traceButton.classList.contains("pending"),
     70    "The tracer button is no longer pending after the user interaction"
     71  );
     72 
     73  is(
     74    (await findConsoleMessages(dbg.toolbox, "λ foo")).length,
     75    0,
     76    "Even after the click, the code called before the click is still not logged"
     77  );
     78 
     79  // But if we call this code again, now it should be logged
     80  invokeInTab("foo");
     81  await hasConsoleMessage(dbg, "λ foo");
     82  ok(true, "foo was traced as expected");
     83 
     84  info("Stop tracing");
     85  await toggleJsTracer(dbg.toolbox);
     86 
     87  is(
     88    traceButton.getAttribute("aria-pressed"),
     89    "false",
     90    "The tracer button is no longer highlighted as active"
     91  );
     92  ok(
     93    !traceButton.classList.contains("pending"),
     94    "The tracer button is still not pending after disabling"
     95  );
     96 
     97  // Reset the trace on next interaction setting
     98  Services.prefs.clearUserPref(
     99    "devtools.debugger.javascript-tracing-on-next-interaction"
    100  );
    101 });
    102 
    103 add_task(async function testInteractionBetweenDebuggerAndConsole() {
    104  const jsCode = `function foo() {};`;
    105  const dbg = await initDebuggerWithAbsoluteURL(
    106    "data:text/html," + encodeURIComponent(`<script>${jsCode}</script>`)
    107  );
    108 
    109  info("Enable the tracing via the toolbox button");
    110  await toggleJsTracer(dbg.toolbox);
    111 
    112  invokeInTab("foo");
    113 
    114  await hasConsoleMessage(dbg, "λ foo");
    115 
    116  info("Disable the tracing via a console command");
    117  const { hud } = await dbg.toolbox.getPanel("webconsole");
    118  let msg = await evaluateExpressionInConsole(hud, ":trace", "console-api");
    119  is(msg.textContent.trim(), "Stopped tracing");
    120 
    121  const button = dbg.toolbox.doc.getElementById("command-button-jstracer");
    122  await waitFor(() => !button.classList.contains("checked"));
    123 
    124  info(
    125    "Clear the console output from the first tracing session started from the debugger"
    126  );
    127  hud.ui.clearOutput();
    128  await waitFor(
    129    async () => !(await findConsoleMessages(dbg.toolbox, "λ foo")).length,
    130    "Wait for console to be cleared"
    131  );
    132 
    133  info("Enable the tracing via a console command");
    134  msg = await evaluateExpressionInConsole(hud, ":trace", "console-api");
    135  is(msg.textContent.trim(), "Started tracing to Web Console");
    136 
    137  info("Wait for tracing to be also enabled in toolbox button");
    138  await waitFor(() => button.classList.contains("checked"));
    139 
    140  invokeInTab("foo");
    141 
    142  await hasConsoleMessage(dbg, "λ foo");
    143 
    144  info("Disable the tracing via the debugger button");
    145  // togglejsTracer will assert that the console logged the "stopped tracing" message
    146  await toggleJsTracer(dbg.toolbox);
    147 
    148  info("Wait for tracing to be disabled per toolbox button");
    149  await waitFor(() => !button.classList.contains("checked"));
    150 });