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