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"
16 #include "url/url_util.h"
18 using base::ASCIIToUTF16
;
19 using metrics::OmniboxEventProto
;
21 TEST(AutocompleteInputTest
, InputType
) {
22 // TODO(mukai): Fix this scheme setup.
23 url::AddStandardScheme("chrome");
24 url::AddStandardScheme("chrome-devtools");
25 url::AddStandardScheme("chrome-search");
28 const base::string16 input
;
29 const metrics::OmniboxInputType::Type type
;
31 { base::string16(), metrics::OmniboxInputType::INVALID
},
32 { ASCIIToUTF16("?"), metrics::OmniboxInputType::FORCED_QUERY
},
33 { ASCIIToUTF16("?foo"), metrics::OmniboxInputType::FORCED_QUERY
},
34 { ASCIIToUTF16("?foo bar"), metrics::OmniboxInputType::FORCED_QUERY
},
35 { ASCIIToUTF16("?http://foo.com/bar"),
36 metrics::OmniboxInputType::FORCED_QUERY
},
37 { ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN
},
38 { ASCIIToUTF16("localhost"), metrics::OmniboxInputType::URL
},
39 { ASCIIToUTF16("foo.c"), metrics::OmniboxInputType::UNKNOWN
},
40 { ASCIIToUTF16("foo.com"), metrics::OmniboxInputType::URL
},
41 { ASCIIToUTF16("-foo.com"), metrics::OmniboxInputType::URL
},
42 { ASCIIToUTF16("foo-.com"), metrics::OmniboxInputType::URL
},
43 { ASCIIToUTF16("foo_.com"), metrics::OmniboxInputType::UNKNOWN
},
44 { ASCIIToUTF16("foo.-com"), metrics::OmniboxInputType::QUERY
},
45 { ASCIIToUTF16("foo/"), metrics::OmniboxInputType::URL
},
46 { ASCIIToUTF16("foo/bar"), metrics::OmniboxInputType::UNKNOWN
},
47 { ASCIIToUTF16("foo/bar%00"), metrics::OmniboxInputType::QUERY
},
48 { ASCIIToUTF16("foo/bar/"), metrics::OmniboxInputType::URL
},
49 { ASCIIToUTF16("foo/bar baz\\"), metrics::OmniboxInputType::URL
},
50 { ASCIIToUTF16("foo.com/bar"), metrics::OmniboxInputType::URL
},
51 { ASCIIToUTF16("foo;bar"), metrics::OmniboxInputType::UNKNOWN
},
52 { ASCIIToUTF16("foo/bar baz"), metrics::OmniboxInputType::UNKNOWN
},
53 { ASCIIToUTF16("foo bar.com"), metrics::OmniboxInputType::QUERY
},
54 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY
},
55 { ASCIIToUTF16("foo+bar"), metrics::OmniboxInputType::QUERY
},
56 { ASCIIToUTF16("foo+bar.com"), metrics::OmniboxInputType::UNKNOWN
},
57 { ASCIIToUTF16("\"foo:bar\""), metrics::OmniboxInputType::QUERY
},
58 { ASCIIToUTF16("link:foo.com"), metrics::OmniboxInputType::UNKNOWN
},
59 { ASCIIToUTF16("foo:81"), metrics::OmniboxInputType::URL
},
60 { ASCIIToUTF16("localhost:8080"), metrics::OmniboxInputType::URL
},
61 { ASCIIToUTF16("www.foo.com:81"), metrics::OmniboxInputType::URL
},
62 { ASCIIToUTF16("foo.com:123456"), metrics::OmniboxInputType::QUERY
},
63 { ASCIIToUTF16("foo.com:abc"), metrics::OmniboxInputType::QUERY
},
64 { ASCIIToUTF16("1.2.3.4:abc"), metrics::OmniboxInputType::QUERY
},
65 { ASCIIToUTF16("user@foo.com"), metrics::OmniboxInputType::UNKNOWN
},
66 { ASCIIToUTF16("user@foo/z"), metrics::OmniboxInputType::URL
},
67 { ASCIIToUTF16("user@foo/z z"), metrics::OmniboxInputType::URL
},
68 { ASCIIToUTF16("user@foo.com/z"), metrics::OmniboxInputType::URL
},
69 { ASCIIToUTF16("user:pass@"), metrics::OmniboxInputType::UNKNOWN
},
70 { ASCIIToUTF16("user:pass@!foo.com"), metrics::OmniboxInputType::UNKNOWN
},
71 { ASCIIToUTF16("user:pass@foo"), metrics::OmniboxInputType::URL
},
72 { ASCIIToUTF16("user:pass@foo.c"), metrics::OmniboxInputType::URL
},
73 { ASCIIToUTF16("user:pass@foo.com"), metrics::OmniboxInputType::URL
},
74 { ASCIIToUTF16("user:pass@foo.com:81"), metrics::OmniboxInputType::URL
},
75 { ASCIIToUTF16("user:pass@foo:81"), metrics::OmniboxInputType::URL
},
76 { ASCIIToUTF16("1.2"), metrics::OmniboxInputType::UNKNOWN
},
77 { ASCIIToUTF16("1.2/45"), metrics::OmniboxInputType::UNKNOWN
},
78 { ASCIIToUTF16("1.2:45"), metrics::OmniboxInputType::UNKNOWN
},
79 { ASCIIToUTF16("user@1.2:45"), metrics::OmniboxInputType::URL
},
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::UNKNOWN
},
84 { ASCIIToUTF16("host/path?query"), metrics::OmniboxInputType::URL
},
85 { ASCIIToUTF16("host/path#ref"), metrics::OmniboxInputType::URL
},
86 { ASCIIToUTF16("en.wikipedia.org/wiki/Jim Beam"),
87 metrics::OmniboxInputType::URL
},
88 // In Chrome itself, mailto: will get handled by ShellExecute, but in
89 // unittest mode, we don't have the data loaded in the external protocol
90 // handler to know this.
91 // { ASCIIToUTF16("mailto:abuse@foo.com"), metrics::OmniboxInputType::URL },
92 { ASCIIToUTF16("view-source:http://www.foo.com/"),
93 metrics::OmniboxInputType::URL
},
94 { ASCIIToUTF16("javascript:alert(\"Hi there\");"),
95 metrics::OmniboxInputType::URL
},
97 { ASCIIToUTF16("C:\\Program Files"), metrics::OmniboxInputType::URL
},
98 { ASCIIToUTF16("\\\\Server\\Folder\\File"),
99 metrics::OmniboxInputType::URL
},
100 #endif // defined(OS_WIN)
101 { ASCIIToUTF16("http:foo"), metrics::OmniboxInputType::URL
},
102 { ASCIIToUTF16("http://foo"), metrics::OmniboxInputType::URL
},
103 { ASCIIToUTF16("http://foo.c"), metrics::OmniboxInputType::URL
},
104 { ASCIIToUTF16("http://foo.com"), metrics::OmniboxInputType::URL
},
105 { ASCIIToUTF16("http://foo_bar.com"), metrics::OmniboxInputType::URL
},
106 { ASCIIToUTF16("http://foo/bar%00"), metrics::OmniboxInputType::QUERY
},
107 { ASCIIToUTF16("http://foo/bar baz"), metrics::OmniboxInputType::URL
},
108 { ASCIIToUTF16("http://-foo.com"), metrics::OmniboxInputType::URL
},
109 { ASCIIToUTF16("http://foo-.com"), metrics::OmniboxInputType::URL
},
110 { ASCIIToUTF16("http://foo_.com"), metrics::OmniboxInputType::UNKNOWN
},
111 { ASCIIToUTF16("http://foo.-com"), metrics::OmniboxInputType::UNKNOWN
},
112 { ASCIIToUTF16("http://_foo_.com"), metrics::OmniboxInputType::UNKNOWN
},
113 { ASCIIToUTF16("http://foo.com:abc"), metrics::OmniboxInputType::QUERY
},
114 { ASCIIToUTF16("http://foo.com:123456"), metrics::OmniboxInputType::QUERY
},
115 { ASCIIToUTF16("http://1.2.3.4:abc"), metrics::OmniboxInputType::QUERY
},
116 { ASCIIToUTF16("http:user@foo.com"), metrics::OmniboxInputType::URL
},
117 { ASCIIToUTF16("http://user@foo.com"), metrics::OmniboxInputType::URL
},
118 { ASCIIToUTF16("http:user:pass@foo.com"), metrics::OmniboxInputType::URL
},
119 { ASCIIToUTF16("http://user:pass@foo.com"),
120 metrics::OmniboxInputType::URL
},
121 { ASCIIToUTF16("http://1.2"), metrics::OmniboxInputType::URL
},
122 { ASCIIToUTF16("http://1.2/45"), metrics::OmniboxInputType::URL
},
123 { ASCIIToUTF16("http:ps/2 games"), metrics::OmniboxInputType::URL
},
124 { ASCIIToUTF16("https://foo.com"), metrics::OmniboxInputType::URL
},
125 { ASCIIToUTF16("127.0.0.1"), metrics::OmniboxInputType::URL
},
126 { ASCIIToUTF16("127.0.1"), metrics::OmniboxInputType::UNKNOWN
},
127 { ASCIIToUTF16("127.0.1/"), metrics::OmniboxInputType::URL
},
128 { ASCIIToUTF16("browser.tabs.closeButtons"),
129 metrics::OmniboxInputType::UNKNOWN
},
130 { base::WideToUTF16(L
"\u6d4b\u8bd5"), metrics::OmniboxInputType::UNKNOWN
},
131 { ASCIIToUTF16("[2001:]"), metrics::OmniboxInputType::QUERY
},
132 { ASCIIToUTF16("[2001:dB8::1]"), metrics::OmniboxInputType::URL
},
133 { ASCIIToUTF16("192.168.0.256"), metrics::OmniboxInputType::QUERY
},
134 { ASCIIToUTF16("[foo.com]"), metrics::OmniboxInputType::QUERY
},
135 { ASCIIToUTF16("filesystem:http://a.com/t/bar"),
136 metrics::OmniboxInputType::URL
},
137 { ASCIIToUTF16("filesystem:http://a.com/"),
138 metrics::OmniboxInputType::QUERY
},
139 { ASCIIToUTF16("filesystem:file://"), metrics::OmniboxInputType::QUERY
},
140 { ASCIIToUTF16("filesystem:http"), metrics::OmniboxInputType::QUERY
},
141 { ASCIIToUTF16("filesystem:"), metrics::OmniboxInputType::QUERY
},
142 { ASCIIToUTF16("chrome-search://"), metrics::OmniboxInputType::QUERY
},
143 { ASCIIToUTF16("chrome-devtools:"), metrics::OmniboxInputType::QUERY
},
144 { ASCIIToUTF16("about://f;"), metrics::OmniboxInputType::QUERY
},
145 { ASCIIToUTF16("://w"), metrics::OmniboxInputType::QUERY
},
146 { ASCIIToUTF16(":w"), metrics::OmniboxInputType::QUERY
},
149 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
150 SCOPED_TRACE(input_cases
[i
].input
);
151 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
152 base::string16(), GURL(),
153 OmniboxEventProto::INVALID_SPEC
, true, false, true,
154 true, TestSchemeClassifier());
155 EXPECT_EQ(input_cases
[i
].type
, input
.type());
159 TEST(AutocompleteInputTest
, InputTypeWithDesiredTLD
) {
161 const base::string16 input
;
162 const metrics::OmniboxInputType::Type type
;
163 const std::string spec
; // Unused if not a URL.
165 { ASCIIToUTF16("401k"), metrics::OmniboxInputType::URL
,
166 std::string("http://www.401k.com/") },
167 { ASCIIToUTF16("999999999999999"), metrics::OmniboxInputType::URL
,
168 std::string("http://www.999999999999999.com/") },
169 { ASCIIToUTF16("x@y"), metrics::OmniboxInputType::URL
,
170 std::string("http://x@www.y.com/") },
171 { ASCIIToUTF16("y/z z"), metrics::OmniboxInputType::URL
,
172 std::string("http://www.y.com/z%20z") },
173 { ASCIIToUTF16("abc.com"), metrics::OmniboxInputType::URL
,
174 std::string("http://abc.com/") },
175 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY
,
179 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
180 SCOPED_TRACE(input_cases
[i
].input
);
181 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
182 ASCIIToUTF16("com"), GURL(),
183 OmniboxEventProto::INVALID_SPEC
, true, false, true,
184 true, TestSchemeClassifier());
185 EXPECT_EQ(input_cases
[i
].type
, input
.type());
186 if (input_cases
[i
].type
== metrics::OmniboxInputType::URL
)
187 EXPECT_EQ(input_cases
[i
].spec
, input
.canonicalized_url().spec());
191 // This tests for a regression where certain input in the omnibox caused us to
192 // crash. As long as the test completes without crashing, we're fine.
193 TEST(AutocompleteInputTest
, InputCrash
) {
194 AutocompleteInput
input(base::WideToUTF16(L
"\uff65@s"), base::string16::npos
,
195 base::string16(), GURL(),
196 OmniboxEventProto::INVALID_SPEC
, true, false,
197 true, true, TestSchemeClassifier());
200 TEST(AutocompleteInputTest
, ParseForEmphasizeComponent
) {
201 using url::Component
;
202 Component
kInvalidComponent(0, -1);
204 const base::string16 input
;
205 const Component scheme
;
206 const Component host
;
208 { base::string16(), kInvalidComponent
, kInvalidComponent
},
209 { ASCIIToUTF16("?"), kInvalidComponent
, kInvalidComponent
},
210 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent
,
212 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent
, Component(0, 3) },
213 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) },
214 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent
},
215 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent
, Component(0, 11) },
216 { base::WideToUTF16(L
"\u6d4b\u8bd5"), kInvalidComponent
, Component(0, 2) },
217 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4),
219 { ASCIIToUTF16("view-source:https://example.com/"),
220 Component(12, 5), Component(20, 11) },
221 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent
,
223 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent
},
224 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent
,
226 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4),
228 { ASCIIToUTF16("view-source:view-source:http://example.com/"),
229 Component(12, 11), kInvalidComponent
}
232 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
233 SCOPED_TRACE(input_cases
[i
].input
);
234 Component scheme
, host
;
235 AutocompleteInput::ParseForEmphasizeComponents(input_cases
[i
].input
,
236 TestSchemeClassifier(),
239 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
240 base::string16(), GURL(),
241 OmniboxEventProto::INVALID_SPEC
, true,
242 false, true, true, TestSchemeClassifier());
243 EXPECT_EQ(input_cases
[i
].scheme
.begin
, scheme
.begin
);
244 EXPECT_EQ(input_cases
[i
].scheme
.len
, scheme
.len
);
245 EXPECT_EQ(input_cases
[i
].host
.begin
, host
.begin
);
246 EXPECT_EQ(input_cases
[i
].host
.len
, host
.len
);
250 TEST(AutocompleteInputTest
, InputTypeWithCursorPosition
) {
252 const base::string16 input
;
253 size_t cursor_position
;
254 const base::string16 normalized_input
;
255 size_t normalized_cursor_position
;
257 { ASCIIToUTF16("foo bar"), base::string16::npos
,
258 ASCIIToUTF16("foo bar"), base::string16::npos
},
260 // regular case, no changes.
261 { ASCIIToUTF16("foo bar"), 3, ASCIIToUTF16("foo bar"), 3 },
263 // extra leading space.
264 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 1 },
265 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 0 },
266 { ASCIIToUTF16(" foo bar "), 2, ASCIIToUTF16("foo bar "), 0 },
269 { ASCIIToUTF16("?foo bar"), 2, ASCIIToUTF16("foo bar"), 1 },
270 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
271 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
272 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 },
275 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
276 SCOPED_TRACE(input_cases
[i
].input
);
277 AutocompleteInput
input(input_cases
[i
].input
,
278 input_cases
[i
].cursor_position
,
279 base::string16(), GURL(),
280 OmniboxEventProto::INVALID_SPEC
,
281 true, false, true, true, TestSchemeClassifier());
282 EXPECT_EQ(input_cases
[i
].normalized_input
, input
.text());
283 EXPECT_EQ(input_cases
[i
].normalized_cursor_position
,
284 input
.cursor_position());