tor-browser

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

browser_aboutdebugging_serviceworker_console.js (4649B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /* import-globals-from helper-serviceworker.js */
      7 Services.scriptloader.loadSubScript(
      8  CHROME_URL_ROOT + "helper-serviceworker.js",
      9  this
     10 );
     11 
     12 Services.scriptloader.loadSubScript(
     13  "chrome://mochitests/content/browser/devtools/client/debugger/test/mochitest/shared-head.js",
     14  this
     15 );
     16 
     17 const SW_TAB_URL =
     18  URL_ROOT_SSL + "resources/service-workers/controlled-sw.html";
     19 const SW_URL =
     20  URL_ROOT_SSL + "resources/service-workers/controlled-sw.worker.js";
     21 
     22 /**
     23 * Test various simple debugging operation against service workers debugged through about:debugging.
     24 */
     25 add_task(async function () {
     26  await enableServiceWorkerDebugging();
     27  await pushPref("devtools.toolbox.splitconsoleHeight", 400);
     28 
     29  const { document, tab, window } = await openAboutDebugging({
     30    enableWorkerUpdates: true,
     31  });
     32  await selectThisFirefoxPage(document, window.AboutDebugging.store);
     33 
     34  // Open a tab that registers a basic service worker.
     35  const swTab = await addTab(SW_TAB_URL);
     36 
     37  // Wait for the registration to make sure service worker has been started, and that we
     38  // are not just reading STOPPED as the initial state.
     39  await waitForRegistration(swTab);
     40 
     41  info("Open a toolbox to debug the worker");
     42  const { devtoolsTab, devtoolsWindow } = await openAboutDevtoolsToolbox(
     43    document,
     44    tab,
     45    window,
     46    SW_URL
     47  );
     48 
     49  const toolbox = getToolbox(devtoolsWindow);
     50 
     51  info("Assert the default tools displayed in worker toolboxes");
     52  const toolTabs = toolbox.doc.querySelectorAll(".devtools-tab");
     53  const activeTools = [...toolTabs].map(toolTab =>
     54    toolTab.getAttribute("data-id")
     55  );
     56 
     57  is(
     58    activeTools.join(","),
     59    "webconsole,jsdebugger",
     60    "Correct set of tools supported by worker"
     61  );
     62 
     63  const webconsole = await toolbox.selectTool("webconsole");
     64  const { hud } = webconsole;
     65 
     66  info(
     67    "Checked that (cached) messages logged before opening the toolbox are displayed"
     68  );
     69  await waitFor(() =>
     70    findMessageByType(hud, "service worker installed", ".log")
     71  );
     72 
     73  info("Evaluate location in the console");
     74  await executeAndWaitForMessage(hud, "this.location.toString()", SW_URL);
     75  ok(true, "Got the location logged in the console");
     76 
     77  info(
     78    "Evaluate Date and RegExp to ensure their formater also work from worker threads"
     79  );
     80  await executeAndWaitForMessage(
     81    hud,
     82    "new Date(2013, 3, 1)",
     83    "Mon Apr 01 2013 00:00:00"
     84  );
     85  ok(true, "Date object has expected text content");
     86  await executeAndWaitForMessage(hud, "new RegExp('.*')", "/.*/");
     87  ok(true, "RegExp has expected text content");
     88  await executeAndWaitForMessage(
     89    hud,
     90    "console.log(globalThis)",
     91    "ServiceWorkerGlobalScope",
     92    ".log"
     93  );
     94  ok(true, "console.log has expected text content");
     95 
     96  await toolbox.selectTool("jsdebugger");
     97  const dbg = createDebuggerContext(toolbox);
     98 
     99  info("Trigger some code in the worker and wait for pause");
    100  await SpecialPowers.spawn(swTab.linkedBrowser, [], async function () {
    101    content.wrappedJSObject.installServiceWorkerAndPause();
    102  });
    103  await waitForPaused(dbg);
    104  ok(true, "successfully paused");
    105 
    106  info(
    107    "Evaluate some variable only visible if we execute in the breakpoint frame"
    108  );
    109  // open split console
    110  const onSplitConsole = toolbox.once("split-console");
    111  EventUtils.synthesizeKey("VK_ESCAPE", {}, devtoolsWindow);
    112  await onSplitConsole;
    113  await executeAndWaitForMessage(
    114    hud,
    115    "event.data",
    116    "install-service-worker-and-pause"
    117  );
    118 
    119  info("Resume execution");
    120  await resume(dbg);
    121 
    122  info("Test pausing from console evaluation");
    123  hud.ui.wrapper.dispatchEvaluateExpression("debugger; 42");
    124  await waitForPaused(dbg);
    125  ok(true, "successfully paused");
    126  info("Immediately resume");
    127  await resume(dbg);
    128  await waitFor(() => findMessageByType(hud, "42", ".result"));
    129  ok("The paused console evaluation resumed and logged its magic number");
    130 
    131  info("Destroy the toolbox");
    132  await closeAboutDevtoolsToolbox(document, devtoolsTab, window);
    133 
    134  info("Unregister service worker");
    135  await unregisterServiceWorker(swTab);
    136 
    137  info("Wait until the service worker disappears from about:debugging");
    138  await waitUntil(() => !findDebugTargetByText(SW_URL, document));
    139 
    140  info("Remove tabs");
    141  await removeTab(swTab);
    142  await removeTab(tab);
    143 });
    144 
    145 async function executeAndWaitForMessage(
    146  hud,
    147  evaluationString,
    148  expectedResult,
    149  messageClass = ".result"
    150 ) {
    151  hud.ui.wrapper.dispatchEvaluateExpression(evaluationString);
    152  await waitFor(
    153    () => findMessageByType(hud, expectedResult, messageClass),
    154    "",
    155    100
    156  );
    157 }