summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_initiator.js
blob: 4082db6fa29098abf2643dc6bba74f1b4c02de4c (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";
const {
  getUrlBaseName,
} = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");
/**
 * Tests if request initiator is reported correctly.
 */

const INITIATOR_FILE_NAME = "html_cause-test-page.html";
const INITIATOR_URL = HTTPS_EXAMPLE_URL + INITIATOR_FILE_NAME;

const EXPECTED_REQUESTS = [
  {
    method: "GET",
    url: INITIATOR_URL,
    causeType: "document",
    causeUri: null,
    stack: false,
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "stylesheet_request",
    causeType: "stylesheet",
    causeUri: INITIATOR_URL,
    stack: false,
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "img_request",
    causeType: "img",
    causeUri: INITIATOR_URL,
    stack: false,
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "img_srcset_request",
    causeType: "imageset",
    causeUri: INITIATOR_URL,
    stack: false,
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "xhr_request",
    causeType: "xhr",
    causeUri: INITIATOR_URL,
    stack: [{ fn: "performXhrRequestCallback", file: INITIATOR_URL, line: 32 }],
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "fetch_request",
    causeType: "fetch",
    causeUri: INITIATOR_URL,
    stack: [{ fn: "performFetchRequest", file: INITIATOR_URL, line: 37 }],
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "promise_fetch_request",
    causeType: "fetch",
    causeUri: INITIATOR_URL,
    stack: [
      {
        fn: "performPromiseFetchRequestCallback",
        file: INITIATOR_URL,
        line: 43,
      },
      {
        fn: "performPromiseFetchRequest",
        file: INITIATOR_URL,
        line: 42,
        asyncCause: "promise callback",
      },
    ],
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "timeout_fetch_request",
    causeType: "fetch",
    causeUri: INITIATOR_URL,
    stack: [
      {
        fn: "performTimeoutFetchRequestCallback2",
        file: INITIATOR_URL,
        line: 50,
      },
      {
        fn: "performTimeoutFetchRequestCallback1",
        file: INITIATOR_URL,
        line: 49,
        asyncCause: "setTimeout handler",
      },
    ],
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "favicon_request",
    causeType: "img",
    causeUri: INITIATOR_URL,
    // the favicon request is triggered in FaviconLoader.jsm module, it should
    // NOT be shown in the stack (bug 1280266).  For now we intentionally
    // specify the file and the line number to be properly sorted.
    // NOTE: The line number can be an arbitrary number greater than 0.
    stack: [
      {
        file: "resource:///modules/FaviconLoader.jsm",
        line: Number.MAX_SAFE_INTEGER,
      },
    ],
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "lazy_img_request",
    causeType: "lazy-img",
    causeUri: INITIATOR_URL,
    stack: false,
  },
  {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + "lazy_img_srcset_request",
    causeType: "lazy-imageset",
    causeUri: INITIATOR_URL,
    stack: false,
  },
  {
    method: "POST",
    url: HTTPS_EXAMPLE_URL + "beacon_request",
    causeType: "beacon",
    causeUri: INITIATOR_URL,
    stack: [{ fn: "performBeaconRequest", file: INITIATOR_URL, line: 82 }],
  },
];

add_task(async function () {
  // the initNetMonitor function clears the network request list after the
  // page is loaded. That's why we first load a bogus page from SIMPLE_URL,
  // and only then load the real thing from INITIATOR_URL - we want to catch
  // all the requests the page is making, not only the XHRs.
  // We can't use about:blank here, because initNetMonitor checks that the
  // page has actually made at least one request.
  const { tab, monitor } = await initNetMonitor(SIMPLE_URL, {
    requestCount: 1,
  });

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

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

  const wait = waitForNetworkEvents(monitor, EXPECTED_REQUESTS.length);
  BrowserTestUtils.loadURIString(tab.linkedBrowser, INITIATOR_URL);

  registerFaviconNotifier(tab.linkedBrowser);

  await wait;

  // For all expected requests
  for (const [index, { stack }] of EXPECTED_REQUESTS.entries()) {
    if (!stack) {
      continue;
    }

    EventUtils.sendMouseEvent(
      { type: "mousedown" },
      document.querySelectorAll(
        ".request-list-item .requests-list-initiator-lastframe"
      )[index]
    );

    // Clicking on the initiator column should open the Stack Trace panel
    const onStackTraceRendered = waitUntil(() =>
      document.querySelector("#stack-trace-panel .stack-trace .frame-link")
    );
    await onStackTraceRendered;
  }

  is(
    store.getState().requests.requests.length,
    EXPECTED_REQUESTS.length,
    "All the page events should be recorded."
  );

  validateRequests(EXPECTED_REQUESTS, monitor);

  // Sort the requests by initiator and check the order
  EventUtils.sendMouseEvent(
    { type: "click" },
    document.querySelector("#requests-list-initiator-button")
  );

  const expectedOrder = EXPECTED_REQUESTS.sort(initiatorSortPredicate).map(
    r => {
      let isChromeFrames = false;
      const lastFrameExists = !!r.stack;
      let initiator = "";
      let lineNumber = "";
      if (lastFrameExists) {
        const { file, line: _lineNumber } = r.stack[0];
        initiator = getUrlBaseName(file);
        lineNumber = ":" + _lineNumber;
        isChromeFrames = file.startsWith("resource:///");
      }
      const causeStr = lastFrameExists ? " (" + r.causeType + ")" : r.causeType;
      return {
        initiatorStr: initiator + lineNumber + causeStr,
        isChromeFrames,
      };
    }
  );

  expectedOrder.forEach((expectedInitiator, i) => {
    const request = getSortedRequests(store.getState())[i];
    let initiator;
    // In cases of chrome frames, we shouldn't have stack.
    if (
      request.cause.stacktraceAvailable &&
      !expectedInitiator.isChromeFrames
    ) {
      const { filename, lineNumber } = request.cause.lastFrame;
      initiator =
        getUrlBaseName(filename) +
        ":" +
        lineNumber +
        " (" +
        request.cause.type +
        ")";
    } else {
      initiator = request.cause.type;
    }

    if (expectedInitiator.isChromeFrames) {
      todo_is(
        initiator,
        expectedInitiator.initiatorStr,
        `The request #${i} has the expected initiator after sorting`
      );
    } else {
      is(
        initiator,
        expectedInitiator.initiatorStr,
        `The request #${i} has the expected initiator after sorting`
      );
    }
  });

  await teardown(monitor);
});

// derived from devtools/client/netmonitor/src/utils/sort-predicates.js
function initiatorSortPredicate(first, second) {
  const firstLastFrame = first.stack ? first.stack[0] : null;
  const secondLastFrame = second.stack ? second.stack[0] : null;

  let firstInitiator = "";
  let firstInitiatorLineNumber = 0;

  if (firstLastFrame) {
    firstInitiator = getUrlBaseName(firstLastFrame.file);
    firstInitiatorLineNumber = firstLastFrame.line;
  }

  let secondInitiator = "";
  let secondInitiatorLineNumber = 0;

  if (secondLastFrame) {
    secondInitiator = getUrlBaseName(secondLastFrame.file);
    secondInitiatorLineNumber = secondLastFrame.line;
  }

  let result;
  // if both initiators don't have a stack trace, compare their causes
  if (!firstInitiator && !secondInitiator) {
    result = compareValues(first.causeType, second.causeType);
  } else if (!firstInitiator || !secondInitiator) {
    // if one initiator doesn't have a stack trace but the other does, former should precede the latter
    result = compareValues(firstInitiatorLineNumber, secondInitiatorLineNumber);
  } else {
    result = compareValues(firstInitiator, secondInitiator);
    if (result === 0) {
      result = compareValues(
        firstInitiatorLineNumber,
        secondInitiatorLineNumber
      );
    }
  }
  return result;
}