tor-browser

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

browser_net_pause.js (4301B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Tests if the pause/resume button works.
      8 */
      9 add_task(async function () {
     10  const { tab, monitor, toolbox } = await initNetMonitor(PAUSE_URL, {
     11    requestCount: 1,
     12  });
     13  info("Starting test... ");
     14 
     15  const { document, store, windowRequire } = monitor.panelWin;
     16  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     17  const pauseButton = document.querySelector(".requests-list-pause-button");
     18 
     19  store.dispatch(Actions.batchEnable(false));
     20 
     21  // Make sure we start in a sane state.
     22  assertRequestCount(store, 0);
     23 
     24  // Load one request and assert it shows up in the list.
     25  await performRequestAndWait(tab, monitor, SIMPLE_URL + "?id=1");
     26  assertRequestCount(store, 1);
     27 
     28  let noRequest = true;
     29  monitor.panelWin.api.once(TEST_EVENTS.NETWORK_EVENT, () => {
     30    noRequest = false;
     31  });
     32 
     33  monitor.panelWin.api.on(EVENTS.PAYLOAD_READY, () => {
     34    noRequest = false;
     35  });
     36 
     37  // Click pause, load second request and make sure they don't show up.
     38  EventUtils.sendMouseEvent({ type: "click" }, pauseButton);
     39  await waitForPauseButtonToChange(document, true);
     40 
     41  await performPausedRequest(tab, monitor, toolbox);
     42 
     43  ok(noRequest, "There should be no activity when paused.");
     44  assertRequestCount(store, 1);
     45 
     46  // Click pause again to resume monitoring. Load a third request
     47  // and make sure they will show up.
     48  EventUtils.sendMouseEvent({ type: "click" }, pauseButton);
     49  await waitForPauseButtonToChange(document, false);
     50 
     51  await performRequestAndWait(tab, monitor, SIMPLE_URL + "?id=2");
     52 
     53  ok(!noRequest, "There should be activity when resumed.");
     54  assertRequestCount(store, 2);
     55 
     56  // Click pause, reload the page and check that there are
     57  // some requests.
     58  EventUtils.sendMouseEvent({ type: "click" }, pauseButton);
     59  await waitForPauseButtonToChange(document, true);
     60 
     61  await waitForAllNetworkUpdateEvents();
     62  // Page reload should auto-resume
     63  await reloadBrowser();
     64  await waitForPauseButtonToChange(document, false);
     65  await performRequestAndWait(tab, monitor, SIMPLE_URL + "?id=3");
     66 
     67  ok(!noRequest, "There should be activity when resumed.");
     68 
     69  return teardown(monitor);
     70 });
     71 
     72 /**
     73 * Wait until a request is visible in the request list
     74 */
     75 function waitForRequest(doc, url) {
     76  return waitUntil(() =>
     77    [...doc.querySelectorAll(".request-list-item .requests-list-file")].some(
     78      columns => columns.title.includes(url)
     79    )
     80  );
     81 }
     82 
     83 /**
     84 * Waits for the state of the paused/resume button to change.
     85 */
     86 async function waitForPauseButtonToChange(doc, isPaused) {
     87  await waitUntil(
     88    () =>
     89      !!doc.querySelector(
     90        `.requests-list-pause-button[aria-pressed="${
     91          isPaused ? "true" : "false"
     92        }"]`
     93      )
     94  );
     95  ok(
     96    true,
     97    `The pause button is correctly in the ${
     98      isPaused ? "paused" : "resumed"
     99    } state`
    100  );
    101 }
    102 
    103 /**
    104 * Asserts the number of requests in the network monitor.
    105 */
    106 function assertRequestCount(store, count) {
    107  is(
    108    store.getState().requests.requests.length,
    109    count,
    110    "There should be correct number of requests"
    111  );
    112 }
    113 
    114 /**
    115 * Execute simple GET request and wait till it's done.
    116 */
    117 async function performRequestAndWait(tab, monitor, requestURL) {
    118  const wait = waitForRequest(monitor.panelWin.document, requestURL);
    119  await SpecialPowers.spawn(
    120    tab.linkedBrowser,
    121    [requestURL],
    122    async function (url) {
    123      await content.wrappedJSObject.performRequests(url);
    124    }
    125  );
    126  await wait;
    127 }
    128 
    129 /**
    130 * Execute simple GET request, and uses a one time listener to
    131 * know when the resource is available.
    132 */
    133 async function performPausedRequest(tab, monitor, toolbox) {
    134  const { onResource: waitForEventWhenPaused } =
    135    await toolbox.commands.resourceCommand.waitForNextResource(
    136      toolbox.commands.resourceCommand.TYPES.NETWORK_EVENT,
    137      {
    138        ignoreExistingResources: true,
    139      }
    140    );
    141  await SpecialPowers.spawn(
    142    tab.linkedBrowser,
    143    [SIMPLE_URL],
    144    async function (url) {
    145      await content.wrappedJSObject.performRequests(url);
    146    }
    147  );
    148  // Wait for NETWORK_EVENT resources to be fetched, in order to ensure
    149  // that there is no pending request related to their processing.
    150  await waitForEventWhenPaused;
    151 }