summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_userInteraction.js
blob: c024f4d9f7fd2eeafc7daa6a5424e9443ef19bc3 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

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

add_task(async function () {
  info("Starting subResources test");

  await SpecialPowers.flushPrefEnv();
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userInteraction.document.interval", 1],
      [
        "network.cookie.cookieBehavior",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      [
        "network.cookie.cookieBehavior.pbmode",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      ["privacy.trackingprotection.enabled", false],
      ["privacy.trackingprotection.pbmode.enabled", false],
      ["privacy.trackingprotection.annotate_channels", true],
    ],
  });

  await UrlClassifierTestUtils.addTestTrackers();

  info("Creating a new tab");
  let tab = BrowserTestUtils.addTab(gBrowser, TEST_TOP_PAGE);
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let uri = Services.io.newURI(TEST_DOMAIN);
  is(
    PermissionTestUtils.testPermission(uri, "storageAccessAPI"),
    Services.perms.UNKNOWN_ACTION,
    "Before user-interaction we don't have a permission"
  );

  let promise = TestUtils.topicObserved("perm-changed", aSubject => {
    let permission = aSubject.QueryInterface(Ci.nsIPermission);
    return (
      permission.type == "storageAccessAPI" &&
      permission.principal.equalsURI(uri)
    );
  });

  info("Simulating user-interaction.");
  await SpecialPowers.spawn(browser, [], async function () {
    content.document.userInteractionForTesting();
  });

  info("Waiting to have a permissions set.");
  await promise;

  // Let's see if the document is able to update the permission correctly.
  for (var i = 0; i < 3; ++i) {
    // Another perm-changed event should be triggered by the timer.
    promise = TestUtils.topicObserved("perm-changed", aSubject => {
      let permission = aSubject.QueryInterface(Ci.nsIPermission);
      return (
        permission.type == "storageAccessAPI" &&
        permission.principal.equalsURI(uri)
      );
    });

    info("Simulating another user-interaction.");
    await SpecialPowers.spawn(browser, [], async function () {
      content.document.userInteractionForTesting();
    });

    info("Waiting to have a permissions set.");
    await promise;
  }

  // Let's disable the document.interval.
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.userInteraction.document.interval", 0]],
  });

  promise = new Promise(resolve => {
    let id;

    function observer() {
      ok(false, "Notification received!");
      Services.obs.removeObserver(observer, "perm-changed");
      clearTimeout(id);
      resolve();
    }

    Services.obs.addObserver(observer, "perm-changed");

    id = setTimeout(() => {
      ok(true, "No notification received!");
      Services.obs.removeObserver(observer, "perm-changed");
      resolve();
    }, 2000);
  });

  info("Simulating another user-interaction.");
  await SpecialPowers.spawn(browser, [], async function () {
    content.document.userInteractionForTesting();
  });

  info("Waiting to have a permissions set.");
  await promise;

  info("Removing the tab");
  BrowserTestUtils.removeTab(tab);

  UrlClassifierTestUtils.cleanupTestTrackers();
});

add_task(async function () {
  info("Cleaning up.");
  await new Promise(resolve => {
    Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
      resolve()
    );
  });
});