summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_autofill_bookmarked.js
blob: 9b8b77e82c8aa5ce29ab04d6732bf1cd823d6ba1 (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
/* 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 specific autofill test to ensure we pick the correct bookmarked
// state of an origin. Regardless of the order of origins, we should always pick
// the correct bookmarked status.

add_task(async function () {
  registerCleanupFunction(async () => {
    Services.prefs.clearUserPref("browser.urlbar.suggest.searches");
    Services.prefs.clearUserPref("browser.urlbar.suggest.quickactions");
  });
  Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false);
  Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);

  let host = "example.com";
  // Add a bookmark to the http version, but ensure the https version has an
  // higher frecency.
  let bookmark = await PlacesUtils.bookmarks.insert({
    url: `http://${host}`,
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });
  for (let i = 0; i < 3; i++) {
    await PlacesTestUtils.addVisits(`https://${host}`);
  }
  // ensure both fall below the threshold.
  for (let i = 0; i < 15; i++) {
    await PlacesTestUtils.addVisits(`https://not-${host}`);
  }

  async function check_autofill() {
    let threshold = await getOriginAutofillThreshold();
    let httpOriginFrecency = await getOriginFrecency("http://", host);
    Assert.less(
      httpOriginFrecency,
      threshold,
      "Http origin frecency should be below the threshold"
    );
    let httpsOriginFrecency = await getOriginFrecency("https://", host);
    Assert.less(
      httpsOriginFrecency,
      threshold,
      "Https origin frecency should be below the threshold"
    );
    Assert.less(
      httpOriginFrecency,
      httpsOriginFrecency,
      "Http origin frecency should be below the https origin frecency"
    );

    // The http version should be filled because it's bookmarked, but with the
    // https prefix that is more frecent.
    let context = createContext("ex", { isPrivate: false });
    await check_results({
      context,
      autofilled: `${host}/`,
      completed: `https://${host}/`,
      matches: [
        makeVisitResult(context, {
          uri: `https://${host}/`,
          title: `test visit for https://${host}/`,
          heuristic: true,
        }),
        makeVisitResult(context, {
          uri: `https://not-${host}/`,
          title: `test visit for https://not-${host}/`,
        }),
      ],
    });
  }

  await check_autofill();

  // Now remove the bookmark, ensure to remove the orphans, then reinsert the
  // bookmark; thus we physically invert the order of the rows in the table.
  await checkOriginsOrder(host, ["http://", "https://"]);
  await PlacesUtils.bookmarks.remove(bookmark);
  await PlacesUtils.withConnectionWrapper("removeOrphans", async db => {
    db.execute(`DELETE FROM moz_places WHERE url = :url`, {
      url: `http://${host}/`,
    });
    db.execute(
      `DELETE FROM moz_origins WHERE prefix = "http://" AND host = :host`,
      { host }
    );
  });
  bookmark = await PlacesUtils.bookmarks.insert({
    url: `http://${host}`,
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });

  await checkOriginsOrder(host, ["https://", "http://"]);

  await check_autofill();
  await PlacesUtils.history.clear();
  await PlacesUtils.bookmarks.remove(bookmark);
});

add_task(async function test_www() {
  // Add a bookmark to the www version
  let host = "example.com";
  await PlacesUtils.bookmarks.insert({
    url: `http://www.${host}`,
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });

  info("search for start of www.");
  let context = createContext("w", { isPrivate: false });
  await check_results({
    context,
    autofilled: `www.${host}/`,
    completed: `http://www.${host}/`,
    matches: [
      makeVisitResult(context, {
        uri: `http://www.${host}/`,
        fallbackTitle: `www.${host}`,
        heuristic: true,
      }),
    ],
  });
  info("search for full www.");
  context = createContext("www.", { isPrivate: false });
  await check_results({
    context,
    autofilled: `www.${host}/`,
    completed: `http://www.${host}/`,
    matches: [
      makeVisitResult(context, {
        uri: `http://www.${host}/`,
        fallbackTitle: `www.${host}`,
        heuristic: true,
      }),
    ],
  });
  info("search for host without www.");
  context = createContext("ex", { isPrivate: false });
  await check_results({
    context,
    autofilled: `${host}/`,
    completed: `http://www.${host}/`,
    matches: [
      makeVisitResult(context, {
        uri: `http://www.${host}/`,
        fallbackTitle: `www.${host}`,
        heuristic: true,
      }),
    ],
  });
});