Fix experimental app list search box disappearing on profile switch.
[chromium-blink-merge.git] / components / omnibox / autocomplete_input_unittest.cc
blobd95ce84aee35355c44b97080e86c099c414cea38
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("1.2:45"), metrics::OmniboxInputType::QUERY },
79 { ASCIIToUTF16("user@1.2:45"), metrics::OmniboxInputType::QUERY },
80 { ASCIIToUTF16("user@foo:45"), metrics::OmniboxInputType::URL },
81 { ASCIIToUTF16("user:pass@1.2:45"), metrics::OmniboxInputType::URL },
82 { ASCIIToUTF16("host?query"), metrics::OmniboxInputType::UNKNOWN },
83 { ASCIIToUTF16("host#ref"), metrics::OmniboxInputType::QUERY },
84 { ASCIIToUTF16("host#"), metrics::OmniboxInputType::QUERY },
85 { ASCIIToUTF16("host#ref"), metrics::OmniboxInputType::QUERY },
86 { ASCIIToUTF16("host# ref"), metrics::OmniboxInputType::QUERY },
87 { ASCIIToUTF16("host/#ref"), metrics::OmniboxInputType::URL },
88 { ASCIIToUTF16("host/?#ref"), metrics::OmniboxInputType::URL },
89 { ASCIIToUTF16("host/?#"), metrics::OmniboxInputType::URL },
90 { ASCIIToUTF16("host.com#ref"), metrics::OmniboxInputType::URL },
91 { ASCIIToUTF16("http://host#ref"), metrics::OmniboxInputType::URL },
92 { ASCIIToUTF16("host/path?query"), metrics::OmniboxInputType::URL },
93 { ASCIIToUTF16("host/path#ref"), metrics::OmniboxInputType::URL },
94 { ASCIIToUTF16("en.wikipedia.org/wiki/Jim Beam"),
95 metrics::OmniboxInputType::URL },
96 // In Chrome itself, mailto: will get handled by ShellExecute, but in
97 // unittest mode, we don't have the data loaded in the external protocol
98 // handler to know this.
99 // { ASCIIToUTF16("mailto:abuse@foo.com"), metrics::OmniboxInputType::URL },
100 { ASCIIToUTF16("view-source:http://www.foo.com/"),
101 metrics::OmniboxInputType::URL },
102 { ASCIIToUTF16("javascript:alert(\"Hi there\");"),
103 metrics::OmniboxInputType::URL },
104 #if defined(OS_WIN)
105 { ASCIIToUTF16("C:\\Program Files"), metrics::OmniboxInputType::URL },
106 { ASCIIToUTF16("\\\\Server\\Folder\\File"),
107 metrics::OmniboxInputType::URL },
108 #endif // defined(OS_WIN)
109 { ASCIIToUTF16("http:foo"), metrics::OmniboxInputType::URL },
110 { ASCIIToUTF16("http://foo"), metrics::OmniboxInputType::URL },
111 { ASCIIToUTF16("http://foo.c"), metrics::OmniboxInputType::URL },
112 { ASCIIToUTF16("http://foo.com"), metrics::OmniboxInputType::URL },
113 { ASCIIToUTF16("http://foo_bar.com"), metrics::OmniboxInputType::URL },
114 { ASCIIToUTF16("http://foo/bar%00"), metrics::OmniboxInputType::QUERY },
115 { ASCIIToUTF16("http://foo/bar baz"), metrics::OmniboxInputType::URL },
116 { ASCIIToUTF16("http://-foo.com"), metrics::OmniboxInputType::URL },
117 { ASCIIToUTF16("http://foo-.com"), metrics::OmniboxInputType::URL },
118 { ASCIIToUTF16("http://foo_.com"), metrics::OmniboxInputType::UNKNOWN },
119 { ASCIIToUTF16("http://foo.-com"), metrics::OmniboxInputType::UNKNOWN },
120 { ASCIIToUTF16("http://_foo_.com"), metrics::OmniboxInputType::UNKNOWN },
121 { ASCIIToUTF16("http://foo.com:abc"), metrics::OmniboxInputType::QUERY },
122 { ASCIIToUTF16("http://foo.com:123456"), metrics::OmniboxInputType::QUERY },
123 { ASCIIToUTF16("http://1.2.3.4:abc"), metrics::OmniboxInputType::QUERY },
124 { ASCIIToUTF16("http:user@foo.com"), metrics::OmniboxInputType::URL },
125 { ASCIIToUTF16("http://user@foo.com"), metrics::OmniboxInputType::URL },
126 { ASCIIToUTF16("http://user:pass@foo"), metrics::OmniboxInputType::URL },
127 { ASCIIToUTF16("http:user:pass@foo.com"), metrics::OmniboxInputType::URL },
128 { ASCIIToUTF16("http://user:pass@foo.com"),
129 metrics::OmniboxInputType::URL },
130 { ASCIIToUTF16("http://1.2"), metrics::OmniboxInputType::URL },
131 { ASCIIToUTF16("http:user@1.2"), metrics::OmniboxInputType::URL },
132 { ASCIIToUTF16("http://1.2/45"), metrics::OmniboxInputType::URL },
133 { ASCIIToUTF16("http:ps/2 games"), metrics::OmniboxInputType::URL },
134 { ASCIIToUTF16("https://foo.com"), metrics::OmniboxInputType::URL },
135 { ASCIIToUTF16("127.0.0.1"), metrics::OmniboxInputType::URL },
136 { ASCIIToUTF16("127.0.1"), metrics::OmniboxInputType::QUERY },
137 { ASCIIToUTF16("127.0.1/"), metrics::OmniboxInputType::URL },
138 { ASCIIToUTF16("browser.tabs.closeButtons"),
139 metrics::OmniboxInputType::UNKNOWN },
140 { base::WideToUTF16(L"\u6d4b\u8bd5"), metrics::OmniboxInputType::UNKNOWN },
141 { ASCIIToUTF16("[2001:]"), metrics::OmniboxInputType::QUERY },
142 { ASCIIToUTF16("[2001:dB8::1]"), metrics::OmniboxInputType::URL },
143 { ASCIIToUTF16("192.168.0.256"), metrics::OmniboxInputType::QUERY },
144 { ASCIIToUTF16("[foo.com]"), metrics::OmniboxInputType::QUERY },
145 { ASCIIToUTF16("filesystem:http://a.com/t/bar"),
146 metrics::OmniboxInputType::URL },
147 { ASCIIToUTF16("filesystem:http://a.com/"),
148 metrics::OmniboxInputType::QUERY },
149 { ASCIIToUTF16("filesystem:file://"), metrics::OmniboxInputType::QUERY },
150 { ASCIIToUTF16("filesystem:http"), metrics::OmniboxInputType::QUERY },
151 { ASCIIToUTF16("filesystem:"), metrics::OmniboxInputType::QUERY },
152 { ASCIIToUTF16("chrome-search://"), metrics::OmniboxInputType::QUERY },
153 { ASCIIToUTF16("chrome-devtools:"), metrics::OmniboxInputType::QUERY },
154 { ASCIIToUTF16("about://f;"), metrics::OmniboxInputType::QUERY },
155 { ASCIIToUTF16("://w"), metrics::OmniboxInputType::QUERY },
156 { ASCIIToUTF16(":w"), metrics::OmniboxInputType::QUERY },
157 { base::WideToUTF16(L".\u062A"), metrics::OmniboxInputType::UNKNOWN },
160 for (size_t i = 0; i < arraysize(input_cases); ++i) {
161 SCOPED_TRACE(input_cases[i].input);
162 AutocompleteInput input(input_cases[i].input, base::string16::npos,
163 std::string(), GURL(),
164 OmniboxEventProto::INVALID_SPEC, true, false, true,
165 true, TestSchemeClassifier());
166 EXPECT_EQ(input_cases[i].type, input.type());
170 TEST(AutocompleteInputTest, InputTypeWithDesiredTLD) {
171 struct test_data {
172 const base::string16 input;
173 const metrics::OmniboxInputType::Type type;
174 const std::string spec; // Unused if not a URL.
175 } input_cases[] = {
176 { ASCIIToUTF16("401k"), metrics::OmniboxInputType::URL,
177 std::string("http://www.401k.com/") },
178 { ASCIIToUTF16("56"), metrics::OmniboxInputType::URL,
179 std::string("http://www.56.com/") },
180 { ASCIIToUTF16("1.2"), metrics::OmniboxInputType::URL,
181 std::string("http://www.1.2.com/") },
182 { ASCIIToUTF16("1.2/3.4"), metrics::OmniboxInputType::URL,
183 std::string("http://www.1.2.com/3.4") },
184 { ASCIIToUTF16("192.168.0.1"), metrics::OmniboxInputType::URL,
185 std::string("http://www.192.168.0.1.com/") },
186 { ASCIIToUTF16("999999999999999"), metrics::OmniboxInputType::URL,
187 std::string("http://www.999999999999999.com/") },
188 { ASCIIToUTF16("x@y"), metrics::OmniboxInputType::URL,
189 std::string("http://x@www.y.com/") },
190 { ASCIIToUTF16("x@y.com"), metrics::OmniboxInputType::URL,
191 std::string("http://x@y.com/") },
192 { ASCIIToUTF16("y/z z"), metrics::OmniboxInputType::URL,
193 std::string("http://www.y.com/z%20z") },
194 { ASCIIToUTF16("abc.com"), metrics::OmniboxInputType::URL,
195 std::string("http://abc.com/") },
196 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY,
197 std::string() },
200 for (size_t i = 0; i < arraysize(input_cases); ++i) {
201 SCOPED_TRACE(input_cases[i].input);
202 AutocompleteInput input(input_cases[i].input, base::string16::npos, "com",
203 GURL(), OmniboxEventProto::INVALID_SPEC, true,
204 false, true, true, TestSchemeClassifier());
205 EXPECT_EQ(input_cases[i].type, input.type());
206 if (input_cases[i].type == metrics::OmniboxInputType::URL)
207 EXPECT_EQ(input_cases[i].spec, input.canonicalized_url().spec());
211 // This tests for a regression where certain input in the omnibox caused us to
212 // crash. As long as the test completes without crashing, we're fine.
213 TEST(AutocompleteInputTest, InputCrash) {
214 AutocompleteInput input(base::WideToUTF16(L"\uff65@s"), base::string16::npos,
215 std::string(), GURL(),
216 OmniboxEventProto::INVALID_SPEC, true, false,
217 true, true, TestSchemeClassifier());
220 TEST(AutocompleteInputTest, ParseForEmphasizeComponent) {
221 using url::Component;
222 Component kInvalidComponent(0, -1);
223 struct test_data {
224 const base::string16 input;
225 const Component scheme;
226 const Component host;
227 } input_cases[] = {
228 { base::string16(), kInvalidComponent, kInvalidComponent },
229 { ASCIIToUTF16("?"), kInvalidComponent, kInvalidComponent },
230 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent,
231 kInvalidComponent },
232 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent, Component(0, 3) },
233 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) },
234 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent },
235 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent, Component(0, 11) },
236 { base::WideToUTF16(L"\u6d4b\u8bd5"), kInvalidComponent, Component(0, 2) },
237 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4),
238 Component(19, 11) },
239 { ASCIIToUTF16("view-source:https://example.com/"),
240 Component(12, 5), Component(20, 11) },
241 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent,
242 Component(12, 11) },
243 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent },
244 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent,
245 Component(12, 7) },
246 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4),
247 Component(19, 4) },
248 { ASCIIToUTF16("view-source:view-source:http://example.com/"),
249 Component(12, 11), kInvalidComponent }
252 for (size_t i = 0; i < arraysize(input_cases); ++i) {
253 SCOPED_TRACE(input_cases[i].input);
254 Component scheme, host;
255 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input,
256 TestSchemeClassifier(),
257 &scheme,
258 &host);
259 AutocompleteInput input(input_cases[i].input, base::string16::npos,
260 std::string(), GURL(),
261 OmniboxEventProto::INVALID_SPEC, true,
262 false, true, true, TestSchemeClassifier());
263 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin);
264 EXPECT_EQ(input_cases[i].scheme.len, scheme.len);
265 EXPECT_EQ(input_cases[i].host.begin, host.begin);
266 EXPECT_EQ(input_cases[i].host.len, host.len);
270 TEST(AutocompleteInputTest, InputTypeWithCursorPosition) {
271 struct test_data {
272 const base::string16 input;
273 size_t cursor_position;
274 const base::string16 normalized_input;
275 size_t normalized_cursor_position;
276 } input_cases[] = {
277 { ASCIIToUTF16("foo bar"), base::string16::npos,
278 ASCIIToUTF16("foo bar"), base::string16::npos },
280 // regular case, no changes.
281 { ASCIIToUTF16("foo bar"), 3, ASCIIToUTF16("foo bar"), 3 },
283 // extra leading space.
284 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 1 },
285 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 0 },
286 { ASCIIToUTF16(" foo bar "), 2, ASCIIToUTF16("foo bar "), 0 },
288 // forced query.
289 { ASCIIToUTF16("?foo bar"), 2, ASCIIToUTF16("foo bar"), 1 },
290 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
291 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
292 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 },
295 for (size_t i = 0; i < arraysize(input_cases); ++i) {
296 SCOPED_TRACE(input_cases[i].input);
297 AutocompleteInput input(input_cases[i].input,
298 input_cases[i].cursor_position, std::string(),
299 GURL(), OmniboxEventProto::INVALID_SPEC, true,
300 false, true, true, TestSchemeClassifier());
301 EXPECT_EQ(input_cases[i].normalized_input, input.text());
302 EXPECT_EQ(input_cases[i].normalized_cursor_position,
303 input.cursor_position());