summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_open_request_in_tab.js
blob: db96c350cabd1150ce3d521bad8ce8f98c01db0d (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests if Open in new tab works by ContextMenu.
 */

add_task(async function () {
  const { tab, monitor } = await initNetMonitor(OPEN_REQUEST_IN_TAB_URL, {
    requestCount: 1,
  });
  info("Starting test...");

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

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

  // Post data may be fetched by the Header panel,
  // so set the Timings panel as the new default.
  store.getState().ui.detailsPanelSelectedTab = "timings";

  // Open GET request in new tab
  await performRequest(monitor, tab, "GET");
  newTab = await openLastRequestInTab();
  await checkTabResponse(newTab, "GET");
  gBrowser.removeCurrentTab();

  // Open POST request in new tab
  await performRequest(
    monitor,
    tab,
    "POST",
    "application/x-www-form-urlencoded",
    "foo=bar&baz=42"
  );
  newTab = await openLastRequestInTab();
  await checkTabResponse(
    newTab,
    "POST",
    "application/x-www-form-urlencoded",
    "foo=bar&baz=42"
  );
  gBrowser.removeCurrentTab();

  // Open POST application/json request in new tab
  await performRequest(
    monitor,
    tab,
    "POST",
    "application/json",
    '{"foo":"bar"}'
  );
  newTab = await openLastRequestInTab();
  await checkTabResponse(newTab, "POST", "application/json", '{"foo":"bar"}');
  gBrowser.removeCurrentTab();

  await teardown(monitor);

  // OpenLastRequestInTab by ContextMenu
  async function openLastRequestInTab() {
    const requestItems = document.querySelectorAll(".request-list-item");
    const lastRequest = requestItems[requestItems.length - 1];
    EventUtils.sendMouseEvent({ type: "mousedown" }, lastRequest);
    EventUtils.sendMouseEvent({ type: "contextmenu" }, lastRequest);

    const onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
    await selectContextMenuItem(monitor, "request-list-context-newtab");
    await onTabOpen;
    info("A new tab has been opened");

    const awaitedTab = gBrowser.selectedTab;
    await BrowserTestUtils.browserLoaded(awaitedTab.linkedBrowser);
    info("The tab load completed");

    return awaitedTab;
  }
});

/**
 * Tests if Open in new tab works by DoubleClick RequestItem.
 */

add_task(async function () {
  const { tab, monitor } = await initNetMonitor(OPEN_REQUEST_IN_TAB_URL, {
    requestCount: 1,
  });
  info("Starting test...");

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

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

  // Post data may be fetched by the Header panel,
  // so set the Timings panel as the new default.
  store.getState().ui.detailsPanelSelectedTab = "timings";

  // Open GET request in new tab
  await performRequest(monitor, tab, "GET");
  newTab = await openLastRequestInTab();
  await checkTabResponse(newTab, "GET");
  gBrowser.removeCurrentTab();

  // Open POST request in new tab
  await performRequest(
    monitor,
    tab,
    "POST",
    "application/x-www-form-urlencoded",
    "foo=bar&baz=42"
  );
  newTab = await openLastRequestInTab();
  await checkTabResponse(
    newTab,
    "POST",
    "application/x-www-form-urlencoded",
    "foo=bar&baz=42"
  );
  gBrowser.removeCurrentTab();

  // Open POST application/json request in new tab
  await performRequest(
    monitor,
    tab,
    "POST",
    "application/json",
    '{"foo":"bar"}'
  );
  newTab = await openLastRequestInTab();
  await checkTabResponse(newTab, "POST", "application/json", '{"foo":"bar"}');
  gBrowser.removeCurrentTab();

  await teardown(monitor);

  // OpenLastRequestInTab by DoubleClick
  async function openLastRequestInTab() {
    const requestItems = document.querySelectorAll(".request-list-item");
    const lastRequest = requestItems[requestItems.length - 1];

    const onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
    EventUtils.sendMouseEvent({ type: "dblclick" }, lastRequest);
    await onTabOpen;
    info("A new tab has been opened");

    const awaitedTab = gBrowser.selectedTab;
    await BrowserTestUtils.browserLoaded(awaitedTab.linkedBrowser);
    info("The tab load completed");

    return awaitedTab;
  }
});

/**
 * Tests if Open in new tab works by middle click RequestItem.
 */

add_task(async function () {
  const { tab, monitor } = await initNetMonitor(OPEN_REQUEST_IN_TAB_URL, {
    requestCount: 1,
  });
  const MIDDLE_MOUSE_BUTTON = 1;
  info("Starting test...");

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

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

  // Post data may be fetched by the Header panel,
  // so set the Timings panel as the new default.
  store.getState().ui.detailsPanelSelectedTab = "timings";

  // Open GET request in new tab
  await performRequest(monitor, tab, "GET");
  newTab = await openLastRequestInTab();
  await checkTabResponse(newTab, "GET");
  gBrowser.removeCurrentTab();

  // Open POST request in new tab
  await performRequest(
    monitor,
    tab,
    "POST",
    "application/x-www-form-urlencoded",
    "foo=bar&baz=42"
  );
  newTab = await openLastRequestInTab();
  await checkTabResponse(
    newTab,
    "POST",
    "application/x-www-form-urlencoded",
    "foo=bar&baz=42"
  );
  gBrowser.removeCurrentTab();

  // Open POST application/json request in new tab
  await performRequest(
    monitor,
    tab,
    "POST",
    "application/json",
    '{"foo":"bar"}'
  );
  newTab = await openLastRequestInTab();
  await checkTabResponse(newTab, "POST", "application/json", '{"foo":"bar"}');
  gBrowser.removeCurrentTab();

  await teardown(monitor);

  // OpenLastRequestInTab by middle click
  async function openLastRequestInTab() {
    const requestItems = document.querySelectorAll(".request-list-item");
    const lastRequest = requestItems[requestItems.length - 1];

    const onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
    EventUtils.sendMouseEvent(
      { type: "mousedown", button: MIDDLE_MOUSE_BUTTON },
      lastRequest
    );
    await onTabOpen;
    info("A new tab has been opened");

    const awaitedTab = gBrowser.selectedTab;
    await BrowserTestUtils.browserLoaded(awaitedTab.linkedBrowser);
    info("The tab load completed");

    return awaitedTab;
  }
});

async function performRequest(monitor, tab, method, contentType, payload) {
  const wait = waitForNetworkEvents(monitor, 1);
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [[method, contentType, payload]],
    async function ([method_, contentType_, payload_]) {
      content.wrappedJSObject.performRequest(method_, contentType_, payload_);
    }
  );
  await wait;
  info("Performed request to test server");
}

async function checkTabResponse(checkedTab, method, contentType, payload) {
  await SpecialPowers.spawn(
    checkedTab.linkedBrowser,
    [[method, contentType, payload]],
    async function ([method_, contentType_, payload_]) {
      const { body } = content.wrappedJSObject.document;
      const expected = [method_, contentType_, payload_].join("\n");
      info("Response from the server:" + body.innerHTML.replace(/\n/g, "\\n"));
      ok(
        body.innerHTML.includes(expected),
        "Tab method and data match original request"
      );
    }
  );
}