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
712
713
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/* Basic UI tests for the protections panel */
"use strict";
const TRACKING_PAGE =
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
"http://tracking.example.org/browser/browser/base/content/test/protectionsUI/trackingPage.html";
ChromeUtils.defineESModuleGetters(this, {
ContentBlockingAllowList:
"resource://gre/modules/ContentBlockingAllowList.sys.mjs",
});
const { CustomizableUITestUtils } = ChromeUtils.importESModule(
"resource://testing-common/CustomizableUITestUtils.sys.mjs"
);
add_setup(async function () {
await SpecialPowers.pushPrefEnv({
set: [
// Set the auto hide timing to 100ms for blocking the test less.
["browser.protections_panel.toast.timeout", 100],
// Hide protections cards so as not to trigger more async messaging
// when landing on the page.
["browser.contentblocking.report.monitor.enabled", false],
["browser.contentblocking.report.lockwise.enabled", false],
["browser.contentblocking.report.proxy.enabled", false],
["privacy.trackingprotection.enabled", true],
],
});
let oldCanRecord = Services.telemetry.canRecordExtended;
Services.telemetry.canRecordExtended = true;
Services.telemetry.clearEvents();
registerCleanupFunction(() => {
Services.telemetry.canRecordExtended = oldCanRecord;
Services.telemetry.clearEvents();
});
});
add_task(async function testToggleSwitch() {
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
TRACKING_PAGE
);
await openProtectionsPanel();
await TestUtils.waitForCondition(() => {
return gProtectionsHandler._protectionsPopup.hasAttribute("blocking");
});
let events = Services.telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS
).parent;
let buttonEvents = events.filter(
e =>
e[1] == "security.ui.protectionspopup" &&
e[2] == "open" &&
e[3] == "protections_popup"
);
is(buttonEvents.length, 1, "recorded telemetry for opening the popup");
// Check the visibility of the "Site not working?" link.
ok(
BrowserTestUtils.is_visible(
gProtectionsHandler._protectionsPopupTPSwitchBreakageLink
),
"The 'Site not working?' link should be visible."
);
// The 'Site Fixed?' link should be hidden.
ok(
BrowserTestUtils.is_hidden(
gProtectionsHandler._protectionsPopupTPSwitchBreakageFixedLink
),
"The 'Site Fixed?' link should be hidden."
);
// Navigate through the 'Site Not Working?' flow and back to the main view,
// checking for telemetry on the way.
let siteNotWorkingView = document.getElementById(
"protections-popup-siteNotWorkingView"
);
let viewShown = BrowserTestUtils.waitForEvent(
siteNotWorkingView,
"ViewShown"
);
gProtectionsHandler._protectionsPopupTPSwitchBreakageLink.click();
await viewShown;
checkClickTelemetry("sitenotworking_link");
let sendReportButton = document.getElementById(
"protections-popup-siteNotWorkingView-sendReport"
);
let sendReportView = document.getElementById(
"protections-popup-sendReportView"
);
viewShown = BrowserTestUtils.waitForEvent(sendReportView, "ViewShown");
sendReportButton.click();
await viewShown;
checkClickTelemetry("send_report_link");
viewShown = BrowserTestUtils.waitForEvent(siteNotWorkingView, "ViewShown");
sendReportView.querySelector(".subviewbutton-back").click();
await viewShown;
let mainView = document.getElementById("protections-popup-mainView");
viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
siteNotWorkingView.querySelector(".subviewbutton-back").click();
await viewShown;
ok(
gProtectionsHandler._protectionsPopupTPSwitch.hasAttribute("enabled"),
"TP Switch should be enabled"
);
let popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
let browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
gProtectionsHandler._protectionsPopupTPSwitch.click();
// The 'Site not working?' link should be hidden after clicking the TP switch.
ok(
BrowserTestUtils.is_hidden(
gProtectionsHandler._protectionsPopupTPSwitchBreakageLink
),
"The 'Site not working?' link should be hidden after TP switch turns to off."
);
// Same for the 'Site Fixed?' link
ok(
BrowserTestUtils.is_hidden(
gProtectionsHandler._protectionsPopupTPSwitchBreakageFixedLink
),
"The 'Site Fixed?' link should be hidden."
);
await popuphiddenPromise;
checkClickTelemetry("etp_toggle_off");
// We need to wait toast's popup shown and popup hidden events. It won't fire
// the popup shown event if we open the protections panel while the toast is
// opening.
let popupShownPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popupshown"
);
popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
await browserLoadedPromise;
// Wait until the toast is shown and hidden.
await popupShownPromise;
await popuphiddenPromise;
await openProtectionsPanel();
ok(
!gProtectionsHandler._protectionsPopupTPSwitch.hasAttribute("enabled"),
"TP Switch should be disabled"
);
// The 'Site not working?' link should be hidden if the TP is off.
ok(
BrowserTestUtils.is_hidden(
gProtectionsHandler._protectionsPopupTPSwitchBreakageLink
),
"The 'Site not working?' link should be hidden if TP is off."
);
// The 'Site Fixed?' link should be shown if TP is off.
ok(
BrowserTestUtils.is_visible(
gProtectionsHandler._protectionsPopupTPSwitchBreakageFixedLink
),
"The 'Site Fixed?' link should be visible."
);
// Check telemetry for 'Site Fixed?' link.
viewShown = BrowserTestUtils.waitForEvent(sendReportView, "ViewShown");
gProtectionsHandler._protectionsPopupTPSwitchBreakageFixedLink.click();
await viewShown;
checkClickTelemetry("sitenotworking_link", "sitefixed");
viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
sendReportView.querySelector(".subviewbutton-back").click();
await viewShown;
// Click the TP switch again and check the visibility of the 'Site not
// Working?'. It should be hidden after toggling the TP switch.
browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
gProtectionsHandler._protectionsPopupTPSwitch.click();
ok(
BrowserTestUtils.is_hidden(
gProtectionsHandler._protectionsPopupTPSwitchBreakageLink
),
`The 'Site not working?' link should be still hidden after toggling TP
switch to on from off.`
);
ok(
BrowserTestUtils.is_hidden(
gProtectionsHandler._protectionsPopupTPSwitchBreakageFixedLink
),
"The 'Site Fixed?' link should be hidden."
);
await browserLoadedPromise;
checkClickTelemetry("etp_toggle_on");
ContentBlockingAllowList.remove(tab.linkedBrowser);
BrowserTestUtils.removeTab(tab);
});
/**
* A test for the protection settings button.
*/
add_task(async function testSettingsButton() {
// Open a tab and its protection panel.
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"https://example.com"
);
await openProtectionsPanel();
let popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
let newTabPromise = BrowserTestUtils.waitForNewTab(
gBrowser,
"about:preferences#privacy"
);
gProtectionsHandler._protectionsPopupSettingsButton.click();
// The protection popup should be hidden after clicking settings button.
await popuphiddenPromise;
// Wait until the about:preferences has been opened correctly.
let newTab = await newTabPromise;
ok(true, "about:preferences has been opened successfully");
checkClickTelemetry("settings");
BrowserTestUtils.removeTab(newTab);
BrowserTestUtils.removeTab(tab);
});
/**
* A test for ensuring Tracking Protection label is shown correctly
*/
add_task(async function testTrackingProtectionLabel() {
// Open a tab.
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"https://example.com"
);
await openProtectionsPanel();
let trackingProtectionLabel = document.getElementById(
"protections-popup-footer-protection-type-label"
);
is(
trackingProtectionLabel.textContent,
"Custom",
"The label is correctly set to Custom."
);
await closeProtectionsPanel();
Services.prefs.setStringPref("browser.contentblocking.category", "standard");
await openProtectionsPanel();
is(
trackingProtectionLabel.textContent,
"Standard",
"The label is correctly set to Standard."
);
await closeProtectionsPanel();
Services.prefs.setStringPref("browser.contentblocking.category", "strict");
await openProtectionsPanel();
is(
trackingProtectionLabel.textContent,
"Strict",
"The label is correctly set to Strict."
);
await closeProtectionsPanel();
Services.prefs.setStringPref("browser.contentblocking.category", "custom");
BrowserTestUtils.removeTab(tab);
});
/**
* A test for the 'Show Full Report' button in the footer section.
*/
add_task(async function testShowFullReportButton() {
// Open a tab and its protection panel.
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"https://example.com"
);
await openProtectionsPanel();
let popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
let newTabPromise = waitForAboutProtectionsTab();
let showFullReportButton = document.getElementById(
"protections-popup-show-report-button"
);
showFullReportButton.click();
// The protection popup should be hidden after clicking the link.
await popuphiddenPromise;
// Wait until the 'about:protections' has been opened correctly.
let newTab = await newTabPromise;
ok(true, "about:protections has been opened successfully");
checkClickTelemetry("full_report");
BrowserTestUtils.removeTab(newTab);
BrowserTestUtils.removeTab(tab);
});
/**
* A test for ensuring the mini panel is working correctly
*/
add_task(async function testMiniPanel() {
// Open a tab.
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"https://example.com"
);
// Open the mini panel.
await openProtectionsPanel(true);
let popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
// Check that only the header is displayed.
let mainView = document.getElementById("protections-popup-mainView");
for (let item of mainView.childNodes) {
if (item.id !== "protections-popup-mainView-panel-header-section") {
ok(
!BrowserTestUtils.is_visible(item),
`The section '${item.id}' is hidden in the toast.`
);
} else {
ok(
BrowserTestUtils.is_visible(item),
"The panel header is displayed as the content of the toast."
);
}
}
// Wait until the auto hide is happening.
await popuphiddenPromise;
ok(true, "The mini panel hides automatically.");
BrowserTestUtils.removeTab(tab);
});
/**
* A test for the toggle switch flow
*/
add_task(async function testToggleSwitchFlow() {
// Open a tab.
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"https://example.com"
);
await openProtectionsPanel();
let popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
let popupShownPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popupshown"
);
let browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
// Click the TP switch, from On -> Off.
gProtectionsHandler._protectionsPopupTPSwitch.click();
// Check that the icon state has been changed.
ok(
gProtectionsHandler.iconBox.hasAttribute("hasException"),
"The tracking protection icon state has been changed to disabled."
);
// The panel should be closed and the mini panel will show up after refresh.
await popuphiddenPromise;
await browserLoadedPromise;
await popupShownPromise;
ok(
gProtectionsHandler._protectionsPopup.hasAttribute("toast"),
"The protections popup should have the 'toast' attribute."
);
// Click on the mini panel and making sure the protection popup shows up.
popupShownPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popupshown"
);
popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
document.getElementById("protections-popup-mainView-panel-header").click();
await popuphiddenPromise;
await popupShownPromise;
ok(
!gProtectionsHandler._protectionsPopup.hasAttribute("toast"),
"The 'toast' attribute should be cleared on the protections popup."
);
// Click the TP switch again, from Off -> On.
popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
popupShownPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popupshown"
);
browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
gProtectionsHandler._protectionsPopupTPSwitch.click();
// Check that the icon state has been changed.
ok(
!gProtectionsHandler.iconBox.hasAttribute("hasException"),
"The tracking protection icon state has been changed to enabled."
);
// Protections popup hidden -> Page refresh -> Mini panel shows up.
await popuphiddenPromise;
popuphiddenPromise = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopup,
"popuphidden"
);
await browserLoadedPromise;
await popupShownPromise;
ok(
gProtectionsHandler._protectionsPopup.hasAttribute("toast"),
"The protections popup should have the 'toast' attribute."
);
// Wait until the auto hide is happening.
await popuphiddenPromise;
// Clean up the TP state.
ContentBlockingAllowList.remove(tab.linkedBrowser);
BrowserTestUtils.removeTab(tab);
});
/**
* A test for ensuring the tracking protection icon will show a correct
* icon according to the TP enabling state.
*/
add_task(async function testTrackingProtectionIcon() {
// Open a tab and its protection panel.
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"https://example.com"
);
let TPIcon = document.getElementById("tracking-protection-icon");
// Check the icon url. It will show a shield icon if TP is enabled.
is(
gBrowser.ownerGlobal
.getComputedStyle(TPIcon)
.getPropertyValue("list-style-image"),
`url("chrome://browser/skin/tracking-protection.svg")`,
"The tracking protection icon shows a shield icon."
);
// Disable the tracking protection.
let browserLoadedPromise = BrowserTestUtils.browserLoaded(
tab.linkedBrowser,
false,
"https://example.com/"
);
gProtectionsHandler.disableForCurrentPage();
await browserLoadedPromise;
// Check that the tracking protection icon should show a strike-through shield
// icon after page is reloaded.
is(
gBrowser.ownerGlobal
.getComputedStyle(TPIcon)
.getPropertyValue("list-style-image"),
`url("chrome://browser/skin/tracking-protection-disabled.svg")`,
"The tracking protection icon shows a strike through shield icon."
);
// Clean up the TP state.
ContentBlockingAllowList.remove(tab.linkedBrowser);
BrowserTestUtils.removeTab(tab);
});
/**
* A test for ensuring the number of blocked trackers is displayed properly.
*/
add_task(async function testNumberOfBlockedTrackers() {
// First, clear the tracking database.
await TrackingDBService.clearAll();
// Open a tab.
let tab = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
"https://example.com"
);
await openProtectionsPanel();
let trackerCounterBox = document.getElementById(
"protections-popup-trackers-blocked-counter-box"
);
let trackerCounterDesc = document.getElementById(
"protections-popup-trackers-blocked-counter-description"
);
// Check that whether the counter is not shown if the number of blocked
// trackers is zero.
ok(
BrowserTestUtils.is_hidden(trackerCounterBox),
"The blocked tracker counter is hidden if there is no blocked tracker."
);
await closeProtectionsPanel();
// Add one tracker into the database and check that the tracker counter is
// properly shown.
await addTrackerDataIntoDB(1);
// A promise for waiting the `showing` attributes has been set to the counter
// box. This means the database access is finished.
let counterShownPromise = BrowserTestUtils.waitForAttribute(
"showing",
trackerCounterBox
);
await openProtectionsPanel();
await counterShownPromise;
// Check that the number of blocked trackers is shown.
ok(
BrowserTestUtils.is_visible(trackerCounterBox),
"The blocked tracker counter is shown if there is one blocked tracker."
);
is(
trackerCounterDesc.textContent,
"1 Blocked",
"The blocked tracker counter is correct."
);
await closeProtectionsPanel();
await TrackingDBService.clearAll();
// Add trackers into the database and check that the tracker counter is
// properly shown as well as whether the pre-fetch is triggered by the
// keyboard navigation.
await addTrackerDataIntoDB(10);
// We cannot wait for the change of "showing" attribute here since this
// attribute will only be set if the previous counter is zero. Instead, we
// wait for the change of the text content of the counter.
let updateCounterPromise = new Promise(resolve => {
let mut = new MutationObserver(mutations => {
resolve();
mut.disconnect();
});
mut.observe(trackerCounterDesc, {
childList: true,
});
});
await openProtectionsPanelWithKeyNav();
await updateCounterPromise;
// Check that the number of blocked trackers is shown.
ok(
BrowserTestUtils.is_visible(trackerCounterBox),
"The blocked tracker counter is shown if there are more than one blocked tracker."
);
is(
trackerCounterDesc.textContent,
"10 Blocked",
"The blocked tracker counter is correct."
);
await closeProtectionsPanel();
await TrackingDBService.clearAll();
BrowserTestUtils.removeTab(tab);
});
add_task(async function testSubViewTelemetry() {
let items = [
["protections-popup-category-trackers", "trackers"],
["protections-popup-category-socialblock", "social"],
["protections-popup-category-cookies", "cookies"],
["protections-popup-category-cryptominers", "cryptominers"],
["protections-popup-category-fingerprinters", "fingerprinters"],
].map(item => [document.getElementById(item[0]), item[1]]);
for (let [item, telemetryId] of items) {
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
await BrowserTestUtils.withNewTab("http://www.example.com", async () => {
await openProtectionsPanel();
item.classList.remove("notFound"); // Force visible for test
gProtectionsHandler._categoryItemOrderInvalidated = true;
gProtectionsHandler.reorderCategoryItems();
let viewShownEvent = BrowserTestUtils.waitForEvent(
gProtectionsHandler._protectionsPopupMultiView,
"ViewShown"
);
item.click();
let panelView = (await viewShownEvent).originalTarget;
checkClickTelemetry(telemetryId);
let prefsTabPromise = BrowserTestUtils.waitForNewTab(
gBrowser,
"about:preferences#privacy"
);
panelView.querySelector(".panel-subview-footer-button").click();
let prefsTab = await prefsTabPromise;
BrowserTestUtils.removeTab(prefsTab);
checkClickTelemetry("subview_settings", telemetryId);
});
}
});
/**
* A test to make sure the TP state won't apply incorrectly if we quickly switch
* tab after toggling the TP switch.
*/
add_task(async function testQuickSwitchTabAfterTogglingTPSwitch() {
const FIRST_TEST_SITE = "https://example.com/";
const SECOND_TEST_SITE = "https://example.org/";
// First, clear the tracking database.
await TrackingDBService.clearAll();
// Open two tabs with different origins.
let tabOne = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
FIRST_TEST_SITE
);
let tabTwo = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
SECOND_TEST_SITE
);
// Open the protection panel of the second tab.
await openProtectionsPanel();
// A promise to check the reload happens on the second tab.
let browserLoadedPromise = BrowserTestUtils.browserLoaded(
tabTwo.linkedBrowser,
false,
SECOND_TEST_SITE
);
// Toggle the TP state and switch tab without waiting it to be finished.
gProtectionsHandler._protectionsPopupTPSwitch.click();
gBrowser.selectedTab = tabOne;
// Wait for the second tab to be reloaded.
await browserLoadedPromise;
// Check that the first tab is still with ETP enabled.
ok(
!ContentBlockingAllowList.includes(gBrowser.selectedBrowser),
"The ETP state of the first tab is still enabled."
);
// Check the ETP is disabled on the second origin.
ok(
ContentBlockingAllowList.includes(tabTwo.linkedBrowser),
"The ETP state of the second tab has been changed to disabled."
);
// Clean up the state of the allow list for the second tab.
ContentBlockingAllowList.remove(tabTwo.linkedBrowser);
BrowserTestUtils.removeTab(tabOne);
BrowserTestUtils.removeTab(tabTwo);
// Finally, clear the tracking database.
await TrackingDBService.clearAll();
});
|