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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/**
* Tests the cookie banner handling section in the protections panel.
*/
const { TelemetryTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/TelemetryTestUtils.sys.mjs"
);
const {
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
MODE_UNSET,
} = Ci.nsICookieBannerService;
/**
* Determines whether the cookie banner section in the protections panel should
* be visible with the given configuration.
* @param {*} options - Configuration to test.
* @param {Number} options.featureMode - nsICookieBannerService::Modes value for
* normal browsing.
* @param {Number} options.featureModePBM - nsICookieBannerService::Modes value
* for private browsing.
* @param {boolean} options.visibilityPref - State of the cookie banner UI
* visibility pref.
* @param {boolean} options.testPBM - Whether the window is in private browsing
* mode (true) or not (false).
* @returns {boolean} Whether the section should be visible for the given
* config.
*/
function cookieBannerSectionIsVisible({
featureMode,
featureModePBM,
visibilityPref,
testPBM,
}) {
if (!visibilityPref) {
return false;
}
return (
(testPBM &&
featureModePBM != MODE_DISABLED &&
featureModePBM != MODE_DETECT_ONLY) ||
(!testPBM &&
featureMode != MODE_DISABLED &&
featureMode != MODE_DETECT_ONLY)
);
}
/**
* Runs a visibility test of the cookie banner section in the protections panel.
* @param {*} options - Test options.
* @param {Window} options.win - Browser window to use for testing. It's
* browsing mode should match the testPBM variable.
* @param {Number} options.featureMode - nsICookieBannerService::Modes value for
* normal browsing.
* @param {Number} options.featureModePBM - nsICookieBannerService::Modes value
* for private browsing.
* @param {boolean} options.visibilityPref - State of the cookie banner UI
* visibility pref.
* @param {boolean} options.testPBM - Whether the window is in private browsing
* mode (true) or not (false).
* @returns {Promise} Resolves once the test is complete.
*/
async function testSectionVisibility({
win,
featureMode,
featureModePBM,
visibilityPref,
testPBM,
}) {
info(
"testSectionVisibility " +
JSON.stringify({ featureMode, featureModePBM, visibilityPref, testPBM })
);
// initialize the pref environment
await SpecialPowers.pushPrefEnv({
set: [
["cookiebanners.service.mode", featureMode],
["cookiebanners.service.mode.privateBrowsing", featureModePBM],
["cookiebanners.ui.desktop.enabled", visibilityPref],
],
});
// Open a tab with example.com so the protections panel can be opened.
await BrowserTestUtils.withNewTab(
{ gBrowser: win.gBrowser, url: "https://example.com" },
async () => {
await openProtectionsPanel(null, win);
// Get panel elements to test
let el = {
section: win.document.getElementById(
"protections-popup-cookie-banner-section"
),
sectionSeparator: win.document.getElementById(
"protections-popup-cookie-banner-section-separator"
),
switch: win.document.getElementById(
"protections-popup-cookie-banner-switch"
),
};
let expectVisible = cookieBannerSectionIsVisible({
featureMode,
featureModePBM,
visibilityPref,
testPBM,
});
is(
BrowserTestUtils.is_visible(el.section),
expectVisible,
`Cookie banner section should be ${
expectVisible ? "visible" : "not visible"
}.`
);
is(
BrowserTestUtils.is_visible(el.sectionSeparator),
expectVisible,
`Cookie banner section separator should be ${
expectVisible ? "visible" : "not visible"
}.`
);
is(
BrowserTestUtils.is_visible(el.switch),
expectVisible,
`Cookie banner switch should be ${
expectVisible ? "visible" : "not visible"
}.`
);
}
);
await SpecialPowers.popPrefEnv();
}
/**
* Tests cookie banner section visibility state in different configurations.
*/
add_task(async function test_section_visibility() {
// Test all combinations of cookie banner service modes and normal and
// private browsing.
for (let testPBM of [false, true]) {
let win = window;
// Open a new private window to test the panel in for testing PBM, otherwise
// reuse the existing window.
if (testPBM) {
win = await BrowserTestUtils.openNewBrowserWindow({
private: true,
});
win.focus();
}
for (let featureMode of [
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
]) {
for (let featureModePBM of [
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
]) {
await testSectionVisibility({
win,
featureMode,
featureModePBM,
testPBM,
visibilityPref: true,
});
}
}
if (testPBM) {
await BrowserTestUtils.closeWindow(win);
}
}
});
/**
* Tests that the cookie banner section is only visible if enabled by UI pref.
*/
add_task(async function test_section_visibility_pref() {
for (let visibilityPref of [false, true]) {
await testSectionVisibility({
win: window,
featureMode: MODE_REJECT,
featureModePBM: MODE_DISABLED,
testPBM: false,
visibilityPref,
});
}
});
/**
* Test the state of the per-site exception switch in the cookie banner section
* and whether a matching per-site exception is set.
* @param {*} options
* @param {Window} options.win - Chrome window to test exception for (selected
* tab).
* @param {boolean} options.isPBM - Whether the given window is in private
* browsing mode.
* @param {boolean} options.expectEnabled - Whether the switch is expected to be
* enabled, this also indicates no exception set.
*/
function assertSwitchAndPrefState({ win, isPBM, expectEnabled }) {
let el = {
switch: win.document.getElementById(
"protections-popup-cookie-banner-switch"
),
labelON: win.document.querySelector(
".protections-popup-cookie-banner-switch-on-header"
),
labelOFF: win.document.querySelector(
".protections-popup-cookie-banner-switch-off-header"
),
};
info("Test switch state.");
ok(BrowserTestUtils.is_visible(el.switch), "Switch should be visible");
is(
el.switch.hasAttribute("enabled"),
expectEnabled,
`Switch is ${expectEnabled ? "enabled" : "disabled"}.`
);
info("Test switch labels.");
if (expectEnabled) {
ok(BrowserTestUtils.is_visible(el.labelON), "ON label should be visible");
ok(
!BrowserTestUtils.is_visible(el.labelOFF),
"OFF label should not be visible"
);
} else {
ok(
!BrowserTestUtils.is_visible(el.labelON),
"ON label should not be visible"
);
ok(BrowserTestUtils.is_visible(el.labelOFF), "OFF label should be visible");
}
info("Test per-site exception state.");
let currentURI = win.gBrowser.currentURI;
let pref = Services.cookieBanners.getDomainPref(currentURI, isPBM);
if (expectEnabled) {
is(
pref,
MODE_UNSET,
`There should be no per-site exception for ${currentURI.spec}.`
);
} else {
is(
pref,
MODE_DISABLED,
`There should be a per-site exception for ${currentURI.spec}.`
);
}
}
/**
* Test the telemetry associated with the cookie banner toggle. To be called
* after interacting with the toggle.
* @param {*} options
* @param {boolean|null} - Expected telemetry state matching the button state.
* button on = true = cookieb_toggle_on event. Pass null to expect no event
* recorded.
*/
function assertTelemetryState({ expectEnabled = null } = {}) {
info("Test telemetry state.");
let events = [];
const CATEGORY = "security.ui.protectionspopup";
const METHOD = "click";
if (expectEnabled != null) {
events.push({
category: CATEGORY,
method: METHOD,
object: expectEnabled ? "cookieb_toggle_on" : "cookieb_toggle_off",
});
}
// Assert event state and clear event list.
TelemetryTestUtils.assertEvents(events, {
category: CATEGORY,
method: METHOD,
});
}
/**
* Tests the cookie banner section per-site preference toggle.
*/
add_task(async function test_section_toggle() {
// initialize the pref environment
await SpecialPowers.pushPrefEnv({
set: [
["cookiebanners.service.mode", MODE_REJECT_OR_ACCEPT],
["cookiebanners.service.mode.privateBrowsing", MODE_REJECT],
["cookiebanners.ui.desktop.enabled", true],
],
});
// Test both normal and private browsing windows. For normal windows we reuse
// the existing one, for private windows we need to open a new window.
for (let testPBM of [false, true]) {
let win = window;
if (testPBM) {
win = await BrowserTestUtils.openNewBrowserWindow({
private: true,
});
}
await BrowserTestUtils.withNewTab(
{ gBrowser: win.gBrowser, url: "https://example.com" },
async () => {
await openProtectionsPanel(null, win);
let switchEl = win.document.getElementById(
"protections-popup-cookie-banner-switch"
);
info("Testing initial switch ON state.");
assertSwitchAndPrefState({
win,
isPBM: testPBM,
switchEl,
expectEnabled: true,
});
assertTelemetryState();
info("Testing switch state after toggle OFF");
switchEl.click();
assertSwitchAndPrefState({
win,
isPBM: testPBM,
switchEl,
expectEnabled: false,
});
assertTelemetryState({ expectEnabled: false });
info("Reopen the panel to test the initial switch OFF state.");
await closeProtectionsPanel(win);
await openProtectionsPanel(null, win);
assertSwitchAndPrefState({
win,
isPBM: testPBM,
switchEl,
expectEnabled: false,
});
assertTelemetryState();
info("Testing switch state after toggle ON.");
switchEl.click();
assertSwitchAndPrefState({
win,
isPBM: testPBM,
switchEl,
expectEnabled: true,
});
assertTelemetryState({ expectEnabled: true });
info("Reopen the panel to test the initial switch ON state.");
await closeProtectionsPanel(win);
await openProtectionsPanel(null, win);
assertSwitchAndPrefState({
win,
isPBM: testPBM,
switchEl,
expectEnabled: true,
});
assertTelemetryState();
}
);
if (testPBM) {
await BrowserTestUtils.closeWindow(win);
}
}
});
|