summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_empty_search.js
blob: 2c6dffe8e66be0627889d6e1f7cb4b3825dba548 (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
/* 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/. */

/**
 * Test for bug 426864 that makes sure searching a space only shows typed pages
 * from history.
 */

testEngine_setup();

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

  let uri1 = Services.io.newURI("http://t.foo/1");
  let uri2 = Services.io.newURI("http://t.foo/2");
  let uri3 = Services.io.newURI("http://t.foo/3");
  let uri4 = Services.io.newURI("http://t.foo/4");
  let uri5 = Services.io.newURI("http://t.foo/5");
  let uri6 = Services.io.newURI("http://t.foo/6");
  let uri7 = Services.io.newURI("http://t.foo/7");

  await PlacesTestUtils.addVisits([
    { uri: uri7, title: "title" },
    { uri: uri6, title: "title" },
    { uri: uri4, title: "title" },
    { uri: uri3, title: "title" },
    { uri: uri2, title: "title" },
    { uri: uri1, title: "title" },
  ]);

  await PlacesTestUtils.addBookmarkWithDetails({ uri: uri6, title: "title" });
  await PlacesTestUtils.addBookmarkWithDetails({ uri: uri5, title: "title" });
  await PlacesTestUtils.addBookmarkWithDetails({ uri: uri4, title: "title" });
  await PlacesTestUtils.addBookmarkWithDetails({ uri: uri2, title: "title" });

  await addOpenPages(uri7, 1);

  // Now remove page 6 from history, so it is an unvisited bookmark.
  await PlacesUtils.history.remove(uri6);

  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  // With the changes above, the sites in descending order of frecency are:
  // uri2
  // uri4
  // uri5
  // uri6
  // uri1
  // uri3
  // uri7

  info("Match everything");
  let context = createContext("foo", { isPrivate: false });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        engineName: SUGGESTIONS_ENGINE_NAME,
        heuristic: true,
      }),
      makeBookmarkResult(context, {
        uri: uri2.spec,
        title: "title",
      }),
      makeBookmarkResult(context, {
        uri: uri4.spec,
        title: "title",
      }),
      makeBookmarkResult(context, {
        uri: uri5.spec,
        title: "title",
      }),
      makeBookmarkResult(context, {
        uri: uri6.spec,
        title: "title",
      }),
      makeVisitResult(context, { uri: uri1.spec, title: "title" }),
      makeVisitResult(context, { uri: uri3.spec, title: "title" }),
      makeTabSwitchResult(context, { uri: uri7.spec, title: "title" }),
    ],
  });

  info("Match only history");
  context = createContext(`foo ${UrlbarTokenizer.RESTRICT.HISTORY}`, {
    isPrivate: false,
  });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        engineName: SUGGESTIONS_ENGINE_NAME,
        heuristic: true,
      }),
      makeVisitResult(context, { uri: uri2.spec, title: "title" }),
      makeVisitResult(context, { uri: uri4.spec, title: "title" }),
      makeVisitResult(context, { uri: uri1.spec, title: "title" }),
      makeVisitResult(context, { uri: uri3.spec, title: "title" }),
      makeVisitResult(context, { uri: uri7.spec, title: "title" }),
    ],
  });

  info("Drop-down empty search matches history sorted by frecency desc");
  context = createContext(" ", { isPrivate: false });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        engineName: SUGGESTIONS_ENGINE_NAME,
        // query is made explict so makeSearchResult doesn't trim it.
        query: " ",
        heuristic: true,
      }),
      makeVisitResult(context, { uri: uri2.spec, title: "title" }),
      makeVisitResult(context, { uri: uri4.spec, title: "title" }),
      makeVisitResult(context, { uri: uri1.spec, title: "title" }),
      makeVisitResult(context, { uri: uri3.spec, title: "title" }),
      makeVisitResult(context, { uri: uri7.spec, title: "title" }),
    ],
  });

  info("Empty search matches only bookmarks when history is disabled");
  Services.prefs.setBoolPref("browser.urlbar.suggest.history", false);
  Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", true);
  context = createContext(" ", { isPrivate: false });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        engineName: SUGGESTIONS_ENGINE_NAME,
        // query is made explict so makeSearchResult doesn't trim it.
        query: " ",
        heuristic: true,
      }),
      makeBookmarkResult(context, {
        uri: uri2.spec,
        title: "title",
      }),
      makeBookmarkResult(context, {
        uri: uri4.spec,
        title: "title",
      }),
      makeBookmarkResult(context, {
        uri: uri5.spec,
        title: "title",
      }),
      makeBookmarkResult(context, {
        uri: uri6.spec,
        title: "title",
      }),
    ],
  });

  info(
    "Empty search matches only open tabs when bookmarks and history are disabled"
  );
  Services.prefs.setBoolPref("browser.urlbar.suggest.bookmark", false);
  context = createContext(" ", { isPrivate: false });
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        engineName: SUGGESTIONS_ENGINE_NAME,
        // query is made explict so makeSearchResult doesn't trim it.
        query: " ",
        heuristic: true,
      }),
      makeTabSwitchResult(context, { uri: uri7.spec, title: "title" }),
    ],
  });

  Services.prefs.clearUserPref("browser.urlbar.suggest.history");
  Services.prefs.clearUserPref("browser.urlbar.suggest.bookmark");

  await cleanupPlaces();
});