OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/autocomplete_input.h" | 5 #include "chrome/browser/autocomplete/autocomplete_input.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "url/url_parse.h" | 12 #include "url/url_parse.h" |
13 | 13 |
14 TEST(AutocompleteInputTest, InputType) { | 14 TEST(AutocompleteInputTest, InputType) { |
15 struct test_data { | 15 struct test_data { |
16 const string16 input; | 16 const base::string16 input; |
17 const AutocompleteInput::Type type; | 17 const AutocompleteInput::Type type; |
18 } input_cases[] = { | 18 } input_cases[] = { |
19 { string16(), AutocompleteInput::INVALID }, | 19 { base::string16(), AutocompleteInput::INVALID }, |
20 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, | 20 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, |
21 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, | 21 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, |
22 { ASCIIToUTF16("?foo bar"), AutocompleteInput::FORCED_QUERY }, | 22 { ASCIIToUTF16("?foo bar"), AutocompleteInput::FORCED_QUERY }, |
23 { ASCIIToUTF16("?http://foo.com/bar"), AutocompleteInput::FORCED_QUERY }, | 23 { ASCIIToUTF16("?http://foo.com/bar"), AutocompleteInput::FORCED_QUERY }, |
24 { ASCIIToUTF16("foo"), AutocompleteInput::UNKNOWN }, | 24 { ASCIIToUTF16("foo"), AutocompleteInput::UNKNOWN }, |
25 { ASCIIToUTF16("localhost"), AutocompleteInput::URL }, | 25 { ASCIIToUTF16("localhost"), AutocompleteInput::URL }, |
26 { ASCIIToUTF16("foo.c"), AutocompleteInput::UNKNOWN }, | 26 { ASCIIToUTF16("foo.c"), AutocompleteInput::UNKNOWN }, |
27 { ASCIIToUTF16("foo.com"), AutocompleteInput::URL }, | 27 { ASCIIToUTF16("foo.com"), AutocompleteInput::URL }, |
28 { ASCIIToUTF16("-foo.com"), AutocompleteInput::URL }, | 28 { ASCIIToUTF16("-foo.com"), AutocompleteInput::URL }, |
29 { ASCIIToUTF16("foo-.com"), AutocompleteInput::URL }, | 29 { ASCIIToUTF16("foo-.com"), AutocompleteInput::URL }, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 { ASCIIToUTF16("filesystem:http://a.com/t/bar"), AutocompleteInput::URL }, | 114 { ASCIIToUTF16("filesystem:http://a.com/t/bar"), AutocompleteInput::URL }, |
115 { ASCIIToUTF16("filesystem:http:foo"), AutocompleteInput::URL }, | 115 { ASCIIToUTF16("filesystem:http:foo"), AutocompleteInput::URL }, |
116 { ASCIIToUTF16("filesystem:file://"), AutocompleteInput::URL }, | 116 { ASCIIToUTF16("filesystem:file://"), AutocompleteInput::URL }, |
117 { ASCIIToUTF16("filesystem:http"), AutocompleteInput::URL }, | 117 { ASCIIToUTF16("filesystem:http"), AutocompleteInput::URL }, |
118 { ASCIIToUTF16("filesystem:"), AutocompleteInput::URL }, | 118 { ASCIIToUTF16("filesystem:"), AutocompleteInput::URL }, |
119 { ASCIIToUTF16("ftp:"), AutocompleteInput::URL }, | 119 { ASCIIToUTF16("ftp:"), AutocompleteInput::URL }, |
120 }; | 120 }; |
121 | 121 |
122 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 122 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
123 SCOPED_TRACE(input_cases[i].input); | 123 SCOPED_TRACE(input_cases[i].input); |
124 AutocompleteInput input(input_cases[i].input, string16::npos, string16(), | 124 AutocompleteInput input(input_cases[i].input, base::string16::npos, |
125 GURL(), AutocompleteInput::INVALID_SPEC, true, | 125 base::string16(), GURL(), |
126 false, true, AutocompleteInput::ALL_MATCHES); | 126 AutocompleteInput::INVALID_SPEC, true, false, true, |
| 127 AutocompleteInput::ALL_MATCHES); |
127 EXPECT_EQ(input_cases[i].type, input.type()); | 128 EXPECT_EQ(input_cases[i].type, input.type()); |
128 } | 129 } |
129 } | 130 } |
130 | 131 |
131 TEST(AutocompleteInputTest, InputTypeWithDesiredTLD) { | 132 TEST(AutocompleteInputTest, InputTypeWithDesiredTLD) { |
132 struct test_data { | 133 struct test_data { |
133 const string16 input; | 134 const base::string16 input; |
134 const AutocompleteInput::Type type; | 135 const AutocompleteInput::Type type; |
135 const std::string spec; // Unused if not a URL. | 136 const std::string spec; // Unused if not a URL. |
136 } input_cases[] = { | 137 } input_cases[] = { |
137 { ASCIIToUTF16("401k"), AutocompleteInput::URL, | 138 { ASCIIToUTF16("401k"), AutocompleteInput::URL, |
138 std::string("http://www.401k.com/") }, | 139 std::string("http://www.401k.com/") }, |
139 { ASCIIToUTF16("999999999999999"), AutocompleteInput::URL, | 140 { ASCIIToUTF16("999999999999999"), AutocompleteInput::URL, |
140 std::string("http://www.999999999999999.com/") }, | 141 std::string("http://www.999999999999999.com/") }, |
141 { ASCIIToUTF16("x@y"), AutocompleteInput::URL, | 142 { ASCIIToUTF16("x@y"), AutocompleteInput::URL, |
142 std::string("http://x@www.y.com/") }, | 143 std::string("http://x@www.y.com/") }, |
143 { ASCIIToUTF16("y/z z"), AutocompleteInput::URL, | 144 { ASCIIToUTF16("y/z z"), AutocompleteInput::URL, |
144 std::string("http://www.y.com/z%20z") }, | 145 std::string("http://www.y.com/z%20z") }, |
145 { ASCIIToUTF16("abc.com"), AutocompleteInput::URL, | 146 { ASCIIToUTF16("abc.com"), AutocompleteInput::URL, |
146 std::string("http://abc.com/") }, | 147 std::string("http://abc.com/") }, |
147 { ASCIIToUTF16("foo bar"), AutocompleteInput::QUERY, std::string() }, | 148 { ASCIIToUTF16("foo bar"), AutocompleteInput::QUERY, std::string() }, |
148 }; | 149 }; |
149 | 150 |
150 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 151 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
151 SCOPED_TRACE(input_cases[i].input); | 152 SCOPED_TRACE(input_cases[i].input); |
152 AutocompleteInput input(input_cases[i].input, string16::npos, | 153 AutocompleteInput input(input_cases[i].input, base::string16::npos, |
153 ASCIIToUTF16("com"), GURL(), | 154 ASCIIToUTF16("com"), GURL(), |
154 AutocompleteInput::INVALID_SPEC, true, false, true, | 155 AutocompleteInput::INVALID_SPEC, true, false, true, |
155 AutocompleteInput::ALL_MATCHES); | 156 AutocompleteInput::ALL_MATCHES); |
156 EXPECT_EQ(input_cases[i].type, input.type()); | 157 EXPECT_EQ(input_cases[i].type, input.type()); |
157 if (input_cases[i].type == AutocompleteInput::URL) | 158 if (input_cases[i].type == AutocompleteInput::URL) |
158 EXPECT_EQ(input_cases[i].spec, input.canonicalized_url().spec()); | 159 EXPECT_EQ(input_cases[i].spec, input.canonicalized_url().spec()); |
159 } | 160 } |
160 } | 161 } |
161 | 162 |
162 // This tests for a regression where certain input in the omnibox caused us to | 163 // This tests for a regression where certain input in the omnibox caused us to |
163 // crash. As long as the test completes without crashing, we're fine. | 164 // crash. As long as the test completes without crashing, we're fine. |
164 TEST(AutocompleteInputTest, InputCrash) { | 165 TEST(AutocompleteInputTest, InputCrash) { |
165 AutocompleteInput input(WideToUTF16(L"\uff65@s"), string16::npos, string16(), | 166 AutocompleteInput input(WideToUTF16(L"\uff65@s"), base::string16::npos, |
166 GURL(), AutocompleteInput::INVALID_SPEC, true, false, | 167 base::string16(), GURL(), |
| 168 AutocompleteInput::INVALID_SPEC, true, false, |
167 true, AutocompleteInput::ALL_MATCHES); | 169 true, AutocompleteInput::ALL_MATCHES); |
168 } | 170 } |
169 | 171 |
170 TEST(AutocompleteInputTest, ParseForEmphasizeComponent) { | 172 TEST(AutocompleteInputTest, ParseForEmphasizeComponent) { |
171 using url_parse::Component; | 173 using url_parse::Component; |
172 Component kInvalidComponent(0, -1); | 174 Component kInvalidComponent(0, -1); |
173 struct test_data { | 175 struct test_data { |
174 const string16 input; | 176 const base::string16 input; |
175 const Component scheme; | 177 const Component scheme; |
176 const Component host; | 178 const Component host; |
177 } input_cases[] = { | 179 } input_cases[] = { |
178 { string16(), kInvalidComponent, kInvalidComponent }, | 180 { base::string16(), kInvalidComponent, kInvalidComponent }, |
179 { ASCIIToUTF16("?"), kInvalidComponent, kInvalidComponent }, | 181 { ASCIIToUTF16("?"), kInvalidComponent, kInvalidComponent }, |
180 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent, | 182 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent, |
181 kInvalidComponent }, | 183 kInvalidComponent }, |
182 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent, Component(0, 3) }, | 184 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent, Component(0, 3) }, |
183 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) }, | 185 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) }, |
184 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent }, | 186 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent }, |
185 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent, Component(0, 11) }, | 187 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent, Component(0, 11) }, |
186 { WideToUTF16(L"\u6d4b\u8bd5"), kInvalidComponent, Component(0, 2) }, | 188 { WideToUTF16(L"\u6d4b\u8bd5"), kInvalidComponent, Component(0, 2) }, |
187 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4), | 189 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4), |
188 Component(19, 11) }, | 190 Component(19, 11) }, |
189 { ASCIIToUTF16("view-source:https://example.com/"), | 191 { ASCIIToUTF16("view-source:https://example.com/"), |
190 Component(12, 5), Component(20, 11) }, | 192 Component(12, 5), Component(20, 11) }, |
191 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent, | 193 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent, |
192 Component(12, 11) }, | 194 Component(12, 11) }, |
193 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent }, | 195 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent }, |
194 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent, | 196 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent, |
195 Component(12, 7) }, | 197 Component(12, 7) }, |
196 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4), | 198 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4), |
197 Component(19, 4) }, | 199 Component(19, 4) }, |
198 { ASCIIToUTF16("view-source:view-source:http://example.com/"), | 200 { ASCIIToUTF16("view-source:view-source:http://example.com/"), |
199 Component(12, 11), kInvalidComponent } | 201 Component(12, 11), kInvalidComponent } |
200 }; | 202 }; |
201 | 203 |
202 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 204 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
203 SCOPED_TRACE(input_cases[i].input); | 205 SCOPED_TRACE(input_cases[i].input); |
204 Component scheme, host; | 206 Component scheme, host; |
205 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, | 207 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, |
206 &scheme, | 208 &scheme, |
207 &host); | 209 &host); |
208 AutocompleteInput input(input_cases[i].input, string16::npos, string16(), | 210 AutocompleteInput input(input_cases[i].input, base::string16::npos, |
209 GURL(), AutocompleteInput::INVALID_SPEC, true, | 211 base::string16(), GURL(), |
| 212 AutocompleteInput::INVALID_SPEC, true, |
210 false, true, AutocompleteInput::ALL_MATCHES); | 213 false, true, AutocompleteInput::ALL_MATCHES); |
211 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin); | 214 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin); |
212 EXPECT_EQ(input_cases[i].scheme.len, scheme.len); | 215 EXPECT_EQ(input_cases[i].scheme.len, scheme.len); |
213 EXPECT_EQ(input_cases[i].host.begin, host.begin); | 216 EXPECT_EQ(input_cases[i].host.begin, host.begin); |
214 EXPECT_EQ(input_cases[i].host.len, host.len); | 217 EXPECT_EQ(input_cases[i].host.len, host.len); |
215 } | 218 } |
216 } | 219 } |
217 | 220 |
218 TEST(AutocompleteInputTest, InputTypeWithCursorPosition) { | 221 TEST(AutocompleteInputTest, InputTypeWithCursorPosition) { |
219 struct test_data { | 222 struct test_data { |
220 const string16 input; | 223 const base::string16 input; |
221 size_t cursor_position; | 224 size_t cursor_position; |
222 const string16 normalized_input; | 225 const base::string16 normalized_input; |
223 size_t normalized_cursor_position; | 226 size_t normalized_cursor_position; |
224 } input_cases[] = { | 227 } input_cases[] = { |
225 { ASCIIToUTF16("foo bar"), string16::npos, | 228 { ASCIIToUTF16("foo bar"), base::string16::npos, |
226 ASCIIToUTF16("foo bar"), string16::npos }, | 229 ASCIIToUTF16("foo bar"), base::string16::npos }, |
227 | 230 |
228 // regular case, no changes. | 231 // regular case, no changes. |
229 { ASCIIToUTF16("foo bar"), 3, ASCIIToUTF16("foo bar"), 3 }, | 232 { ASCIIToUTF16("foo bar"), 3, ASCIIToUTF16("foo bar"), 3 }, |
230 | 233 |
231 // extra leading space. | 234 // extra leading space. |
232 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 1 }, | 235 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 1 }, |
233 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 0 }, | 236 { ASCIIToUTF16(" foo bar"), 3, ASCIIToUTF16("foo bar"), 0 }, |
234 { ASCIIToUTF16(" foo bar "), 2, ASCIIToUTF16("foo bar "), 0 }, | 237 { ASCIIToUTF16(" foo bar "), 2, ASCIIToUTF16("foo bar "), 0 }, |
235 | 238 |
236 // forced query. | 239 // forced query. |
237 { ASCIIToUTF16("?foo bar"), 2, ASCIIToUTF16("foo bar"), 1 }, | 240 { ASCIIToUTF16("?foo bar"), 2, ASCIIToUTF16("foo bar"), 1 }, |
238 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, | 241 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, |
239 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, | 242 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, |
240 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 }, | 243 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 }, |
241 }; | 244 }; |
242 | 245 |
243 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 246 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
244 SCOPED_TRACE(input_cases[i].input); | 247 SCOPED_TRACE(input_cases[i].input); |
245 AutocompleteInput input(input_cases[i].input, | 248 AutocompleteInput input(input_cases[i].input, |
246 input_cases[i].cursor_position, | 249 input_cases[i].cursor_position, |
247 string16(), GURL(), AutocompleteInput::INVALID_SPEC, | 250 base::string16(), GURL(), |
| 251 AutocompleteInput::INVALID_SPEC, |
248 true, false, true, AutocompleteInput::ALL_MATCHES); | 252 true, false, true, AutocompleteInput::ALL_MATCHES); |
249 EXPECT_EQ(input_cases[i].normalized_input, input.text()); | 253 EXPECT_EQ(input_cases[i].normalized_input, input.text()); |
250 EXPECT_EQ(input_cases[i].normalized_cursor_position, | 254 EXPECT_EQ(input_cases[i].normalized_cursor_position, |
251 input.cursor_position()); | 255 input.cursor_position()); |
252 } | 256 } |
253 } | 257 } |
OLD | NEW |