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/browser/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/browser/test_scheme_classifier.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/third_party/mozilla/url_parse.h"
17 using base::ASCIIToUTF16
;
18 using metrics::OmniboxEventProto
;
20 TEST(AutocompleteInputTest
, InputType
) {
22 const base::string16 input
;
23 const metrics::OmniboxInputType::Type type
;
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._"), metrics::OmniboxInputType::QUERY
},
34 { ASCIIToUTF16("foo.c"), metrics::OmniboxInputType::UNKNOWN
},
35 { ASCIIToUTF16("foo.com"), metrics::OmniboxInputType::URL
},
36 { ASCIIToUTF16("-foo.com"), metrics::OmniboxInputType::URL
},
37 { ASCIIToUTF16("foo-.com"), metrics::OmniboxInputType::URL
},
38 { ASCIIToUTF16("foo_.com"), metrics::OmniboxInputType::URL
},
39 { ASCIIToUTF16("foo.-com"), metrics::OmniboxInputType::QUERY
},
40 { ASCIIToUTF16("foo/"), metrics::OmniboxInputType::URL
},
41 { ASCIIToUTF16("foo/bar"), metrics::OmniboxInputType::UNKNOWN
},
42 { ASCIIToUTF16("foo/bar%00"), metrics::OmniboxInputType::QUERY
},
43 { ASCIIToUTF16("foo/bar/"), metrics::OmniboxInputType::URL
},
44 { ASCIIToUTF16("foo/bar baz\\"), metrics::OmniboxInputType::URL
},
45 { ASCIIToUTF16("foo.com/bar"), metrics::OmniboxInputType::URL
},
46 { ASCIIToUTF16("foo;bar"), metrics::OmniboxInputType::UNKNOWN
},
47 { ASCIIToUTF16("foo/bar baz"), metrics::OmniboxInputType::UNKNOWN
},
48 { ASCIIToUTF16("foo bar.com"), metrics::OmniboxInputType::QUERY
},
49 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY
},
50 { ASCIIToUTF16("foo+bar"), metrics::OmniboxInputType::QUERY
},
51 { ASCIIToUTF16("foo+bar.com"), metrics::OmniboxInputType::UNKNOWN
},
52 { ASCIIToUTF16("\"foo:bar\""), metrics::OmniboxInputType::QUERY
},
53 { ASCIIToUTF16("link:foo.com"), metrics::OmniboxInputType::UNKNOWN
},
54 { ASCIIToUTF16("foo:81"), metrics::OmniboxInputType::URL
},
55 { ASCIIToUTF16("localhost:8080"), metrics::OmniboxInputType::URL
},
56 { ASCIIToUTF16("www.foo.com:81"), metrics::OmniboxInputType::URL
},
57 { ASCIIToUTF16("foo.com:123456"), metrics::OmniboxInputType::QUERY
},
58 { ASCIIToUTF16("foo.com:abc"), metrics::OmniboxInputType::QUERY
},
59 { ASCIIToUTF16("1.2.3.4:abc"), metrics::OmniboxInputType::QUERY
},
60 { ASCIIToUTF16("user@foo"), metrics::OmniboxInputType::UNKNOWN
},
61 { ASCIIToUTF16("user@foo.com"), metrics::OmniboxInputType::UNKNOWN
},
62 { ASCIIToUTF16("user@foo/z"), metrics::OmniboxInputType::URL
},
63 { ASCIIToUTF16("user@foo/z z"), metrics::OmniboxInputType::URL
},
64 { ASCIIToUTF16("user@foo.com/z"), metrics::OmniboxInputType::URL
},
65 { ASCIIToUTF16("user:pass@"), metrics::OmniboxInputType::UNKNOWN
},
66 { ASCIIToUTF16("user:pass@!foo.com"), metrics::OmniboxInputType::UNKNOWN
},
67 { ASCIIToUTF16("user:pass@foo"), metrics::OmniboxInputType::URL
},
68 { ASCIIToUTF16("user:pass@foo.c"), metrics::OmniboxInputType::URL
},
69 { ASCIIToUTF16("user:pass@foo.com"), metrics::OmniboxInputType::URL
},
70 { ASCIIToUTF16("user:pass@foo.com:81"), metrics::OmniboxInputType::URL
},
71 { ASCIIToUTF16("user:pass@foo:81"), metrics::OmniboxInputType::URL
},
72 { ASCIIToUTF16(".1"), metrics::OmniboxInputType::QUERY
},
73 { ASCIIToUTF16(".1/3"), metrics::OmniboxInputType::QUERY
},
74 { ASCIIToUTF16("1.2"), metrics::OmniboxInputType::QUERY
},
75 { ASCIIToUTF16(".1.2"), metrics::OmniboxInputType::UNKNOWN
},
76 { ASCIIToUTF16("1.2/"), metrics::OmniboxInputType::URL
},
77 { ASCIIToUTF16("1.2/45"), metrics::OmniboxInputType::QUERY
},
78 { ASCIIToUTF16("6008/32768"), metrics::OmniboxInputType::QUERY
},
79 { ASCIIToUTF16("12345678/"), metrics::OmniboxInputType::QUERY
},
80 { ASCIIToUTF16("123456789/"), metrics::OmniboxInputType::URL
},
81 { ASCIIToUTF16("1.2:45"), metrics::OmniboxInputType::QUERY
},
82 { ASCIIToUTF16("user@1.2:45"), metrics::OmniboxInputType::QUERY
},
83 { ASCIIToUTF16("user@foo:45"), metrics::OmniboxInputType::URL
},
84 { ASCIIToUTF16("user:pass@1.2:45"), metrics::OmniboxInputType::URL
},
85 { ASCIIToUTF16("host?query"), metrics::OmniboxInputType::UNKNOWN
},
86 { ASCIIToUTF16("host#ref"), metrics::OmniboxInputType::QUERY
},
87 { ASCIIToUTF16("host#"), metrics::OmniboxInputType::QUERY
},
88 { ASCIIToUTF16("host#ref"), metrics::OmniboxInputType::QUERY
},
89 { ASCIIToUTF16("host# ref"), metrics::OmniboxInputType::QUERY
},
90 { ASCIIToUTF16("host/#ref"), metrics::OmniboxInputType::URL
},
91 { ASCIIToUTF16("host/?#ref"), metrics::OmniboxInputType::URL
},
92 { ASCIIToUTF16("host/?#"), metrics::OmniboxInputType::URL
},
93 { ASCIIToUTF16("host.com#ref"), metrics::OmniboxInputType::URL
},
94 { ASCIIToUTF16("http://host#ref"), metrics::OmniboxInputType::URL
},
95 { ASCIIToUTF16("host/path?query"), metrics::OmniboxInputType::URL
},
96 { ASCIIToUTF16("host/path#ref"), metrics::OmniboxInputType::URL
},
97 { ASCIIToUTF16("en.wikipedia.org/wiki/Jim Beam"),
98 metrics::OmniboxInputType::URL
},
99 // In Chrome itself, mailto: will get handled by ShellExecute, but in
100 // unittest mode, we don't have the data loaded in the external protocol
101 // handler to know this.
102 // { ASCIIToUTF16("mailto:abuse@foo.com"), metrics::OmniboxInputType::URL },
103 { ASCIIToUTF16("view-source:http://www.foo.com/"),
104 metrics::OmniboxInputType::URL
},
105 { ASCIIToUTF16("javascript:alert(\"Hi there\");"),
106 metrics::OmniboxInputType::URL
},
108 { ASCIIToUTF16("C:\\Program Files"), metrics::OmniboxInputType::URL
},
109 { ASCIIToUTF16("\\\\Server\\Folder\\File"),
110 metrics::OmniboxInputType::URL
},
111 #endif // defined(OS_WIN)
112 { ASCIIToUTF16("http:foo"), metrics::OmniboxInputType::URL
},
113 { ASCIIToUTF16("http://foo"), metrics::OmniboxInputType::URL
},
114 { ASCIIToUTF16("http://foo._"), metrics::OmniboxInputType::UNKNOWN
},
115 { ASCIIToUTF16("http://foo.c"), metrics::OmniboxInputType::URL
},
116 { ASCIIToUTF16("http://foo.com"), metrics::OmniboxInputType::URL
},
117 { ASCIIToUTF16("http://foo_bar.com"), metrics::OmniboxInputType::URL
},
118 { ASCIIToUTF16("http://foo/bar%00"), metrics::OmniboxInputType::QUERY
},
119 { ASCIIToUTF16("http://foo/bar baz"), metrics::OmniboxInputType::URL
},
120 { ASCIIToUTF16("http://-foo.com"), metrics::OmniboxInputType::URL
},
121 { ASCIIToUTF16("http://foo-.com"), metrics::OmniboxInputType::URL
},
122 { ASCIIToUTF16("http://foo_.com"), metrics::OmniboxInputType::URL
},
123 { ASCIIToUTF16("http://foo.-com"), metrics::OmniboxInputType::UNKNOWN
},
124 { ASCIIToUTF16("http://_foo_.com"), metrics::OmniboxInputType::URL
},
125 { ASCIIToUTF16("http://foo.com:abc"), metrics::OmniboxInputType::QUERY
},
126 { ASCIIToUTF16("http://foo.com:123456"), metrics::OmniboxInputType::QUERY
},
127 { ASCIIToUTF16("http://1.2.3.4:abc"), metrics::OmniboxInputType::QUERY
},
128 { ASCIIToUTF16("http:user@foo.com"), metrics::OmniboxInputType::URL
},
129 { ASCIIToUTF16("http://user@foo.com"), metrics::OmniboxInputType::URL
},
130 { ASCIIToUTF16("http://user:pass@foo"), metrics::OmniboxInputType::URL
},
131 { ASCIIToUTF16("http:user:pass@foo.com"), metrics::OmniboxInputType::URL
},
132 { ASCIIToUTF16("http://user:pass@foo.com"),
133 metrics::OmniboxInputType::URL
},
134 { ASCIIToUTF16("http://1.2"), metrics::OmniboxInputType::URL
},
135 { ASCIIToUTF16("http:user@1.2"), metrics::OmniboxInputType::URL
},
136 { ASCIIToUTF16("http://1.2/45"), metrics::OmniboxInputType::URL
},
137 { ASCIIToUTF16("http:ps/2 games"), metrics::OmniboxInputType::URL
},
138 { ASCIIToUTF16("https://foo.com"), metrics::OmniboxInputType::URL
},
139 { ASCIIToUTF16("127.0.0.1"), metrics::OmniboxInputType::URL
},
140 { ASCIIToUTF16("127.0.1"), metrics::OmniboxInputType::QUERY
},
141 { ASCIIToUTF16("127.0.1/"), metrics::OmniboxInputType::URL
},
142 { ASCIIToUTF16("0.0.0"), metrics::OmniboxInputType::QUERY
},
143 { ASCIIToUTF16("0.0.0.0"), metrics::OmniboxInputType::URL
},
144 { ASCIIToUTF16("0.0.0.1"), metrics::OmniboxInputType::QUERY
},
145 { ASCIIToUTF16("http://0.0.0.1/"), metrics::OmniboxInputType::QUERY
},
146 { ASCIIToUTF16("browser.tabs.closeButtons"),
147 metrics::OmniboxInputType::UNKNOWN
},
148 { base::WideToUTF16(L
"\u6d4b\u8bd5"), metrics::OmniboxInputType::UNKNOWN
},
149 { ASCIIToUTF16("[2001:]"), metrics::OmniboxInputType::QUERY
},
150 { ASCIIToUTF16("[2001:dB8::1]"), metrics::OmniboxInputType::URL
},
151 { ASCIIToUTF16("192.168.0.256"), metrics::OmniboxInputType::QUERY
},
152 { ASCIIToUTF16("[foo.com]"), metrics::OmniboxInputType::QUERY
},
153 { ASCIIToUTF16("filesystem:http://a.com/t/bar"),
154 metrics::OmniboxInputType::URL
},
155 { ASCIIToUTF16("filesystem:http://a.com/"),
156 metrics::OmniboxInputType::QUERY
},
157 { ASCIIToUTF16("filesystem:file://"), metrics::OmniboxInputType::QUERY
},
158 { ASCIIToUTF16("filesystem:http"), metrics::OmniboxInputType::QUERY
},
159 { ASCIIToUTF16("filesystem:"), metrics::OmniboxInputType::QUERY
},
160 { ASCIIToUTF16("chrome-search://"), metrics::OmniboxInputType::QUERY
},
161 { ASCIIToUTF16("chrome-devtools:"), metrics::OmniboxInputType::QUERY
},
162 { ASCIIToUTF16("about://f;"), metrics::OmniboxInputType::QUERY
},
163 { ASCIIToUTF16("://w"), metrics::OmniboxInputType::QUERY
},
164 { ASCIIToUTF16(":w"), metrics::OmniboxInputType::QUERY
},
165 { base::WideToUTF16(L
".\u062A"), metrics::OmniboxInputType::UNKNOWN
},
168 for (size_t i
= 0; i
< arraysize(input_cases
); ++i
) {
169 SCOPED_TRACE(input_cases
[i
].input
);
170 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
171 std::string(), GURL(),
172 OmniboxEventProto::INVALID_SPEC
, true, false, true,
173 true, false, TestSchemeClassifier());
174 EXPECT_EQ(input_cases
[i
].type
, input
.type());
178 TEST(AutocompleteInputTest
, InputTypeWithDesiredTLD
) {
180 const base::string16 input
;
181 const metrics::OmniboxInputType::Type type
;
182 const std::string spec
; // Unused if not a URL.
184 { ASCIIToUTF16("401k"), metrics::OmniboxInputType::URL
,
185 std::string("http://www.401k.com/") },
186 { ASCIIToUTF16("56"), metrics::OmniboxInputType::URL
,
187 std::string("http://www.56.com/") },
188 { ASCIIToUTF16("1.2"), metrics::OmniboxInputType::URL
,
189 std::string("http://www.1.2.com/") },
190 { ASCIIToUTF16("1.2/3.4"), metrics::OmniboxInputType::URL
,
191 std::string("http://www.1.2.com/3.4") },
192 { ASCIIToUTF16("192.168.0.1"), metrics::OmniboxInputType::URL
,
193 std::string("http://www.192.168.0.1.com/") },
194 { ASCIIToUTF16("999999999999999"), metrics::OmniboxInputType::URL
,
195 std::string("http://www.999999999999999.com/") },
196 { ASCIIToUTF16("x@y"), metrics::OmniboxInputType::URL
,
197 std::string("http://x@www.y.com/") },
198 { ASCIIToUTF16("x@y.com"), metrics::OmniboxInputType::URL
,
199 std::string("http://x@y.com/") },
200 { ASCIIToUTF16("y/z z"), metrics::OmniboxInputType::URL
,
201 std::string("http://www.y.com/z%20z") },
202 { ASCIIToUTF16("abc.com"), metrics::OmniboxInputType::URL
,
203 std::string("http://abc.com/") },
204 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY
,
208 for (size_t i
= 0; i
< arraysize(input_cases
); ++i
) {
209 SCOPED_TRACE(input_cases
[i
].input
);
210 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
, "com",
211 GURL(), OmniboxEventProto::INVALID_SPEC
, true,
212 false, true, true, false, TestSchemeClassifier());
213 EXPECT_EQ(input_cases
[i
].type
, input
.type());
214 if (input_cases
[i
].type
== metrics::OmniboxInputType::URL
)
215 EXPECT_EQ(input_cases
[i
].spec
, input
.canonicalized_url().spec());
219 // This tests for a regression where certain input in the omnibox caused us to
220 // crash. As long as the test completes without crashing, we're fine.
221 TEST(AutocompleteInputTest
, InputCrash
) {
222 AutocompleteInput
input(base::WideToUTF16(L
"\uff65@s"), base::string16::npos
,
223 std::string(), GURL(),
224 OmniboxEventProto::INVALID_SPEC
, true, false, true,
225 true, false, TestSchemeClassifier());
228 TEST(AutocompleteInputTest
, ParseForEmphasizeComponent
) {
229 using url::Component
;
230 Component
kInvalidComponent(0, -1);
232 const base::string16 input
;
233 const Component scheme
;
234 const Component host
;
236 { base::string16(), kInvalidComponent
, kInvalidComponent
},
237 { ASCIIToUTF16("?"), kInvalidComponent
, kInvalidComponent
},
238 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent
,
240 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent
, Component(0, 3) },
241 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) },
242 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent
},
243 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent
, Component(0, 11) },
244 { base::WideToUTF16(L
"\u6d4b\u8bd5"), kInvalidComponent
, Component(0, 2) },
245 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4),
247 { ASCIIToUTF16("view-source:https://example.com/"),
248 Component(12, 5), Component(20, 11) },
249 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent
,
251 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent
},
252 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent
,
254 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4),
256 { ASCIIToUTF16("view-source:view-source:http://example.com/"),
257 Component(12, 11), kInvalidComponent
}
260 for (size_t i
= 0; i
< arraysize(input_cases
); ++i
) {
261 SCOPED_TRACE(input_cases
[i
].input
);
262 Component scheme
, host
;
263 AutocompleteInput::ParseForEmphasizeComponents(input_cases
[i
].input
,
264 TestSchemeClassifier(),
267 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
268 std::string(), GURL(),
269 OmniboxEventProto::INVALID_SPEC
, true, false, true,
270 true, false, TestSchemeClassifier());
271 EXPECT_EQ(input_cases
[i
].scheme
.begin
, scheme
.begin
);
272 EXPECT_EQ(input_cases
[i
].scheme
.len
, scheme
.len
);
273 EXPECT_EQ(input_cases
[i
].host
.begin
, host
.begin
);
274 EXPECT_EQ(input_cases
[i
].host
.len
, host
.len
);
278 TEST(AutocompleteInputTest
, InputTypeWithCursorPosition
) {
280 const base::string16 input
;
281 size_t cursor_position
;
282 const base::string16 normalized_input
;
283 size_t normalized_cursor_position
;
285 { ASCIIToUTF16("foo bar"), base::string16::npos
,
286 ASCIIToUTF16("foo bar"), base::string16::npos
},
288 // regular case, no changes.
289 { ASCIIToUTF16("foo bar"), 3, ASCIIToUTF16("foo bar"), 3 },
291 // extra leading space.
292 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 1 },
293 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 0 },
294 { ASCIIToUTF16(" foo bar "), 2, ASCIIToUTF16("foo bar "), 0 },
297 { ASCIIToUTF16("?foo bar"), 2, ASCIIToUTF16("foo bar"), 1 },
298 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
299 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
300 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 },
303 for (size_t i
= 0; i
< arraysize(input_cases
); ++i
) {
304 SCOPED_TRACE(input_cases
[i
].input
);
305 AutocompleteInput
input(input_cases
[i
].input
,
306 input_cases
[i
].cursor_position
, std::string(),
307 GURL(), OmniboxEventProto::INVALID_SPEC
, true,
308 false, true, true, false, TestSchemeClassifier());
309 EXPECT_EQ(input_cases
[i
].normalized_input
, input
.text());
310 EXPECT_EQ(input_cases
[i
].normalized_cursor_position
,
311 input
.cursor_position());