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

const ROOT_URI =
  "http://mochi.test:8888/tests/toolkit/components/places/tests/browser/";
const REDIRECT_URI = Services.io.newURI(ROOT_URI + "redirect_thrice.sjs");
const INTERMEDIATE_URI_1 = Services.io.newURI(
  ROOT_URI + "redirect_twice_perma.sjs"
);
const INTERMEDIATE_URI_2 = Services.io.newURI(ROOT_URI + "redirect_once.sjs");
const TARGET_URI = Services.io.newURI(
  "http://test1.example.com/tests/toolkit/components/places/tests/browser/final.html"
);

const REDIRECT_SOURCE_VISIT_BONUS = Services.prefs.getIntPref(
  "places.frecency.redirectSourceVisitBonus"
);
const PERM_REDIRECT_VISIT_BONUS = Services.prefs.getIntPref(
  "places.frecency.permRedirectVisitBonus"
);
const TYPED_VISIT_BONUS = Services.prefs.getIntPref(
  "places.frecency.typedVisitBonus"
);

// Ensure that decay frecency doesn't kick in during tests (as a result
// of idle-daily).
Services.prefs.setCharPref("places.frecency.decayRate", "1.0");

registerCleanupFunction(async function () {
  Services.prefs.clearUserPref("places.frecency.decayRate");
  await PlacesUtils.history.clear();
});

async function check_uri(uri, frecency, hidden) {
  is(
    await PlacesTestUtils.getDatabaseValue("moz_places", "frecency", {
      url: uri,
    }),
    frecency,
    "Frecency of the page is the expected one"
  );
  is(
    await PlacesTestUtils.getDatabaseValue("moz_places", "hidden", {
      url: uri,
    }),
    hidden,
    "Hidden value of the page is the expected one"
  );
}

async function waitVisitedNotifications() {
  let redirectNotified = false;
  await PlacesTestUtils.waitForNotification("page-visited", visits => {
    is(visits.length, 1, "Was notified for the right number of visits.");
    let { url } = visits[0];
    info("Received 'page-visited': " + url);
    if (url == REDIRECT_URI.spec) {
      redirectNotified = true;
    }
    return url == TARGET_URI.spec;
  });
  return redirectNotified;
}

let firstRedirectBonus = 0;
let nextRedirectBonus = 0;
let targetBonus = 0;

add_task(async function test_multiple_redirect() {
  // The redirect source bonus overrides the link bonus.
  let visitedPromise = waitVisitedNotifications();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: REDIRECT_URI.spec,
    },
    async function () {
      info("Waiting for onVisits");
      let redirectNotified = await visitedPromise;
      ok(redirectNotified, "The redirect should have been notified");

      firstRedirectBonus += REDIRECT_SOURCE_VISIT_BONUS;
      await check_uri(REDIRECT_URI, firstRedirectBonus, 1);
      nextRedirectBonus += REDIRECT_SOURCE_VISIT_BONUS;
      await check_uri(INTERMEDIATE_URI_1, nextRedirectBonus, 1);
      await check_uri(INTERMEDIATE_URI_2, nextRedirectBonus, 1);
      // TODO Bug 487813 - This should be TYPED_VISIT_BONUS, however as we don't
      // currently track redirects across multiple redirects, we fallback to the
      // PERM_REDIRECT_VISIT_BONUS.
      targetBonus += PERM_REDIRECT_VISIT_BONUS;
      await check_uri(TARGET_URI, targetBonus, 0);
    }
  );
});

add_task(async function test_multiple_redirect_typed() {
  // The typed bonus wins because the redirect is permanent.
  PlacesUtils.history.markPageAsTyped(REDIRECT_URI);
  let visitedPromise = waitVisitedNotifications();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: REDIRECT_URI.spec,
    },
    async function () {
      info("Waiting for onVisits");
      let redirectNotified = await visitedPromise;
      ok(redirectNotified, "The redirect should have been notified");

      firstRedirectBonus += TYPED_VISIT_BONUS;
      await check_uri(REDIRECT_URI, firstRedirectBonus, 1);
      nextRedirectBonus += REDIRECT_SOURCE_VISIT_BONUS;
      await check_uri(INTERMEDIATE_URI_1, nextRedirectBonus, 1);
      await check_uri(INTERMEDIATE_URI_2, nextRedirectBonus, 1);
      // TODO Bug 487813 - This should be TYPED_VISIT_BONUS, however as we don't
      // currently track redirects across multiple redirects, we fallback to the
      // PERM_REDIRECT_VISIT_BONUS.
      targetBonus += PERM_REDIRECT_VISIT_BONUS;
      await check_uri(TARGET_URI, targetBonus, 0);
    }
  );
});

add_task(async function test_second_typed_visit() {
  // The typed bonus wins because the redirect is permanent.
  PlacesUtils.history.markPageAsTyped(REDIRECT_URI);
  let visitedPromise = waitVisitedNotifications();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: REDIRECT_URI.spec,
    },
    async function () {
      info("Waiting for onVisits");
      let redirectNotified = await visitedPromise;
      ok(redirectNotified, "The redirect should have been notified");

      firstRedirectBonus += TYPED_VISIT_BONUS;
      await check_uri(REDIRECT_URI, firstRedirectBonus, 1);
      nextRedirectBonus += REDIRECT_SOURCE_VISIT_BONUS;
      await check_uri(INTERMEDIATE_URI_1, nextRedirectBonus, 1);
      await check_uri(INTERMEDIATE_URI_2, nextRedirectBonus, 1);
      // TODO Bug 487813 - This should be TYPED_VISIT_BONUS, however as we don't
      // currently track redirects across multiple redirects, we fallback to the
      // PERM_REDIRECT_VISIT_BONUS.
      targetBonus += PERM_REDIRECT_VISIT_BONUS;
      await check_uri(TARGET_URI, targetBonus, 0);
    }
  );
});

add_task(async function test_subsequent_link_visit() {
  // Another non typed visit.
  let visitedPromise = waitVisitedNotifications();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: REDIRECT_URI.spec,
    },
    async function () {
      info("Waiting for onVisits");
      let redirectNotified = await visitedPromise;
      ok(redirectNotified, "The redirect should have been notified");

      firstRedirectBonus += REDIRECT_SOURCE_VISIT_BONUS;
      await check_uri(REDIRECT_URI, firstRedirectBonus, 1);
      nextRedirectBonus += REDIRECT_SOURCE_VISIT_BONUS;
      await check_uri(INTERMEDIATE_URI_1, nextRedirectBonus, 1);
      await check_uri(INTERMEDIATE_URI_2, nextRedirectBonus, 1);
      // TODO Bug 487813 - This should be TYPED_VISIT_BONUS, however as we don't
      // currently track redirects across multiple redirects, we fallback to the
      // PERM_REDIRECT_VISIT_BONUS.
      targetBonus += PERM_REDIRECT_VISIT_BONUS;
      await check_uri(TARGET_URI, targetBonus, 0);
    }
  );
});