summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/protectionsUI/browser_protectionsUI_cookies_subview.js
blob: 5eb16dbac7d11a12f7dda28e9a1d050339207bb2 (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
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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-disable mozilla/no-arbitrary-setTimeout */

"use strict";

const COOKIE_PAGE =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://not-tracking.example.com/browser/browser/base/content/test/protectionsUI/cookiePage.html";
const CONTAINER_PAGE =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://not-tracking.example.com/browser/browser/base/content/test/protectionsUI/containerPage.html";

const TPC_PREF = "network.cookie.cookieBehavior";

add_setup(async function () {
  await UrlClassifierTestUtils.addTestTrackers();

  registerCleanupFunction(() => {
    UrlClassifierTestUtils.cleanupTestTrackers();
  });
});

/*
 * Accepts an array containing 6 elements that identify the testcase:
 * [0] - boolean indicating whether trackers are blocked.
 * [1] - boolean indicating whether third party cookies are blocked.
 * [2] - boolean indicating whether first party cookies are blocked.
 * [3] - integer indicating number of expected content blocking events.
 * [4] - integer indicating number of expected subview list headers.
 * [5] - integer indicating number of expected cookie list items.
 * [6] - integer indicating number of expected cookie list items
 *       after loading a cookie-setting third party URL in an iframe
 * [7] - integer indicating number of expected cookie list items
 *       after loading a cookie-setting first party URL in an iframe
 */
async function assertSitesListed(testCase) {
  let sitesListedTestCases = [
    [true, false, false, 4, 1, 1, 1, 1],
    [true, true, false, 5, 1, 1, 2, 2],
    [true, true, true, 6, 2, 2, 3, 3],
    [false, false, false, 3, 1, 1, 1, 1],
  ];
  let [
    trackersBlocked,
    thirdPartyBlocked,
    firstPartyBlocked,
    contentBlockingEventCount,
    listHeaderCount,
    cookieItemsCount1,
    cookieItemsCount2,
    cookieItemsCount3,
  ] = sitesListedTestCases[testCase];
  let promise = BrowserTestUtils.openNewForegroundTab({
    url: COOKIE_PAGE,
    gBrowser,
  });
  let [tab] = await Promise.all([
    promise,
    waitForContentBlockingEvent(contentBlockingEventCount),
  ]);
  let browser = tab.linkedBrowser;

  await openProtectionsPanel();

  let categoryItem = document.getElementById(
    "protections-popup-category-cookies"
  );

  // Explicitly waiting for the category item becoming visible.
  await TestUtils.waitForCondition(() => {
    return BrowserTestUtils.is_visible(categoryItem);
  });

  ok(BrowserTestUtils.is_visible(categoryItem), "TP category item is visible");
  let cookiesView = document.getElementById("protections-popup-cookiesView");
  let viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
  categoryItem.click();
  await viewShown;

  ok(true, "Cookies view was shown");

  let listHeaders = cookiesView.querySelectorAll(
    ".protections-popup-cookiesView-list-header"
  );
  is(
    listHeaders.length,
    listHeaderCount,
    `We have ${listHeaderCount} list headers.`
  );
  if (listHeaderCount == 1) {
    ok(
      !BrowserTestUtils.is_visible(listHeaders[0]),
      "Only one header, should be hidden"
    );
  } else {
    for (let header of listHeaders) {
      ok(
        BrowserTestUtils.is_visible(header),
        "Multiple list headers - all should be visible."
      );
    }
  }

  let emptyLabels = cookiesView.querySelectorAll(
    ".protections-popup-empty-label"
  );
  is(emptyLabels.length, 0, `We have no empty labels`);

  let listItems = cookiesView.querySelectorAll(".protections-popup-list-item");
  is(
    listItems.length,
    cookieItemsCount1,
    `We have ${cookieItemsCount1} cookies in the list`
  );

  if (trackersBlocked) {
    let trackerTestItem;
    for (let item of listItems) {
      let label = item.querySelector(".protections-popup-list-host-label");
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      if (label.value == "http://trackertest.org") {
        trackerTestItem = item;
        break;
      }
    }
    ok(trackerTestItem, "Has an item for trackertest.org");
    ok(BrowserTestUtils.is_visible(trackerTestItem), "List item is visible");
  }

  if (firstPartyBlocked) {
    let notTrackingExampleItem;
    for (let item of listItems) {
      let label = item.querySelector(".protections-popup-list-host-label");
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      if (label.value == "http://not-tracking.example.com") {
        notTrackingExampleItem = item;
        break;
      }
    }
    ok(notTrackingExampleItem, "Has an item for not-tracking.example.com");
    ok(
      BrowserTestUtils.is_visible(notTrackingExampleItem),
      "List item is visible"
    );
  }
  let mainView = document.getElementById("protections-popup-mainView");
  viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
  let backButton = cookiesView.querySelector(".subviewbutton-back");
  backButton.click();
  await viewShown;

  ok(true, "Main view was shown");

  let change = waitForContentBlockingEvent();
  let timeoutPromise = new Promise(resolve => setTimeout(resolve, 1000));

  await SpecialPowers.spawn(browser, [], function () {
    content.postMessage("third-party-cookie", "*");
  });

  let result = await Promise.race([change, timeoutPromise]);
  is(result, undefined, "No contentBlockingEvent events should be received");

  viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
  categoryItem.click();
  await viewShown;

  ok(true, "Cookies view was shown");

  emptyLabels = cookiesView.querySelectorAll(".protections-popup-empty-label");
  is(emptyLabels.length, 0, `We have no empty labels`);

  listItems = cookiesView.querySelectorAll(".protections-popup-list-item");
  is(
    listItems.length,
    cookieItemsCount2,
    `We have ${cookieItemsCount2} cookies in the list`
  );

  if (thirdPartyBlocked) {
    let test1ExampleItem;
    for (let item of listItems) {
      let label = item.querySelector(".protections-popup-list-host-label");
      if (label.value == "https://test1.example.org") {
        test1ExampleItem = item;
        break;
      }
    }
    ok(test1ExampleItem, "Has an item for test1.example.org");
    ok(BrowserTestUtils.is_visible(test1ExampleItem), "List item is visible");
  }

  if (trackersBlocked || thirdPartyBlocked || firstPartyBlocked) {
    let trackerTestItem;
    for (let item of listItems) {
      let label = item.querySelector(".protections-popup-list-host-label");
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      if (label.value == "http://trackertest.org") {
        trackerTestItem = item;
        break;
      }
    }
    ok(trackerTestItem, "List item should exist for http://trackertest.org");
    ok(BrowserTestUtils.is_visible(trackerTestItem), "List item is visible");
  }

  viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
  backButton.click();
  await viewShown;

  ok(true, "Main view was shown");

  change = waitForSecurityChange();
  timeoutPromise = new Promise(resolve => setTimeout(resolve, 1000));

  await SpecialPowers.spawn(browser, [], function () {
    content.postMessage("first-party-cookie", "*");
  });

  result = await Promise.race([change, timeoutPromise]);
  is(result, undefined, "No securityChange events should be received");

  viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
  categoryItem.click();
  await viewShown;

  ok(true, "Cookies view was shown");

  emptyLabels = cookiesView.querySelectorAll(".protections-popup-empty-label");
  is(emptyLabels.length, 0, "We have no empty labels");

  listItems = cookiesView.querySelectorAll(".protections-popup-list-item");
  is(
    listItems.length,
    cookieItemsCount3,
    `We have ${cookieItemsCount3} cookies in the list`
  );

  if (firstPartyBlocked) {
    let notTrackingExampleItem;
    for (let item of listItems) {
      let label = item.querySelector(".protections-popup-list-host-label");
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      if (label.value == "http://not-tracking.example.com") {
        notTrackingExampleItem = item;
        break;
      }
    }
    ok(notTrackingExampleItem, "Has an item for not-tracking.example.com");
    ok(
      BrowserTestUtils.is_visible(notTrackingExampleItem),
      "List item is visible"
    );
  }

  BrowserTestUtils.removeTab(tab);
}

add_task(async function testCookiesSubView() {
  info("Testing cookies subview with reject tracking cookies.");
  Services.prefs.setIntPref(
    TPC_PREF,
    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
  );
  let testCaseIndex = 0;
  await assertSitesListed(testCaseIndex++);
  info("Testing cookies subview with reject third party cookies.");
  Services.prefs.setIntPref(
    TPC_PREF,
    Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN
  );
  await assertSitesListed(testCaseIndex++);
  info("Testing cookies subview with reject all cookies.");
  Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT);
  await assertSitesListed(testCaseIndex++);
  info("Testing cookies subview with accept all cookies.");
  Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
  await assertSitesListed(testCaseIndex++);

  Services.prefs.clearUserPref(TPC_PREF);
});

add_task(async function testCookiesSubViewAllowed() {
  Services.prefs.setIntPref(
    TPC_PREF,
    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
  );
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      "http://trackertest.org/"
    );
  Services.perms.addFromPrincipal(
    principal,
    "cookie",
    Services.perms.ALLOW_ACTION
  );

  let promise = BrowserTestUtils.openNewForegroundTab({
    url: COOKIE_PAGE,
    gBrowser,
  });
  let [tab] = await Promise.all([promise, waitForContentBlockingEvent(3)]);

  await openProtectionsPanel();

  let categoryItem = document.getElementById(
    "protections-popup-category-cookies"
  );

  // Explicitly waiting for the category item becoming visible.
  await TestUtils.waitForCondition(() => {
    return BrowserTestUtils.is_visible(categoryItem);
  });

  ok(BrowserTestUtils.is_visible(categoryItem), "TP category item is visible");
  let cookiesView = document.getElementById("protections-popup-cookiesView");
  let viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
  categoryItem.click();
  await viewShown;

  ok(true, "Cookies view was shown");

  let listItems = cookiesView.querySelectorAll(".protections-popup-list-item");
  is(listItems.length, 1, "We have 1 cookie in the list");

  let listItem = listItems[0];
  let label = listItem.querySelector(".protections-popup-list-host-label");
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  is(label.value, "http://trackertest.org", "has an item for trackertest.org");
  ok(BrowserTestUtils.is_visible(listItem), "list item is visible");
  ok(
    listItem.classList.contains("allowed"),
    "indicates whether the cookie was blocked or allowed"
  );

  let stateLabel = listItem.querySelector(
    ".protections-popup-list-state-label"
  );
  ok(stateLabel, "List item has a state label");
  ok(BrowserTestUtils.is_visible(stateLabel), "State label is visible");
  is(
    stateLabel.value,
    gNavigatorBundle.getString("contentBlocking.cookiesView.allowed.label"),
    "State label has correct text"
  );

  let button = listItem.querySelector(
    ".permission-popup-permission-remove-button"
  );
  ok(
    BrowserTestUtils.is_visible(button),
    "Permission remove button is visible"
  );
  button.click();
  is(
    Services.perms.testExactPermissionFromPrincipal(principal, "cookie"),
    Services.perms.UNKNOWN_ACTION,
    "Button click should remove cookie pref."
  );
  ok(!listItem.classList.contains("allowed"), "Has removed the allowed class");

  BrowserTestUtils.removeTab(tab);

  Services.prefs.clearUserPref(TPC_PREF);
});

add_task(async function testCookiesSubViewAllowedHeuristic() {
  Services.prefs.setIntPref(
    TPC_PREF,
    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
  );
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      "http://not-tracking.example.com/"
    );

  // Pretend that the tracker has already been interacted with
  let trackerPrincipal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      "http://trackertest.org/"
    );
  Services.perms.addFromPrincipal(
    trackerPrincipal,
    "storageAccessAPI",
    Services.perms.ALLOW_ACTION
  );

  let promise = BrowserTestUtils.openNewForegroundTab({
    url: COOKIE_PAGE,
    gBrowser,
  });
  let [tab] = await Promise.all([promise, waitForContentBlockingEvent(5)]);
  let browser = tab.linkedBrowser;

  let popup;
  let windowCreated = TestUtils.topicObserved(
    "chrome-document-global-created",
    (subject, data) => {
      popup = subject;
      return true;
    }
  );
  let permChanged = TestUtils.topicObserved("perm-changed", (subject, data) => {
    return (
      subject &&
      subject.QueryInterface(Ci.nsIPermission).type ==
        "3rdPartyStorage^http://trackertest.org" &&
      subject.principal.origin == principal.origin &&
      data == "added"
    );
  });

  await SpecialPowers.spawn(browser, [], function () {
    content.postMessage("window-open", "*");
  });
  await Promise.all([windowCreated, permChanged]);

  await new Promise(resolve => waitForFocus(resolve, popup));
  await new Promise(resolve => waitForFocus(resolve, window));

  await openProtectionsPanel();

  let categoryItem = document.getElementById(
    "protections-popup-category-cookies"
  );

  // Explicitly waiting for the category item becoming visible.
  await TestUtils.waitForCondition(() => {
    return BrowserTestUtils.is_visible(categoryItem);
  });

  ok(BrowserTestUtils.is_visible(categoryItem), "TP category item is visible");
  let cookiesView = document.getElementById("protections-popup-cookiesView");
  let viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
  categoryItem.click();
  await viewShown;

  ok(true, "Cookies view was shown");

  let listItems = cookiesView.querySelectorAll(".protections-popup-list-item");
  is(listItems.length, 1, "We have 1 cookie in the list");

  let listItem = listItems[0];
  let label = listItem.querySelector(".protections-popup-list-host-label");
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  is(label.value, "http://trackertest.org", "has an item for trackertest.org");
  ok(BrowserTestUtils.is_visible(listItem), "list item is visible");
  ok(
    listItem.classList.contains("allowed"),
    "indicates whether the cookie was blocked or allowed"
  );

  let button = listItem.querySelector(
    ".permission-popup-permission-remove-button"
  );
  ok(
    BrowserTestUtils.is_visible(button),
    "Permission remove button is visible"
  );
  button.click();
  is(
    Services.perms.testExactPermissionFromPrincipal(
      principal,
      "3rdPartyStorage^http://trackertest.org"
    ),
    Services.perms.UNKNOWN_ACTION,
    "Button click should remove the storage pref."
  );
  ok(!listItem.classList.contains("allowed"), "Has removed the allowed class");

  await SpecialPowers.spawn(browser, [], function () {
    content.postMessage("window-close", "*");
  });

  BrowserTestUtils.removeTab(tab);

  Services.prefs.clearUserPref(TPC_PREF);
});

add_task(async function testCookiesSubViewBlockedDoublyNested() {
  Services.prefs.setIntPref(
    TPC_PREF,
    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
  );

  let promise = BrowserTestUtils.openNewForegroundTab({
    url: CONTAINER_PAGE,
    gBrowser,
  });
  let [tab] = await Promise.all([promise, waitForContentBlockingEvent(3)]);

  await openProtectionsPanel();

  let categoryItem = document.getElementById(
    "protections-popup-category-cookies"
  );

  // Explicitly waiting for the category item becoming visible.
  await TestUtils.waitForCondition(() => {
    return BrowserTestUtils.is_visible(categoryItem);
  });

  ok(BrowserTestUtils.is_visible(categoryItem), "TP category item is visible");
  let cookiesView = document.getElementById("protections-popup-cookiesView");
  let viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
  categoryItem.click();
  await viewShown;

  ok(true, "Cookies view was shown");

  let listItems = cookiesView.querySelectorAll(".protections-popup-list-item");
  is(listItems.length, 1, "We have 1 cookie in the list");

  let listItem = listItems[0];
  let label = listItem.querySelector(".protections-popup-list-host-label");
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  is(label.value, "http://trackertest.org", "has an item for trackertest.org");
  ok(BrowserTestUtils.is_visible(listItem), "list item is visible");
  ok(
    !listItem.classList.contains("allowed"),
    "indicates whether the cookie was blocked or allowed"
  );

  let button = listItem.querySelector(
    ".permission-popup-permission-remove-button"
  );
  ok(!button, "Permission remove button doesn't exist");

  BrowserTestUtils.removeTab(tab);

  Services.prefs.clearUserPref(TPC_PREF);
});