summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_privacy_cookieBannerHandling.js
blob: 722fe9a215a18be045dcf2f715a3b94ed6b65912 (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

// This file tests the Privacy pane's Cookie Banner Handling UI.

"use strict";

const FEATURE_PREF = "cookiebanners.ui.desktop.enabled";
const MODE_PREF = "cookiebanners.service.mode";
const PBM_MODE_PREF = "cookiebanners.service.mode.privateBrowsing";
const DETECT_ONLY_PREF = "cookiebanners.service.detectOnly";

const GROUPBOX_ID = "cookieBannerHandlingGroup";
const CHECKBOX_ID = "handleCookieBanners";

// Test the section is hidden on page load if the feature pref is disabled.
add_task(async function test_section_hidden_when_feature_flag_disabled() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [FEATURE_PREF, false],
      [MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED],
    ],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:preferences#privacy" },
    async function (browser) {
      let groupbox = browser.contentDocument.getElementById(GROUPBOX_ID);
      is_element_hidden(groupbox, "#cookieBannerHandlingGroup is hidden");
    }
  );

  await SpecialPowers.popPrefEnv();
});

// Test the section is shown on page load if the feature pref is enabled.
add_task(async function test_section_shown_when_feature_flag_enabled() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [FEATURE_PREF, true],
      [MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED],
    ],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:preferences#privacy" },
    async function (browser) {
      let groupbox = browser.contentDocument.getElementById(GROUPBOX_ID);
      is_element_visible(groupbox, "#cookieBannerHandlingGroup is visible");
    }
  );

  await SpecialPowers.popPrefEnv();
});

// Test the checkbox is unchecked in DISABLED mode.
add_task(async function test_checkbox_unchecked_disabled_mode() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [FEATURE_PREF, true],
      [MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED],
    ],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:preferences#privacy" },
    async function (browser) {
      let checkbox = browser.contentDocument.getElementById(CHECKBOX_ID);
      ok(!checkbox.checked, "checkbox is not checked in DISABLED mode");
    }
  );

  await SpecialPowers.popPrefEnv();
});

// Test the checkbox is unchecked in detect-only mode.
add_task(async function test_checkbox_unchecked_detect_only_mode() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [FEATURE_PREF, true],
      [MODE_PREF, Ci.nsICookieBannerService.MODE_REJECT],
      [DETECT_ONLY_PREF, true],
    ],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:preferences#privacy" },
    async function (browser) {
      let checkbox = browser.contentDocument.getElementById(CHECKBOX_ID);
      ok(!checkbox.checked, "checkbox is not checked in detect-only mode");
    }
  );

  await SpecialPowers.popPrefEnv();
});

// Test the checkbox is checked in REJECT_OR_ACCEPT mode.
add_task(async function test_checkbox_checked_reject_or_accept_mode() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [FEATURE_PREF, true],
      [MODE_PREF, Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT],
    ],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:preferences#privacy" },
    async function (browser) {
      let checkbox = browser.contentDocument.getElementById(CHECKBOX_ID);
      ok(checkbox.checked, "checkbox is checked in REJECT_OR_ACCEPT mode");
    }
  );

  await SpecialPowers.popPrefEnv();
});

// Test the checkbox is checked in REJECT mode.
add_task(async function test_checkbox_checked_reject_mode() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [FEATURE_PREF, true],
      [MODE_PREF, Ci.nsICookieBannerService.MODE_REJECT],
    ],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:preferences#privacy" },
    async function (browser) {
      let checkbox = browser.contentDocument.getElementById(CHECKBOX_ID);
      ok(checkbox.checked, "checkbox is checked in REJECT mode");
    }
  );

  await SpecialPowers.popPrefEnv();
});

// Test that toggling the checkbox toggles the mode pref value as expected,
// and also disables detect only mode, as expected.
add_task(async function test_checkbox_modifies_prefs() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [FEATURE_PREF, true],
      [MODE_PREF, Ci.nsICookieBannerService.MODE_UNSET],
      [PBM_MODE_PREF, Ci.nsICookieBannerService.MODE_UNSET],
      [DETECT_ONLY_PREF, true],
    ],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:preferences#privacy" },
    async function (browser) {
      let checkboxSelector = "#" + CHECKBOX_ID;
      let checkbox = browser.contentDocument.querySelector(checkboxSelector);
      let section = browser.contentDocument.getElementById(GROUPBOX_ID);

      section.scrollIntoView();

      Assert.ok(
        !checkbox.checked,
        "initially, the checkbox should be unchecked"
      );

      await BrowserTestUtils.synthesizeMouseAtCenter(
        checkboxSelector,
        {},
        browser
      );
      Assert.ok(checkbox.checked, "checkbox should be checked");
      Assert.equal(
        Ci.nsICookieBannerService.MODE_REJECT,
        Services.prefs.getIntPref(MODE_PREF),
        "cookie banner handling mode should be set to REJECT mode after checking the checkbox"
      );
      Assert.equal(
        Ci.nsICookieBannerService.MODE_REJECT,
        Services.prefs.getIntPref(PBM_MODE_PREF),
        "cookie banner handling mode for PBM should be set to REJECT mode after checking the checkbox"
      );
      Assert.equal(
        false,
        Services.prefs.getBoolPref(DETECT_ONLY_PREF),
        "cookie banner handling detect-only mode should be disabled after checking the checkbox"
      );

      await BrowserTestUtils.synthesizeMouseAtCenter(
        checkboxSelector,
        {},
        browser
      );
      Assert.ok(!checkbox.checked, "checkbox should be unchecked");
      Assert.equal(
        Ci.nsICookieBannerService.MODE_DISABLED,
        Services.prefs.getIntPref(MODE_PREF),
        "cookie banner handling mode should be set to DISABLED mode after unchecking the checkbox"
      );
      Assert.equal(
        Ci.nsICookieBannerService.MODE_DISABLED,
        Services.prefs.getIntPref(PBM_MODE_PREF),
        "cookie banner handling mode for PBM should be set to DISABLED mode after unchecking the checkbox"
      );
      Assert.equal(
        false,
        Services.prefs.getBoolPref(DETECT_ONLY_PREF),
        "cookie banner handling detect-only mode should still be disabled after unchecking the checkbox"
      );
    }
  );

  await SpecialPowers.popPrefEnv();
});