tor-browser

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

browser_toolbox_races.js (2472B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Toggling the toolbox three time can take more than 45s on slow test machine
      7 requestLongerTimeout(2);
      8 
      9 // Test toggling the toolbox quickly and see if there is any race breaking it.
     10 
     11 const URL = "data:text/html;charset=utf-8,Toggling devtools quickly";
     12 const {
     13  gDevToolsBrowser,
     14 } = require("resource://devtools/client/framework/devtools-browser.js");
     15 
     16 add_task(async function () {
     17  await addTab(URL);
     18 
     19  let ready = 0,
     20    destroy = 0,
     21    destroyed = 0;
     22  const onReady = () => {
     23    ready++;
     24  };
     25  const onDestroy = () => {
     26    destroy++;
     27  };
     28  const onDestroyed = () => {
     29    destroyed++;
     30  };
     31  gDevTools.on("toolbox-ready", onReady);
     32  gDevTools.on("toolbox-destroy", onDestroy);
     33  gDevTools.on("toolbox-destroyed", onDestroyed);
     34 
     35  // The current implementation won't toggle the toolbox many times,
     36  // instead it will ignore toggles that happens while the toolbox is still
     37  // creating or still destroying.
     38 
     39  info("Toggle the toolbox many times in a row");
     40  toggle();
     41  toggle();
     42  toggle();
     43  toggle();
     44  toggle();
     45  await wait(500);
     46 
     47  await waitFor(() => ready == 1);
     48  is(
     49    ready,
     50    1,
     51    "No matter how many times we called toggle, it will only open the toolbox once"
     52  );
     53  is(
     54    destroy,
     55    0,
     56    "All subsequent, synchronous call to toggle will be ignored and the toolbox won't be destroyed"
     57  );
     58  is(destroyed, 0);
     59 
     60  info("Retoggle the toolbox many times in a row");
     61  toggle();
     62  toggle();
     63  toggle();
     64  toggle();
     65  toggle();
     66  await wait(500);
     67 
     68  await waitFor(() => destroyed == 1);
     69  is(destroyed, 1, "Similarly, the toolbox will be closed");
     70  is(destroy, 1);
     71  is(
     72    ready,
     73    1,
     74    "and no other toolbox will be opened. The subsequent toggle will be ignored."
     75  );
     76 
     77  gDevTools.off("toolbox-ready", onReady);
     78  gDevTools.off("toolbox-destroy", onDestroy);
     79  gDevTools.off("toolbox-destroyed", onDestroyed);
     80  await wait(1000);
     81 
     82  gBrowser.removeCurrentTab();
     83 });
     84 
     85 function toggle() {
     86  // When enabling the input event prioritization, we'll reserve some time to
     87  // process input events in each frame. In that case, the synthesized input
     88  // events may delay the normal events. Replace synthesized key events by
     89  // toggleToolboxCommand to prevent the synthesized input events jam the
     90  // content process and cause the test timeout.
     91  gDevToolsBrowser.toggleToolboxCommand(window.gBrowser);
     92 }