summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/target/tests/browser_target_command_bfcache.js
blob: c5ce76848efbeff39a325f2f625625fa7005e390 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the TargetCommand API when bfcache navigations happen

const TEST_COM_URL = URL_ROOT_SSL + "simple_document.html";

add_task(async function () {
  // Disable the preloaded process as it gets created lazily and may interfere
  // with process count assertions
  await pushPref("dom.ipc.processPrelaunch.enabled", false);
  // This preference helps destroying the content process when we close the tab
  await pushPref("dom.ipc.keepProcessesAlive.web", 1);

  info("## Test with bfcache in parent DISABLED");
  await pushPref("fission.bfcacheInParent", false);
  await testTopLevelNavigations(false);
  await testIframeNavigations(false);
  await testTopLevelNavigationsOnDocumentWithIframe(false);

  // bfcacheInParent only works if sessionHistoryInParent is enable
  // so only test it if both settings are enabled.
  // (it looks like sessionHistoryInParent is enabled by default when fission is enabled)
  if (Services.appinfo.sessionHistoryInParent) {
    info("## Test with bfcache in parent ENABLED");
    await pushPref("fission.bfcacheInParent", true);
    await testTopLevelNavigations(true);
    await testIframeNavigations(true);
    await testTopLevelNavigationsOnDocumentWithIframe(true);
  }
});

async function testTopLevelNavigations(bfcacheInParent) {
  info(" # Test TOP LEVEL navigations");
  // Create a TargetCommand for a given test tab
  const tab = await addTab(TEST_COM_URL);
  const commands = await CommandsFactory.forTab(tab);
  const targetCommand = commands.targetCommand;
  const { TYPES } = targetCommand;

  await targetCommand.startListening();

  // Assert that watchTargets will call the create callback for all existing frames
  const targets = [];
  const onAvailable = async ({ targetFront }) => {
    is(
      targetFront.targetType,
      TYPES.FRAME,
      "We are only notified about frame targets"
    );
    ok(targetFront.isTopLevel, "all targets of this test are top level");
    targets.push(targetFront);
  };
  const destroyedTargets = [];
  const onDestroyed = async ({ targetFront }) => {
    is(
      targetFront.targetType,
      TYPES.FRAME,
      "We are only notified about frame targets"
    );
    ok(targetFront.isTopLevel, "all targets of this test are top level");
    destroyedTargets.push(targetFront);
  };

  await targetCommand.watchTargets({
    types: [TYPES.FRAME],
    onAvailable,
    onDestroyed,
  });
  is(targets.length, 1, "retrieved only the top level target");
  is(targets[0], targetCommand.targetFront, "the target is the top level one");
  is(
    destroyedTargets.length,
    0,
    "We get no destruction when calling watchTargets"
  );
  ok(
    targets[0].targetForm.followWindowGlobalLifeCycle,
    "the first server side target follows the WindowGlobal lifecycle, when server target switching is enabled"
  );

  // Navigate to the same page with query params
  info("Load the second page");
  const secondPageUrl = TEST_COM_URL + "?second-load";
  const previousBrowsingContextID = gBrowser.selectedBrowser.browsingContext.id;
  ok(
    previousBrowsingContextID,
    "Fetch the tab's browsing context id before navigation"
  );
  const onLoaded = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    secondPageUrl
  );
  BrowserTestUtils.startLoadingURIString(
    gBrowser.selectedBrowser,
    secondPageUrl
  );
  await onLoaded;

  // Assert BrowsingContext changes as it impact the behavior of targets
  if (bfcacheInParent) {
    isnot(
      previousBrowsingContextID,
      gBrowser.selectedBrowser.browsingContext.id,
      "When bfcacheInParent is enabled, same-origin navigations spawn new BrowsingContext"
    );
  } else {
    is(
      previousBrowsingContextID,
      gBrowser.selectedBrowser.browsingContext.id,
      "When bfcacheInParent is disabled, same-origin navigations re-use the same BrowsingContext"
    );
  }

  // Same-origin navigations also spawn a new top level target
  await waitFor(
    () => targets.length == 2,
    "wait for the next top level target"
  );
  is(
    targets[1],
    targetCommand.targetFront,
    "the second target is the top level one"
  );
  // As targetFront.url isn't reliable and might be about:blank,
  // try to assert that we got the right target via other means.
  // outerWindowID should change when navigating to another process,
  // while it would stay equal for in-process navigations.
  is(
    targets[1].outerWindowID,
    gBrowser.selectedBrowser.outerWindowID,
    "the second target is for the second page"
  );
  ok(
    targets[1].targetForm.followWindowGlobalLifeCycle,
    "the new server side target follows the WindowGlobal lifecycle"
  );
  ok(targets[0].isDestroyed(), "the first target is destroyed");
  is(destroyedTargets.length, 1, "We get one target being destroyed...");
  is(destroyedTargets[0], targets[0], "...and that's the first one");

  // Go back to the first page, this should be a bfcache navigation, and,
  // we should get a new target
  info("Go back to the first page");
  gBrowser.selectedBrowser.goBack();

  await waitFor(
    () => targets.length == 3,
    "wait for the next top level target"
  );
  is(
    targets[2],
    targetCommand.targetFront,
    "the third target is the top level one"
  );
  // Here as this is revived from cache, the url should always be correct
  is(targets[2].url, TEST_COM_URL, "the third target is for the first url");
  ok(
    targets[2].targetForm.followWindowGlobalLifeCycle,
    "the third target for bfcache navigations is following the WindowGlobal lifecycle"
  );
  ok(targets[1].isDestroyed(), "the second target is destroyed");
  is(
    destroyedTargets.length,
    2,
    "We get one additional target being destroyed..."
  );
  is(destroyedTargets[1], targets[1], "...and that's the second one");

  // Wait for full attach in order to having breaking any pending requests
  // when navigating to another page and switching to new process and target.
  await waitForAllTargetsToBeAttached(targetCommand);

  // Go forward and resurect the second page, this should also be a bfcache navigation, and,
  // get a new target.
  info("Go forward to the second page");

  // When a new target will be created, we need to wait until it's fully processed
  // to avoid pending promises.
  const onNewTargetProcessed = bfcacheInParent
    ? new Promise(resolve => {
        targetCommand.on(
          "processed-available-target",
          function onProcessedAvailableTarget(targetFront) {
            if (targetFront === targets[3]) {
              resolve();
              targetCommand.off(
                "processed-available-target",
                onProcessedAvailableTarget
              );
            }
          }
        );
      })
    : null;

  gBrowser.selectedBrowser.goForward();

  await waitFor(
    () => targets.length == 4,
    "wait for the next top level target"
  );
  is(
    targets[3],
    targetCommand.targetFront,
    "the 4th target is the top level one"
  );
  // Same here, as the document is revived from the cache, the url should always be correct
  is(targets[3].url, secondPageUrl, "the 4th target is for the second url");
  ok(
    targets[3].targetForm.followWindowGlobalLifeCycle,
    "the 4th target for bfcache navigations is following the WindowGlobal lifecycle"
  );
  ok(targets[2].isDestroyed(), "the third target is destroyed");
  is(
    destroyedTargets.length,
    3,
    "We get one additional target being destroyed..."
  );
  is(destroyedTargets[2], targets[2], "...and that's the third one");

  // Wait for full attach in order to having breaking any pending requests
  // when navigating to another page and switching to new process and target.
  await waitForAllTargetsToBeAttached(targetCommand);
  await onNewTargetProcessed;

  await waitForAllTargetsToBeAttached(targetCommand);

  targetCommand.unwatchTargets({ types: [TYPES.FRAME], onAvailable });

  BrowserTestUtils.removeTab(tab);

  await commands.destroy();
}

async function testTopLevelNavigationsOnDocumentWithIframe() {
  info(" # Test TOP LEVEL navigations on document with iframe");
  // Create a TargetCommand for a given test tab
  const tab =
    await addTab(`https://example.com/document-builder.sjs?id=top&html=
    <h1>Top level</h1>
    <iframe src="${encodeURIComponent(
      "https://example.com/document-builder.sjs?id=iframe&html=<h2>In iframe</h2>"
    )}">
    </iframe>`);
  const getLocationIdParam = url =>
    new URLSearchParams(new URL(url).search).get("id");

  const commands = await CommandsFactory.forTab(tab);
  const targetCommand = commands.targetCommand;
  const { TYPES } = targetCommand;

  await targetCommand.startListening();

  // Assert that watchTargets will call the create callback for all existing frames
  const targets = [];
  const onAvailable = async ({ targetFront }) => {
    is(
      targetFront.targetType,
      TYPES.FRAME,
      "We are only notified about frame targets"
    );
    targets.push(targetFront);
  };
  const destroyedTargets = [];
  const onDestroyed = async ({ targetFront }) => {
    is(
      targetFront.targetType,
      TYPES.FRAME,
      "We are only notified about frame targets"
    );
    destroyedTargets.push(targetFront);
  };

  await targetCommand.watchTargets({
    types: [TYPES.FRAME],
    onAvailable,
    onDestroyed,
  });

  if (isEveryFrameTargetEnabled()) {
    is(
      targets.length,
      2,
      "retrieved targets for top level and iframe documents"
    );
    is(
      targets[0],
      targetCommand.targetFront,
      "the target is the top level one"
    );
    is(
      getLocationIdParam(targets[1].url),
      "iframe",
      "the second target is the iframe one"
    );
  } else {
    is(targets.length, 1, "retrieved only the top level target");
    is(
      targets[0],
      targetCommand.targetFront,
      "the target is the top level one"
    );
  }

  is(
    destroyedTargets.length,
    0,
    "We get no destruction when calling watchTargets"
  );

  info("Navigate to a new page");
  let targetCountBeforeNavigation = targets.length;
  const secondPageUrl = `https://example.com/document-builder.sjs?html=second`;
  const onLoaded = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    secondPageUrl
  );
  BrowserTestUtils.startLoadingURIString(
    gBrowser.selectedBrowser,
    secondPageUrl
  );
  await onLoaded;

  // Same-origin navigations also spawn a new top level target
  await waitFor(
    () => targets.length == targetCountBeforeNavigation + 1,
    "wait for the next top level target"
  );
  is(
    targets.at(-1),
    targetCommand.targetFront,
    "the new target is the top level one"
  );

  ok(targets[0].isDestroyed(), "the first target is destroyed");
  if (isEveryFrameTargetEnabled()) {
    ok(targets[1].isDestroyed(), "the second target is destroyed");
    is(destroyedTargets.length, 2, "The two targets were destroyed");
  } else {
    is(destroyedTargets.length, 1, "Only one target was destroyed");
  }

  // Go back to the first page, this should be a bfcache navigation, and,
  // we should get a new target (or 2 if EFT is enabled)
  targetCountBeforeNavigation = targets.length;
  info("Go back to the first page");
  gBrowser.selectedBrowser.goBack();

  await waitFor(
    () =>
      targets.length ===
      targetCountBeforeNavigation + (isEveryFrameTargetEnabled() ? 2 : 1),
    "wait for the next top level target"
  );

  if (isEveryFrameTargetEnabled()) {
    await waitFor(() => targets.at(-2).url && targets.at(-1).url);
    is(
      getLocationIdParam(targets.at(-2).url),
      "top",
      "the first new target is for the top document…"
    );
    is(
      getLocationIdParam(targets.at(-1).url),
      "iframe",
      "…and the second one is for the iframe"
    );
  } else {
    is(
      getLocationIdParam(targets.at(-1).url),
      "top",
      "the new target is for the first url"
    );
  }

  ok(
    targets[targetCountBeforeNavigation - 1].isDestroyed(),
    "the target for the second page is destroyed"
  );
  is(
    destroyedTargets.length,
    targetCountBeforeNavigation,
    "We get one additional target being destroyed…"
  );
  is(
    destroyedTargets.at(-1),
    targets[targetCountBeforeNavigation - 1],
    "…and that's the second page one"
  );

  await waitForAllTargetsToBeAttached(targetCommand);

  targetCommand.unwatchTargets({
    types: [TYPES.FRAME],
    onAvailable,
    onDestroyed,
  });

  BrowserTestUtils.removeTab(tab);

  await commands.destroy();
}

async function testIframeNavigations() {
  info(" # Test IFRAME navigations");
  // Create a TargetCommand for a given test tab
  const tab = await addTab(
    `http://example.org/document-builder.sjs?html=<iframe src="${TEST_COM_URL}"></iframe>`
  );
  const commands = await CommandsFactory.forTab(tab);
  const targetCommand = commands.targetCommand;
  const { TYPES } = targetCommand;

  await targetCommand.startListening();

  // Assert that watchTargets will call the create callback for all existing frames
  const targets = [];
  const onAvailable = async ({ targetFront }) => {
    is(
      targetFront.targetType,
      TYPES.FRAME,
      "We are only notified about frame targets"
    );
    targets.push(targetFront);
  };
  await targetCommand.watchTargets({ types: [TYPES.FRAME], onAvailable });

  // When fission/EFT is off, there isn't much to test for iframes as they are debugged
  // when the unique top level target
  if (!isFissionEnabled() && !isEveryFrameTargetEnabled()) {
    is(
      targets.length,
      1,
      "Without fission/EFT, there is only the top level target"
    );
    await commands.destroy();
    return;
  }
  is(targets.length, 2, "retrieved the top level and the iframe targets");
  is(
    targets[0],
    targetCommand.targetFront,
    "the first target is the top level one"
  );
  is(targets[1].url, TEST_COM_URL, "the second target is the iframe one");

  // Navigate to the same page with query params
  info("Load the second page");
  const secondPageUrl = TEST_COM_URL + "?second-load";
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [secondPageUrl],
    function (url) {
      const iframe = content.document.querySelector("iframe");
      iframe.src = url;
    }
  );

  await waitFor(() => targets.length == 3, "wait for the next target");
  is(targets[2].url, secondPageUrl, "the second target is for the second url");
  ok(targets[1].isDestroyed(), "the first target is destroyed");

  // Go back to the first page, this should be a bfcache navigation, and,
  // we should get a new target
  info("Go back to the first page");
  const iframeBrowsingContext = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    function () {
      const iframe = content.document.querySelector("iframe");
      return iframe.browsingContext;
    }
  );
  await SpecialPowers.spawn(iframeBrowsingContext, [], function () {
    content.history.back();
  });

  await waitFor(() => targets.length == 4, "wait for the next target");
  is(targets[3].url, TEST_COM_URL, "the third target is for the first url");
  ok(targets[2].isDestroyed(), "the second target is destroyed");

  // Go forward and resurect the second page, this should also be a bfcache navigation, and,
  // get a new target.
  info("Go forward to the second page");
  await SpecialPowers.spawn(iframeBrowsingContext, [], function () {
    content.history.forward();
  });

  await waitFor(() => targets.length == 5, "wait for the next target");
  is(targets[4].url, secondPageUrl, "the 4th target is for the second url");
  ok(targets[3].isDestroyed(), "the third target is destroyed");

  targetCommand.unwatchTargets({ types: [TYPES.FRAME], onAvailable });

  await waitForAllTargetsToBeAttached(targetCommand);

  BrowserTestUtils.removeTab(tab);

  await commands.destroy();
}