tor-browser

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

browser_net_block-context.js (5359B)


      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 that context menus for blocked requests work
      8 */
      9 
     10 add_task(async function () {
     11  const { monitor } = await initNetMonitor(HTTPS_SIMPLE_URL, {
     12    requestCount: 1,
     13  });
     14  info("Starting test... ");
     15 
     16  requestLongerTimeout(2);
     17 
     18  const { document, store, windowRequire } = monitor.panelWin;
     19  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     20 
     21  info("Loading initial page");
     22  const wait = waitForNetworkEvents(monitor, 1);
     23  await navigateTo(HTTPS_SIMPLE_URL);
     24  await wait;
     25 
     26  const requestListBlockingButton = document.querySelector(
     27    ".requests-list-blocking-button"
     28  );
     29  is(
     30    requestListBlockingButton.getAttribute("aria-pressed"),
     31    "false",
     32    "The block toolbar button should not be highlighted"
     33  );
     34  ok(
     35    !requestListBlockingButton.classList.contains(
     36      "requests-list-blocking-button-enabled"
     37    ),
     38    "The button should not have the requests-list-blocking-button-enabled class when there's no blocked items"
     39  );
     40 
     41  info("Opening the blocked requests panel");
     42  requestListBlockingButton.click();
     43 
     44  info("Adding sample block strings");
     45  const waitForBlockingContents = waitForDOM(
     46    document,
     47    ".request-blocking-contents"
     48  );
     49  await waitForBlockingAction(store, () => Actions.addBlockedUrl("test-page"));
     50  await waitForBlockingAction(store, () => Actions.addBlockedUrl("Two"));
     51  await waitForBlockingContents;
     52 
     53  is(getListitems(document), 2);
     54 
     55  is(
     56    requestListBlockingButton.getAttribute("aria-pressed"),
     57    "true",
     58    "The block toolbar button should be highlighted"
     59  );
     60  ok(
     61    requestListBlockingButton.classList.contains(
     62      "requests-list-blocking-button-enabled"
     63    ),
     64    "The button should have the requests-list-blocking-button-enabled class after adding blocked items"
     65  );
     66  is(
     67    document
     68      .querySelector(".devtools-search-icon")
     69      .getAttribute("aria-pressed"),
     70    "false",
     71    "The search toolbar button should not be highlighted"
     72  );
     73  is(
     74    document
     75      .querySelector(".devtools-http-custom-request-icon")
     76      .getAttribute("aria-pressed"),
     77    "false",
     78    "The new request toolbar button should not be highlighted"
     79  );
     80 
     81  info("Reloading page, URLs should be blocked in request list");
     82  await reloadPage(monitor, { isRequestBlocked: true });
     83  is(checkIfRequestIsBlocked(document), true);
     84 
     85  info("Disabling all blocked strings");
     86  await openMenuAndClick(
     87    monitor,
     88    store,
     89    document,
     90    "request-blocking-disable-all"
     91  );
     92  is(getCheckedCheckboxes(document), 0);
     93 
     94  info("Reloading page, URLs should not be blocked in request list");
     95  await reloadPage(monitor, { isRequestBlocked: false });
     96 
     97  is(checkIfRequestIsBlocked(document), false);
     98 
     99  info("Enabling all blocked strings");
    100  await openMenuAndClick(
    101    monitor,
    102    store,
    103    document,
    104    "request-blocking-enable-all"
    105  );
    106  is(getCheckedCheckboxes(document), 2);
    107 
    108  info("Reloading page, URLs should be blocked in request list");
    109  await reloadPage(monitor, { isRequestBlocked: true });
    110 
    111  is(checkIfRequestIsBlocked(document), true);
    112 
    113  info("Removing all blocked strings");
    114  await openMenuAndClick(
    115    monitor,
    116    store,
    117    document,
    118    "request-blocking-remove-all"
    119  );
    120  is(getListitems(document), 0);
    121 
    122  info("Reloading page, URLs should no longer be blocked in request list");
    123  await reloadPage(monitor, { isRequestBlocked: false });
    124  is(checkIfRequestIsBlocked(document), false);
    125 
    126  ok(
    127    !requestListBlockingButton.classList.contains(
    128      "requests-list-blocking-button-enabled"
    129    ),
    130    "The button should not have the requests-list-blocking-button-enabled class after removing blocked items"
    131  );
    132 
    133  return teardown(monitor);
    134 });
    135 
    136 async function waitForBlockingAction(store, action) {
    137  const wait = waitForDispatch(store, "REQUEST_BLOCKING_UPDATE_COMPLETE");
    138  store.dispatch(action());
    139  await wait;
    140 }
    141 
    142 async function openMenuAndClick(monitor, store, document, itemSelector) {
    143  info(`Right clicking on white-space in the header to get the context menu`);
    144  EventUtils.sendMouseEvent(
    145    { type: "contextmenu" },
    146    document.querySelector(".request-blocking-contents")
    147  );
    148 
    149  const wait = waitForDispatch(store, "REQUEST_BLOCKING_UPDATE_COMPLETE");
    150  await selectContextMenuItem(monitor, itemSelector);
    151  await wait;
    152 }
    153 
    154 async function reloadPage(monitor, { isRequestBlocked = false } = {}) {
    155  const wait = waitForNetworkEvents(monitor, 1);
    156  if (isRequestBlocked) {
    157    // Note: Do not use navigateTo or reloadBrowser here as the request will
    158    // be blocked and no navigation happens
    159    gBrowser.selectedBrowser.reload();
    160  } else {
    161    await reloadBrowser();
    162  }
    163  await wait;
    164 }
    165 
    166 function getCheckedCheckboxes(document) {
    167  const checkboxes = [
    168    ...document.querySelectorAll(".request-blocking-contents li input"),
    169  ];
    170  return checkboxes.filter(checkbox => checkbox.checked).length;
    171 }
    172 
    173 function getListitems(document) {
    174  return document.querySelectorAll(".request-blocking-contents li").length;
    175 }
    176 
    177 function checkIfRequestIsBlocked(document) {
    178  const firstRequest = document.querySelectorAll(".request-list-item")[0];
    179  const blockedRequestSize = firstRequest.querySelector(
    180    ".requests-list-transferred"
    181  ).textContent;
    182  return blockedRequestSize.includes("Blocked");
    183 }