tor-browser

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

browser_toolbox_many_toggles.js (4055B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // This test can be slow on linux debug
      7 requestLongerTimeout(2);
      8 
      9 // As we are closing devtools while it may still be loading,
     10 // we are getting various exception about pending request still in flight while closing
     11 const { PromiseTestUtils } = ChromeUtils.importESModule(
     12  "resource://testing-common/PromiseTestUtils.sys.mjs"
     13 );
     14 PromiseTestUtils.allowMatchingRejectionsGlobally(/Connection closed/);
     15 
     16 // Bug 1898490: DevTools may prevents opening when some random content process is being destroyed
     17 // in middle of DevTools initialization.
     18 // So try opening DevTools a couple of times while destroying content processes in the background.
     19 
     20 const URL1 =
     21  "data:text/html;charset=utf8,test many toggles with other content process destructions";
     22 
     23 add_task(
     24  async function manyTogglesWithContentProcessDestructionsInBackground() {
     25    const tab = await addTab(URL1);
     26 
     27    const ProcessTools = Cc["@mozilla.org/processtools-service;1"].getService(
     28      Ci.nsIProcessToolsService
     29    );
     30 
     31    const openedTabs = [];
     32    const interval = setInterval(() => {
     33      // Close the process specific to about:home, which is using a privilegedabout process type
     34      const pid = ChromeUtils.getAllDOMProcesses().filter(
     35        r => r.remoteType == "privilegedabout"
     36      )[0]?.osPid;
     37      if (!pid) {
     38        return;
     39      }
     40 
     41      try {
     42        ProcessTools.kill(pid);
     43      } catch (ex) {
     44        info(`ProcessTools.kill(${pid}) returned: ${ex.result}`);
     45 
     46        // NS_ERROR_NOT_AVAILABLE is thrown is the process disappeared (ESRCH)
     47        // So we should be fine ignoring this
     48        if (ex.result !== Cr.NS_ERROR_NOT_AVAILABLE) {
     49          throw ex;
     50        }
     51      }
     52 
     53      // The privilegedabout process wouldn't be automatically re-created, so open a new tab to force creating a new process.
     54      openedTabs.push(BrowserTestUtils.addTab(gBrowser, "about:home"));
     55    }, 10);
     56 
     57    info(
     58      "Open/close DevTools many times in a row while some processes get destroyed"
     59    );
     60    for (let i = 0; i < 5; i++) {
     61      const toolbox = await gDevTools.showToolboxForTab(tab, {
     62        toolId: "webconsole",
     63      });
     64      await toolbox.destroy();
     65    }
     66 
     67    clearInterval(interval);
     68 
     69    info("Close all tabs that were used to spawn a new content process");
     70    for (const tab of openedTabs) {
     71      await removeTab(tab);
     72    }
     73 
     74    await removeTab(tab);
     75  }
     76 );
     77 
     78 // Bug 1903980: DevTools throw in background and becomes blank when closing them while iframe are being destroyed.
     79 const URL2 = `data:text/html,Test toggling DevTools with many destroying iframes`;
     80 
     81 add_task(async function manyTogglesWithDestroyingIframes() {
     82  const tab = await addTab(URL2);
     83 
     84  // Run the infinite loop creating iframe *after* having called addTab
     85  // as it may confuse the test helper and make it consider the page as still loading.
     86  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     87    let iframe;
     88    content.interval = content.setInterval(function () {
     89      if (iframe) {
     90        iframe.remove();
     91      }
     92      iframe = content.document.createElement("iframe");
     93      iframe.src = "data:text/html,foo";
     94      content.document.body.appendChild(iframe);
     95 
     96      // Do not use a timeout lower than 100 as it would freeze all executions with MOZ_CHAOS mode
     97      // If the interval is lower than 50, no RDP message would be able to be emitted
     98      // and between 50 and 100, only frameUpdate would be notified without allowing to load the toolbox
     99    }, 100);
    100  });
    101 
    102  info(
    103    "Open/close DevTools many times in a row while some processes get destroyed"
    104  );
    105  for (let i = 0; i < 3; i++) {
    106    info(` # Toggle DevTools attempt #${i}`);
    107    const toolbox = await gDevTools.showToolboxForTab(tab, {
    108      toolId: "webconsole",
    109    });
    110    await toolbox.destroy();
    111  }
    112 
    113  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    114    content.clearInterval(content.interval);
    115  });
    116 
    117  await removeTab(tab);
    118 });