tor-browser

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

browser_net_prefs-reload.js (9588B)


      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 prefs that should survive across tool reloads work.
      8 */
      9 
     10 add_task(async function () {
     11  let { monitor } = await initNetMonitor(SIMPLE_URL, { requestCount: 1 });
     12  const Actions = monitor.panelWin.windowRequire(
     13    "devtools/client/netmonitor/src/actions/index"
     14  );
     15  info("Starting test... ");
     16 
     17  // This test reopens the network monitor a bunch of times, for different
     18  // hosts (bottom, side, window). This seems to be slow on debug builds.
     19  requestLongerTimeout(3);
     20 
     21  // Use these getters instead of caching instances inside the panel win,
     22  // since the tool is reopened a bunch of times during this test
     23  // and the instances will differ.
     24  const getDoc = () => monitor.panelWin.document;
     25  const getPrefs = () =>
     26    monitor.panelWin.windowRequire("devtools/client/netmonitor/src/utils/prefs")
     27      .Prefs;
     28  const getStore = () => monitor.panelWin.store;
     29  const getState = () => getStore().getState();
     30 
     31  const prefsToCheck = {
     32    filters: {
     33      // A custom new value to be used for the verified preference.
     34      newValue: ["html", "css"],
     35      // Getter used to retrieve the current value from the frontend, in order
     36      // to verify that the pref was applied properly.
     37      validateValue: () =>
     38        Object.entries(getState().filters.requestFilterTypes)
     39          .filter(([, check]) => check)
     40          .map(([type]) => type),
     41      // Predicate used to modify the frontend when setting the new pref value,
     42      // before trying to validate the changes.
     43      modifyFrontend: value =>
     44        value.forEach(e =>
     45          getStore().dispatch(Actions.toggleRequestFilterType(e))
     46        ),
     47    },
     48    networkDetailsWidth: {
     49      newValue: ~~(Math.random() * 200 + 100),
     50      validateValue: () =>
     51        getDoc().querySelector(".monitor-panel .split-box .controlled")
     52          .clientWidth,
     53      modifyFrontend(value) {
     54        getDoc().querySelector(
     55          ".monitor-panel .split-box .controlled"
     56        ).style.width = `${value}px`;
     57      },
     58    },
     59    networkDetailsHeight: {
     60      newValue: ~~(Math.random() * 300 + 100),
     61      validateValue: () =>
     62        getDoc().querySelector(".monitor-panel .split-box .controlled")
     63          .clientHeight,
     64      modifyFrontend(value) {
     65        getDoc().querySelector(
     66          ".monitor-panel .split-box .controlled"
     67        ).style.height = `${value}px`;
     68      },
     69    },
     70    /* add more prefs here... */
     71  };
     72 
     73  await testBottom();
     74  await testSide();
     75  await testWindow();
     76 
     77  info("Moving toolbox back to the bottom...");
     78  await monitor.toolbox.switchHost("bottom");
     79  return teardown(monitor);
     80 
     81  function storeFirstPrefValues() {
     82    info("Caching initial pref values.");
     83 
     84    for (const name in prefsToCheck) {
     85      const currentValue = getPrefs()[name];
     86      prefsToCheck[name].firstValue = currentValue;
     87    }
     88  }
     89 
     90  function validateFirstPrefValues(isVerticalSplitter) {
     91    info("Validating current pref values to the UI elements.");
     92 
     93    for (const name in prefsToCheck) {
     94      if (
     95        (isVerticalSplitter && name === "networkDetailsHeight") ||
     96        (!isVerticalSplitter && name === "networkDetailsWidth")
     97      ) {
     98        continue;
     99      }
    100 
    101      const currentValue = getPrefs()[name];
    102      const { firstValue, validateValue } = prefsToCheck[name];
    103 
    104      is(
    105        firstValue.toString(),
    106        currentValue.toString(),
    107        "Pref " + name + " should be equal to first value: " + currentValue
    108      );
    109      is(
    110        validateValue().toString(),
    111        currentValue.toString(),
    112        "Pref " + name + " should validate: " + currentValue
    113      );
    114    }
    115  }
    116 
    117  function modifyFrontend(isVerticalSplitter) {
    118    info("Modifying UI elements to the specified new values.");
    119 
    120    for (const name in prefsToCheck) {
    121      if (
    122        (isVerticalSplitter && name === "networkDetailsHeight") ||
    123        (!isVerticalSplitter && name === "networkDetailsWidth")
    124      ) {
    125        continue;
    126      }
    127 
    128      const currentValue = getPrefs()[name];
    129      const { firstValue, newValue, validateValue } = prefsToCheck[name];
    130      const modFrontend = prefsToCheck[name].modifyFrontend;
    131 
    132      modFrontend(newValue);
    133      info("Modified UI element affecting " + name + " to: " + newValue);
    134 
    135      is(
    136        firstValue.toString(),
    137        currentValue.toString(),
    138        "Pref " +
    139          name +
    140          " should still be equal to first value: " +
    141          currentValue
    142      );
    143      isnot(
    144        newValue.toString(),
    145        currentValue.toString(),
    146        "Pref " +
    147          name +
    148          " should't yet be equal to second value: " +
    149          currentValue
    150      );
    151      is(
    152        validateValue().toString(),
    153        newValue.toString(),
    154        "The UI element affecting " + name + " should validate: " + newValue
    155      );
    156    }
    157  }
    158 
    159  function validateNewPrefValues(isVerticalSplitter) {
    160    info("Invalidating old pref values to the modified UI elements.");
    161 
    162    for (const name in prefsToCheck) {
    163      if (
    164        (isVerticalSplitter && name === "networkDetailsHeight") ||
    165        (!isVerticalSplitter && name === "networkDetailsWidth")
    166      ) {
    167        continue;
    168      }
    169 
    170      const currentValue = getPrefs()[name];
    171      const { firstValue, newValue, validateValue } = prefsToCheck[name];
    172 
    173      isnot(
    174        firstValue.toString(),
    175        currentValue.toString(),
    176        "Pref " + name + " should't be equal to first value: " + currentValue
    177      );
    178      is(
    179        newValue.toString(),
    180        currentValue.toString(),
    181        "Pref " + name + " should now be equal to second value: " + currentValue
    182      );
    183      is(
    184        validateValue().toString(),
    185        newValue.toString(),
    186        "The UI element affecting " + name + " should validate: " + newValue
    187      );
    188    }
    189  }
    190 
    191  function resetFrontend(isVerticalSplitter) {
    192    info("Resetting UI elements to the cached initial pref values.");
    193 
    194    for (const name in prefsToCheck) {
    195      if (
    196        (isVerticalSplitter && name === "networkDetailsHeight") ||
    197        (!isVerticalSplitter && name === "networkDetailsWidth")
    198      ) {
    199        continue;
    200      }
    201 
    202      const currentValue = getPrefs()[name];
    203      const { firstValue, newValue, validateValue } = prefsToCheck[name];
    204      const modFrontend = prefsToCheck[name].modifyFrontend;
    205 
    206      modFrontend(firstValue);
    207      info("Modified UI element affecting " + name + " to: " + firstValue);
    208 
    209      isnot(
    210        firstValue.toString(),
    211        currentValue.toString(),
    212        "Pref " +
    213          name +
    214          " should't yet be equal to first value: " +
    215          currentValue
    216      );
    217      is(
    218        newValue.toString(),
    219        currentValue.toString(),
    220        "Pref " +
    221          name +
    222          " should still be equal to second value: " +
    223          currentValue
    224      );
    225      is(
    226        validateValue().toString(),
    227        firstValue.toString(),
    228        "The UI element affecting " + name + " should validate: " + firstValue
    229      );
    230    }
    231  }
    232 
    233  async function restartNetMonitorAndSetupEnv() {
    234    const newMonitor = await restartNetMonitor(monitor, { requestCount: 1 });
    235    monitor = newMonitor.monitor;
    236 
    237    const networkEvent = waitForNetworkEvents(monitor, 1);
    238    await reloadBrowser();
    239    await networkEvent;
    240 
    241    const wait = waitForDOM(getDoc(), ".network-details-bar");
    242    getStore().dispatch(Actions.toggleNetworkDetails());
    243    await wait;
    244  }
    245 
    246  async function testBottom() {
    247    await restartNetMonitorAndSetupEnv();
    248 
    249    info("Testing prefs reload for a bottom host.");
    250    storeFirstPrefValues();
    251 
    252    // Validate and modify while toolbox is on the bottom.
    253    validateFirstPrefValues(true);
    254    modifyFrontend(true);
    255 
    256    await restartNetMonitorAndSetupEnv();
    257 
    258    // Revalidate and reset frontend while toolbox is on the bottom.
    259    validateNewPrefValues(true);
    260    resetFrontend(true);
    261 
    262    await restartNetMonitorAndSetupEnv();
    263 
    264    // Revalidate.
    265    validateFirstPrefValues(true);
    266  }
    267 
    268  async function testSide() {
    269    await restartNetMonitorAndSetupEnv();
    270 
    271    info("Moving toolbox to the right...");
    272 
    273    await monitor.toolbox.switchHost("right");
    274 
    275    // Switching hosts is not correctly waiting when DevTools run in content frame
    276    // See Bug 1571421.
    277    await wait(1000);
    278 
    279    info("Testing prefs reload for a right host.");
    280    storeFirstPrefValues();
    281 
    282    // Validate and modify frontend while toolbox is on the side.
    283    validateFirstPrefValues(false);
    284    modifyFrontend(false);
    285 
    286    await restartNetMonitorAndSetupEnv();
    287 
    288    // Revalidate and reset frontend while toolbox is on the side.
    289    validateNewPrefValues(false);
    290    resetFrontend(false);
    291 
    292    await restartNetMonitorAndSetupEnv();
    293 
    294    // Revalidate.
    295    validateFirstPrefValues(false);
    296  }
    297 
    298  async function testWindow() {
    299    await restartNetMonitorAndSetupEnv();
    300 
    301    info("Moving toolbox into a window...");
    302 
    303    await monitor.toolbox.switchHost("window");
    304 
    305    // Switching hosts is not correctly waiting when DevTools run in content frame
    306    // See Bug 1571421.
    307    await wait(1000);
    308 
    309    info("Testing prefs reload for a window host.");
    310    storeFirstPrefValues();
    311 
    312    // Validate and modify frontend while toolbox is in a window.
    313    validateFirstPrefValues(true);
    314    modifyFrontend(true);
    315 
    316    await restartNetMonitorAndSetupEnv();
    317 
    318    // Revalidate and reset frontend while toolbox is in a window.
    319    validateNewPrefValues(true);
    320    resetFrontend(true);
    321 
    322    await restartNetMonitorAndSetupEnv();
    323 
    324    // Revalidate.
    325    validateFirstPrefValues(true);
    326  }
    327 });