summaryrefslogtreecommitdiffstats
path: root/remote/shared/test/browser/browser_NavigationManager.js
blob: 7e0464c2fa6305558c2098b14b22a4d1bf727d80 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const { NavigationManager } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/NavigationManager.sys.mjs"
);
const { TabManager } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/TabManager.sys.mjs"
);

const FIRST_URL = "https://example.com/document-builder.sjs?html=first";
const SECOND_URL = "https://example.com/document-builder.sjs?html=second";
const THIRD_URL = "https://example.com/document-builder.sjs?html=third";

const FIRST_COOP_URL =
  "https://example.com/document-builder.sjs?headers=Cross-Origin-Opener-Policy:same-origin&html=first_coop";
const SECOND_COOP_URL =
  "https://example.net/document-builder.sjs?headers=Cross-Origin-Opener-Policy:same-origin&html=second_coop";

add_task(async function test_simpleNavigation() {
  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  const tab = addTab(gBrowser, FIRST_URL);
  const browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser);

  const navigableId = TabManager.getIdForBrowser(browser);

  navigationManager.startMonitoring();
  is(
    navigationManager.getNavigationForBrowsingContext(browser.browsingContext),
    null,
    "No navigation recorded yet"
  );
  is(events.length, 0, "No event recorded");

  await loadURL(browser, SECOND_URL);

  const firstNavigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );
  assertNavigation(firstNavigation, SECOND_URL);

  is(events.length, 2, "Two events recorded");
  assertNavigationEvents(
    events,
    SECOND_URL,
    firstNavigation.navigationId,
    navigableId
  );

  await loadURL(browser, THIRD_URL);

  const secondNavigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );
  assertNavigation(secondNavigation, THIRD_URL);
  assertUniqueNavigationIds(firstNavigation, secondNavigation);

  is(events.length, 4, "Two new events recorded");
  assertNavigationEvents(
    events,
    THIRD_URL,
    secondNavigation.navigationId,
    navigableId
  );

  navigationManager.stopMonitoring();

  // Navigate again to the first URL
  await loadURL(browser, FIRST_URL);
  is(events.length, 4, "No new event recorded");
  is(
    navigationManager.getNavigationForBrowsingContext(browser.browsingContext),
    null,
    "No navigation recorded"
  );

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("navigation-stopped", onEvent);
});

add_task(async function test_loadTwoTabsSimultaneously() {
  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  navigationManager.startMonitoring();

  info("Add two tabs simultaneously");
  const tab1 = addTab(gBrowser, FIRST_URL);
  const browser1 = tab1.linkedBrowser;
  const navigableId1 = TabManager.getIdForBrowser(browser1);
  const onLoad1 = BrowserTestUtils.browserLoaded(browser1, false, FIRST_URL);

  const tab2 = addTab(gBrowser, SECOND_URL);
  const browser2 = tab2.linkedBrowser;
  const navigableId2 = TabManager.getIdForBrowser(browser2);
  const onLoad2 = BrowserTestUtils.browserLoaded(browser2, false, SECOND_URL);

  info("Wait for the tabs to load");
  await Promise.all([onLoad1, onLoad2]);

  is(events.length, 4, "Recorded 4 navigation events");

  info("Check navigation monitored for tab1");
  const nav1 = navigationManager.getNavigationForBrowsingContext(
    browser1.browsingContext
  );
  assertNavigation(nav1, FIRST_URL);
  assertNavigationEvents(events, FIRST_URL, nav1.navigationId, navigableId1);

  info("Check navigation monitored for tab2");
  const nav2 = navigationManager.getNavigationForBrowsingContext(
    browser2.browsingContext
  );
  assertNavigation(nav2, SECOND_URL);
  assertNavigationEvents(events, SECOND_URL, nav2.navigationId, navigableId2);
  assertUniqueNavigationIds(nav1, nav2);

  info("Reload the two tabs simultaneously");
  await Promise.all([
    BrowserTestUtils.reloadTab(tab1),
    BrowserTestUtils.reloadTab(tab2),
  ]);

  is(events.length, 8, "Recorded 8 navigation events");

  info("Check the second navigation for tab1");
  const nav3 = navigationManager.getNavigationForBrowsingContext(
    browser1.browsingContext
  );
  assertNavigation(nav3, FIRST_URL);
  assertNavigationEvents(events, FIRST_URL, nav3.navigationId, navigableId1);

  info("Check the second navigation monitored for tab2");
  const nav4 = navigationManager.getNavigationForBrowsingContext(
    browser2.browsingContext
  );
  assertNavigation(nav4, SECOND_URL);
  assertNavigationEvents(events, SECOND_URL, nav4.navigationId, navigableId2);
  assertUniqueNavigationIds(nav1, nav2, nav3, nav4);

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("navigation-stopped", onEvent);
  navigationManager.stopMonitoring();
});

add_task(async function test_loadPageWithIframes() {
  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  navigationManager.startMonitoring();

  info("Add a tab with iframes");
  const testUrl = createTestPageWithFrames();
  const tab = addTab(gBrowser, testUrl);
  const browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser, false, testUrl);

  is(events.length, 8, "Recorded 8 navigation events");
  const contexts = browser.browsingContext.getAllBrowsingContextsInSubtree();

  const navigations = [];
  for (const context of contexts) {
    const navigation =
      navigationManager.getNavigationForBrowsingContext(context);
    const navigable = TabManager.getIdForBrowsingContext(context);

    const url = context.currentWindowGlobal.documentURI.spec;
    assertNavigation(navigation, url);
    assertNavigationEvents(events, url, navigation.navigationId, navigable);
    navigations.push(navigation);
  }
  assertUniqueNavigationIds(...navigations);

  await BrowserTestUtils.reloadTab(tab);

  is(events.length, 16, "Recorded 8 additional navigation events");
  const newContexts = browser.browsingContext.getAllBrowsingContextsInSubtree();

  for (const context of newContexts) {
    const navigation =
      navigationManager.getNavigationForBrowsingContext(context);
    const navigable = TabManager.getIdForBrowsingContext(context);

    const url = context.currentWindowGlobal.documentURI.spec;
    assertNavigation(navigation, url);
    assertNavigationEvents(events, url, navigation.navigationId, navigable);
    navigations.push(navigation);
  }
  assertUniqueNavigationIds(...navigations);

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("navigation-stopped", onEvent);
  navigationManager.stopMonitoring();
});

add_task(async function test_loadPageWithCoop() {
  const tab = addTab(gBrowser, FIRST_COOP_URL);
  const browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser, false, FIRST_COOP_URL);

  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  navigationManager.startMonitoring();

  const navigableId = TabManager.getIdForBrowser(browser);
  await loadURL(browser, SECOND_COOP_URL);

  const coopNavigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );
  assertNavigation(coopNavigation, SECOND_COOP_URL);

  is(events.length, 2, "Two events recorded");
  assertNavigationEvents(
    events,
    SECOND_COOP_URL,
    coopNavigation.navigationId,
    navigableId
  );

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("navigation-stopped", onEvent);
  navigationManager.stopMonitoring();
});

add_task(async function test_sameDocumentNavigation() {
  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("location-changed", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  const url = "https://example.com/document-builder.sjs?html=test";
  const tab = addTab(gBrowser, url);
  const browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser);

  navigationManager.startMonitoring();
  const navigableId = TabManager.getIdForBrowser(browser);

  is(events.length, 0, "No event recorded");

  info("Perform a same-document navigation");
  let onLocationChanged = navigationManager.once("location-changed");
  BrowserTestUtils.startLoadingURIString(browser, url + "#hash");
  await onLocationChanged;

  const hashNavigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );
  is(events.length, 1, "Recorded 1 navigation event");
  assertNavigationEvents(
    events,
    url + "#hash",
    hashNavigation.navigationId,
    navigableId,
    true
  );

  // Navigate from `url + "#hash"` to `url`, this will trigger a regular
  // navigation and we can use `loadURL` to properly wait for the navigation to
  // complete.
  info("Perform a regular navigation");
  await loadURL(browser, url);

  const regularNavigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );
  is(events.length, 3, "Recorded 2 additional navigation events");
  assertNavigationEvents(
    events,
    url,
    regularNavigation.navigationId,
    navigableId
  );

  info("Perform another same-document navigation");
  onLocationChanged = navigationManager.once("location-changed");
  BrowserTestUtils.startLoadingURIString(browser, url + "#foo");
  await onLocationChanged;

  const otherHashNavigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );

  is(events.length, 4, "Recorded 1 additional navigation event");

  info("Perform a same-hash navigation");
  onLocationChanged = navigationManager.once("location-changed");
  BrowserTestUtils.startLoadingURIString(browser, url + "#foo");
  await onLocationChanged;

  const sameHashNavigation = navigationManager.getNavigationForBrowsingContext(
    browser.browsingContext
  );

  is(events.length, 5, "Recorded 1 additional navigation event");
  assertNavigationEvents(
    events,
    url + "#foo",
    sameHashNavigation.navigationId,
    navigableId,
    true
  );

  assertUniqueNavigationIds([
    hashNavigation,
    regularNavigation,
    otherHashNavigation,
    sameHashNavigation,
  ]);

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("location-changed", onEvent);
  navigationManager.off("navigation-stopped", onEvent);

  navigationManager.stopMonitoring();
});

add_task(async function test_startNavigationAndCloseTab() {
  const events = [];
  const onEvent = (name, data) => events.push({ name, data });

  const navigationManager = new NavigationManager();
  navigationManager.on("navigation-started", onEvent);
  navigationManager.on("navigation-stopped", onEvent);

  const tab = addTab(gBrowser, FIRST_URL);
  const browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser);

  navigationManager.startMonitoring();
  loadURL(browser, SECOND_URL);
  gBrowser.removeTab(tab);

  // On top of the assertions below, the test also validates that there is no
  // unhandled promise rejection related to handling the navigation-started event
  // for the destroyed browsing context.
  is(events.length, 0, "No event was received");
  is(
    navigationManager.getNavigationForBrowsingContext(browser.browsingContext),
    null,
    "No navigation was recorded for the destroyed tab"
  );
  navigationManager.stopMonitoring();

  navigationManager.off("navigation-started", onEvent);
  navigationManager.off("navigation-stopped", onEvent);
});