tor-browser

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

browser_net_headers-resize.js (6895B)


      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 resizing of columns in NetMonitor.
      8 */
      9 add_task(async function () {
     10  await testForGivenDir("ltr");
     11 
     12  await testForGivenDir("rtl");
     13 });
     14 
     15 async function testForGivenDir(dir) {
     16  if (dir === "rtl") {
     17    await pushPref("intl.l10n.pseudo", "bidi");
     18  } else {
     19    await pushPref("intl.l10n.pseudo", "");
     20  }
     21 
     22  // Reset visibleColumns so we only get the default ones
     23  // and not all that are set in head.js
     24  Services.prefs.clearUserPref("devtools.netmonitor.visibleColumns");
     25 
     26  const initialColumnData = Services.prefs.getCharPref(
     27    "devtools.netmonitor.columnsData"
     28  );
     29 
     30  // Init network monitor
     31  const { monitor } = await initNetMonitor(SIMPLE_URL, {
     32    requestCount: 1,
     33  });
     34  info("Starting test... ");
     35  let visibleColumns = getCurrentVisibleColumns(monitor);
     36  const { document, windowRequire, store } = monitor.panelWin;
     37  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     38  store.dispatch(Actions.batchEnable(false));
     39 
     40  // Wait for network events (to have some requests in the table)
     41  const wait = waitForNetworkEvents(monitor, 1);
     42  await reloadBrowser();
     43  await wait;
     44 
     45  const headers = document.querySelector(".requests-list-headers");
     46  const parentWidth = headers.getBoundingClientRect().width;
     47 
     48  // 1. Change File column from 25% (default) to 20%
     49  // Size column should then change from 5% (default) to 10%
     50  // When File width changes, contentSize should compensate the change.
     51  info("Resize file & check changed prefs...");
     52  const fileHeader = document.querySelector(`#requests-list-file-header-box`);
     53 
     54  resizeColumn(fileHeader, 20, parentWidth, dir);
     55 
     56  // after resize - get fresh prefs for tests
     57  let columnsData = JSON.parse(
     58    Services.prefs.getCharPref("devtools.netmonitor.columnsData")
     59  );
     60  checkColumnsData(columnsData, "file", 20);
     61  checkColumnsData(columnsData, "contentSize", 10);
     62  checkSumOfVisibleColumns(columnsData, visibleColumns);
     63 
     64  // 2. Change Waterfall column width and check that the size
     65  // of waterfall changed correctly and all the other columns changed size.
     66  info("Resize waterfall & check changed prefs...");
     67  const waterfallHeader = document.querySelector(
     68    `#requests-list-waterfall-header-box`
     69  );
     70  // before resizing waterfall -> save old columnsData for later testing
     71  const oldColumnsData = JSON.parse(
     72    Services.prefs.getCharPref("devtools.netmonitor.columnsData")
     73  );
     74  resizeWaterfallColumn(waterfallHeader, 30, parentWidth, dir); // 30 fails currently!
     75 
     76  // after resize - get fresh prefs for tests
     77  columnsData = JSON.parse(
     78    Services.prefs.getCharPref("devtools.netmonitor.columnsData")
     79  );
     80 
     81  checkColumnsData(columnsData, "waterfall", 30);
     82  checkSumOfVisibleColumns(columnsData, visibleColumns);
     83  checkAllColumnsChanged(columnsData, oldColumnsData, visibleColumns);
     84 
     85  // 3. Check that all rows have the right column sizes.
     86  info("Checking alignment of columns and headers...");
     87  const requestsContainer = document.querySelector(".requests-list-row-group");
     88  testColumnsAlignment(headers, requestsContainer);
     89 
     90  // 4. Hide all columns but size and waterfall
     91  // and check that they resize correctly. Then resize
     92  // waterfall to 50% => size should take up 50%
     93  info("Hide all but 2 columns - size & waterfall and check resizing...");
     94  await hideMoreColumns(monitor, [
     95    "status",
     96    "method",
     97    "domain",
     98    "file",
     99    "initiator",
    100    "type",
    101    "transferred",
    102  ]);
    103 
    104  resizeWaterfallColumn(waterfallHeader, 50, parentWidth, dir);
    105  // after resize - get fresh prefs for tests
    106  columnsData = JSON.parse(
    107    Services.prefs.getCharPref("devtools.netmonitor.columnsData")
    108  );
    109  visibleColumns = getCurrentVisibleColumns(monitor);
    110 
    111  checkColumnsData(columnsData, "contentSize", 50);
    112  checkColumnsData(columnsData, "waterfall", 50);
    113  checkSumOfVisibleColumns(columnsData, visibleColumns);
    114 
    115  // 5. Hide all columns but domain and file
    116  // and resize domain to 50% => file should be 50%
    117  info("Hide all but 2 columns - domain & file and check resizing...");
    118  await showMoreColumns(monitor, ["domain", "file"]);
    119  await hideMoreColumns(monitor, ["contentSize", "waterfall"]);
    120 
    121  const domainHeader = document.querySelector(
    122    `#requests-list-domain-header-box`
    123  );
    124  resizeColumn(domainHeader, 50, parentWidth, dir);
    125 
    126  // after resize - get fresh prefs for tests
    127  columnsData = JSON.parse(
    128    Services.prefs.getCharPref("devtools.netmonitor.columnsData")
    129  );
    130 
    131  visibleColumns = JSON.parse(
    132    Services.prefs.getCharPref("devtools.netmonitor.visibleColumns")
    133  );
    134 
    135  checkColumnsData(columnsData, "domain", 50);
    136  checkColumnsData(columnsData, "file", 50);
    137  checkSumOfVisibleColumns(columnsData, visibleColumns);
    138 
    139  // Done: clean up.
    140  Services.prefs.setCharPref(
    141    "devtools.netmonitor.columnsData",
    142    initialColumnData
    143  );
    144  return teardown(monitor);
    145 }
    146 
    147 async function hideMoreColumns(monitor, arr) {
    148  for (let i = 0; i < arr.length; i++) {
    149    await hideColumn(monitor, arr[i]);
    150  }
    151 }
    152 
    153 async function showMoreColumns(monitor, arr) {
    154  for (let i = 0; i < arr.length; i++) {
    155    await showColumn(monitor, arr[i]);
    156  }
    157 }
    158 
    159 function checkColumnsData(columnsData, column, expectedWidth) {
    160  const widthInPref = Math.round(getWidthFromPref(columnsData, column));
    161  is(widthInPref, expectedWidth, "Column " + column + " has expected size.");
    162 }
    163 
    164 function checkSumOfVisibleColumns(columnsData, visibleColumns) {
    165  let sum = 0;
    166  visibleColumns.forEach(column => {
    167    sum += getWidthFromPref(columnsData, column);
    168  });
    169  sum = Math.round(sum);
    170  is(sum, 100, "All visible columns cover 100%.");
    171 }
    172 
    173 function getWidthFromPref(columnsData, column) {
    174  const widthInPref = columnsData.find(function (element) {
    175    return element.name === column;
    176  }).width;
    177  return widthInPref;
    178 }
    179 
    180 function checkAllColumnsChanged(columnsData, oldColumnsData, visibleColumns) {
    181  const oldWaterfallWidth = getWidthFromPref(oldColumnsData, "waterfall");
    182  const newWaterfallWidth = getWidthFromPref(columnsData, "waterfall");
    183  visibleColumns.forEach(column => {
    184    // do not test waterfall against waterfall
    185    if (column !== "waterfall") {
    186      const oldWidth = getWidthFromPref(oldColumnsData, column);
    187      const newWidth = getWidthFromPref(columnsData, column);
    188 
    189      // Test that if waterfall is smaller all other columns are bigger
    190      if (oldWaterfallWidth > newWaterfallWidth) {
    191        is(
    192          oldWidth < newWidth,
    193          true,
    194          "Column " + column + " has changed width correctly."
    195        );
    196      }
    197      // Test that if waterfall is bigger all other columns are smaller
    198      if (oldWaterfallWidth < newWaterfallWidth) {
    199        is(
    200          oldWidth > newWidth,
    201          true,
    202          "Column " + column + " has changed width correctly."
    203        );
    204      }
    205    }
    206  });
    207 }