summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/protectionsUI/browser_protectionsUI_categories.js
blob: b647b44d644ece945c37f644240d2ae35a74480b (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
const CAT_PREF = "browser.contentblocking.category";
const TP_PREF = "privacy.trackingprotection.enabled";
const TP_PB_PREF = "privacy.trackingprotection.pbmode.enabled";
const TPC_PREF = "network.cookie.cookieBehavior";
const CM_PREF = "privacy.trackingprotection.cryptomining.enabled";
const FP_PREF = "privacy.trackingprotection.fingerprinting.enabled";
const ST_PREF = "privacy.trackingprotection.socialtracking.enabled";
const STC_PREF = "privacy.socialtracking.block_cookies.enabled";

const { CustomizableUITestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/CustomizableUITestUtils.sys.mjs"
);

registerCleanupFunction(function () {
  Services.prefs.clearUserPref(CAT_PREF);
  Services.prefs.clearUserPref(TP_PREF);
  Services.prefs.clearUserPref(TP_PB_PREF);
  Services.prefs.clearUserPref(TPC_PREF);
  Services.prefs.clearUserPref(CM_PREF);
  Services.prefs.clearUserPref(FP_PREF);
  Services.prefs.clearUserPref(ST_PREF);
  Services.prefs.clearUserPref(STC_PREF);
});

add_task(async function testCookieCategoryLabel() {
  await BrowserTestUtils.withNewTab(
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://www.example.com",
    async function () {
      // Ensure the category nodes exist.
      await openProtectionsPanel();
      await closeProtectionsPanel();
      let categoryItem = document.getElementById(
        "protections-popup-category-cookies"
      );
      let categoryLabel = document.getElementById(
        "protections-popup-cookies-category-label"
      );

      Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
      await TestUtils.waitForCondition(
        () => !categoryItem.classList.contains("blocked"),
        "The category label has updated correctly"
      );
      ok(!categoryItem.classList.contains("blocked"));

      Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT);
      await TestUtils.waitForCondition(
        () => categoryItem.classList.contains("blocked"),
        "The category label has updated correctly"
      );
      ok(categoryItem.classList.contains("blocked"));
      await TestUtils.waitForCondition(
        () =>
          categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blockingAll2.label"
          ),
        "The category label has updated correctly"
      );
      ok(
        categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blockingAll2.label"
          )
      );

      Services.prefs.setIntPref(
        TPC_PREF,
        Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN
      );
      await TestUtils.waitForCondition(
        () => categoryItem.classList.contains("blocked"),
        "The category label has updated correctly"
      );
      ok(categoryItem.classList.contains("blocked"));
      await TestUtils.waitForCondition(
        () =>
          categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blocking3rdParty2.label"
          ),
        "The category label has updated correctly"
      );
      ok(
        categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blocking3rdParty2.label"
          )
      );

      Services.prefs.setIntPref(
        TPC_PREF,
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
      );
      await TestUtils.waitForCondition(
        () => categoryItem.classList.contains("blocked"),
        "The category label has updated correctly"
      );
      ok(categoryItem.classList.contains("blocked"));
      await TestUtils.waitForCondition(
        () =>
          categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blockingTrackers3.label"
          ),
        "The category label has updated correctly"
      );
      ok(
        categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blockingTrackers3.label"
          )
      );

      Services.prefs.setIntPref(
        TPC_PREF,
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN
      );
      await TestUtils.waitForCondition(
        () => categoryItem.classList.contains("blocked"),
        "The category label has updated correctly"
      );
      ok(categoryItem.classList.contains("blocked"));
      await TestUtils.waitForCondition(
        () =>
          categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blockingTrackers3.label"
          ),
        "The category label has updated correctly"
      );
      ok(
        categoryLabel.textContent ==
          gNavigatorBundle.getString(
            "contentBlocking.cookies.blockingTrackers3.label"
          )
      );

      Services.prefs.setIntPref(
        TPC_PREF,
        Ci.nsICookieService.BEHAVIOR_LIMIT_FOREIGN
      );
      await TestUtils.waitForCondition(
        () => !categoryItem.classList.contains("blocked"),
        "The category label has updated correctly"
      );
      ok(!categoryItem.classList.contains("blocked"));
    }
  );
});

let categoryEnabledPrefs = [TP_PREF, STC_PREF, TPC_PREF, CM_PREF, FP_PREF];

let detectedStateFlags = [
  Ci.nsIWebProgressListener.STATE_BLOCKED_TRACKING_CONTENT,
  Ci.nsIWebProgressListener.STATE_BLOCKED_SOCIALTRACKING_CONTENT,
  Ci.nsIWebProgressListener.STATE_COOKIES_LOADED,
  Ci.nsIWebProgressListener.STATE_BLOCKED_CRYPTOMINING_CONTENT,
  Ci.nsIWebProgressListener.STATE_BLOCKED_FINGERPRINTING_CONTENT,
];

async function waitForClass(item, className, shouldBePresent = true) {
  await TestUtils.waitForCondition(() => {
    return item.classList.contains(className) == shouldBePresent;
  }, `Target class ${className} should be ${shouldBePresent ? "present" : "not present"} on item ${item.id}`);

  ok(
    item.classList.contains(className) == shouldBePresent,
    `item.classList.contains(${className}) is ${shouldBePresent} for ${item.id}`
  );
}

add_task(async function testCategorySections() {
  Services.prefs.setBoolPref(ST_PREF, true);

  for (let pref of categoryEnabledPrefs) {
    if (pref == TPC_PREF) {
      Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
    } else {
      Services.prefs.setBoolPref(pref, false);
    }
  }

  await BrowserTestUtils.withNewTab(
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://www.example.com",
    async function () {
      // Ensure the category nodes exist.
      await openProtectionsPanel();
      await closeProtectionsPanel();

      let categoryItems = [
        "protections-popup-category-trackers",
        "protections-popup-category-socialblock",
        "protections-popup-category-cookies",
        "protections-popup-category-cryptominers",
        "protections-popup-category-fingerprinters",
      ].map(id => document.getElementById(id));

      for (let item of categoryItems) {
        await waitForClass(item, "notFound");
        await waitForClass(item, "blocked", false);
      }

      // For every item, we enable the category and spoof a content blocking event,
      // and check that .notFound goes away and .blocked is set. Then we disable the
      // category and checks that .blocked goes away, and .notFound is still unset.
      let contentBlockingState = 0;
      for (let i = 0; i < categoryItems.length; i++) {
        let itemToTest = categoryItems[i];
        let enabledPref = categoryEnabledPrefs[i];
        contentBlockingState |= detectedStateFlags[i];
        if (enabledPref == TPC_PREF) {
          Services.prefs.setIntPref(
            TPC_PREF,
            Ci.nsICookieService.BEHAVIOR_REJECT
          );
        } else {
          Services.prefs.setBoolPref(enabledPref, true);
        }
        gProtectionsHandler.onContentBlockingEvent(contentBlockingState);
        gProtectionsHandler.updatePanelForBlockingEvent(contentBlockingState);
        await waitForClass(itemToTest, "notFound", false);
        await waitForClass(itemToTest, "blocked", true);
        if (enabledPref == TPC_PREF) {
          Services.prefs.setIntPref(
            TPC_PREF,
            Ci.nsICookieService.BEHAVIOR_ACCEPT
          );
        } else {
          Services.prefs.setBoolPref(enabledPref, false);
        }
        await waitForClass(itemToTest, "notFound", false);
        await waitForClass(itemToTest, "blocked", false);
      }
    }
  );
});

/**
 * Check that when we open the popup in a new window, the initial state is correct
 * wrt the pref.
 */
add_task(async function testCategorySectionInitial() {
  let categoryItems = [
    "protections-popup-category-trackers",
    "protections-popup-category-socialblock",
    "protections-popup-category-cookies",
    "protections-popup-category-cryptominers",
    "protections-popup-category-fingerprinters",
  ];
  for (let i = 0; i < categoryItems.length; i++) {
    for (let shouldBlock of [true, false]) {
      let win = await BrowserTestUtils.openNewBrowserWindow();
      // Open non-about: page so our protections are active.
      await BrowserTestUtils.openNewForegroundTab(
        win.gBrowser,
        "https://example.com/"
      );
      let enabledPref = categoryEnabledPrefs[i];
      let contentBlockingState = detectedStateFlags[i];
      if (enabledPref == TPC_PREF) {
        Services.prefs.setIntPref(
          TPC_PREF,
          shouldBlock
            ? Ci.nsICookieService.BEHAVIOR_REJECT
            : Ci.nsICookieService.BEHAVIOR_ACCEPT
        );
      } else {
        Services.prefs.setBoolPref(enabledPref, shouldBlock);
      }
      win.gProtectionsHandler.onContentBlockingEvent(contentBlockingState);
      await openProtectionsPanel(false, win);
      let categoryItem = win.document.getElementById(categoryItems[i]);
      let expectedFound = true;
      // Accepting cookies outright won't mark this as found.
      if (i == 2 && !shouldBlock) {
        // See bug 1653019
        expectedFound = false;
      }
      is(
        categoryItem.classList.contains("notFound"),
        !expectedFound,
        `Should have found ${categoryItems[i]} when it was ${
          shouldBlock ? "blocked" : "allowed"
        }`
      );
      is(
        categoryItem.classList.contains("blocked"),
        shouldBlock,
        `Should ${shouldBlock ? "have blocked" : "not have blocked"} ${
          categoryItems[i]
        }`
      );
      await closeProtectionsPanel(win);
      await BrowserTestUtils.closeWindow(win);
    }
  }
});