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

"use strict";

/**
 * This tests that keywords are displayed and handled correctly.
 */

async function promise_first_result(inputText) {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: inputText,
  });

  return UrlbarTestUtils.getDetailsOfResultAt(window, 0);
}

function assertURL(result, expectedUrl, keyword, input, postData) {
  Assert.equal(result.url, expectedUrl, "Should have the correct URL");
  if (postData) {
    Assert.equal(
      NetUtil.readInputStreamToString(
        result.postData,
        result.postData.available()
      ),
      postData,
      "Should have the correct postData"
    );
  }
}

const TEST_URL = `${TEST_BASE_URL}print_postdata.sjs`;

add_setup(async function () {
  await PlacesUtils.keywords.insert({
    keyword: "get",
    url: TEST_URL + "?q=%s",
  });
  await PlacesUtils.keywords.insert({
    keyword: "post",
    url: TEST_URL,
    postData: "q=%s",
  });
  await PlacesUtils.keywords.insert({
    keyword: "question?",
    url: TEST_URL + "?q2=%s",
  });
  await PlacesUtils.keywords.insert({
    keyword: "?question",
    url: TEST_URL + "?q3=%s",
  });
  // Avoid fetching search suggestions.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.suggest.searches", false]],
  });
  await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + "searchSuggestionEngine.xml",
    setAsDefault: true,
  });

  registerCleanupFunction(async function () {
    await PlacesUtils.keywords.remove("get");
    await PlacesUtils.keywords.remove("post");
    await PlacesUtils.keywords.remove("question?");
    await PlacesUtils.keywords.remove("?question");
    while (gBrowser.tabs.length > 1) {
      BrowserTestUtils.removeTab(gBrowser.selectedTab);
    }
  });
});

add_task(async function test_display_keyword_without_query() {
  await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:mozilla");

  // Test a keyword that also has blank spaces to ensure they are ignored as well.
  let result = await promise_first_result("get  ");

  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.KEYWORD,
    "Should have a keyword result"
  );
  Assert.equal(
    result.displayed.title,
    "https://example.com/browser/browser/components/urlbar/tests/browser/print_postdata.sjs?q=",
    "Node should contain the url of the bookmark"
  );
  let [action] = await document.l10n.formatValues([
    { id: "urlbar-result-action-visit" },
  ]);
  Assert.equal(result.displayed.action, action, "Should have visit indicated");
});

add_task(async function test_keyword_using_get() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:mozilla"
  );

  let result = await promise_first_result("get something");

  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.KEYWORD,
    "Should have a keyword result"
  );
  Assert.equal(
    result.displayed.title,
    "example.com: something",
    "Node should contain the name of the bookmark and query"
  );
  Assert.ok(!result.displayed.action, "Should have an empty action");

  assertURL(result, TEST_URL + "?q=something", "get", "get something");

  let element = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);

  // Click on the result
  info("Normal click on result");
  let tabPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  EventUtils.synthesizeMouseAtCenter(element, {});
  await tabPromise;
  Assert.equal(
    tab.linkedBrowser.currentURI.spec,
    TEST_URL + "?q=something",
    "Tab should have loaded from clicking on result"
  );

  // Middle-click on the result
  info("Middle-click on result");
  result = await promise_first_result("get somethingmore");

  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.KEYWORD,
    "Should have a keyword result"
  );

  assertURL(result, TEST_URL + "?q=somethingmore", "get", "get somethingmore");

  tabPromise = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
  element = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);
  EventUtils.synthesizeMouseAtCenter(element, { button: 1 });
  let tabOpenEvent = await tabPromise;
  let newTab = tabOpenEvent.target;
  await BrowserTestUtils.browserLoaded(newTab.linkedBrowser);
  Assert.equal(
    newTab.linkedBrowser.currentURI.spec,
    TEST_URL + "?q=somethingmore",
    "Tab should have loaded from middle-clicking on result"
  );
});

add_task(async function test_keyword_using_post() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:mozilla"
  );

  let result = await promise_first_result("post something");

  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.KEYWORD,
    "Should have a keyword result"
  );
  Assert.equal(
    result.displayed.title,
    "example.com: something",
    "Node should contain the name of the bookmark and query"
  );
  Assert.ok(!result.displayed.action, "Should have an empty action");

  assertURL(result, TEST_URL, "post", "post something", "q=something");

  // Click on the result
  info("Normal click on result");
  let tabPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  let element = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);
  EventUtils.synthesizeMouseAtCenter(element, {});
  info("waiting for tab");
  await tabPromise;
  Assert.equal(
    tab.linkedBrowser.currentURI.spec,
    TEST_URL,
    "Tab should have loaded from clicking on result"
  );

  let postData = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    async function () {
      return content.document.body.textContent;
    }
  );
  Assert.equal(postData, "q=something", "post data was submitted correctly");
});

add_task(async function test_keyword_with_question_mark() {
  // TODO Bug 1517140: keywords containing restriction chars should not be
  // allowed, or properly supported.
  let result = await promise_first_result("question?");
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.SEARCH,
    "Result should be a search"
  );
  Assert.equal(result.searchParams.query, "question?", "Check search query");

  result = await promise_first_result("question? something");
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.KEYWORD,
    "Result should be a keyword"
  );
  Assert.equal(result.keyword, "question?", "Check search query");

  result = await promise_first_result("?question");
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.SEARCH,
    "Result should be a search"
  );
  Assert.equal(result.searchParams.query, "question", "Check search query");

  result = await promise_first_result("?question something");
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.SEARCH,
    "Result should be a search"
  );
  Assert.equal(
    result.searchParams.query,
    "question something",
    "Check search query"
  );
});