tor-browser

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

browser_net_status-bar-transferred-size.js (5371B)


      1 "use strict";
      2 
      3 /**
      4 * Test if the value of total data transferred is displayed correctly in the Status Bar
      5 * Test for Bug 1481002
      6 */
      7 add_task(async function testTotalTransferredSize() {
      8  // Clear cache, so we see expected number of cached requests.
      9  Services.cache2.clear();
     10  // Disable rcwn to make cache behavior deterministic.
     11  await pushPref("network.http.rcwn.enabled", false);
     12 
     13  const {
     14    getFormattedSize,
     15  } = require("resource://devtools/client/netmonitor/src/utils/format-utils.js");
     16 
     17  const { tab, monitor } = await initNetMonitor(STATUS_CODES_URL, {
     18    enableCache: true,
     19    requestCount: 1,
     20  });
     21  info("Starting test... ");
     22 
     23  const { document, store, windowRequire } = monitor.panelWin;
     24  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     25  const { getDisplayedRequestsSummary } = windowRequire(
     26    "devtools/client/netmonitor/src/selectors/index"
     27  );
     28  const l10n = new Localization(["devtools/client/netmonitor.ftl"], true);
     29 
     30  store.dispatch(Actions.batchEnable(false));
     31 
     32  info("Performing requests...");
     33  const onNetworkEvents = waitForNetworkEvents(monitor, 2);
     34  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
     35    content.wrappedJSObject.performOneCachedRequest();
     36  });
     37  info("Wait until we get network events for cached request ");
     38  await onNetworkEvents;
     39 
     40  let cachedItemsInUI = 0;
     41  for (const requestItem of document.querySelectorAll(".request-list-item")) {
     42    const requestTransferStatus = requestItem.querySelector(
     43      ".requests-list-transferred"
     44    ).textContent;
     45    if (requestTransferStatus === "cached") {
     46      cachedItemsInUI++;
     47    }
     48  }
     49 
     50  is(cachedItemsInUI, 1, "Number of cached requests displayed is correct");
     51 
     52  const state = store.getState();
     53  const totalRequestsCount = state.requests.requests.length;
     54  const requestsSummary = getDisplayedRequestsSummary(state);
     55  info(`Current requests: ${requestsSummary.count} of ${totalRequestsCount}.`);
     56 
     57  const valueTransfer = document.querySelector(
     58    ".requests-list-network-summary-transfer"
     59  ).textContent;
     60  info("Current summary transfer: " + valueTransfer);
     61  const expectedTransfer = l10n.formatValueSync(
     62    "network-menu-summary-transferred",
     63    {
     64      formattedContentSize: getFormattedSize(requestsSummary.contentSize),
     65      formattedTransferredSize: getFormattedSize(
     66        requestsSummary.transferredSize
     67      ),
     68    }
     69  );
     70 
     71  is(
     72    valueTransfer,
     73    expectedTransfer,
     74    "The current summary transfer is correct."
     75  );
     76 
     77  await teardown(monitor);
     78 });
     79 
     80 // Tests the size for the service worker requests are not included as part
     81 // of the total transferred size.
     82 add_task(async function testTotalTransferredSizeWithServiceWorkerRequests() {
     83  // Service workers only work on https
     84  const TEST_URL = HTTPS_EXAMPLE_URL + "service-workers/status-codes.html";
     85  const { tab, monitor } = await initNetMonitor(TEST_URL, {
     86    enableCache: true,
     87    requestCount: 1,
     88  });
     89  info("Starting test... ");
     90 
     91  const { store, windowRequire } = monitor.panelWin;
     92  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     93  const { getDisplayedRequestsSummary, getDisplayedRequests } = windowRequire(
     94    "devtools/client/netmonitor/src/selectors/index"
     95  );
     96 
     97  store.dispatch(Actions.batchEnable(false));
     98 
     99  info("Performing requests before service worker...");
    100  await performRequests(monitor, tab, 1);
    101 
    102  info("Registering the service worker...");
    103  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    104    await content.wrappedJSObject.registerServiceWorker();
    105  });
    106 
    107  info("Performing requests which are intercepted by service worker...");
    108  await performRequests(monitor, tab, 1);
    109 
    110  let displayedServiceWorkerRequests = 0;
    111  //let totalRequestsTransferredSize = 0;
    112  let totalRequestsTransferredSizeWithoutServiceWorkers = 0;
    113 
    114  const displayedRequests = getDisplayedRequests(store.getState());
    115 
    116  for (const request of displayedRequests) {
    117    if (request.fromServiceWorker === true) {
    118      displayedServiceWorkerRequests++;
    119    } else {
    120      totalRequestsTransferredSizeWithoutServiceWorkers +=
    121        request.transferredSize;
    122    }
    123    //totalRequestsTransferredSize += request.transferredSize;
    124  }
    125 
    126  is(
    127    displayedServiceWorkerRequests,
    128    4,
    129    "Number of service worker requests displayed is correct"
    130  );
    131 
    132  /* 
    133    NOTE: Currently only intercepted service worker requests are displayed and the transferred times for these are 
    134    mostly always zero. Once Bug 1432311 (for fetch requests from the service worker) gets fixed, there should be requests with
    135    > 0 transfered times, allowing to assert this properly.
    136    isnot(
    137      totalRequestsTransferredSize,
    138      totalRequestsTransferredSizeWithoutServiceWorkers, 
    139      "The  total transferred size including service worker requests is not equal to the total transferred size excluding service worker requests"
    140    );
    141  */
    142 
    143  const requestsSummary = getDisplayedRequestsSummary(store.getState());
    144 
    145  is(
    146    totalRequestsTransferredSizeWithoutServiceWorkers,
    147    requestsSummary.transferredSize,
    148    "The current total transferred size is correct."
    149  );
    150 
    151  info("Unregistering the service worker...");
    152  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    153    await content.wrappedJSObject.unregisterServiceWorker();
    154  });
    155 
    156  await teardown(monitor);
    157 });