tor-browser

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

browser_jsterm_await_paused.js (2911B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that top-level await expression work as expected when debugger is paused.
      5 
      6 "use strict";
      7 
      8 const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>Web Console test top-level await when debugger paused`;
      9 
     10 add_task(async function () {
     11  // Enable await mapping.
     12  await pushPref("devtools.debugger.features.map-await-expression", true);
     13 
     14  // Force the split console to be closed.
     15  await pushPref("devtools.toolbox.splitconsole.open", false);
     16  const hud = await openNewTabAndConsole(TEST_URI);
     17 
     18  const pauseExpression = `(() => {
     19    var inPausedExpression = ["bar"];
     20    /* Will pause the script and open the debugger panel */
     21    debugger;
     22    return "pauseExpression-res";
     23  })()`;
     24  execute(hud, pauseExpression);
     25 
     26  // wait for the debugger to be opened and paused.
     27  const toolbox = gDevTools.getToolboxForTab(gBrowser.selectedTab);
     28 
     29  await waitFor(() => toolbox.getPanel("jsdebugger"));
     30  const dbg = createDebuggerContext(toolbox);
     31  await waitForPaused(dbg);
     32 
     33  await toolbox.openSplitConsole();
     34 
     35  const awaitExpression = `await new Promise(res => {
     36    const result = ["res", ...inPausedExpression];
     37    setTimeout(() => res(result), 2000);
     38    console.log("awaitExpression executed");
     39  })`;
     40 
     41  const onAwaitResultMessage = waitForMessageByType(
     42    hud,
     43    `[ "res", "bar" ]`,
     44    ".result"
     45  );
     46  const onAwaitExpressionExecuted = waitForMessageByType(
     47    hud,
     48    "awaitExpression executed",
     49    ".console-api"
     50  );
     51  execute(hud, awaitExpression);
     52 
     53  // We send an evaluation just after the await one to ensure the await evaluation was
     54  // done. We can't await on the previous execution because it waits for the result to
     55  // be send, which won't happen until we resume the debugger.
     56  await executeAndWaitForResultMessage(hud, `"smoke"`, `"smoke"`);
     57 
     58  // Give the engine some time to evaluate the await expression before resuming.
     59  // Otherwise the awaitExpression may be evaluate while the thread is already resumed!
     60  await onAwaitExpressionExecuted;
     61 
     62  // Click on the resume button to not be paused anymore.
     63  await resume(dbg);
     64 
     65  info("Wait for the paused expression result to be displayed");
     66  await waitFor(() => findEvaluationResultMessage(hud, "pauseExpression-res"));
     67 
     68  await onAwaitResultMessage;
     69  const messages = hud.ui.outputNode.querySelectorAll(
     70    ".message.result .message-body"
     71  );
     72  const messagesText = Array.from(messages).map(n => n.textContent);
     73  const expectedMessages = [
     74    // Result of "smoke"
     75    `"smoke"`,
     76    // The result of pauseExpression (after smoke since pauseExpression iife was paused)
     77    `"pauseExpression-res"`,
     78    // Result of await
     79    `Array [ "res", "bar" ]`,
     80  ];
     81  Assert.deepEqual(
     82    messagesText,
     83    expectedMessages,
     84    "The output contains the the expected messages, in the expected order"
     85  );
     86 });