Chromium Code Reviews| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 | 25 |
| 26 static std::ostream& operator<<(std::ostream& os, | 26 static std::ostream& operator<<(std::ostream& os, |
| 27 const AutocompleteResult::const_iterator& it) { | 27 const AutocompleteResult::const_iterator& it) { |
| 28 return os << static_cast<const AutocompleteMatch*>(&(*it)); | 28 return os << static_cast<const AutocompleteMatch*>(&(*it)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const size_t num_results_per_provider = 3; | 33 const size_t num_results_per_provider = 3; |
| 34 | 34 |
| 35 } // namespace | |
|
Peter Kasting
2011/12/15 22:56:04
Nit: For short namespaces like this let's just kil
| |
| 36 | |
| 35 // Autocomplete provider that provides known results. Note that this is | 37 // Autocomplete provider that provides known results. Note that this is |
| 36 // refcounted so that it can also be a task on the message loop. | 38 // refcounted so that it can also be a task on the message loop. |
| 37 class TestProvider : public AutocompleteProvider { | 39 class TestProvider : public AutocompleteProvider { |
| 38 public: | 40 public: |
| 39 TestProvider(int relevance, const string16& prefix) | 41 TestProvider(int relevance, const string16& prefix) |
| 40 : AutocompleteProvider(NULL, NULL, ""), | 42 : AutocompleteProvider(NULL, NULL, ""), |
| 41 relevance_(relevance), | 43 relevance_(relevance), |
| 42 prefix_(prefix) { | 44 prefix_(prefix) { |
| 43 } | 45 } |
| 44 | 46 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 | 108 |
| 107 class AutocompleteProviderTest : public testing::Test, | 109 class AutocompleteProviderTest : public testing::Test, |
| 108 public content::NotificationObserver { | 110 public content::NotificationObserver { |
| 109 protected: | 111 protected: |
| 110 void ResetControllerWithTestProviders(bool same_destinations); | 112 void ResetControllerWithTestProviders(bool same_destinations); |
| 111 | 113 |
| 112 // Runs a query on the input "a", and makes sure both providers' input is | 114 // Runs a query on the input "a", and makes sure both providers' input is |
| 113 // properly collected. | 115 // properly collected. |
| 114 void RunTest(); | 116 void RunTest(); |
| 115 | 117 |
| 118 void RunQuery(string16 query); | |
|
Peter Kasting
2011/12/15 22:56:04
Nit: const string16&
| |
| 119 | |
| 116 void ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); | 120 void ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); |
| 121 void ResetControllerWithKeywordProvider(); | |
| 117 void RunExactKeymatchTest(bool allow_exact_keyword_match); | 122 void RunExactKeymatchTest(bool allow_exact_keyword_match); |
| 118 | 123 |
| 119 // These providers are owned by the controller once it's created. | 124 // These providers are owned by the controller once it's created. |
| 120 ACProviders providers_; | 125 ACProviders providers_; |
| 121 | 126 |
| 122 AutocompleteResult result_; | 127 AutocompleteResult result_; |
| 128 scoped_ptr<AutocompleteController> controller_; | |
| 123 | 129 |
| 124 private: | 130 private: |
| 125 // content::NotificationObserver | 131 // content::NotificationObserver |
| 126 virtual void Observe(int type, | 132 virtual void Observe(int type, |
| 127 const content::NotificationSource& source, | 133 const content::NotificationSource& source, |
| 128 const content::NotificationDetails& details); | 134 const content::NotificationDetails& details); |
| 129 | 135 |
| 130 MessageLoopForUI message_loop_; | 136 MessageLoopForUI message_loop_; |
| 131 scoped_ptr<AutocompleteController> controller_; | |
| 132 content::NotificationRegistrar registrar_; | 137 content::NotificationRegistrar registrar_; |
| 133 TestingProfile profile_; | 138 TestingProfile profile_; |
| 134 }; | 139 }; |
| 135 | 140 |
| 136 void AutocompleteProviderTest::ResetControllerWithTestProviders( | 141 void AutocompleteProviderTest::ResetControllerWithTestProviders( |
| 137 bool same_destinations) { | 142 bool same_destinations) { |
| 138 // Forget about any existing providers. The controller owns them and will | 143 // Forget about any existing providers. The controller owns them and will |
| 139 // Release() them below, when we delete it during the call to reset(). | 144 // Release() them below, when we delete it during the call to reset(). |
| 140 providers_.clear(); | 145 providers_.clear(); |
| 141 | 146 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 AutocompleteProvider* keyword_provider = new KeywordProvider(NULL, | 200 AutocompleteProvider* keyword_provider = new KeywordProvider(NULL, |
| 196 &profile_); | 201 &profile_); |
| 197 keyword_provider->AddRef(); | 202 keyword_provider->AddRef(); |
| 198 providers_.push_back(keyword_provider); | 203 providers_.push_back(keyword_provider); |
| 199 AutocompleteProvider* search_provider = new SearchProvider(NULL, &profile_); | 204 AutocompleteProvider* search_provider = new SearchProvider(NULL, &profile_); |
| 200 search_provider->AddRef(); | 205 search_provider->AddRef(); |
| 201 providers_.push_back(search_provider); | 206 providers_.push_back(search_provider); |
| 202 | 207 |
| 203 AutocompleteController* controller = | 208 AutocompleteController* controller = |
| 204 new AutocompleteController(providers_, &profile_); | 209 new AutocompleteController(providers_, &profile_); |
| 205 controller_.reset(controller); | 210 controller_.reset(controller); |
|
Peter Kasting
2011/12/15 22:56:04
Nit: Just combine these two lines
| |
| 206 } | 211 } |
| 207 | 212 |
| 213 void AutocompleteProviderTest:: | |
| 214 ResetControllerWithKeywordProvider() { | |
| 215 profile_.CreateTemplateURLService(); | |
| 216 | |
| 217 TemplateURLService* turl_model = | |
| 218 TemplateURLServiceFactory::GetForProfile(&profile_); | |
| 219 | |
| 220 // Create a TemplateURL for KeywordProvider. | |
| 221 TemplateURL* keyword_t_url = new TemplateURL(); | |
| 222 keyword_t_url->set_short_name(ASCIIToUTF16("foo.com")); | |
| 223 keyword_t_url->set_keyword(ASCIIToUTF16("foo.com")); | |
| 224 keyword_t_url->SetURL("http://foo.com/{searchTerms}", 0, 0); | |
| 225 turl_model->Add(keyword_t_url); | |
| 226 ASSERT_NE(0, keyword_t_url->id()); | |
| 227 | |
| 228 // Create another TemplateURL for KeywordProvider. | |
| 229 keyword_t_url = new TemplateURL(); | |
| 230 keyword_t_url->set_short_name(ASCIIToUTF16("bar.com")); | |
| 231 keyword_t_url->set_keyword(ASCIIToUTF16("bar.com")); | |
| 232 keyword_t_url->SetURL("http://bar.com/{searchTerms}", 0, 0); | |
| 233 turl_model->Add(keyword_t_url); | |
| 234 ASSERT_NE(0, keyword_t_url->id()); | |
| 235 | |
| 236 // Forget about any existing providers. The controller owns them and will | |
| 237 // Release() them below, when we delete it during the call to reset(). | |
| 238 providers_.clear(); | |
| 239 | |
| 240 // Create both a keyword and search provider, and add them in that order. | |
| 241 // (Order is important; see comments in RunExactKeymatchTest().) | |
| 242 KeywordProvider* keyword_provider = new KeywordProvider(NULL, | |
| 243 &profile_); | |
| 244 keyword_provider->AddRef(); | |
| 245 providers_.push_back(keyword_provider); | |
| 246 | |
| 247 AutocompleteController* controller = | |
| 248 new AutocompleteController(providers_, &profile_); | |
| 249 controller->set_keyword_provider(keyword_provider); | |
| 250 controller_.reset(controller); | |
| 251 } | |
| 252 | |
| 208 void AutocompleteProviderTest::RunTest() { | 253 void AutocompleteProviderTest::RunTest() { |
| 254 RunQuery(ASCIIToUTF16("a")); | |
| 255 } | |
| 256 | |
| 257 void AutocompleteProviderTest::RunQuery(string16 query) { | |
| 209 result_.Reset(); | 258 result_.Reset(); |
| 210 controller_->Start(ASCIIToUTF16("a"), string16(), true, false, true, | 259 controller_->Start(query, string16(), true, false, true, |
| 211 AutocompleteInput::ALL_MATCHES); | 260 AutocompleteInput::ALL_MATCHES); |
| 212 | 261 |
| 213 // The message loop will terminate when all autocomplete input has been | 262 if (!controller_->done()) |
| 214 // collected. | 263 // The message loop will terminate when all autocomplete input has been |
| 215 MessageLoop::current()->Run(); | 264 // collected. |
| 265 MessageLoop::current()->Run(); | |
| 216 } | 266 } |
| 217 | 267 |
| 218 void AutocompleteProviderTest::RunExactKeymatchTest( | 268 void AutocompleteProviderTest::RunExactKeymatchTest( |
| 219 bool allow_exact_keyword_match) { | 269 bool allow_exact_keyword_match) { |
| 220 // Send the controller input which exactly matches the keyword provider we | 270 // Send the controller input which exactly matches the keyword provider we |
| 221 // created in ResetControllerWithKeywordAndSearchProviders(). The default | 271 // created in ResetControllerWithKeywordAndSearchProviders(). The default |
| 222 // match should thus be a keyword match iff |allow_exact_keyword_match| is | 272 // match should thus be a keyword match iff |allow_exact_keyword_match| is |
| 223 // true. | 273 // true. |
| 224 controller_->Start(ASCIIToUTF16("k test"), string16(), true, false, | 274 controller_->Start(ASCIIToUTF16("k test"), string16(), true, false, |
| 225 allow_exact_keyword_match, | 275 allow_exact_keyword_match, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 i != result_.end(); ++i) | 315 i != result_.end(); ++i) |
| 266 EXPECT_EQ(providers_[1], i->provider); | 316 EXPECT_EQ(providers_[1], i->provider); |
| 267 } | 317 } |
| 268 | 318 |
| 269 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { | 319 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { |
| 270 ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); | 320 ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); |
| 271 RunExactKeymatchTest(true); | 321 RunExactKeymatchTest(true); |
| 272 RunExactKeymatchTest(false); | 322 RunExactKeymatchTest(false); |
| 273 } | 323 } |
| 274 | 324 |
| 325 // Test that redundant associated keywords are removed. | |
| 326 TEST_F(AutocompleteProviderTest, RedundantKeywordsIgnoredInResult) { | |
| 327 ResetControllerWithKeywordProvider(); | |
| 328 | |
| 329 // Get the controller's internal members in the correct state. | |
| 330 RunQuery(ASCIIToUTF16("fo")); | |
| 331 | |
| 332 struct test_data { | |
|
Peter Kasting
2011/12/15 22:56:04
Nit: Struct names use CamelCase.
| |
| 333 const string16 fill_into_edit; | |
| 334 const string16 keyword; | |
| 335 const bool expected_keyword_result; | |
| 336 } result_data[] = { | |
| 337 { ASCIIToUTF16("fo"), ASCIIToUTF16(""), false }, | |
|
Peter Kasting
2011/12/15 22:56:04
Nit: ASCIIToUTF16("") -> string16() (many places)
| |
| 338 { ASCIIToUTF16("foo.com"), ASCIIToUTF16(""), true }, | |
| 339 { ASCIIToUTF16("foo.com"), ASCIIToUTF16(""), false }, | |
| 340 { string16(), string16(), false }, | |
|
Peter Kasting
2011/12/15 22:56:04
The usage of "blank rows" in here to trigger the U
| |
| 341 { ASCIIToUTF16("foo.com"), ASCIIToUTF16("foo.com"), false }, | |
| 342 { ASCIIToUTF16("foo.com"), ASCIIToUTF16(""), false }, | |
| 343 { string16(), string16(), false }, | |
| 344 { ASCIIToUTF16("fo"), ASCIIToUTF16(""), false }, | |
| 345 { ASCIIToUTF16("foo.com"), ASCIIToUTF16(""), true }, | |
| 346 { ASCIIToUTF16("foo.com"), ASCIIToUTF16(""), false }, | |
| 347 { ASCIIToUTF16("bar.com"), ASCIIToUTF16(""), true }, | |
| 348 { string16(), string16(), false }, | |
| 349 }; | |
| 350 | |
| 351 ACMatches matches; | |
| 352 size_t set = 1; | |
| 353 size_t set_begin = 0; | |
| 354 | |
| 355 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(result_data); ++i) { | |
| 356 if (result_data[i].fill_into_edit.length() > 0) { | |
| 357 AutocompleteMatch match; | |
| 358 match.fill_into_edit = result_data[i].fill_into_edit; | |
| 359 match.keyword = result_data[i].keyword; | |
| 360 matches.push_back(match); | |
| 361 } else { | |
| 362 AutocompleteResult result; | |
| 363 result.AppendMatches(matches); | |
| 364 | |
| 365 controller_->UpdateAssociatedKeywords(&result); | |
| 366 for (size_t j = 0; j < result.size(); ++j) | |
| 367 EXPECT_EQ(result_data[set_begin + j].expected_keyword_result, | |
| 368 result.match_at(j).associated_keyword.get() != NULL) << | |
| 369 "Set: " << set << " Result: " << (j + 1); | |
| 370 | |
| 371 matches.clear(); | |
| 372 set++; | |
| 373 set_begin = i + 1; | |
| 374 } | |
| 375 } | |
| 376 } | |
| 377 | |
| 275 typedef testing::Test AutocompleteTest; | 378 typedef testing::Test AutocompleteTest; |
| 276 | 379 |
| 277 TEST_F(AutocompleteTest, InputType) { | 380 TEST_F(AutocompleteTest, InputType) { |
| 278 struct test_data { | 381 struct test_data { |
| 279 const string16 input; | 382 const string16 input; |
| 280 const AutocompleteInput::Type type; | 383 const AutocompleteInput::Type type; |
| 281 } input_cases[] = { | 384 } input_cases[] = { |
| 282 { ASCIIToUTF16(""), AutocompleteInput::INVALID }, | 385 { ASCIIToUTF16(""), AutocompleteInput::INVALID }, |
| 283 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, | 386 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, |
| 284 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, | 387 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 480 &scheme, | 583 &scheme, |
| 481 &host); | 584 &host); |
| 482 AutocompleteInput input(input_cases[i].input, string16(), true, false, | 585 AutocompleteInput input(input_cases[i].input, string16(), true, false, |
| 483 true, AutocompleteInput::ALL_MATCHES); | 586 true, AutocompleteInput::ALL_MATCHES); |
| 484 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin); | 587 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin); |
| 485 EXPECT_EQ(input_cases[i].scheme.len, scheme.len); | 588 EXPECT_EQ(input_cases[i].scheme.len, scheme.len); |
| 486 EXPECT_EQ(input_cases[i].host.begin, host.begin); | 589 EXPECT_EQ(input_cases[i].host.begin, host.begin); |
| 487 EXPECT_EQ(input_cases[i].host.len, host.len); | 590 EXPECT_EQ(input_cases[i].host.len, host.len); |
| 488 } | 591 } |
| 489 } | 592 } |
| 490 | |
| 491 } // namespace | |
| OLD | NEW |