summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/browser/browser_visituri_restriction.js
blob: 2af41e6f51620774c6d2aa117c418e4711d21a73 (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

const TEST_PAGE = `data:text/html,
 <a href="https://example.com/" target="_blank">https://example.com</a>
 <a href="http://example.com/" target="_blank">http://example.com</a>
 <a href="https://www.example.com/" target="_blank">https://www.example.com</a>
 <a href="https://example.org/" target="_blank">https://example.org</a>`;

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Enable restriction feature.
      ["places.history.floodingPrevention.enabled", true],
      // Restrict from the second visit.
      ["places.history.floodingPrevention.restrictionCount", 1],
      ["places.history.floodingPrevention.restrictionExpireSeconds", 4],
      // Not apply flooding prevention until some seconds elapse after user
      // interaction begins.
      [
        "places.history.floodingPrevention.maxSecondsFromLastUserInteraction",
        4,
      ],
      // To enable UserActivation by EventUtils.synthesizeMouseAtCenter() in
      // ContentTask.spawn() in synthesizeVisitByUser().
      ["test.events.async.enabled", true],
    ],
  });
  await clearHistoryAndHistoryCache();
});

add_task(async function basic() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: TEST_PAGE },
    async browser => {
      info("Sanity check");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 0,
        isVisited: false,
      });
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 0,
        isVisited: false,
      });
      await assertLinkVisitedStatus(browser, "https://www.example.com/", {
        visitCount: 0,
        isVisited: false,
      });
      await assertLinkVisitedStatus(browser, "https://example.org/", {
        visitCount: 0,
        isVisited: false,
      });

      info("Visit https://example.com/ by user");
      await synthesizeVisitByUser(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 1,
        isVisited: true,
      });

      info(
        "Visit https://example.com/ by script within maxSecondsFromLastUserInteraction"
      );
      await synthesizeVisitByScript(browser, "https://example.com/");
      await synthesizeVisitByScript(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 3,
        isVisited: true,
      });

      await waitForPrefSeconds("maxSecondsFromLastUserInteraction");

      info(
        "Visit https://example.com/ by script without maxSecondsFromLastUserInteraction"
      );
      await synthesizeVisitByScript(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 4,
        isVisited: true,
      });

      info("Visit again, but it should be restricted");
      await synthesizeVisitByScript(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 4,
        isVisited: true,
      });

      info("Check other");
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      await assertLinkVisitedStatus(browser, "http://example.com/", {
        visitCount: 0,
        isVisited: false,
      });
      await assertLinkVisitedStatus(browser, "https://www.example.com/", {
        visitCount: 0,
        isVisited: false,
      });
      await assertLinkVisitedStatus(browser, "https://example.org/", {
        visitCount: 0,
        isVisited: false,
      });
    }
  );

  await clearHistoryAndHistoryCache();
});

add_task(async function expireRestriction() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: TEST_PAGE },
    async browser => {
      info("Visit https://example.com/ by user");
      await synthesizeVisitByUser(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 1,
        isVisited: true,
      });

      info(
        "Visit https://example.com/ by script within maxSecondsFromLastUserInteraction"
      );
      await synthesizeVisitByScript(browser, "https://example.com/");
      await synthesizeVisitByScript(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 3,
        isVisited: true,
      });

      await waitForPrefSeconds("maxSecondsFromLastUserInteraction");

      info(
        "Visit https://example.com/ by script without maxSecondsFromLastUserInteraction"
      );
      await synthesizeVisitByScript(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 4,
        isVisited: true,
      });

      await waitForPrefSeconds("restrictionExpireSeconds");

      info("Visit again, it should not be restricted");
      await synthesizeVisitByScript(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 5,
        isVisited: true,
      });
    }
  );

  await clearHistoryAndHistoryCache();
});

add_task(async function userInputAlwaysAcceptable() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: TEST_PAGE },
    async browser => {
      info("Visit by user");
      await synthesizeVisitByUser(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 1,
        isVisited: true,
      });
      await synthesizeVisitByUser(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 2,
        isVisited: true,
      });

      await waitForPrefSeconds("maxSecondsFromLastUserInteraction");

      info("Visit by user input");
      await synthesizeVisitByUser(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 3,
        isVisited: true,
      });
      await synthesizeVisitByUser(browser, "https://example.com/");
      await assertLinkVisitedStatus(browser, "https://example.com/", {
        visitCount: 4,
        isVisited: true,
      });
    }
  );

  await clearHistoryAndHistoryCache();
});

add_task(async function disable() {
  await SpecialPowers.pushPrefEnv({
    set: [["places.history.floodingPrevention.enabled", false]],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: TEST_PAGE },
    async browser => {
      info("Any visits are stored");
      for (let i = 0; i < 3; i++) {
        await synthesizeVisitByScript(browser, "https://example.com/");
        await assertLinkVisitedStatus(browser, "https://example.com/", {
          visitCount: i + 1,
          isVisited: true,
        });
      }
    }
  );

  await clearHistoryAndHistoryCache();
});

async function waitForPrefSeconds(pref) {
  info(`Wait until elapsing ${pref}`);
  return new Promise(r =>
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    setTimeout(
      r,
      Services.prefs.getIntPref(`places.history.floodingPrevention.${pref}`) *
        1000 +
        100
    )
  );
}