OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.h" |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete.h" | 10 #include "chrome/browser/autocomplete/autocomplete.h" |
11 #include "chrome/browser/autocomplete/autocomplete_match.h" | 11 #include "chrome/browser/autocomplete/autocomplete_match.h" |
12 #include "chrome/browser/autocomplete/keyword_provider.h" | 12 #include "chrome/browser/autocomplete/keyword_provider.h" |
13 #include "chrome/browser/autocomplete/search_provider.h" | 13 #include "chrome/browser/autocomplete/search_provider.h" |
14 #include "chrome/browser/search_engines/template_url.h" | 14 #include "chrome/browser/search_engines/template_url.h" |
15 #include "chrome/browser/search_engines/template_url_model.h" | 15 #include "chrome/browser/search_engines/template_url_model.h" |
16 #include "chrome/common/notification_observer.h" | 16 #include "chrome/common/notification_observer.h" |
17 #include "chrome/common/notification_registrar.h" | 17 #include "chrome/common/notification_registrar.h" |
18 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
19 #include "chrome/test/testing_profile.h" | 19 #include "chrome/test/testing_profile.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 // identifiers for known autocomplete providers | |
23 #define HISTORY_IDENTIFIER L"Chrome:History" | |
24 #define SEARCH_IDENTIFIER L"google.com/websearch/en" | |
25 | |
26 static std::ostream& operator<<(std::ostream& os, | 22 static std::ostream& operator<<(std::ostream& os, |
27 const AutocompleteResult::const_iterator& it) { | 23 const AutocompleteResult::const_iterator& it) { |
28 return os << static_cast<const AutocompleteMatch*>(&(*it)); | 24 return os << static_cast<const AutocompleteMatch*>(&(*it)); |
29 } | 25 } |
30 | 26 |
31 namespace { | 27 namespace { |
32 | 28 |
33 const size_t num_results_per_provider = 3; | 29 const size_t num_results_per_provider = 3; |
34 | 30 |
35 // Autocomplete provider that provides known results. Note that this is | 31 // Autocomplete provider that provides known results. Note that this is |
36 // refcounted so that it can also be a task on the message loop. | 32 // refcounted so that it can also be a task on the message loop. |
37 class TestProvider : public AutocompleteProvider { | 33 class TestProvider : public AutocompleteProvider { |
38 public: | 34 public: |
39 TestProvider(int relevance, const std::wstring& prefix) | 35 TestProvider(int relevance, const string16& prefix) |
40 : AutocompleteProvider(NULL, NULL, ""), | 36 : AutocompleteProvider(NULL, NULL, ""), |
41 relevance_(relevance), | 37 relevance_(relevance), |
42 prefix_(prefix) { | 38 prefix_(prefix) { |
43 } | 39 } |
44 | 40 |
45 virtual void Start(const AutocompleteInput& input, | 41 virtual void Start(const AutocompleteInput& input, |
46 bool minimal_changes); | 42 bool minimal_changes); |
47 | 43 |
48 void set_listener(ACProviderListener* listener) { | 44 void set_listener(ACProviderListener* listener) { |
49 listener_ = listener; | 45 listener_ = listener; |
50 } | 46 } |
51 | 47 |
52 private: | 48 private: |
53 ~TestProvider() {} | 49 ~TestProvider() {} |
54 | 50 |
55 void Run(); | 51 void Run(); |
56 | 52 |
57 void AddResults(int start_at, int num); | 53 void AddResults(int start_at, int num); |
58 | 54 |
59 int relevance_; | 55 int relevance_; |
60 const std::wstring prefix_; | 56 const string16 prefix_; |
61 }; | 57 }; |
62 | 58 |
63 void TestProvider::Start(const AutocompleteInput& input, | 59 void TestProvider::Start(const AutocompleteInput& input, |
64 bool minimal_changes) { | 60 bool minimal_changes) { |
65 if (minimal_changes) | 61 if (minimal_changes) |
66 return; | 62 return; |
67 | 63 |
68 matches_.clear(); | 64 matches_.clear(); |
69 | 65 |
70 // Generate one result synchronously, the rest later. | 66 // Generate one result synchronously, the rest later. |
(...skipping 12 matching lines...) Expand all Loading... | |
83 done_ = true; | 79 done_ = true; |
84 DCHECK(listener_); | 80 DCHECK(listener_); |
85 listener_->OnProviderUpdate(true); | 81 listener_->OnProviderUpdate(true); |
86 } | 82 } |
87 | 83 |
88 void TestProvider::AddResults(int start_at, int num) { | 84 void TestProvider::AddResults(int start_at, int num) { |
89 for (int i = start_at; i < num; i++) { | 85 for (int i = start_at; i < num; i++) { |
90 AutocompleteMatch match(this, relevance_ - i, false, | 86 AutocompleteMatch match(this, relevance_ - i, false, |
91 AutocompleteMatch::URL_WHAT_YOU_TYPED); | 87 AutocompleteMatch::URL_WHAT_YOU_TYPED); |
92 | 88 |
93 match.fill_into_edit = prefix_ + UTF8ToWide(base::IntToString(i)); | 89 match.fill_into_edit = prefix_ + UTF8ToUTF16(base::IntToString(i)); |
94 match.destination_url = GURL(WideToUTF8(match.fill_into_edit)); | 90 match.destination_url = GURL(UTF16ToUTF8(match.fill_into_edit)); |
95 | 91 |
96 match.contents = match.fill_into_edit; | 92 match.contents = match.fill_into_edit; |
97 match.contents_class.push_back( | 93 match.contents_class.push_back( |
98 ACMatchClassification(0, ACMatchClassification::NONE)); | 94 ACMatchClassification(0, ACMatchClassification::NONE)); |
99 match.description = match.fill_into_edit; | 95 match.description = match.fill_into_edit; |
100 match.description_class.push_back( | 96 match.description_class.push_back( |
101 ACMatchClassification(0, ACMatchClassification::NONE)); | 97 ACMatchClassification(0, ACMatchClassification::NONE)); |
102 | 98 |
103 matches_.push_back(match); | 99 matches_.push_back(match); |
104 } | 100 } |
(...skipping 29 matching lines...) Expand all Loading... | |
134 }; | 130 }; |
135 | 131 |
136 void AutocompleteProviderTest::ResetControllerWithTestProviders( | 132 void AutocompleteProviderTest::ResetControllerWithTestProviders( |
137 bool same_destinations) { | 133 bool same_destinations) { |
138 // Forget about any existing providers. The controller owns them and will | 134 // Forget about any existing providers. The controller owns them and will |
139 // Release() them below, when we delete it during the call to reset(). | 135 // Release() them below, when we delete it during the call to reset(). |
140 providers_.clear(); | 136 providers_.clear(); |
141 | 137 |
142 // Construct two new providers, with either the same or different prefixes. | 138 // Construct two new providers, with either the same or different prefixes. |
143 TestProvider* providerA = new TestProvider(num_results_per_provider, | 139 TestProvider* providerA = new TestProvider(num_results_per_provider, |
144 L"http://a"); | 140 ASCIIToUTF16("http://a")); |
145 providerA->AddRef(); | 141 providerA->AddRef(); |
146 providers_.push_back(providerA); | 142 providers_.push_back(providerA); |
147 | 143 |
148 TestProvider* providerB = new TestProvider(num_results_per_provider * 2, | 144 TestProvider* providerB = new TestProvider(num_results_per_provider * 2, |
149 same_destinations ? L"http://a" : L"http://b"); | 145 same_destinations ? ASCIIToUTF16("http://a") : ASCIIToUTF16("http://b")); |
150 providerB->AddRef(); | 146 providerB->AddRef(); |
151 providers_.push_back(providerB); | 147 providers_.push_back(providerB); |
152 | 148 |
153 // Reset the controller to contain our new providers. | 149 // Reset the controller to contain our new providers. |
154 AutocompleteController* controller = new AutocompleteController(providers_); | 150 AutocompleteController* controller = new AutocompleteController(providers_); |
155 controller_.reset(controller); | 151 controller_.reset(controller); |
156 providerA->set_listener(controller); | 152 providerA->set_listener(controller); |
157 providerB->set_listener(controller); | 153 providerB->set_listener(controller); |
158 | 154 |
159 // The providers don't complete synchronously, so listen for "result updated" | 155 // The providers don't complete synchronously, so listen for "result updated" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 AutocompleteProvider* search_provider = new SearchProvider(NULL, &profile_); | 192 AutocompleteProvider* search_provider = new SearchProvider(NULL, &profile_); |
197 search_provider->AddRef(); | 193 search_provider->AddRef(); |
198 providers_.push_back(search_provider); | 194 providers_.push_back(search_provider); |
199 | 195 |
200 AutocompleteController* controller = new AutocompleteController(providers_); | 196 AutocompleteController* controller = new AutocompleteController(providers_); |
201 controller_.reset(controller); | 197 controller_.reset(controller); |
202 } | 198 } |
203 | 199 |
204 void AutocompleteProviderTest::RunTest() { | 200 void AutocompleteProviderTest::RunTest() { |
205 result_.Reset(); | 201 result_.Reset(); |
206 controller_->Start(L"a", std::wstring(), true, false, true, false); | 202 controller_->Start(ASCIIToUTF16("a"), string16(), true, false, true, false); |
207 | 203 |
208 // The message loop will terminate when all autocomplete input has been | 204 // The message loop will terminate when all autocomplete input has been |
209 // collected. | 205 // collected. |
210 MessageLoop::current()->Run(); | 206 MessageLoop::current()->Run(); |
211 } | 207 } |
212 | 208 |
213 void AutocompleteProviderTest::RunExactKeymatchTest( | 209 void AutocompleteProviderTest::RunExactKeymatchTest( |
214 bool allow_exact_keyword_match) { | 210 bool allow_exact_keyword_match) { |
215 // Send the controller input which exactly matches the keyword provider we | 211 // Send the controller input which exactly matches the keyword provider we |
216 // created in ResetControllerWithKeywordAndSearchProviders(). The default | 212 // created in ResetControllerWithKeywordAndSearchProviders(). The default |
217 // match should thus be a keyword match iff |allow_exact_keyword_match| is | 213 // match should thus be a keyword match iff |allow_exact_keyword_match| is |
218 // true. | 214 // true. |
219 controller_->Start(L"k test", std::wstring(), true, false, | 215 controller_->Start(ASCIIToUTF16("k test"), string16(), true, false, |
220 allow_exact_keyword_match, true); | 216 allow_exact_keyword_match, true); |
221 EXPECT_TRUE(controller_->done()); | 217 EXPECT_TRUE(controller_->done()); |
222 // ResetControllerWithKeywordAndSearchProviders() adds the keyword provider | 218 // ResetControllerWithKeywordAndSearchProviders() adds the keyword provider |
223 // first, then the search provider. So if the default match is a keyword | 219 // first, then the search provider. So if the default match is a keyword |
224 // match, it will come from provider 0, otherwise from provider 1. | 220 // match, it will come from provider 0, otherwise from provider 1. |
225 EXPECT_EQ(providers_[allow_exact_keyword_match ? 0 : 1], | 221 EXPECT_EQ(providers_[allow_exact_keyword_match ? 0 : 1], |
226 controller_->result().default_match()->provider); | 222 controller_->result().default_match()->provider); |
227 } | 223 } |
228 | 224 |
229 void AutocompleteProviderTest::Observe(NotificationType type, | 225 void AutocompleteProviderTest::Observe(NotificationType type, |
(...skipping 30 matching lines...) Expand all Loading... | |
260 } | 256 } |
261 | 257 |
262 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { | 258 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { |
263 ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); | 259 ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); |
264 RunExactKeymatchTest(true); | 260 RunExactKeymatchTest(true); |
265 RunExactKeymatchTest(false); | 261 RunExactKeymatchTest(false); |
266 } | 262 } |
267 | 263 |
268 TEST(AutocompleteTest, InputType) { | 264 TEST(AutocompleteTest, InputType) { |
269 struct test_data { | 265 struct test_data { |
270 const wchar_t* input; | 266 const string16 input; |
271 const AutocompleteInput::Type type; | 267 const AutocompleteInput::Type type; |
272 } input_cases[] = { | 268 } input_cases[] = { |
273 { L"", AutocompleteInput::INVALID }, | 269 { ASCIIToUTF16(""), AutocompleteInput::INVALID }, |
274 { L"?", AutocompleteInput::FORCED_QUERY }, | 270 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, |
275 { L"?foo", AutocompleteInput::FORCED_QUERY }, | 271 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, |
276 { L"?foo bar", AutocompleteInput::FORCED_QUERY }, | 272 { ASCIIToUTF16("?foo bar"), AutocompleteInput::FORCED_QUERY }, |
277 { L"?http://foo.com/bar", AutocompleteInput::FORCED_QUERY }, | 273 { ASCIIToUTF16("?http://foo.com/bar"), AutocompleteInput::FORCED_QUERY }, |
278 { L"foo", AutocompleteInput::UNKNOWN }, | 274 { ASCIIToUTF16("foo"), AutocompleteInput::UNKNOWN }, |
279 { L"foo.c", AutocompleteInput::UNKNOWN }, | 275 { ASCIIToUTF16("foo.c"), AutocompleteInput::UNKNOWN }, |
280 { L"foo.com", AutocompleteInput::URL }, | 276 { ASCIIToUTF16("foo.com"), AutocompleteInput::URL }, |
281 { L"-.com", AutocompleteInput::UNKNOWN }, | 277 { ASCIIToUTF16("-.com"), AutocompleteInput::UNKNOWN }, |
282 { L"foo/bar", AutocompleteInput::URL }, | 278 { ASCIIToUTF16("foo/bar"), AutocompleteInput::URL }, |
283 { L"foo;bar", AutocompleteInput::QUERY }, | 279 { ASCIIToUTF16("foo;bar"), AutocompleteInput::QUERY }, |
284 { L"foo/bar baz", AutocompleteInput::UNKNOWN }, | 280 { ASCIIToUTF16("foo/bar baz"), AutocompleteInput::UNKNOWN }, |
285 { L"foo bar.com", AutocompleteInput::QUERY }, | 281 { ASCIIToUTF16("foo bar.com"), AutocompleteInput::QUERY }, |
286 { L"foo bar", AutocompleteInput::QUERY }, | 282 { ASCIIToUTF16("foo bar"), AutocompleteInput::QUERY }, |
287 { L"foo+bar", AutocompleteInput::QUERY }, | 283 { ASCIIToUTF16("foo+bar"), AutocompleteInput::QUERY }, |
288 { L"foo+bar.com", AutocompleteInput::UNKNOWN }, | 284 { ASCIIToUTF16("foo+bar.com"), AutocompleteInput::UNKNOWN }, |
289 { L"\"foo:bar\"", AutocompleteInput::QUERY }, | 285 { ASCIIToUTF16("\"foo:bar\""), AutocompleteInput::QUERY }, |
290 { L"link:foo.com", AutocompleteInput::UNKNOWN }, | 286 { ASCIIToUTF16("link:foo.com"), AutocompleteInput::UNKNOWN }, |
291 { L"foo:81", AutocompleteInput::URL }, | 287 { ASCIIToUTF16("foo:81"), AutocompleteInput::URL }, |
292 { L"www.foo.com:81", AutocompleteInput::URL }, | 288 { ASCIIToUTF16("www.foo.com:81"), AutocompleteInput::URL }, |
293 { L"localhost:8080", AutocompleteInput::URL }, | 289 { ASCIIToUTF16("localhost:8080"), AutocompleteInput::URL }, |
294 { L"foo.com:123456", AutocompleteInput::QUERY }, | 290 { ASCIIToUTF16("foo.com:123456"), AutocompleteInput::QUERY }, |
295 { L"foo.com:abc", AutocompleteInput::QUERY }, | 291 { ASCIIToUTF16("foo.com:abc"), AutocompleteInput::QUERY }, |
296 { L"1.2.3.4:abc", AutocompleteInput::QUERY }, | 292 { ASCIIToUTF16("1.2.3.4:abc"), AutocompleteInput::QUERY }, |
297 { L"user@foo.com", AutocompleteInput::UNKNOWN }, | 293 { ASCIIToUTF16("user@foo.com"), AutocompleteInput::UNKNOWN }, |
298 { L"user:pass@", AutocompleteInput::UNKNOWN }, | 294 { ASCIIToUTF16("user:pass@"), AutocompleteInput::UNKNOWN }, |
299 { L"user:pass@!foo.com", AutocompleteInput::UNKNOWN }, | 295 { ASCIIToUTF16("user:pass@!foo.com"), AutocompleteInput::UNKNOWN }, |
300 { L"user:pass@foo", AutocompleteInput::URL }, | 296 { ASCIIToUTF16("user:pass@foo"), AutocompleteInput::URL }, |
301 { L"user:pass@foo.c", AutocompleteInput::URL }, | 297 { ASCIIToUTF16("user:pass@foo.c"), AutocompleteInput::URL }, |
302 { L"user:pass@foo.com", AutocompleteInput::URL }, | 298 { ASCIIToUTF16("user:pass@foo.com"), AutocompleteInput::URL }, |
303 { L"user:pass@foo.com:81", AutocompleteInput::URL }, | 299 { ASCIIToUTF16("user:pass@foo.com:81"), AutocompleteInput::URL }, |
304 { L"user:pass@foo:81", AutocompleteInput::URL }, | 300 { ASCIIToUTF16("user:pass@foo:81"), AutocompleteInput::URL }, |
305 { L"1.2", AutocompleteInput::UNKNOWN }, | 301 { ASCIIToUTF16("1.2"), AutocompleteInput::UNKNOWN }, |
306 { L"1.2/45", AutocompleteInput::UNKNOWN }, | 302 { ASCIIToUTF16("1.2/45"), AutocompleteInput::UNKNOWN }, |
307 { L"1.2:45", AutocompleteInput::UNKNOWN }, | 303 { ASCIIToUTF16("1.2:45"), AutocompleteInput::UNKNOWN }, |
308 { L"user@1.2:45", AutocompleteInput::UNKNOWN }, | 304 { ASCIIToUTF16("user@1.2:45"), AutocompleteInput::UNKNOWN }, |
309 { L"user:pass@1.2:45", AutocompleteInput::URL }, | 305 { ASCIIToUTF16("user:pass@1.2:45"), AutocompleteInput::URL }, |
310 { L"ps/2 games", AutocompleteInput::UNKNOWN }, | 306 { ASCIIToUTF16("ps/2 games"), AutocompleteInput::UNKNOWN }, |
311 { L"en.wikipedia.org/wiki/James Bond", AutocompleteInput::URL }, | 307 { ASCIIToUTF16("en.wikipedia.org/wiki/James Bond"), |
308 AutocompleteInput::URL }, | |
312 // In Chrome itself, mailto: will get handled by ShellExecute, but in | 309 // In Chrome itself, mailto: will get handled by ShellExecute, but in |
313 // unittest mode, we don't have the data loaded in the external protocol | 310 // unittest mode, we don't have the data loaded in the external protocol |
314 // handler to know this. | 311 // handler to know this. |
315 // { L"mailto:abuse@foo.com", AutocompleteInput::URL }, | 312 // { ASCIIToUTF16("mailto:abuse@foo.com"), AutocompleteInput::URL }, |
316 { L"view-source:http://www.foo.com/", AutocompleteInput::URL }, | 313 { ASCIIToUTF16("view-source:http://www.foo.com/"), AutocompleteInput::URL }, |
317 { L"javascript:alert(\"Hey there!\");", AutocompleteInput::URL }, | 314 { ASCIIToUTF16("javascript:alert(\"Hey there!\");"), |
315 AutocompleteInput::URL }, | |
318 #if defined(OS_WIN) | 316 #if defined(OS_WIN) |
319 { L"C:\\Program Files", AutocompleteInput::URL }, | 317 { ASCIIToUTF16("C:\\Program Files"), AutocompleteInput::URL }, |
320 { L"\\\\Server\\Folder\\File", AutocompleteInput::URL }, | 318 { ASCIIToUTF16("\\\\Server\\Folder\\File"), AutocompleteInput::URL }, |
321 #endif // defined(OS_WIN) | 319 #endif // defined(OS_WIN) |
322 { L"http:foo", AutocompleteInput::URL }, | 320 { ASCIIToUTF16("http:foo"), AutocompleteInput::URL }, |
323 { L"http://foo", AutocompleteInput::URL }, | 321 { ASCIIToUTF16("http://foo"), AutocompleteInput::URL }, |
324 { L"http://foo.c", AutocompleteInput::URL }, | 322 { ASCIIToUTF16("http://foo.c"), AutocompleteInput::URL }, |
325 { L"http://foo.com", AutocompleteInput::URL }, | 323 { ASCIIToUTF16("http://foo.com"), AutocompleteInput::URL }, |
326 { L"http://foo_bar.com", AutocompleteInput::URL }, | 324 { ASCIIToUTF16("http://foo_bar.com"), AutocompleteInput::URL }, |
327 { L"http://foo/bar baz", AutocompleteInput::URL }, | 325 { ASCIIToUTF16("http://foo/bar baz"), AutocompleteInput::URL }, |
328 { L"http://-.com", AutocompleteInput::UNKNOWN }, | 326 { ASCIIToUTF16("http://-.com"), AutocompleteInput::UNKNOWN }, |
329 { L"http://_foo_.com", AutocompleteInput::UNKNOWN }, | 327 { ASCIIToUTF16("http://_foo_.com"), AutocompleteInput::UNKNOWN }, |
330 { L"http://foo.com:abc", AutocompleteInput::QUERY }, | 328 { ASCIIToUTF16("http://foo.com:abc"), AutocompleteInput::QUERY }, |
331 { L"http://foo.com:123456", AutocompleteInput::QUERY }, | 329 { ASCIIToUTF16("http://foo.com:123456"), AutocompleteInput::QUERY }, |
332 { L"http://1.2.3.4:abc", AutocompleteInput::QUERY }, | 330 { ASCIIToUTF16("http://1.2.3.4:abc"), AutocompleteInput::QUERY }, |
333 { L"http:user@foo.com", AutocompleteInput::URL }, | 331 { ASCIIToUTF16("http:user@foo.com"), AutocompleteInput::URL }, |
334 { L"http://user@foo.com", AutocompleteInput::URL }, | 332 { ASCIIToUTF16("http://user@foo.com"), AutocompleteInput::URL }, |
335 { L"http:user:pass@foo.com", AutocompleteInput::URL }, | 333 { ASCIIToUTF16("http:user:pass@foo.com"), AutocompleteInput::URL }, |
336 { L"http://user:pass@foo.com", AutocompleteInput::URL }, | 334 { ASCIIToUTF16("http://user:pass@foo.com"), AutocompleteInput::URL }, |
337 { L"http://1.2", AutocompleteInput::URL }, | 335 { ASCIIToUTF16("http://1.2"), AutocompleteInput::URL }, |
338 { L"http://1.2/45", AutocompleteInput::URL }, | 336 { ASCIIToUTF16("http://1.2/45"), AutocompleteInput::URL }, |
339 { L"http:ps/2 games", AutocompleteInput::URL }, | 337 { ASCIIToUTF16("http:ps/2 games"), AutocompleteInput::URL }, |
340 { L"http://ps/2 games", AutocompleteInput::URL }, | 338 { ASCIIToUTF16("http://ps/2 games"), AutocompleteInput::URL }, |
341 { L"https://foo.com", AutocompleteInput::URL }, | 339 { ASCIIToUTF16("https://foo.com"), AutocompleteInput::URL }, |
342 { L"127.0.0.1", AutocompleteInput::URL }, | 340 { ASCIIToUTF16("127.0.0.1"), AutocompleteInput::URL }, |
343 { L"127.0.1", AutocompleteInput::UNKNOWN }, | 341 { ASCIIToUTF16("127.0.1"), AutocompleteInput::UNKNOWN }, |
344 { L"127.0.1/", AutocompleteInput::UNKNOWN }, | 342 { ASCIIToUTF16("127.0.1/"), AutocompleteInput::UNKNOWN }, |
345 { L"browser.tabs.closeButtons", AutocompleteInput::UNKNOWN }, | 343 { ASCIIToUTF16("browser.tabs.closeButtons"), AutocompleteInput::UNKNOWN }, |
346 { L"\u6d4b\u8bd5", AutocompleteInput::UNKNOWN }, | 344 { WideToUTF16(L"\u6d4b\u8bd5"), AutocompleteInput::UNKNOWN }, |
347 { L"[2001:]", AutocompleteInput::QUERY }, // Not a valid IP | 345 { ASCIIToUTF16("[2001:]"), AutocompleteInput::QUERY }, // Not a valid IP |
348 { L"[2001:dB8::1]", AutocompleteInput::URL }, | 346 { ASCIIToUTF16("[2001:dB8::1]"), AutocompleteInput::URL }, |
349 { L"192.168.0.256", AutocompleteInput::QUERY }, // Invalid IPv4 literal. | 347 { ASCIIToUTF16("192.168.0.256"), |
350 { L"[foo.com]", AutocompleteInput::QUERY }, // Invalid IPv6 literal. | 348 AutocompleteInput::QUERY }, // Invalid IPv4 literal. |
349 { ASCIIToUTF16("[foo.com]"), | |
350 AutocompleteInput::QUERY }, // Invalid IPv6 literal. | |
evanm
2011/01/24 19:14:14
Might have been simpler to leave them wchar_t* and
Avi (use Gerrit)
2011/01/24 19:46:42
True. I hate wchar_t almost as much as wstring so
| |
351 }; | 351 }; |
352 | 352 |
353 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 353 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
354 SCOPED_TRACE(input_cases[i].input); | 354 SCOPED_TRACE(input_cases[i].input); |
355 AutocompleteInput input(input_cases[i].input, std::wstring(), true, false, | 355 AutocompleteInput input(input_cases[i].input, string16(), true, false, |
356 true, false); | 356 true, false); |
357 EXPECT_EQ(input_cases[i].type, input.type()); | 357 EXPECT_EQ(input_cases[i].type, input.type()); |
358 } | 358 } |
359 } | 359 } |
360 | 360 |
361 TEST(AutocompleteTest, InputTypeWithDesiredTLD) { | 361 TEST(AutocompleteTest, InputTypeWithDesiredTLD) { |
362 struct test_data { | 362 struct test_data { |
363 const wchar_t* input; | 363 const string16 input; |
364 const AutocompleteInput::Type type; | 364 const AutocompleteInput::Type type; |
365 } input_cases[] = { | 365 } input_cases[] = { |
366 { L"401k", AutocompleteInput::REQUESTED_URL }, | 366 { ASCIIToUTF16("401k"), AutocompleteInput::REQUESTED_URL }, |
367 { L"999999999999999", AutocompleteInput::REQUESTED_URL }, | 367 { ASCIIToUTF16("999999999999999"), AutocompleteInput::REQUESTED_URL }, |
368 }; | 368 }; |
369 | 369 |
370 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 370 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
371 AutocompleteInput input(input_cases[i].input, L"com", true, false, true, | 371 AutocompleteInput input(input_cases[i].input, ASCIIToUTF16("com"), true, |
372 false); | 372 false, true, false); |
373 EXPECT_EQ(input_cases[i].type, input.type()) << "Input: " << | 373 EXPECT_EQ(input_cases[i].type, input.type()) << "Input: " << |
374 input_cases[i].input; | 374 input_cases[i].input; |
375 } | 375 } |
376 } | 376 } |
377 | 377 |
378 // This tests for a regression where certain input in the omnibox caused us to | 378 // This tests for a regression where certain input in the omnibox caused us to |
379 // crash. As long as the test completes without crashing, we're fine. | 379 // crash. As long as the test completes without crashing, we're fine. |
380 TEST(AutocompleteTest, InputCrash) { | 380 TEST(AutocompleteTest, InputCrash) { |
381 AutocompleteInput input(L"\uff65@s", std::wstring(), true, false, true, | 381 AutocompleteInput input(WideToUTF16(L"\uff65@s"), string16(), true, false, |
382 false); | 382 true, false); |
383 } | 383 } |
384 | 384 |
385 // Test comparing matches relevance. | 385 // Test comparing matches relevance. |
386 TEST(AutocompleteMatch, MoreRelevant) { | 386 TEST(AutocompleteMatch, MoreRelevant) { |
387 struct RelevantCases { | 387 struct RelevantCases { |
388 int r1; | 388 int r1; |
389 int r2; | 389 int r2; |
390 bool expected_result; | 390 bool expected_result; |
391 } cases[] = { | 391 } cases[] = { |
392 { 10, 0, true }, | 392 { 10, 0, true }, |
(...skipping 12 matching lines...) Expand all Loading... | |
405 m2.relevance = cases[i].r2; | 405 m2.relevance = cases[i].r2; |
406 EXPECT_EQ(cases[i].expected_result, | 406 EXPECT_EQ(cases[i].expected_result, |
407 AutocompleteMatch::MoreRelevant(m1, m2)); | 407 AutocompleteMatch::MoreRelevant(m1, m2)); |
408 } | 408 } |
409 } | 409 } |
410 | 410 |
411 TEST(AutocompleteInput, ParseForEmphasizeComponent) { | 411 TEST(AutocompleteInput, ParseForEmphasizeComponent) { |
412 using url_parse::Component; | 412 using url_parse::Component; |
413 Component kInvalidComponent(0, -1); | 413 Component kInvalidComponent(0, -1); |
414 struct test_data { | 414 struct test_data { |
415 const wchar_t* input; | 415 const string16 input; |
416 const Component scheme; | 416 const Component scheme; |
417 const Component host; | 417 const Component host; |
418 } input_cases[] = { | 418 } input_cases[] = { |
419 { L"", kInvalidComponent, kInvalidComponent }, | 419 { ASCIIToUTF16(""), kInvalidComponent, kInvalidComponent }, |
420 { L"?", kInvalidComponent, kInvalidComponent }, | 420 { ASCIIToUTF16("?"), kInvalidComponent, kInvalidComponent }, |
421 { L"?http://foo.com/bar", kInvalidComponent, kInvalidComponent }, | 421 { ASCIIToUTF16("?http://foo.com/bar"), kInvalidComponent, |
422 { L"foo/bar baz", kInvalidComponent, Component(0, 3) }, | 422 kInvalidComponent }, |
423 { L"http://foo/bar baz", Component(0, 4), Component(7, 3) }, | 423 { ASCIIToUTF16("foo/bar baz"), kInvalidComponent, Component(0, 3) }, |
424 { L"link:foo.com", Component(0, 4), kInvalidComponent }, | 424 { ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) }, |
425 { L"www.foo.com:81", kInvalidComponent, Component(0, 11) }, | 425 { ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent }, |
426 { L"\u6d4b\u8bd5", kInvalidComponent, Component(0, 2) }, | 426 { ASCIIToUTF16("www.foo.com:81"), kInvalidComponent, Component(0, 11) }, |
427 { L"view-source:http://www.foo.com/", Component(12, 4), Component(19, 11) }, | 427 { WideToUTF16(L"\u6d4b\u8bd5"), kInvalidComponent, Component(0, 2) }, |
428 { L"view-source:https://example.com/", | 428 { ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4), |
429 Component(19, 11) }, | |
430 { ASCIIToUTF16("view-source:https://example.com/"), | |
429 Component(12, 5), Component(20, 11) }, | 431 Component(12, 5), Component(20, 11) }, |
430 { L"view-source:www.foo.com", kInvalidComponent, Component(12, 11) }, | 432 { ASCIIToUTF16("view-source:www.foo.com"), kInvalidComponent, |
431 { L"view-source:", Component(0, 11), kInvalidComponent }, | 433 Component(12, 11) }, |
432 { L"view-source:garbage", kInvalidComponent, Component(12, 7) }, | 434 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent }, |
433 { L"view-source:http://http://foo", Component(12, 4), Component(19, 4) }, | 435 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent, |
434 { L"view-source:view-source:http://example.com/", | 436 Component(12, 7) }, |
435 Component(12, 11), kInvalidComponent } | 437 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4), |
438 Component(19, 4) }, | |
439 { ASCIIToUTF16("view-source:view-source:http://example.com/"), | |
440 Component(12, 11), kInvalidComponent } | |
436 }; | 441 }; |
437 | 442 |
438 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 443 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
439 Component scheme, host; | 444 Component scheme, host; |
440 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, | 445 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, |
441 std::wstring(), | 446 string16(), |
442 &scheme, | 447 &scheme, |
443 &host); | 448 &host); |
444 AutocompleteInput input(input_cases[i].input, std::wstring(), true, false, | 449 AutocompleteInput input(input_cases[i].input, string16(), true, false, |
445 true, false); | 450 true, false); |
446 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin) << "Input: " << | 451 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin) << "Input: " << |
447 input_cases[i].input; | 452 input_cases[i].input; |
448 EXPECT_EQ(input_cases[i].scheme.len, scheme.len) << "Input: " << | 453 EXPECT_EQ(input_cases[i].scheme.len, scheme.len) << "Input: " << |
449 input_cases[i].input; | 454 input_cases[i].input; |
450 EXPECT_EQ(input_cases[i].host.begin, host.begin) << "Input: " << | 455 EXPECT_EQ(input_cases[i].host.begin, host.begin) << "Input: " << |
451 input_cases[i].input; | 456 input_cases[i].input; |
452 EXPECT_EQ(input_cases[i].host.len, host.len) << "Input: " << | 457 EXPECT_EQ(input_cases[i].host.len, host.len) << "Input: " << |
453 input_cases[i].input; | 458 input_cases[i].input; |
454 } | 459 } |
455 } | 460 } |
456 | 461 |
457 } // namespace | 462 } // namespace |
OLD | NEW |