summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/protectionsUI/browser_protectionsUI_cookie_banner.js
blob: 1d71f718fbd708b90acf5012aeed6cda66ae295e (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
/* 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 { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

const { MODE_DISABLED, MODE_REJECT, MODE_REJECT_OR_ACCEPT, MODE_UNSET } =
  Ci.nsICookieBannerService;

const exampleRules = JSON.stringify([
  {
    id: "4b18afb0-76db-4f9e-a818-ed9a783fae6a",
    cookies: {},
    click: {
      optIn: "#foo",
      presence: "#bar",
    },
    domains: ["example.com"],
  },
]);

/**
 * 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,
  detectOnly,
  visibilityPref,
  testPBM,
}) {
  if (!visibilityPref) {
    return false;
  }
  if (detectOnly) {
    return false;
  }

  return (
    (testPBM && featureModePBM != MODE_DISABLED) ||
    (!testPBM && featureMode != MODE_DISABLED)
  );
}

/**
 * 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,
    ]) {
      for (let featureModePBM of [
        MODE_DISABLED,
        MODE_REJECT,
        MODE_REJECT_OR_ACCEPT,
      ]) {
        for (let detectOnly of [false, true]) {
          // Testing detect only mode for normal browsing is sufficient.
          if (detectOnly && featureModePBM != MODE_DISABLED) {
            continue;
          }
          await testSectionVisibility({
            win,
            featureMode,
            featureModePBM,
            detectOnly,
            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 {string} options.expectedSwitchState - Whether the switch is expected to be
 * "on" (CBH enabled), "off" (user added exception), or "unsupported" (no rules for site).
 */
function assertSwitchAndPrefState({ win, isPBM, expectedSwitchState }) {
  let el = {
    section: win.document.getElementById(
      "protections-popup-cookie-banner-section"
    ),
    switch: win.document.getElementById(
      "protections-popup-cookie-banner-switch"
    ),
    labelON: win.document.querySelector(
      "#protections-popup-cookie-banner-detected"
    ),
    labelOFF: win.document.querySelector(
      "#protections-popup-cookie-banner-site-disabled"
    ),
    labelUNDETECTED: win.document.querySelector(
      "#protections-popup-cookie-banner-undetected"
    ),
  };

  let currentURI = win.gBrowser.currentURI;
  let pref = Services.cookieBanners.getDomainPref(currentURI, isPBM);
  if (expectedSwitchState == "on") {
    ok(el.section.dataset.state == "detected", "CBH switch is set to ON");

    ok(BrowserTestUtils.is_visible(el.labelON), "ON label should be visible");
    ok(
      !BrowserTestUtils.is_visible(el.labelOFF),
      "OFF label should not be visible"
    );
    ok(
      !BrowserTestUtils.is_visible(el.labelUNDETECTED),
      "UNDETECTED label should not be visible"
    );

    is(
      pref,
      MODE_UNSET,
      `There should be no per-site exception for ${currentURI.spec}.`
    );
  } else if (expectedSwitchState === "off") {
    ok(el.section.dataset.state == "site-disabled", "CBH switch is set to OFF");

    ok(
      !BrowserTestUtils.is_visible(el.labelON),
      "ON label should not be visible"
    );
    ok(BrowserTestUtils.is_visible(el.labelOFF), "OFF label should be visible");
    ok(
      !BrowserTestUtils.is_visible(el.labelUNDETECTED),
      "UNDETECTED label should not be visible"
    );

    is(
      pref,
      MODE_DISABLED,
      `There should be a per-site exception for ${currentURI.spec}.`
    );
  } else {
    ok(el.section.dataset.state == "undetected", "CBH not supported for site");

    ok(
      !BrowserTestUtils.is_visible(el.labelON),
      "ON label should not be visible"
    );
    ok(
      !BrowserTestUtils.is_visible(el.labelOFF),
      "OFF label should not be visible"
    );
    ok(
      BrowserTestUtils.is_visible(el.labelUNDETECTED),
      "UNDETECTED label should be visible"
    );
  }
}

/**
 * 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,
  });
}

/**
 * Test the cookie banner enable / disable by clicking the switch, then
 * clicking the on/off button in the cookie banner subview. Assumes the
 * protections panel is already open.
 *
 * @param {boolean} enable - Whether we want to enable or disable.
 * @param {Window} win - Current chrome window under test.
 */
async function toggleCookieBannerHandling(enable, win) {
  let switchEl = win.document.getElementById(
    "protections-popup-cookie-banner-switch"
  );
  let enableButton = win.document.getElementById(
    "protections-popup-cookieBannerView-enable-button"
  );
  let disableButton = win.document.getElementById(
    "protections-popup-cookieBannerView-disable-button"
  );
  let subView = win.document.getElementById(
    "protections-popup-cookieBannerView"
  );

  let subViewShownPromise = BrowserTestUtils.waitForEvent(subView, "ViewShown");
  switchEl.click();
  await subViewShownPromise;

  if (enable) {
    ok(BrowserTestUtils.is_visible(enableButton), "Enable button is visible");
    enableButton.click();
  } else {
    ok(BrowserTestUtils.is_visible(disableButton), "Disable button is visible");
    disableButton.click();
  }
}

function waitForProtectionsPopupHide(win = window) {
  return BrowserTestUtils.waitForEvent(
    win.document.getElementById("protections-popup"),
    "popuphidden"
  );
}

/**
 * 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_OR_ACCEPT],
      ["cookiebanners.ui.desktop.enabled", true],
      ["cookiebanners.listService.testRules", exampleRules],
      ["cookiebanners.listService.testSkipRemoteSettings", true],
    ],
  });

  Services.cookieBanners.resetRules(false);
  await BrowserTestUtils.waitForCondition(
    () => !!Services.cookieBanners.rules.length,
    "waiting for Services.cookieBanners.rules.length to be greater than 0"
  );

  // 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 () => {
        let clearSiteDataSpy = sinon.spy(window.SiteDataManager, "remove");

        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,
          expectedSwitchState: "on",
        });
        assertTelemetryState();

        info("Testing switch state after toggle OFF");
        let closePromise = waitForProtectionsPopupHide(win);
        await toggleCookieBannerHandling(false, win);
        await closePromise;
        if (testPBM) {
          Assert.ok(
            clearSiteDataSpy.notCalled,
            "clearSiteData should not be called in private browsing mode"
          );
        } else {
          Assert.ok(
            clearSiteDataSpy.calledOnce,
            "clearSiteData should be called in regular browsing mode"
          );
        }
        clearSiteDataSpy.restore();

        await openProtectionsPanel(null, win);
        assertSwitchAndPrefState({
          win,
          isPBM: testPBM,
          switchEl,
          expectedSwitchState: "off",
        });
        assertTelemetryState({ expectEnabled: false });

        info("Testing switch state after toggle ON.");
        closePromise = waitForProtectionsPopupHide(win);
        await toggleCookieBannerHandling(true, win);
        await closePromise;

        await openProtectionsPanel(null, win);
        assertSwitchAndPrefState({
          win,
          isPBM: testPBM,
          switchEl,
          expectedSwitchState: "on",
        });
        assertTelemetryState({ expectEnabled: true });
      }
    );

    if (testPBM) {
      await BrowserTestUtils.closeWindow(win);
    }
  }

  await SpecialPowers.popPrefEnv();
});