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/"), AutocompleteInput::URL
},
37 { ASCIIToUTF16("foo/bar baz\\"), AutocompleteInput::URL
},
38 { ASCIIToUTF16("foo.com/bar"), AutocompleteInput::URL
},
39 { ASCIIToUTF16("foo;bar"), AutocompleteInput::UNKNOWN
},
40 { ASCIIToUTF16("foo/bar baz"), AutocompleteInput::UNKNOWN
},
41 { ASCIIToUTF16("foo bar.com"), AutocompleteInput::QUERY
},
42 { ASCIIToUTF16("foo bar"), AutocompleteInput::QUERY
},
43 { ASCIIToUTF16("foo+bar"), AutocompleteInput::QUERY
},
44 { ASCIIToUTF16("foo+bar.com"), AutocompleteInput::UNKNOWN
},
45 { ASCIIToUTF16("\"foo:bar\""), AutocompleteInput::QUERY
},
46 { ASCIIToUTF16("link:foo.com"), AutocompleteInput::UNKNOWN
},
47 { ASCIIToUTF16("foo:81"), AutocompleteInput::URL
},
48 { ASCIIToUTF16("localhost:8080"), AutocompleteInput::URL
},
49 { ASCIIToUTF16("www.foo.com:81"), AutocompleteInput::URL
},
50 { ASCIIToUTF16("foo.com:123456"), AutocompleteInput::QUERY
},
51 { ASCIIToUTF16("foo.com:abc"), AutocompleteInput::QUERY
},
52 { ASCIIToUTF16("1.2.3.4:abc"), AutocompleteInput::QUERY
},
53 { ASCIIToUTF16("user@foo.com"), AutocompleteInput::UNKNOWN
},
54 { ASCIIToUTF16("user@foo/z"), AutocompleteInput::URL
},
55 { ASCIIToUTF16("user@foo/z z"), AutocompleteInput::URL
},
56 { ASCIIToUTF16("user@foo.com/z"), AutocompleteInput::URL
},
57 { ASCIIToUTF16("user:pass@"), AutocompleteInput::UNKNOWN
},
58 { ASCIIToUTF16("user:pass@!foo.com"), AutocompleteInput::UNKNOWN
},
59 { ASCIIToUTF16("user:pass@foo"), AutocompleteInput::URL
},
60 { ASCIIToUTF16("user:pass@foo.c"), AutocompleteInput::URL
},
61 { ASCIIToUTF16("user:pass@foo.com"), AutocompleteInput::URL
},
62 { ASCIIToUTF16("user:pass@foo.com:81"), AutocompleteInput::URL
},
63 { ASCIIToUTF16("user:pass@foo:81"), AutocompleteInput::URL
},
64 { ASCIIToUTF16("1.2"), AutocompleteInput::UNKNOWN
},
65 { ASCIIToUTF16("1.2/45"), AutocompleteInput::UNKNOWN
},
66 { ASCIIToUTF16("1.2:45"), AutocompleteInput::UNKNOWN
},
67 { ASCIIToUTF16("user@1.2:45"), AutocompleteInput::URL
},
68 { ASCIIToUTF16("user@foo:45"), AutocompleteInput::URL
},
69 { ASCIIToUTF16("user:pass@1.2:45"), AutocompleteInput::URL
},
70 { ASCIIToUTF16("host?query"), AutocompleteInput::UNKNOWN
},
71 { ASCIIToUTF16("host#ref"), AutocompleteInput::UNKNOWN
},
72 { ASCIIToUTF16("host/path?query"), AutocompleteInput::URL
},
73 { ASCIIToUTF16("host/path#ref"), AutocompleteInput::URL
},
74 { ASCIIToUTF16("en.wikipedia.org/wiki/Jim Beam"), AutocompleteInput::URL
},
75 // In Chrome itself, mailto: will get handled by ShellExecute, but in
76 // unittest mode, we don't have the data loaded in the external protocol
77 // handler to know this.
78 // { ASCIIToUTF16("mailto:abuse@foo.com"), AutocompleteInput::URL },
79 { ASCIIToUTF16("view-source:http://www.foo.com/"), AutocompleteInput::URL
},
80 { ASCIIToUTF16("javascript:alert(\"Hi there\");"), AutocompleteInput::URL
},
82 { ASCIIToUTF16("C:\\Program Files"), AutocompleteInput::URL
},
83 { ASCIIToUTF16("\\\\Server\\Folder\\File"), AutocompleteInput::URL
},
84 #endif // defined(OS_WIN)
85 { ASCIIToUTF16("http:foo"), AutocompleteInput::URL
},
86 { ASCIIToUTF16("http://foo"), AutocompleteInput::URL
},
87 { ASCIIToUTF16("http://foo.c"), AutocompleteInput::URL
},
88 { ASCIIToUTF16("http://foo.com"), AutocompleteInput::URL
},
89 { ASCIIToUTF16("http://foo_bar.com"), AutocompleteInput::URL
},
90 { ASCIIToUTF16("http://foo/bar baz"), AutocompleteInput::URL
},
91 { ASCIIToUTF16("http://-foo.com"), AutocompleteInput::URL
},
92 { ASCIIToUTF16("http://foo-.com"), AutocompleteInput::URL
},
93 { ASCIIToUTF16("http://foo_.com"), AutocompleteInput::UNKNOWN
},
94 { ASCIIToUTF16("http://foo.-com"), AutocompleteInput::UNKNOWN
},
95 { ASCIIToUTF16("http://_foo_.com"), AutocompleteInput::UNKNOWN
},
96 { ASCIIToUTF16("http://foo.com:abc"), AutocompleteInput::QUERY
},
97 { ASCIIToUTF16("http://foo.com:123456"), AutocompleteInput::QUERY
},
98 { ASCIIToUTF16("http://1.2.3.4:abc"), AutocompleteInput::QUERY
},
99 { ASCIIToUTF16("http:user@foo.com"), AutocompleteInput::URL
},
100 { ASCIIToUTF16("http://user@foo.com"), AutocompleteInput::URL
},
101 { ASCIIToUTF16("http:user:pass@foo.com"), AutocompleteInput::URL
},
102 { ASCIIToUTF16("http://user:pass@foo.com"), AutocompleteInput::URL
},
103 { ASCIIToUTF16("http://1.2"), AutocompleteInput::URL
},
104 { ASCIIToUTF16("http://1.2/45"), AutocompleteInput::URL
},
105 { ASCIIToUTF16("http:ps/2 games"), AutocompleteInput::URL
},
106 { ASCIIToUTF16("https://foo.com"), AutocompleteInput::URL
},
107 { ASCIIToUTF16("127.0.0.1"), AutocompleteInput::URL
},
108 { ASCIIToUTF16("127.0.1"), AutocompleteInput::UNKNOWN
},
109 { ASCIIToUTF16("127.0.1/"), AutocompleteInput::URL
},
110 { ASCIIToUTF16("browser.tabs.closeButtons"), AutocompleteInput::UNKNOWN
},
111 { base::WideToUTF16(L
"\u6d4b\u8bd5"), AutocompleteInput::UNKNOWN
},
112 { ASCIIToUTF16("[2001:]"), AutocompleteInput::QUERY
},
113 { ASCIIToUTF16("[2001:dB8::1]"), AutocompleteInput::URL
},
114 { ASCIIToUTF16("192.168.0.256"), AutocompleteInput::QUERY
},
115 { ASCIIToUTF16("[foo.com]"), AutocompleteInput::QUERY
},
116 { ASCIIToUTF16("filesystem:http://a.com/t/bar"), AutocompleteInput::URL
},
117 { ASCIIToUTF16("filesystem:http:foo"), AutocompleteInput::URL
},
118 { ASCIIToUTF16("filesystem:file://"), AutocompleteInput::URL
},
119 { ASCIIToUTF16("filesystem:http"), AutocompleteInput::URL
},
120 { ASCIIToUTF16("filesystem:"), AutocompleteInput::URL
},
121 { ASCIIToUTF16("ftp:"), AutocompleteInput::URL
},
124 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
125 SCOPED_TRACE(input_cases
[i
].input
);
126 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
127 base::string16(), GURL(),
128 AutocompleteInput::INVALID_SPEC
, true, false, true,
129 AutocompleteInput::ALL_MATCHES
);
130 EXPECT_EQ(input_cases
[i
].type
, input
.type());
134 TEST(AutocompleteInputTest
, InputTypeWithDesiredTLD
) {
136 const base::string16 input
;
137 const AutocompleteInput::Type type
;
138 const std::string spec
; // Unused if not a URL.
140 { ASCIIToUTF16("401k"), AutocompleteInput::URL
,
141 std::string("http://www.401k.com/") },
142 { ASCIIToUTF16("999999999999999"), AutocompleteInput::URL
,
143 std::string("http://www.999999999999999.com/") },
144 { ASCIIToUTF16("x@y"), AutocompleteInput::URL
,
145 std::string("http://x@www.y.com/") },
146 { ASCIIToUTF16("y/z z"), AutocompleteInput::URL
,
147 std::string("http://www.y.com/z%20z") },
148 { ASCIIToUTF16("abc.com"), AutocompleteInput::URL
,
149 std::string("http://abc.com/") },
150 { ASCIIToUTF16("foo bar"), AutocompleteInput::QUERY
, std::string() },
153 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
154 SCOPED_TRACE(input_cases
[i
].input
);
155 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
156 ASCIIToUTF16("com"), GURL(),
157 AutocompleteInput::INVALID_SPEC
, true, false, true,
158 AutocompleteInput::ALL_MATCHES
);
159 EXPECT_EQ(input_cases
[i
].type
, input
.type());
160 if (input_cases
[i
].type
== AutocompleteInput::URL
)
161 EXPECT_EQ(input_cases
[i
].spec
, input
.canonicalized_url().spec());
165 // This tests for a regression where certain input in the omnibox caused us to
166 // crash. As long as the test completes without crashing, we're fine.
167 TEST(AutocompleteInputTest
, InputCrash
) {
168 AutocompleteInput
input(base::WideToUTF16(L
"\uff65@s"), base::string16::npos
,
169 base::string16(), GURL(),
170 AutocompleteInput::INVALID_SPEC
, true, false,
171 true, AutocompleteInput::ALL_MATCHES
);
174 TEST(AutocompleteInputTest
, ParseForEmphasizeComponent
) {
175 using url_parse::Component
;
176 Component
kInvalidComponent(0, -1);
178 const base::string16 input
;
179 const Component scheme
;
180 const Component host
;
182 { base::string16(), kInvalidComponent
, kInvalidComponent
},
183 { ASCIIToUTF16("?"), kInvalidComponent
, kInvalidComponent
},
184 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent
,
186 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent
, Component(0, 3) },
187 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) },
188 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent
},
189 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent
, Component(0, 11) },
190 { base::WideToUTF16(L
"\u6d4b\u8bd5"), kInvalidComponent
, Component(0, 2) },
191 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4),
193 { ASCIIToUTF16("view-source:https://example.com/"),
194 Component(12, 5), Component(20, 11) },
195 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent
,
197 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent
},
198 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent
,
200 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4),
202 { ASCIIToUTF16("view-source:view-source:http://example.com/"),
203 Component(12, 11), kInvalidComponent
}
206 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
207 SCOPED_TRACE(input_cases
[i
].input
);
208 Component scheme
, host
;
209 AutocompleteInput::ParseForEmphasizeComponents(input_cases
[i
].input
,
212 AutocompleteInput
input(input_cases
[i
].input
, base::string16::npos
,
213 base::string16(), GURL(),
214 AutocompleteInput::INVALID_SPEC
, true,
215 false, true, AutocompleteInput::ALL_MATCHES
);
216 EXPECT_EQ(input_cases
[i
].scheme
.begin
, scheme
.begin
);
217 EXPECT_EQ(input_cases
[i
].scheme
.len
, scheme
.len
);
218 EXPECT_EQ(input_cases
[i
].host
.begin
, host
.begin
);
219 EXPECT_EQ(input_cases
[i
].host
.len
, host
.len
);
223 TEST(AutocompleteInputTest
, InputTypeWithCursorPosition
) {
225 const base::string16 input
;
226 size_t cursor_position
;
227 const base::string16 normalized_input
;
228 size_t normalized_cursor_position
;
230 { ASCIIToUTF16("foo bar"), base::string16::npos
,
231 ASCIIToUTF16("foo bar"), base::string16::npos
},
233 // regular case, no changes.
234 { ASCIIToUTF16("foo bar"), 3, ASCIIToUTF16("foo bar"), 3 },
236 // extra leading space.
237 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 1 },
238 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 0 },
239 { ASCIIToUTF16(" foo bar "), 2, ASCIIToUTF16("foo bar "), 0 },
242 { ASCIIToUTF16("?foo bar"), 2, ASCIIToUTF16("foo bar"), 1 },
243 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
244 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 },
245 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 },
248 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input_cases
); ++i
) {
249 SCOPED_TRACE(input_cases
[i
].input
);
250 AutocompleteInput
input(input_cases
[i
].input
,
251 input_cases
[i
].cursor_position
,
252 base::string16(), GURL(),
253 AutocompleteInput::INVALID_SPEC
,
254 true, false, true, AutocompleteInput::ALL_MATCHES
);
255 EXPECT_EQ(input_cases
[i
].normalized_input
, input
.text());
256 EXPECT_EQ(input_cases
[i
].normalized_cursor_position
,
257 input
.cursor_position());