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

/**
 * Tests heuristic results in search mode.
 */

"use strict";

add_setup(async function () {
  await PlacesUtils.history.clear();
  await PlacesUtils.bookmarks.eraseEverything();

  // Add a new mock default engine so we don't hit the network.
  await SearchTestUtils.installSearchExtension(
    { name: "Test" },
    { setAsDefault: true }
  );

  // Add one bookmark we'll use below.
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/bookmark",
  });
  registerCleanupFunction(async () => {
    await PlacesUtils.bookmarks.eraseEverything();
  });
});

// Enters search mode with no results.
add_task(async function noResults() {
  // Do a search that doesn't match our bookmark and enter bookmark search mode.
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "doesn't match anything",
  });
  await UrlbarTestUtils.enterSearchMode(window, {
    source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
  });

  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    0,
    "Zero results since no bookmark matches"
  );

  // Press enter.  Nothing should happen.
  let loadPromise = waitForLoadOrTimeout();
  EventUtils.synthesizeKey("KEY_Enter");
  let loadEvent = await loadPromise;
  Assert.ok(!loadEvent, "Nothing should have loaded");

  await UrlbarTestUtils.promisePopupClose(window);
});

// Enters a local search mode (bookmarks) with a matching result.  No heuristic
// should be present.
add_task(async function localNoHeuristic() {
  // Do a search that matches our bookmark and enter bookmarks search mode.
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "bookmark",
  });
  await UrlbarTestUtils.enterSearchMode(window, {
    source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
  });

  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    1,
    "There should be one result"
  );

  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
  Assert.equal(
    result.source,
    UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
    "Result source should be BOOKMARKS"
  );
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.URL,
    "Result type should be URL"
  );
  Assert.equal(
    result.url,
    "http://example.com/bookmark",
    "Result URL is our bookmark URL"
  );
  Assert.ok(!result.heuristic, "Result should not be heuristic");

  // Press enter.  Nothing should happen.
  let loadPromise = waitForLoadOrTimeout();
  EventUtils.synthesizeKey("KEY_Enter");
  let loadEvent = await loadPromise;
  Assert.ok(!loadEvent, "Nothing should have loaded");

  await UrlbarTestUtils.promisePopupClose(window);
});

// Enters a local search mode (bookmarks) with a matching autofill result.  The
// result should be the heuristic.
add_task(async function localAutofill() {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    // Do a search that autofills our bookmark's origin and enter bookmarks
    // search mode.
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "example",
    });
    await UrlbarTestUtils.enterSearchMode(window, {
      source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
    });

    Assert.equal(
      UrlbarTestUtils.getResultCount(window),
      2,
      "There should be two results"
    );

    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    Assert.equal(
      result.source,
      UrlbarUtils.RESULT_SOURCE.HISTORY,
      "Result source should be HISTORY"
    );
    Assert.equal(
      result.type,
      UrlbarUtils.RESULT_TYPE.URL,
      "Result type should be URL"
    );
    Assert.equal(
      result.url,
      "http://example.com/",
      "Result URL is our bookmark's origin"
    );
    Assert.ok(result.heuristic, "Result should be heuristic");
    Assert.ok(result.autofill, "Result should be autofill");

    result = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
    Assert.equal(
      result.source,
      UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
      "Result source should be BOOKMARKS"
    );
    Assert.equal(
      result.type,
      UrlbarUtils.RESULT_TYPE.URL,
      "Result type should be URL"
    );
    Assert.equal(
      result.url,
      "http://example.com/bookmark",
      "Result URL is our bookmark URL"
    );

    // Press enter.  Our bookmark's origin should be loaded.
    let loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    EventUtils.synthesizeKey("KEY_Enter");
    await loadPromise;
    Assert.equal(
      gBrowser.currentURI.spec,
      "http://example.com/",
      "Bookmark's origin should have loaded"
    );
  });
});

// Enters a remote engine search mode.  There should be a heuristic.
add_task(async function remote() {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    // Do a search and enter search mode with our test engine.
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "remote",
    });
    await UrlbarTestUtils.enterSearchMode(window, {
      engineName: "Test",
    });

    Assert.equal(
      UrlbarTestUtils.getResultCount(window),
      1,
      "There should be one result"
    );

    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    Assert.equal(
      result.source,
      UrlbarUtils.RESULT_SOURCE.SEARCH,
      "Result source should be SEARCH"
    );
    Assert.equal(
      result.type,
      UrlbarUtils.RESULT_TYPE.SEARCH,
      "Result type should be SEARCH"
    );
    Assert.ok(result.searchParams, "searchParams should be present");
    Assert.equal(
      result.searchParams.engine,
      "Test",
      "searchParams.engine should be our test engine"
    );
    Assert.equal(
      result.searchParams.query,
      "remote",
      "searchParams.query should be our query"
    );
    Assert.ok(result.heuristic, "Result should be heuristic");

    // Press enter.  The engine's SERP should load.
    let loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    EventUtils.synthesizeKey("KEY_Enter");
    await loadPromise;
    Assert.equal(
      gBrowser.currentURI.spec,
      "https://example.com/?q=remote",
      "Engine's SERP should have loaded"
    );
  });
});