summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unifiedcomplete/test_preloaded_sites.js
blob: 62c12ad79c659246f6777af4d0430563b5fcbd79 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
 * Test for bug 1211726 - preload list of top web sites for better
 * autocompletion on empty profiles.
 */

const PREF_FEATURE_ENABLED = "browser.urlbar.usepreloadedtopurls.enabled";
const PREF_FEATURE_EXPIRE_DAYS =
  "browser.urlbar.usepreloadedtopurls.expire_days";

const autocompleteObject = Cc[
  "@mozilla.org/autocomplete/search;1?name=unifiedcomplete"
].getService(Ci.mozIPlacesAutoComplete);

Cu.importGlobalProperties(["fetch"]);

// With or without trailing slash - no matter. URI.spec does have it always.
// Then, check_autocomplete() doesn't cut it off (uses stripPrefix()).
let yahoooURI = NetUtil.newURI("https://yahooo.com/");
let gooogleURI = NetUtil.newURI("https://gooogle.com/");

autocompleteObject.populatePreloadedSiteStorage([
  [yahoooURI.spec, "Yahooo"],
  [gooogleURI.spec, "Gooogle"],
]);

async function assert_feature_works(condition) {
  info("List Results do appear " + condition);
  await check_autocomplete({
    search: "ooo",
    matches: [
      { uri: yahoooURI, title: "Yahooo", style: ["preloaded-top-site"] },
      { uri: gooogleURI, title: "Gooogle", style: ["preloaded-top-site"] },
    ],
  });

  info("Autofill does appear " + condition);
  await check_autocomplete({
    search: "gooo",
    autofilled: "gooogle.com/", // Will fail without trailing slash
    completed: "https://gooogle.com/",
  });
}

async function assert_feature_does_not_appear(condition) {
  info("List Results don't appear " + condition);
  await check_autocomplete({
    search: "ooo",
    matches: [],
  });

  info("Autofill doesn't appear " + condition);
  // "search" is what you type,
  // "autofilled" is what you get in response in the url bar,
  // "completed" is what you get there when you hit enter.
  // So if they are all equal - it's the proof there was no autofill
  // (knowing we have a suitable preloaded top site).
  await check_autocomplete({
    search: "gooo",
    autofilled: "gooo",
    completed: "gooo",
  });
}

add_task(async function test_it_works() {
  // Not expired but OFF
  Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14);
  Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, false);
  await assert_feature_does_not_appear("when OFF by prefs");

  // Now turn it ON
  Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true);
  await assert_feature_works("when ON by prefs");

  // And expire
  Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 0);
  await assert_feature_does_not_appear("when expired");

  await cleanup();
});

add_task(async function test_sorting_against_bookmark() {
  let boookmarkURI = NetUtil.newURI("https://boookmark.com");
  await PlacesTestUtils.addBookmarkWithDetails({
    uri: boookmarkURI,
    title: "Boookmark",
  });

  Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true);
  Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14);

  info("Preloaded Top Sites are placed lower than Bookmarks");
  await check_autocomplete({
    checkSorting: true,
    search: "ooo",
    matches: [
      { uri: boookmarkURI, title: "Boookmark", style: ["bookmark"] },
      { uri: yahoooURI, title: "Yahooo", style: ["preloaded-top-site"] },
      { uri: gooogleURI, title: "Gooogle", style: ["preloaded-top-site"] },
    ],
  });

  await cleanup();
});

add_task(async function test_sorting_against_history() {
  let histoooryURI = NetUtil.newURI("https://histooory.com");
  await PlacesTestUtils.addVisits({ uri: histoooryURI, title: "Histooory" });

  Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true);
  Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14);

  info("Preloaded Top Sites are placed lower than History entries");
  await check_autocomplete({
    checkSorting: true,
    search: "ooo",
    matches: [
      { uri: histoooryURI, title: "Histooory" },
      { uri: yahoooURI, title: "Yahooo", style: ["preloaded-top-site"] },
      { uri: gooogleURI, title: "Gooogle", style: ["preloaded-top-site"] },
    ],
  });

  await cleanup();
});

add_task(async function test_scheme_and_www() {
  // Order is important to check sorting
  let sites = [
    ["https://www.ooops-https-www.com/", "Ooops"],
    ["https://ooops-https.com/", "Ooops"],
    ["HTTP://ooops-HTTP.com/", "Ooops"],
    ["HTTP://www.ooops-HTTP-www.com/", "Ooops"],
    ["https://foo.com/", "Title with www"],
    ["https://www.bar.com/", "Tile"],
  ];

  let titlesMap = new Map(sites);

  autocompleteObject.populatePreloadedSiteStorage(sites);

  let tests = [
    // User typed,
    // Inline autofill (`autofilled`),
    // Substitute after enter is pressed (`completed`),
    //   [List matches, with sorting]
    //   not tested if omitted
    //   !!! first one is always an autofill entry !!!

    [
      // Protocol by itself doesn't match anything
      "https://",
      "https://",
      "https://",
      [],
    ],

    [
      "www.",
      "www.ooops-https-www.com/",
      "https://www.ooops-https-www.com/",
      [
        ["www.ooops-https-www.com/", "https://www.ooops-https-www.com"],
        "HTTP://www.ooops-HTTP-www.com/",
        "https://www.bar.com/",
      ],
    ],

    [
      "http://www.",
      "http://www.ooops-http-www.com/",
      "http://www.ooops-http-www.com/",
      [["http://www.ooops-http-www.com/", "www.ooops-http-www.com"]],
    ],

    ["ftp://ooops", "ftp://ooops", "ftp://ooops", []],

    [
      "ww",
      "www.ooops-https-www.com/",
      "https://www.ooops-https-www.com/",
      [
        ["www.ooops-https-www.com/", "https://www.ooops-https-www.com"],
        "HTTP://www.ooops-HTTP-www.com/",
        ["https://foo.com/", "Title with www", ["preloaded-top-site"]],
        "https://www.bar.com/",
      ],
    ],

    [
      "ooops",
      "ooops-https-www.com/",
      "https://www.ooops-https-www.com/",
      [
        ["ooops-https-www.com/", "https://www.ooops-https-www.com"],
        "https://ooops-https.com/",
        "HTTP://ooops-HTTP.com/",
        "HTTP://www.ooops-HTTP-www.com/",
      ],
    ],

    [
      "www.ooops",
      "www.ooops-https-www.com/",
      "https://www.ooops-https-www.com/",
      [
        ["www.ooops-https-www.com/", "https://www.ooops-https-www.com"],
        "HTTP://www.ooops-HTTP-www.com/",
      ],
    ],

    [
      "ooops-https-www",
      "ooops-https-www.com/",
      "https://www.ooops-https-www.com/",
      [["ooops-https-www.com/", "https://www.ooops-https-www.com"]],
    ],

    ["www.ooops-https.", "www.ooops-https.", "www.ooops-https.", []],

    [
      "https://ooops",
      "https://ooops-https-www.com/",
      "https://www.ooops-https-www.com/",
      [
        ["https://ooops-https-www.com/", "https://www.ooops-https-www.com"],
        "https://ooops-https.com/",
      ],
    ],

    [
      "https://www.ooops",
      "https://www.ooops-https-www.com/",
      "https://www.ooops-https-www.com/",
      [["https://www.ooops-https-www.com/", "https://www.ooops-https-www.com"]],
    ],

    [
      "http://www.ooops-http.",
      "http://www.ooops-http.",
      "http://www.ooops-http.",
      [],
    ],

    ["http://ooops-https", "http://ooops-https", "http://ooops-https", []],
  ];

  function toMatch(entry, index) {
    if (Array.isArray(entry)) {
      return {
        value: entry[0],
        comment: entry[1],
        style: entry[2] || ["autofill", "heuristic", "preloaded-top-site"],
      };
    }
    return {
      uri: NetUtil.newURI(entry),
      title: titlesMap.get(entry),
      style: ["preloaded-top-site"],
    };
  }

  for (let test of tests) {
    let matches = test[3] ? test[3].map(toMatch) : null;
    info("User types: " + test[0]);
    await check_autocomplete({
      checkSorting: true,
      search: test[0],
      autofilled: test[1].toLowerCase(),
      completed: test[2].toLowerCase(),
      matches,
    });
  }

  await cleanup();
});

add_task(async function test_data_file() {
  let response = await fetch(
    "chrome://global/content/unifiedcomplete-top-urls.json"
  );

  info("Source file is supplied and fetched OK");
  Assert.ok(response.ok);

  info("The JSON is parsed");
  let sites = await response.json();

  info("Storage is populated");
  autocompleteObject.populatePreloadedSiteStorage(sites);

  let lastSite = sites.pop();
  let uri = NetUtil.newURI(lastSite[0]);

  info("Storage is populated from JSON correctly");
  await check_autocomplete({
    search: uri.host,
    autofilled: uri.host + "/",
    completed: uri.spec,
  });

  await cleanup();
});

add_task(async function test_partial_scheme() {
  // "tt" should not result in a match of "ttps://whatever.com/".
  autocompleteObject.populatePreloadedSiteStorage([
    ["http://www.ttt.com/", "Test"],
  ]);
  await check_autocomplete({
    search: "tt",
    autofilled: "ttt.com/",
    completed: "http://www.ttt.com/",
    matches: [
      {
        value: "ttt.com/",
        comment: "www.ttt.com",
        style: ["autofill", "heuristic", "preloaded-top-site"],
      },
    ],
  });
  await cleanup();
});