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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Test the ResourceCommand API around DOCUMENT_EVENT
add_task(async function () {
await testDocumentEventResources();
await testDocumentEventResourcesWithIgnoreExistingResources();
await testDomCompleteWithOverloadedConsole();
await testIframeNavigation();
await testBfCacheNavigation();
await testDomCompleteWithWindowStop();
await testCrossOriginNavigation();
});
async function testDocumentEventResources() {
info("Test ResourceCommand for DOCUMENT_EVENT");
// Open a test tab
const title = "DocumentEventsTitle";
const url = `data:text/html,<title>${title}</title>Document Events`;
const tab = await addTab(url);
const listener = new ResourceListener();
const { commands } = await initResourceCommand(tab);
info(
"Check whether the document events are fired correctly even when the document was already loaded"
);
const onLoadingAtInit = listener.once("dom-loading");
const onInteractiveAtInit = listener.once("dom-interactive");
const onCompleteAtInit = listener.once("dom-complete");
await commands.resourceCommand.watchResources(
[commands.resourceCommand.TYPES.DOCUMENT_EVENT],
{
onAvailable: parameters => listener.dispatch(parameters),
}
);
await assertPromises(
commands,
// targetBeforeNavigation is only used when there is a will-navigate and a navigate, but there is none here
null,
// As we started watching on an already loaded document, and no navigation happened since we called watchResources,
// we don't have any will-navigate event
null,
onLoadingAtInit,
onInteractiveAtInit,
onCompleteAtInit
);
ok(
true,
"Document events are fired even when the document was already loaded"
);
let domLoadingResource = await onLoadingAtInit;
is(
domLoadingResource.url,
url,
`resource ${domLoadingResource.name} has expected url`
);
is(
domLoadingResource.title,
undefined,
`resource ${domLoadingResource.name} does not have a title property`
);
let domInteractiveResource = await onInteractiveAtInit;
is(
domInteractiveResource.url,
url,
`resource ${domInteractiveResource.name} has expected url`
);
is(
domInteractiveResource.title,
title,
`resource ${domInteractiveResource.name} has expected title`
);
let domCompleteResource = await onCompleteAtInit;
is(
domCompleteResource.url,
undefined,
`resource ${domCompleteResource.name} does not have a url property`
);
is(
domCompleteResource.title,
undefined,
`resource ${domCompleteResource.name} does not have a title property`
);
info("Check whether the document events are fired correctly when reloading");
const onWillNavigate = listener.once("will-navigate");
const onLoadingAtReloaded = listener.once("dom-loading");
const onInteractiveAtReloaded = listener.once("dom-interactive");
const onCompleteAtReloaded = listener.once("dom-complete");
const targetBeforeNavigation = commands.targetCommand.targetFront;
gBrowser.reloadTab(tab);
await assertPromises(
commands,
targetBeforeNavigation,
onWillNavigate,
onLoadingAtReloaded,
onInteractiveAtReloaded,
onCompleteAtReloaded
);
ok(true, "Document events are fired after reloading");
domLoadingResource = await onLoadingAtReloaded;
is(
domLoadingResource.url,
url,
`resource ${domLoadingResource.name} has expected url after reloading`
);
is(
domLoadingResource.title,
undefined,
`resource ${domLoadingResource.name} does not have a title property after reloading`
);
domInteractiveResource = await onInteractiveAtInit;
is(
domInteractiveResource.url,
url,
`resource ${domInteractiveResource.name} has url property after reloading`
);
is(
domInteractiveResource.title,
title,
`resource ${domInteractiveResource.name} has expected title after reloading`
);
domCompleteResource = await onCompleteAtInit;
is(
domCompleteResource.url,
undefined,
`resource ${domCompleteResource.name} does not have a url property after reloading`
);
is(
domCompleteResource.title,
undefined,
`resource ${domCompleteResource.name} does not have a title property after reloading`
);
await commands.destroy();
}
async function testDocumentEventResourcesWithIgnoreExistingResources() {
info("Test ignoreExistingResources option for DOCUMENT_EVENT");
const tab = await addTab("data:text/html,Document Events");
const { commands } = await initResourceCommand(tab);
info("Check whether the existing document events will not be fired");
const documentEvents = [];
await commands.resourceCommand.watchResources(
[commands.resourceCommand.TYPES.DOCUMENT_EVENT],
{
onAvailable: resources => documentEvents.push(...resources),
ignoreExistingResources: true,
}
);
is(documentEvents.length, 0, "Existing document events are not fired");
info("Check whether the future document events are fired");
const targetBeforeNavigation = commands.targetCommand.targetFront;
gBrowser.reloadTab(tab);
info(
"Wait for will-navigate, dom-loading, dom-interactive and dom-complete events"
);
await waitFor(() => documentEvents.length === 4);
assertEvents({ commands, targetBeforeNavigation, documentEvents });
await commands.destroy();
}
async function testIframeNavigation() {
info("Test iframe navigations for DOCUMENT_EVENT");
const tab = await addTab(
'https://example.com/document-builder.sjs?html=<iframe src="https://example.net/document-builder.sjs?html=net"></iframe>'
);
const secondPageUrl = "https://example.org/document-builder.sjs?html=org";
const { commands } = await initResourceCommand(tab);
let documentEvents = [];
await commands.resourceCommand.watchResources(
[commands.resourceCommand.TYPES.DOCUMENT_EVENT],
{
onAvailable: resources => documentEvents.push(...resources),
}
);
let iframeTarget;
if (isFissionEnabled() || isEveryFrameTargetEnabled()) {
is(
documentEvents.length,
6,
"With fission/EFT, we get two targets and two sets of events: dom-loading, dom-interactive, dom-complete"
);
[, iframeTarget] = await commands.targetCommand.getAllTargets([
commands.targetCommand.TYPES.FRAME,
]);
// Filter out each target events as their order to be random between the two targets
const topTargetEvents = documentEvents.filter(
r => r.targetFront == commands.targetCommand.targetFront
);
const iframeTargetEvents = documentEvents.filter(
r => r.targetFront != commands.targetCommand.targetFront
);
assertEvents({
commands,
documentEvents: [null /* no will-navigate */, ...topTargetEvents],
});
assertEvents({
commands,
documentEvents: [null /* no will-navigate */, ...iframeTargetEvents],
expectedTargetFront: iframeTarget,
});
} else {
assertEvents({
commands,
documentEvents: [null /* no will-navigate */, ...documentEvents],
});
}
info("Navigate the iframe to another process (if fission is enabled)");
documentEvents = [];
await SpecialPowers.spawn(
gBrowser.selectedBrowser,
[secondPageUrl],
function (url) {
const iframe = content.document.querySelector("iframe");
iframe.src = url;
}
);
// We are switching to a new target only when fission is enabled...
if (isFissionEnabled() || isEveryFrameTargetEnabled()) {
await waitFor(() => documentEvents.length >= 3);
is(
documentEvents.length,
3,
"With fission/EFT, we switch to a new target and get: dom-loading, dom-interactive, dom-complete (but no will-navigate as that's only for the top BrowsingContext)"
);
const [, newIframeTarget] = await commands.targetCommand.getAllTargets([
commands.targetCommand.TYPES.FRAME,
]);
assertEvents({
commands,
targetBeforeNavigation: iframeTarget,
documentEvents: [null /* no will-navigate */, ...documentEvents],
expectedTargetFront: newIframeTarget,
expectedNewURI: secondPageUrl,
});
} else {
// Wait for some time in order to let a chance to receive some unexpected events
await wait(250);
is(
documentEvents.length,
0,
"If fission is disabled, we navigate within the same process, we get no new target and no new resource"
);
}
await commands.destroy();
}
function isBfCacheInParentEnabled() {
return (
Services.appinfo.sessionHistoryInParent &&
Services.prefs.getBoolPref("fission.bfcacheInParent", false)
);
}
async function testBfCacheNavigation() {
info("Test bfcache navigations for DOCUMENT_EVENT");
info("Open a first document and navigate to a second one");
const firstLocation = "data:text/html,<title>first</title>first page";
const secondLocation = "data:text/html,<title>second</title>second page";
const tab = await addTab(firstLocation);
const onLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, secondLocation);
await onLoaded;
const { commands } = await initResourceCommand(tab);
const documentEvents = [];
await commands.resourceCommand.watchResources(
[commands.resourceCommand.TYPES.DOCUMENT_EVENT],
{
onAvailable: resources => {
documentEvents.push(...resources);
},
ignoreExistingResources: true,
}
);
// Wait for some time for extra safety
await wait(250);
is(documentEvents.length, 0, "Existing document events are not fired");
info("Navigate back to the first page");
const onSwitched = commands.targetCommand.once("switched-target");
const targetBeforeNavigation = commands.targetCommand.targetFront;
gBrowser.goBack();
// We are switching to a new target only when fission/EFT is enabled...
if (
(isFissionEnabled() || isEveryFrameTargetEnabled()) &&
isBfCacheInParentEnabled()
) {
await onSwitched;
}
info(
"Wait for will-navigate, dom-loading, dom-interactive and dom-complete events"
);
await waitFor(() => documentEvents.length >= 4);
/* Ignore will-navigate timestamp as all other DOCUMENT_EVENTS will be set at the original load date,
which is when we loaded from the network, and not when we loaded from bfcache */
assertEvents({
commands,
targetBeforeNavigation,
documentEvents,
ignoreWillNavigateTimestamp: true,
});
// Wait for some time in order to let a chance to have duplicated dom-loading events
await wait(250);
is(
documentEvents.length,
4,
"There is no duplicated event and only the 4 expected DOCUMENT_EVENT states"
);
const [willNavigateEvent, loadingEvent, interactiveEvent, completeEvent] =
documentEvents;
is(
willNavigateEvent.name,
"will-navigate",
"The first DOCUMENT_EVENT is will-navigate"
);
is(
loadingEvent.name,
"dom-loading",
"The second DOCUMENT_EVENT is dom-loading"
);
is(
interactiveEvent.name,
"dom-interactive",
"The third DOCUMENT_EVENT is dom-interactive"
);
is(
completeEvent.name,
"dom-complete",
"The fourth DOCUMENT_EVENT is dom-complete"
);
is(
loadingEvent.url,
firstLocation,
`resource ${loadingEvent.name} has expected url after navigation back`
);
is(
loadingEvent.title,
undefined,
`resource ${loadingEvent.name} does not have a title property after navigating back`
);
is(
interactiveEvent.url,
firstLocation,
`resource ${interactiveEvent.name} has expected url property after navigating back`
);
is(
interactiveEvent.title,
"first",
`resource ${interactiveEvent.name} has expected title after navigating back`
);
is(
completeEvent.url,
undefined,
`resource ${completeEvent.name} does not have a url property after navigating back`
);
is(
completeEvent.title,
undefined,
`resource ${completeEvent.name} does not have a title property after navigating back`
);
await commands.destroy();
}
async function testCrossOriginNavigation() {
info("Test cross origin navigations for DOCUMENT_EVENT");
const tab = await addTab("https://example.com/document-builder.sjs?html=com");
const { commands } = await initResourceCommand(tab);
const documentEvents = [];
await commands.resourceCommand.watchResources(
[commands.resourceCommand.TYPES.DOCUMENT_EVENT],
{
onAvailable: resources => documentEvents.push(...resources),
ignoreExistingResources: true,
}
);
// Wait for some time for extra safety
await wait(250);
is(documentEvents.length, 0, "Existing document events are not fired");
info("Navigate to another process");
const onSwitched = commands.targetCommand.once("switched-target");
const netUrl =
"https://example.net/document-builder.sjs?html=<head><title>titleNet</title></head>net";
const onLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
const targetBeforeNavigation = commands.targetCommand.targetFront;
BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, netUrl);
await onLoaded;
// We are switching to a new target only when fission is enabled...
if (isFissionEnabled() || isEveryFrameTargetEnabled()) {
await onSwitched;
}
info(
"Wait for will-navigate, dom-loading, dom-interactive and dom-complete events"
);
await waitFor(() => documentEvents.length >= 4);
assertEvents({ commands, targetBeforeNavigation, documentEvents });
// Wait for some time in order to let a chance to have duplicated dom-loading events
await wait(250);
is(
documentEvents.length,
4,
"There is no duplicated event and only the 4 expected DOCUMENT_EVENT states"
);
const [willNavigateEvent, loadingEvent, interactiveEvent, completeEvent] =
documentEvents;
is(
willNavigateEvent.name,
"will-navigate",
"The first DOCUMENT_EVENT is will-navigate"
);
is(
loadingEvent.name,
"dom-loading",
"The second DOCUMENT_EVENT is dom-loading"
);
is(
interactiveEvent.name,
"dom-interactive",
"The third DOCUMENT_EVENT is dom-interactive"
);
is(
completeEvent.name,
"dom-complete",
"The fourth DOCUMENT_EVENT is dom-complete"
);
is(
loadingEvent.url,
encodeURI(netUrl),
`resource ${loadingEvent.name} has expected url after reloading`
);
is(
loadingEvent.title,
undefined,
`resource ${loadingEvent.name} does not have a title property after reloading`
);
is(
interactiveEvent.url,
encodeURI(netUrl),
`resource ${interactiveEvent.name} has expected url property after reloading`
);
is(
interactiveEvent.title,
"titleNet",
`resource ${interactiveEvent.name} has expected title after reloading`
);
is(
completeEvent.url,
undefined,
`resource ${completeEvent.name} does not have a url property after reloading`
);
is(
completeEvent.title,
undefined,
`resource ${completeEvent.name} does not have a title property after reloading`
);
await commands.destroy();
}
async function testDomCompleteWithOverloadedConsole() {
info("Test dom-complete with an overloaded console object");
const tab = await addTab(
"data:text/html,<script>window.console = {};</script>"
);
const { client, resourceCommand, targetCommand } = await initResourceCommand(
tab
);
info("Check that all DOCUMENT_EVENTS are fired for the already loaded page");
const documentEvents = [];
await resourceCommand.watchResources([resourceCommand.TYPES.DOCUMENT_EVENT], {
onAvailable: resources => documentEvents.push(...resources),
});
is(documentEvents.length, 3, "Existing document events are fired");
const domComplete = documentEvents[2];
is(domComplete.name, "dom-complete", "the last resource is the dom-complete");
is(
domComplete.hasNativeConsoleAPI,
false,
"the console object is reported to be overloaded"
);
targetCommand.destroy();
await client.close();
}
async function testDomCompleteWithWindowStop() {
info("Test dom-complete with a page calling window.stop()");
const tab = await addTab("data:text/html,foo");
const { commands, client, resourceCommand, targetCommand } =
await initResourceCommand(tab);
info("Check that all DOCUMENT_EVENTS are fired for the already loaded page");
let documentEvents = [];
await resourceCommand.watchResources([resourceCommand.TYPES.DOCUMENT_EVENT], {
onAvailable: resources => documentEvents.push(...resources),
});
is(documentEvents.length, 3, "Existing document events are fired");
documentEvents = [];
const html = `<!DOCTYPE html><html>
<head>
<title>stopped page</title>
<script>window.stop();</script>
</head>
<body>Page content that shouldn't be displayed</body>
</html>`;
const secondLocation = "data:text/html," + encodeURIComponent(html);
const targetBeforeNavigation = commands.targetCommand.targetFront;
BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, secondLocation);
info(
"Wait for will-navigate, dom-loading, dom-interactive and dom-complete events"
);
await waitFor(() => documentEvents.length === 4);
assertEvents({ commands, targetBeforeNavigation, documentEvents });
targetCommand.destroy();
await client.close();
}
async function assertPromises(
commands,
targetBeforeNavigation,
onWillNavigate,
onLoading,
onInteractive,
onComplete
) {
const willNavigateEvent = await onWillNavigate;
const loadingEvent = await onLoading;
const interactiveEvent = await onInteractive;
const completeEvent = await onComplete;
assertEvents({
commands,
targetBeforeNavigation,
documentEvents: [
willNavigateEvent,
loadingEvent,
interactiveEvent,
completeEvent,
],
});
}
function assertEvents({
commands,
targetBeforeNavigation,
documentEvents,
expectedTargetFront = commands.targetCommand.targetFront,
expectedNewURI = gBrowser.selectedBrowser.currentURI.spec,
ignoreWillNavigateTimestamp = false,
}) {
const [willNavigateEvent, loadingEvent, interactiveEvent, completeEvent] =
documentEvents;
if (willNavigateEvent) {
is(willNavigateEvent.name, "will-navigate", "Received the will-navigate");
is(
willNavigateEvent.newURI,
expectedNewURI,
"will-navigate newURI is set to the current tab new location"
);
}
is(
loadingEvent.name,
"dom-loading",
"loading received in the exepected order"
);
is(
interactiveEvent.name,
"dom-interactive",
"interactive received in the expected order"
);
is(completeEvent.name, "dom-complete", "complete received last");
if (willNavigateEvent) {
is(
typeof willNavigateEvent.time,
"number",
`Type of time attribute for will-navigate event is correct (${willNavigateEvent.time})`
);
}
is(
typeof loadingEvent.time,
"number",
`Type of time attribute for loading event is correct (${loadingEvent.time})`
);
is(
typeof interactiveEvent.time,
"number",
`Type of time attribute for interactive event is correct (${interactiveEvent.time})`
);
is(
typeof completeEvent.time,
"number",
`Type of time attribute for complete event is correct (${completeEvent.time})`
);
if (willNavigateEvent && !ignoreWillNavigateTimestamp) {
ok(
willNavigateEvent.time <= loadingEvent.time,
`Timestamp for dom-loading event is greater than will-navigate event (${willNavigateEvent.time} <= ${loadingEvent.time})`
);
}
ok(
loadingEvent.time <= interactiveEvent.time,
`Timestamp for interactive event is greater than loading event (${loadingEvent.time} <= ${interactiveEvent.time})`
);
ok(
interactiveEvent.time <= completeEvent.time,
`Timestamp for complete event is greater than interactive event (${interactiveEvent.time} <= ${completeEvent.time}).`
);
if (willNavigateEvent) {
// If we switched to a new target, this target will be different from currentTargetFront.
// This only happen if we navigate to another process or if server target switching is enabled.
is(
willNavigateEvent.targetFront,
targetBeforeNavigation,
"will-navigate target was the one before the navigation"
);
}
is(
loadingEvent.targetFront,
expectedTargetFront,
"loading target is the expected one"
);
is(
interactiveEvent.targetFront,
expectedTargetFront,
"interactive target is the expected one"
);
is(
completeEvent.targetFront,
expectedTargetFront,
"complete target is the expected one"
);
is(
completeEvent.hasNativeConsoleAPI,
true,
"None of the tests (except the dedicated one) overload the console object"
);
}
class ResourceListener {
_listeners = new Map();
dispatch(resources) {
for (const resource of resources) {
const resolve = this._listeners.get(resource.name);
if (resolve) {
resolve(resource);
this._listeners.delete(resource.name);
}
}
}
once(resourceName) {
return new Promise(r => this._listeners.set(resourceName, r));
}
}
|