Add an exponential backoff to rechecking the app list doodle.
[chromium-blink-merge.git] / components / omnibox / autocomplete_input_unittest.cc
blobe4527dc1173d54b23de2731009383660b760d5d4
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/omnibox/autocomplete_input.h"
7 #include "base/basictypes.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "build/build_config.h"
11 #include "components/metrics/proto/omnibox_event.pb.h"
12 #include "components/metrics/proto/omnibox_input_type.pb.h"
13 #include "components/omnibox/test_scheme_classifier.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/url_parse.h"
17 using base::ASCIIToUTF16;
18 using metrics::OmniboxEventProto;
20 TEST(AutocompleteInputTest, InputType) {
21 struct test_data {
22 const base::string16 input;
23 const metrics::OmniboxInputType::Type type;
24 } input_cases[] = {
25 { base::string16(), metrics::OmniboxInputType::INVALID },
26 { ASCIIToUTF16("?"), metrics::OmniboxInputType::FORCED_QUERY },
27 { ASCIIToUTF16("?foo"), metrics::OmniboxInputType::FORCED_QUERY },
28 { ASCIIToUTF16("?foo bar"), metrics::OmniboxInputType::FORCED_QUERY },
29 { ASCIIToUTF16("?http://foo.com/bar"),
30 metrics::OmniboxInputType::FORCED_QUERY },
31 { ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN },
32 { ASCIIToUTF16("localhost"), metrics::OmniboxInputType::URL },
33 { ASCIIToUTF16("foo.c"), metrics::OmniboxInputType::UNKNOWN },
34 { ASCIIToUTF16("foo.com"), metrics::OmniboxInputType::URL },
35 { ASCIIToUTF16("-foo.com"), metrics::OmniboxInputType::URL },
36 { ASCIIToUTF16("foo-.com"), metrics::OmniboxInputType::URL },
37 { ASCIIToUTF16("foo_.com"), metrics::OmniboxInputType::UNKNOWN },
38 { ASCIIToUTF16("foo.-com"), metrics::OmniboxInputType::QUERY },
39 { ASCIIToUTF16("foo/"), metrics::OmniboxInputType::URL },
40 { ASCIIToUTF16("foo/bar"), metrics::OmniboxInputType::UNKNOWN },
41 { ASCIIToUTF16("foo/bar%00"), metrics::OmniboxInputType::QUERY },
42 { ASCIIToUTF16("foo/bar/"), metrics::OmniboxInputType::URL },
43 { ASCIIToUTF16("foo/bar baz\\"), metrics::OmniboxInputType::URL },
44 { ASCIIToUTF16("foo.com/bar"), metrics::OmniboxInputType::URL },
45 { ASCIIToUTF16("foo;bar"), metrics::OmniboxInputType::UNKNOWN },
46 { ASCIIToUTF16("foo/bar baz"), metrics::OmniboxInputType::UNKNOWN },
47 { ASCIIToUTF16("foo bar.com"), metrics::OmniboxInputType::QUERY },
48 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY },
49 { ASCIIToUTF16("foo+bar"), metrics::OmniboxInputType::QUERY },
50 { ASCIIToUTF16("foo+bar.com"), metrics::OmniboxInputType::UNKNOWN },
51 { ASCIIToUTF16("\"foo:bar\""), metrics::OmniboxInputType::QUERY },
52 { ASCIIToUTF16("link:foo.com"), metrics::OmniboxInputType::UNKNOWN },
53 { ASCIIToUTF16("foo:81"), metrics::OmniboxInputType::URL },
54 { ASCIIToUTF16("localhost:8080"), metrics::OmniboxInputType::URL },
55 { ASCIIToUTF16("www.foo.com:81"), metrics::OmniboxInputType::URL },
56 { ASCIIToUTF16("foo.com:123456"), metrics::OmniboxInputType::QUERY },
57 { ASCIIToUTF16("foo.com:abc"), metrics::OmniboxInputType::QUERY },
58 { ASCIIToUTF16("1.2.3.4:abc"), metrics::OmniboxInputType::QUERY },
59 { ASCIIToUTF16("user@foo"), metrics::OmniboxInputType::UNKNOWN },
60 { ASCIIToUTF16("user@foo.com"), metrics::OmniboxInputType::UNKNOWN },
61 { ASCIIToUTF16("user@foo/z"), metrics::OmniboxInputType::URL },
62 { ASCIIToUTF16("user@foo/z z"), metrics::OmniboxInputType::URL },
63 { ASCIIToUTF16("user@foo.com/z"), metrics::OmniboxInputType::URL },
64 { ASCIIToUTF16("user:pass@"), metrics::OmniboxInputType::UNKNOWN },
65 { ASCIIToUTF16("user:pass@!foo.com"), metrics::OmniboxInputType::UNKNOWN },
66 { ASCIIToUTF16("user:pass@foo"), metrics::OmniboxInputType::URL },
67 { ASCIIToUTF16("user:pass@foo.c"), metrics::OmniboxInputType::URL },
68 { ASCIIToUTF16("user:pass@foo.com"), metrics::OmniboxInputType::URL },
69 { ASCIIToUTF16("user:pass@foo.com:81"), metrics::OmniboxInputType::URL },
70 { ASCIIToUTF16("user:pass@foo:81"), metrics::OmniboxInputType::URL },
71 { ASCIIToUTF16(".1"), metrics::OmniboxInputType::QUERY },
72 { ASCIIToUTF16(".1/3"), metrics::OmniboxInputType::QUERY },
73 { ASCIIToUTF16("1.2"), metrics::OmniboxInputType::QUERY },
74 { ASCIIToUTF16(".1.2"), metrics::OmniboxInputType::UNKNOWN },
75 { ASCIIToUTF16("1.2/"), metrics::OmniboxInputType::URL },
76 { ASCIIToUTF16("1.2/45"), metrics::OmniboxInputType::QUERY },
77 { ASCIIToUTF16("6008/32768"), metrics::OmniboxInputType::QUERY },
78 { ASCIIToUTF16("12345678/"), metrics::OmniboxInputType::QUERY },
79 { ASCIIToUTF16("123456789/"), metrics::OmniboxInputType::URL },
80 { ASCIIToUTF16("1.2:45"), metrics::OmniboxInputType::QUERY },
81 { ASCIIToUTF16("user@1.2:45"), metrics::OmniboxInputType::QUERY },
82 { ASCIIToUTF16("user@foo:45"), metrics::OmniboxInputType::URL },
83 { ASCIIToUTF16("user:pass@1.2:45"), metrics::OmniboxInputType::URL },
84 { ASCIIToUTF16("host?query"), metrics::OmniboxInputType::UNKNOWN },
85 { ASCIIToUTF16("host#ref"), metrics::OmniboxInputType::QUERY },
86 { ASCIIToUTF16("host#"), metrics::OmniboxInputType::QUERY },
87 { ASCIIToUTF16("host#ref"), metrics::OmniboxInputType::QUERY },
88 { ASCIIToUTF16("host# ref"), metrics::OmniboxInputType::QUERY },
89 { ASCIIToUTF16("host/#ref"), metrics::OmniboxInputType::URL },
90 { ASCIIToUTF16("host/?#ref"), metrics::OmniboxInputType::URL },
91 { ASCIIToUTF16("host/?#"), metrics::OmniboxInputType::URL },
92 { ASCIIToUTF16("host.com#ref"), metrics::OmniboxInputType::URL },
93 { ASCIIToUTF16("http://host#ref"), metrics::OmniboxInputType::URL },
94 { ASCIIToUTF16("host/path?query"), metrics::OmniboxInputType::URL },
95 { ASCIIToUTF16("host/path#ref"), metrics::OmniboxInputType::URL },
96 { ASCIIToUTF16("en.wikipedia.org/wiki/Jim Beam"),
97 metrics::OmniboxInputType::URL },
98 // In Chrome itself, mailto: will get handled by ShellExecute, but in
99 // unittest mode, we don't have the data loaded in the external protocol
100 // handler to know this.
101 // { ASCIIToUTF16("mailto:abuse@foo.com"), metrics::OmniboxInputType::URL },
102 { ASCIIToUTF16("view-source:http://www.foo.com/"),
103 metrics::OmniboxInputType::URL },
104 { ASCIIToUTF16("javascript:alert(\"Hi there\");"),
105 metrics::OmniboxInputType::URL },
106 #if defined(OS_WIN)
107 { ASCIIToUTF16("C:\\Program Files"), metrics::OmniboxInputType::URL },
108 { ASCIIToUTF16("\\\\Server\\Folder\\File"),
109 metrics::OmniboxInputType::URL },
110 #endif // defined(OS_WIN)
111 { ASCIIToUTF16("http:foo"), metrics::OmniboxInputType::URL },
112 { ASCIIToUTF16("http://foo"), metrics::OmniboxInputType::URL },
113 { ASCIIToUTF16("http://foo.c"), metrics::OmniboxInputType::URL },
114 { ASCIIToUTF16("http://foo.com"), metrics::OmniboxInputType::URL },
115 { ASCIIToUTF16("http://foo_bar.com"), metrics::OmniboxInputType::URL },
116 { ASCIIToUTF16("http://foo/bar%00"), metrics::OmniboxInputType::QUERY },
117 { ASCIIToUTF16("http://foo/bar baz"), metrics::OmniboxInputType::URL },
118 { ASCIIToUTF16("http://-foo.com"), metrics::OmniboxInputType::URL },
119 { ASCIIToUTF16("http://foo-.com"), metrics::OmniboxInputType::URL },
120 { ASCIIToUTF16("http://foo_.com"), metrics::OmniboxInputType::UNKNOWN },
121 { ASCIIToUTF16("http://foo.-com"), metrics::OmniboxInputType::UNKNOWN },
122 { ASCIIToUTF16("http://_foo_.com"), metrics::OmniboxInputType::UNKNOWN },
123 { ASCIIToUTF16("http://foo.com:abc"), metrics::OmniboxInputType::QUERY },
124 { ASCIIToUTF16("http://foo.com:123456"), metrics::OmniboxInputType::QUERY },
125 { ASCIIToUTF16("http://1.2.3.4:abc"), metrics::OmniboxInputType::QUERY },
126 { ASCIIToUTF16("http:user@foo.com"), metrics::OmniboxInputType::URL },
127 { ASCIIToUTF16("http://user@foo.com"), metrics::OmniboxInputType::URL },
128 { ASCIIToUTF16("http://user:pass@foo"), metrics::OmniboxInputType::URL },
129 { ASCIIToUTF16("http:user:pass@foo.com"), metrics::OmniboxInputType::URL },
130 { ASCIIToUTF16("http://user:pass@foo.com"),
131 metrics::OmniboxInputType::URL },
132 { ASCIIToUTF16("http://1.2"), metrics::OmniboxInputType::URL },
133 { ASCIIToUTF16("http:user@1.2"), metrics::OmniboxInputType::URL },
134 { ASCIIToUTF16("http://1.2/45"), metrics::OmniboxInputType::URL },
135 { ASCIIToUTF16("http:ps/2 games"), metrics::OmniboxInputType::URL },
136 { ASCIIToUTF16("https://foo.com"), metrics::OmniboxInputType::URL },
137 { ASCIIToUTF16("127.0.0.1"), metrics::OmniboxInputType::URL },
138 { ASCIIToUTF16("127.0.1"), metrics::OmniboxInputType::QUERY },
139 { ASCIIToUTF16("127.0.1/"), metrics::OmniboxInputType::URL },
140 { ASCIIToUTF16("0.0.0"), metrics::OmniboxInputType::QUERY },
141 { ASCIIToUTF16("0.0.0.0"), metrics::OmniboxInputType::URL },
142 { ASCIIToUTF16("0.0.0.1"), metrics::OmniboxInputType::QUERY },
143 { ASCIIToUTF16("http://0.0.0.1/"), metrics::OmniboxInputType::QUERY },
144 { ASCIIToUTF16("browser.tabs.closeButtons"),
145 metrics::OmniboxInputType::UNKNOWN },
146 { base::WideToUTF16(L"\u6d4b\u8bd5"), metrics::OmniboxInputType::UNKNOWN },
147 { ASCIIToUTF16("[2001:]"), metrics::OmniboxInputType::QUERY },
148 { ASCIIToUTF16("[2001:dB8::1]"), metrics::OmniboxInputType::URL },
149 { ASCIIToUTF16("192.168.0.256"), metrics::OmniboxInputType::QUERY },
150 { ASCIIToUTF16("[foo.com]"), metrics::OmniboxInputType::QUERY },
151 { ASCIIToUTF16("filesystem:http://a.com/t/bar"),
152 metrics::OmniboxInputType::URL },
153 { ASCIIToUTF16("filesystem:http://a.com/"),
154 metrics::OmniboxInputType::QUERY },
155 { ASCIIToUTF16("filesystem:file://"), metrics::OmniboxInputType::QUERY },
156 { ASCIIToUTF16("filesystem:http"), metrics::OmniboxInputType::QUERY },
157 { ASCIIToUTF16("filesystem:"), metrics::OmniboxInputType::QUERY },
158 { ASCIIToUTF16("chrome-search://"), metrics::OmniboxInputType::QUERY },
159 { ASCIIToUTF16("chrome-devtools:"), metrics::OmniboxInputType::QUERY },
160 { ASCIIToUTF16("about://f;"), metrics::OmniboxInputType::QUERY },
161 { ASCIIToUTF16("://w"), metrics::OmniboxInputType::QUERY },
162 { ASCIIToUTF16(":w"), metrics::OmniboxInputType::QUERY },
163 { base::WideToUTF16(L".\u062A"), metrics::OmniboxInputType::UNKNOWN },
166 for (size_t i = 0; i < arraysize(input_cases); ++i) {
167 SCOPED_TRACE(input_cases[i].input);
168 AutocompleteInput input(input_cases[i].input, base::string16::npos,
169 std::string(), GURL(),
170 OmniboxEventProto::INVALID_SPEC, true, false, true,
171 true, TestSchemeClassifier());
172 EXPECT_EQ(input_cases[i].type, input.type());
176 TEST(AutocompleteInputTest, InputTypeWithDesiredTLD) {
177 struct test_data {
178 const base::string16 input;
179 const metrics::OmniboxInputType::Type type;
180 const std::string spec; // Unused if not a URL.
181 } input_cases[] = {
182 { ASCIIToUTF16("401k"), metrics::OmniboxInputType::URL,
183 std::string("http://www.401k.com/") },
184 { ASCIIToUTF16("56"), metrics::OmniboxInputType::URL,
185 std::string("http://www.56.com/") },
186 { ASCIIToUTF16("1.2"), metrics::OmniboxInputType::URL,
187 std::string("http://www.1.2.com/") },
188 { ASCIIToUTF16("1.2/3.4"), metrics::OmniboxInputType::URL,
189 std::string("http://www.1.2.com/3.4") },
190 { ASCIIToUTF16("192.168.0.1"), metrics::OmniboxInputType::URL,
191 std::string("http://www.192.168.0.1.com/") },
192 { ASCIIToUTF16("999999999999999"), metrics::OmniboxInputType::URL,
193 std::string("http://www.999999999999999.com/") },
194 { ASCIIToUTF16("x@y"), metrics::OmniboxInputType::URL,
195 std::string("http://x@www.y.com/") },
196 { ASCIIToUTF16("x@y.com"), metrics::OmniboxInputType::URL,
197 std::string("http://x@y.com/") },
198 { ASCIIToUTF16("y/z z"), metrics::OmniboxInputType::URL,
199 std::string("http://www.y.com/z%20z") },
200 { ASCIIToUTF16("abc.com"), metrics::OmniboxInputType::URL,
201 std::string("http://abc.com/") },
202 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY,
203 std::string() },
206 for (size_t i = 0; i < arraysize(input_cases); ++i) {
207 SCOPED_TRACE(input_cases[i].input);
208 AutocompleteInput input(input_cases[i].input, base::string16::npos, "com",
209 GURL(), OmniboxEventProto::INVALID_SPEC, true,
210 false, true, true, TestSchemeClassifier());
211 EXPECT_EQ(input_cases[i].type, input.type());
212 if (input_cases[i].type == metrics::OmniboxInputType::URL)
213 EXPECT_EQ(input_cases[i].spec, input.canonicalized_url().spec());
217 // This tests for a regression where certain input in the omnibox caused us to
218 // crash. As long as the test completes without crashing, we're fine.
219 TEST(AutocompleteInputTest, InputCrash) {
220 AutocompleteInput input(base::WideToUTF16(L"\uff65@s"), base::string16::npos,
221 std::string(), GURL(),
222 OmniboxEventProto::INVALID_SPEC, true, false,
223 true, true, TestSchemeClassifier());
226 TEST(AutocompleteInputTest, ParseForEmphasizeComponent) {
227 using url::Component;
228 Component kInvalidComponent(0, -1);
229 struct test_data {
230 const base::string16 input;
231 const Component scheme;
232 const Component host;
233 } input_cases[] = {
234 { base::string16(), kInvalidComponent, kInvalidComponent },
235 { ASCIIToUTF16("?"), kInvalidComponent, kInvalidComponent },
236 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent,
237 kInvalidComponent },
238 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent, Component(0, 3) },
239 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) },
240 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent },
241 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent, Component(0, 11) },
242 { base::WideToUTF16(L"\u6d4b\u8bd5"), kInvalidComponent, Component(0, 2) },
243 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4),
244 Component(19, 11) },
245 { ASCIIToUTF16("view-source:https://example.com/"),
246 Component(12, 5), Component(20, 11) },
247 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent,
248 Component(12, 11) },
249 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent },
250 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent,
251 Component(12, 7) },
252 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4),
253 Component(19, 4) },
254 { ASCIIToUTF16("view-source:view-source:http://example.com/"),
255 Component(12, 11), kInvalidComponent }
258 for (size_t i = 0; i < arraysize(input_cases); ++i) {
259 SCOPED_TRACE(input_cases[i].input);
260 Component scheme, host;
261 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input,
262 TestSchemeClassifier(),
263 &scheme,
264 &host);
265 AutocompleteInput input(input_cases[i].input, base::string16::npos,
266 std::string(), GURL(),
267 OmniboxEventProto::INVALID_SPEC, true,
268 false, true, true, TestSchemeClassifier());
269 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin);
270 EXPECT_EQ(input_cases[i].scheme.len, scheme.len);
271 EXPECT_EQ(input_cases[i].host.begin, host.begin);
272 EXPECT_EQ(input_cases[i].host.len, host.len);
276 TEST(AutocompleteInputTest, InputTypeWithCursorPosition) {
277 struct test_data {
278 const base::string16 input;
279 size_t cursor_position;
280 const base::string16 normalized_input;
281 size_t normalized_cursor_position;
282 } input_cases[] = {
283 { ASCIIToUTF16("foo bar"), base::string16::npos,
284 ASCIIToUTF16("foo bar"), base::string16::npos },
286 // regular case, no changes.
287 { ASCIIToUTF16("foo bar"), 3, ASCIIToUTF16("foo bar"), 3 },
289 // extra leading space.
290 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 1 },
291 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 0 },
292 { ASCIIToUTF16(" foo bar "), 2, ASCIIToUTF16("foo bar "), 0 },
294 // forced query.
295 { ASCIIToUTF16("?foo bar"), 2, ASCIIToUTF16("foo bar"), 1 },
296 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
297 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
298 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 },
301 for (size_t i = 0; i < arraysize(input_cases); ++i) {
302 SCOPED_TRACE(input_cases[i].input);
303 AutocompleteInput input(input_cases[i].input,
304 input_cases[i].cursor_position, std::string(),
305 GURL(), OmniboxEventProto::INVALID_SPEC, true,
306 false, true, true, TestSchemeClassifier());
307 EXPECT_EQ(input_cases[i].normalized_input, input.text());
308 EXPECT_EQ(input_cases[i].normalized_cursor_position,
309 input.cursor_position());