summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_autofill_origins_alt_frecency.js
blob: 41ff69acf28b03d351d95d3b62b85dc88995a519 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This is a basic autofill test to ensure enabling the alternative frecency
// algorithm doesn't break autofill or tab-to-search. A more comprehensive
// testing of the algorithm itself is not included since it's something that
// may change frequently according to experimentation results.
// Other existing autofill tests will, of course, need to be adapted once an
// algorithm is promoted to be the default.

ChromeUtils.defineLazyGetter(this, "PlacesFrecencyRecalculator", () => {
  return Cc["@mozilla.org/places/frecency-recalculator;1"].getService(
    Ci.nsIObserver
  ).wrappedJSObject;
});

testEngine_setup();

add_task(async function test_autofill() {
  const origin = "example.com";
  let context = createContext(origin.substring(0, 2), { isPrivate: false });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        engineName: "Suggestions",
        heuristic: true,
      }),
    ],
  });
  // Add many visits.
  const url = `https://${origin}/`;
  await PlacesTestUtils.addVisits(new Array(10).fill(url));
  Assert.equal(
    await PlacesUtils.metadata.get("origin_alt_frecency_threshold", 0),
    0,
    "Check there's no threshold initially"
  );
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  Assert.greater(
    await PlacesUtils.metadata.get("origin_alt_frecency_threshold", 0),
    0,
    "Check a threshold has been calculated"
  );
  await check_results({
    context,
    autofilled: `${origin}/`,
    completed: url,
    matches: [
      makeVisitResult(context, {
        uri: url,
        title: `test visit for ${url}`,
        heuristic: true,
      }),
    ],
  });
  await PlacesUtils.history.clear();
});

add_task(async function test_autofill_www() {
  const origin = "example.com";
  // Add many visits.
  const url = `https://www.${origin}/`;
  await PlacesTestUtils.addVisits(new Array(10).fill(url));
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  let context = createContext(origin.substring(0, 2), { isPrivate: false });
  await check_results({
    context,
    autofilled: `${origin}/`,
    completed: url,
    matches: [
      makeVisitResult(context, {
        uri: url,
        title: `test visit for ${url}`,
        heuristic: true,
      }),
    ],
  });
  await PlacesUtils.history.clear();
});

add_task(
  {
    pref_set: [["browser.urlbar.tabToSearch.onboard.interactionsLeft", 0]],
  },
  async function test_autofill_prefix_priority() {
    const origin = "localhost";
    const url = `https://${origin}/`;
    await PlacesTestUtils.addVisits([url, `http://${origin}/`]);
    await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

    let engine = Services.search.defaultEngine;
    let context = createContext(origin.substring(0, 2), { isPrivate: false });
    await check_results({
      context,
      autofilled: `${origin}/`,
      completed: url,
      matches: [
        makeVisitResult(context, {
          uri: url,
          title: `test visit for ${url}`,
          heuristic: true,
        }),
        makeSearchResult(context, {
          engineName: engine.name,
          engineIconUri: UrlbarUtils.ICON.SEARCH_GLASS,
          uri: UrlbarUtils.stripPublicSuffixFromHost(engine.searchUrlDomain),
          providesSearchMode: true,
          query: "",
          providerName: "TabToSearch",
        }),
      ],
    });
    await PlacesUtils.history.clear();
  }
);

add_task(async function test_autofill_threshold() {
  await PlacesTestUtils.addVisits(new Array(10).fill("https://example.com/"));
  // Add more visits to the same origins to differenciate the frecency scores.
  await PlacesTestUtils.addVisits([
    "https://example.com/2",
    "https://example.com/3",
  ]);
  await PlacesTestUtils.addVisits("https://somethingelse.org/");
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  let threshold = await PlacesUtils.metadata.get(
    "origin_alt_frecency_threshold",
    0
  );
  Assert.greater(
    threshold,
    await PlacesTestUtils.getDatabaseValue("moz_origins", "alt_frecency", {
      host: "somethingelse.org",
    }),
    "Check mozilla.org has a lower frecency than the threshold"
  );
  Assert.equal(
    threshold,
    await PlacesTestUtils.getDatabaseValue("moz_origins", "avg(alt_frecency)"),
    "Check the threshold has been calculared correctly"
  );

  let engine = Services.search.defaultEngine;
  let context = createContext("so", { isPrivate: false });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        heuristic: true,
        query: "so",
        engineName: engine.name,
      }),
      makeVisitResult(context, {
        uri: "https://somethingelse.org/",
        title: "test visit for https://somethingelse.org/",
      }),
    ],
  });
  await PlacesUtils.history.clear();
});

add_task(async function test_autofill_cutoff() {
  // Add many visits older than the default 90 days cutoff.
  const visitDate = new Date(Date.now() - 120 * 86400000);
  await PlacesTestUtils.addVisits(
    new Array(10).fill("https://example.com/").map(url => ({ url, visitDate }))
  );
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  Assert.strictEqual(
    await PlacesTestUtils.getDatabaseValue("moz_origins", "alt_frecency", {
      host: "example.com",
    }),
    null,
    "Check example.com has a NULL frecency"
  );

  let engine = Services.search.defaultEngine;
  let context = createContext("ex", { isPrivate: false });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        heuristic: true,
        query: "ex",
        engineName: engine.name,
      }),
      makeVisitResult(context, {
        uri: "https://example.com/",
        title: "test visit for https://example.com/",
      }),
    ],
  });
  await PlacesUtils.history.clear();
});

add_task(async function test_autofill_threshold_www() {
  // Only one visit to the non-www origin, many to the www. version. We expect
  // example.com to autofill even if its frecency is small, because the overall
  // frecency for both origins should be considered.
  await PlacesTestUtils.addVisits("https://example.com/");
  await PlacesTestUtils.addVisits(
    new Array(10).fill("https://www.example.com/")
  );
  await PlacesTestUtils.addVisits(
    new Array(10).fill("https://www.somethingelse.org/")
  );
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  let threshold = await PlacesUtils.metadata.get(
    "origin_alt_frecency_threshold",
    0
  );
  let frecencyOfExampleCom = await PlacesTestUtils.getDatabaseValue(
    "moz_origins",
    "alt_frecency",
    {
      host: "example.com",
    }
  );
  let frecencyOfWwwExampleCom = await PlacesTestUtils.getDatabaseValue(
    "moz_origins",
    "alt_frecency",
    {
      host: "www.example.com",
    }
  );
  Assert.greater(
    threshold,
    frecencyOfExampleCom,
    "example.com frecency is lower than the threshold"
  );
  Assert.greater(
    frecencyOfWwwExampleCom,
    threshold,
    "www.example.com frecency is higher than the threshold"
  );

  // We used to wrongly use the average between the 2 domains, so check also
  // the average would not autofill.
  Assert.greater(
    threshold,
    [frecencyOfExampleCom, frecencyOfWwwExampleCom].reduce(
      (acc, v, i, arr) => acc + v / arr.length,
      0
    ),
    "Check frecency average is lower than the threshold"
  );

  let context = createContext("ex", { isPrivate: false });
  await check_results({
    context,
    autofilled: "example.com/",
    completed: "https://www.example.com/",
    matches: [
      makeVisitResult(context, {
        uri: "https://www.example.com/",
        title: "test visit for https://www.example.com/",
        heuristic: true,
      }),
      makeVisitResult(context, {
        uri: "https://example.com/",
        title: "test visit for https://example.com/",
      }),
    ],
  });
  await PlacesUtils.history.clear();
});