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

const TP_PREF = "privacy.trackingprotection.enabled";
const TRACKING_PAGE =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/trackingPage.html";
const BENIGN_PAGE =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/benignPage.html";
const ABOUT_PAGE = "about:preferences";

/* This asserts that the content blocking event state is correctly reset
 * when navigating to a new location, and that the user is correctly
 * reset when switching between tabs. */

add_task(async function testResetOnLocationChange() {
  Services.prefs.setBoolPref(TP_PREF, true);

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, BENIGN_PAGE);
  let browser = tab.linkedBrowser;

  is(
    browser.getContentBlockingEvents(),
    0,
    "Benign page has no content blocking event"
  );
  ok(
    !gProtectionsHandler.iconBox.hasAttribute("active"),
    "shield is not active"
  );

  await Promise.all([
    promiseTabLoadEvent(tab, TRACKING_PAGE),
    waitForContentBlockingEvent(2),
  ]);

  is(
    browser.getContentBlockingEvents(),
    Ci.nsIWebProgressListener.STATE_BLOCKED_TRACKING_CONTENT,
    "Tracking page has a content blocking event"
  );
  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "shield is active");

  await promiseTabLoadEvent(tab, BENIGN_PAGE);

  is(
    browser.getContentBlockingEvents(),
    0,
    "Benign page has no content blocking event"
  );
  ok(
    !gProtectionsHandler.iconBox.hasAttribute("active"),
    "shield is not active"
  );

  let contentBlockingEvent = waitForContentBlockingEvent(3);
  let trackingTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TRACKING_PAGE
  );
  await contentBlockingEvent;

  is(
    trackingTab.linkedBrowser.getContentBlockingEvents(),
    Ci.nsIWebProgressListener.STATE_BLOCKED_TRACKING_CONTENT,
    "Tracking page has a content blocking event"
  );
  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "shield is active");

  gBrowser.selectedTab = tab;
  is(
    browser.getContentBlockingEvents(),
    0,
    "Benign page has no content blocking event"
  );
  ok(
    !gProtectionsHandler.iconBox.hasAttribute("active"),
    "shield is not active"
  );

  gBrowser.removeTab(trackingTab);
  gBrowser.removeTab(tab);

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

/* Test that the content blocking icon is correctly reset
 * when changing tabs or navigating to an about: page */
add_task(async function testResetOnTabChange() {
  Services.prefs.setBoolPref(TP_PREF, true);

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, ABOUT_PAGE);
  ok(
    !gProtectionsHandler.iconBox.hasAttribute("active"),
    "shield is not active"
  );

  await Promise.all([
    promiseTabLoadEvent(tab, TRACKING_PAGE),
    waitForContentBlockingEvent(3),
  ]);
  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "shield is active");

  await promiseTabLoadEvent(tab, ABOUT_PAGE);
  ok(
    !gProtectionsHandler.iconBox.hasAttribute("active"),
    "shield is not active"
  );

  let contentBlockingEvent = waitForContentBlockingEvent(3);
  let trackingTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TRACKING_PAGE
  );
  await contentBlockingEvent;
  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "shield is active");

  gBrowser.selectedTab = tab;
  ok(
    !gProtectionsHandler.iconBox.hasAttribute("active"),
    "shield is not active"
  );

  gBrowser.removeTab(trackingTab);
  gBrowser.removeTab(tab);

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