tor-browser

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

browser_debugger_server.js (5511B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test basic features of DevToolsServer
      7 
      8 add_task(async function () {
      9  // When running some other tests before, they may not destroy the main server.
     10  // Do it manually before running our tests.
     11  if (DevToolsServer.initialized) {
     12    DevToolsServer.destroy();
     13  }
     14 
     15  await testDevToolsServerInitialized();
     16  await testDevToolsServerKeepAlive();
     17 });
     18 
     19 async function testDevToolsServerInitialized() {
     20  const tab = await addTab("data:text/html;charset=utf-8,foo");
     21 
     22  ok(
     23    !DevToolsServer.initialized,
     24    "By default, the DevToolsServer isn't initialized in parent process"
     25  );
     26  await assertServerInitialized(
     27    tab,
     28    false,
     29    "By default, the DevToolsServer isn't initialized not in content process"
     30  );
     31  await assertDevToolsOpened(
     32    tab,
     33    false,
     34    "By default, the DevTools are reported as closed"
     35  );
     36 
     37  const commands = await CommandsFactory.forTab(tab);
     38 
     39  ok(
     40    DevToolsServer.initialized,
     41    "Creating the commands will initialize the DevToolsServer in parent process"
     42  );
     43  await assertServerInitialized(
     44    tab,
     45    false,
     46    "Creating the commands isn't enough to initialize the DevToolsServer in content process"
     47  );
     48  await assertDevToolsOpened(
     49    tab,
     50    false,
     51    "DevTools are still reported as closed after having created the commands"
     52  );
     53 
     54  await commands.targetCommand.startListening();
     55 
     56  await assertServerInitialized(
     57    tab,
     58    true,
     59    "Initializing the TargetCommand will initialize the DevToolsServer in content process"
     60  );
     61  await assertDevToolsOpened(
     62    tab,
     63    true,
     64    "Initializing the TargetCommand will start reporting the DevTools as opened"
     65  );
     66 
     67  await commands.destroy();
     68 
     69  // Disconnecting the client will remove all connections from both server, in parent and content process.
     70  ok(
     71    !DevToolsServer.initialized,
     72    "Destroying the commands destroys the DevToolsServer in the parent process"
     73  );
     74  await assertServerInitialized(
     75    tab,
     76    false,
     77    "But destroying the commands ends up destroying the DevToolsServer in the content process"
     78  );
     79  await assertDevToolsOpened(
     80    tab,
     81    false,
     82    "Destroying the commands will report DevTools as being closed"
     83  );
     84 
     85  gBrowser.removeCurrentTab();
     86  DevToolsServer.destroy();
     87 }
     88 
     89 async function testDevToolsServerKeepAlive() {
     90  const tab = await addTab("data:text/html;charset=utf-8,foo");
     91 
     92  await assertServerInitialized(
     93    tab,
     94    false,
     95    "Server not started in content process"
     96  );
     97  await assertDevToolsOpened(tab, false, "DevTools are reported as closed");
     98 
     99  const commands = await CommandsFactory.forTab(tab);
    100  await commands.targetCommand.startListening();
    101 
    102  await assertServerInitialized(tab, true, "Server started in content process");
    103  await assertDevToolsOpened(tab, true, "DevTools are reported as opened");
    104 
    105  info("Set DevToolsServer.keepAlive to true in the content process");
    106  DevToolsServer.keepAlive = true;
    107  await setContentServerKeepAlive(tab, true);
    108 
    109  info("Destroy the commands, the content server should be kept alive");
    110  await commands.destroy();
    111 
    112  await assertServerInitialized(
    113    tab,
    114    true,
    115    "Server still running in content process"
    116  );
    117  await assertDevToolsOpened(
    118    tab,
    119    false,
    120    "DevTools are reported as close, even if the server is still running because there is no more client connected"
    121  );
    122 
    123  ok(
    124    DevToolsServer.initialized,
    125    "Destroying the commands never destroys the DevToolsServer in the parent process when keepAlive is true"
    126  );
    127 
    128  info("Set DevToolsServer.keepAlive back to false");
    129  DevToolsServer.keepAlive = false;
    130  await setContentServerKeepAlive(tab, false);
    131 
    132  info("Create and destroy a commands again");
    133  const newCommands = await CommandsFactory.forTab(tab);
    134  await newCommands.targetCommand.startListening();
    135 
    136  await newCommands.destroy();
    137 
    138  await assertServerInitialized(
    139    tab,
    140    false,
    141    "Server stopped in content process"
    142  );
    143  await assertDevToolsOpened(
    144    tab,
    145    false,
    146    "DevTools are reported as closed after destroying the second commands"
    147  );
    148 
    149  ok(
    150    !DevToolsServer.initialized,
    151    "When turning keepAlive to false, the server in the parent process is destroyed"
    152  );
    153 
    154  gBrowser.removeCurrentTab();
    155  DevToolsServer.destroy();
    156 }
    157 
    158 async function assertServerInitialized(tab, expected, message) {
    159  await SpecialPowers.spawn(
    160    tab.linkedBrowser,
    161    [expected, message],
    162    function (_expected, _message) {
    163      const { require } = ChromeUtils.importESModule(
    164        "resource://devtools/shared/loader/Loader.sys.mjs"
    165      );
    166      const {
    167        DevToolsServer,
    168      } = require("resource://devtools/server/devtools-server.js");
    169      is(DevToolsServer.initialized, _expected, _message);
    170    }
    171  );
    172 }
    173 
    174 async function assertDevToolsOpened(tab, expected, message) {
    175  await SpecialPowers.spawn(
    176    tab.linkedBrowser,
    177    [expected, message],
    178    function (_expected, _message) {
    179      is(ChromeUtils.isDevToolsOpened(), _expected, _message);
    180    }
    181  );
    182 }
    183 
    184 async function setContentServerKeepAlive(tab, keepAlive) {
    185  await SpecialPowers.spawn(
    186    tab.linkedBrowser,
    187    [keepAlive],
    188    function (_keepAlive) {
    189      const { require } = ChromeUtils.importESModule(
    190        "resource://devtools/shared/loader/Loader.sys.mjs"
    191      );
    192      const {
    193        DevToolsServer,
    194      } = require("resource://devtools/server/devtools-server.js");
    195      DevToolsServer.keepAlive = _keepAlive;
    196    }
    197  );
    198 }