summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_status-bar-transferred-size.js
blob: 4165ffbcd9b4c7a0d59d7884c6e578768132f722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict";

/**
 * Test if the value of total data transferred is displayed correctly in the Status Bar
 * Test for Bug 1481002
 */
add_task(async function testTotalTransferredSize() {
  // Clear cache, so we see expected number of cached requests.
  Services.cache2.clear();
  // Disable rcwn to make cache behavior deterministic.
  await pushPref("network.http.rcwn.enabled", false);

  const {
    getFormattedSize,
  } = require("resource://devtools/client/netmonitor/src/utils/format-utils.js");

  const { tab, monitor } = await initNetMonitor(STATUS_CODES_URL, {
    enableCache: true,
    requestCount: 1,
  });
  info("Starting test... ");

  const { document, store, windowRequire } = monitor.panelWin;
  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
  const { getDisplayedRequestsSummary } = windowRequire(
    "devtools/client/netmonitor/src/selectors/index"
  );
  const { L10N } = windowRequire("devtools/client/netmonitor/src/utils/l10n");

  store.dispatch(Actions.batchEnable(false));

  info("Performing requests...");
  const onNetworkEvents = waitForNetworkEvents(monitor, 2);
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    content.wrappedJSObject.performOneCachedRequest();
  });
  info("Wait until we get network events for cached request ");
  await onNetworkEvents;

  let cachedItemsInUI = 0;
  for (const requestItem of document.querySelectorAll(".request-list-item")) {
    const requestTransferStatus = requestItem.querySelector(
      ".requests-list-transferred"
    ).textContent;
    if (requestTransferStatus === "cached") {
      cachedItemsInUI++;
    }
  }

  is(cachedItemsInUI, 1, "Number of cached requests displayed is correct");

  const state = store.getState();
  const totalRequestsCount = state.requests.requests.length;
  const requestsSummary = getDisplayedRequestsSummary(state);
  info(`Current requests: ${requestsSummary.count} of ${totalRequestsCount}.`);

  const valueTransfer = document.querySelector(
    ".requests-list-network-summary-transfer"
  ).textContent;
  info("Current summary transfer: " + valueTransfer);
  const expectedTransfer = L10N.getFormatStrWithNumbers(
    "networkMenu.summary.transferred",
    getFormattedSize(requestsSummary.contentSize),
    getFormattedSize(requestsSummary.transferredSize)
  );

  is(
    valueTransfer,
    expectedTransfer,
    "The current summary transfer is correct."
  );

  await teardown(monitor);
});

// Tests the size for the service worker requests are not included as part
// of the total transferred size.
add_task(async function testTotalTransferredSizeWithServiceWorkerRequests() {
  // Service workers only work on https
  const TEST_URL = HTTPS_EXAMPLE_URL + "service-workers/status-codes.html";
  const { tab, monitor } = await initNetMonitor(TEST_URL, {
    enableCache: true,
    requestCount: 1,
  });
  info("Starting test... ");

  const { store, windowRequire } = monitor.panelWin;
  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
  const { getDisplayedRequestsSummary, getDisplayedRequests } = windowRequire(
    "devtools/client/netmonitor/src/selectors/index"
  );

  store.dispatch(Actions.batchEnable(false));

  info("Performing requests before service worker...");
  await performRequests(monitor, tab, 1);

  info("Registering the service worker...");
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    await content.wrappedJSObject.registerServiceWorker();
  });

  info("Performing requests which are intercepted by service worker...");
  await performRequests(monitor, tab, 1);

  let displayedServiceWorkerRequests = 0;
  //let totalRequestsTransferredSize = 0;
  let totalRequestsTransferredSizeWithoutServiceWorkers = 0;

  const displayedRequests = getDisplayedRequests(store.getState());

  for (const request of displayedRequests) {
    if (request.fromServiceWorker === true) {
      displayedServiceWorkerRequests++;
    } else {
      totalRequestsTransferredSizeWithoutServiceWorkers +=
        request.transferredSize;
    }
    //totalRequestsTransferredSize += request.transferredSize;
  }

  is(
    displayedServiceWorkerRequests,
    4,
    "Number of service worker requests displayed is correct"
  );

  /* 
    NOTE: Currently only intercepted service worker requests are displayed and the transferred times for these are 
    mostly always zero. Once Bug 1432311 (for fetch requests from the service worker) gets fixed, there should be requests with
    > 0 transfered times, allowing to assert this properly.
    isnot(
      totalRequestsTransferredSize,
      totalRequestsTransferredSizeWithoutServiceWorkers, 
      "The  total transferred size including service worker requests is not equal to the total transferred size excluding service worker requests"
    );
  */

  const requestsSummary = getDisplayedRequestsSummary(store.getState());

  is(
    totalRequestsTransferredSizeWithoutServiceWorkers,
    requestsSummary.transferredSize,
    "The current total transferred size is correct."
  );

  info("Unregistering the service worker...");
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    await content.wrappedJSObject.unregisterServiceWorker();
  });

  await teardown(monitor);
});