| 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 "chrome/browser/autocomplete/search_provider.h" |
| 6 |
| 5 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 6 #include "base/time.h" | 8 #include "base/time.h" |
| 7 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 8 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete.h" |
| 9 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 10 #include "chrome/browser/autocomplete/search_provider.h" | |
| 11 #include "chrome/browser/history/history.h" | 13 #include "chrome/browser/history/history.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 15 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | 16 #include "chrome/browser/search_engines/template_url_service.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 16 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/testing_browser_process.h" | 19 #include "chrome/test/testing_browser_process.h" |
| 18 #include "chrome/test/testing_browser_process_test.h" | 20 #include "chrome/test/testing_browser_process_test.h" |
| 19 #include "chrome/test/testing_profile.h" | 21 #include "chrome/test/testing_profile.h" |
| 20 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 void SearchProviderTest::SetUp() { | 103 void SearchProviderTest::SetUp() { |
| 102 SearchProvider::set_query_suggest_immediately(true); | 104 SearchProvider::set_query_suggest_immediately(true); |
| 103 | 105 |
| 104 // We need both the history service and template url model loaded. | 106 // We need both the history service and template url model loaded. |
| 105 profile_.CreateHistoryService(true, false); | 107 profile_.CreateHistoryService(true, false); |
| 106 profile_.CreateTemplateURLService(); | 108 profile_.CreateTemplateURLService(); |
| 107 | 109 |
| 108 TemplateURLService* turl_model = | 110 TemplateURLService* turl_model = |
| 109 TemplateURLServiceFactory::GetForProfile(&profile_); | 111 TemplateURLServiceFactory::GetForProfile(&profile_); |
| 110 | 112 |
| 113 turl_model->Load(); |
| 114 |
| 111 // Reset the default TemplateURL. | 115 // Reset the default TemplateURL. |
| 112 default_t_url_ = new TemplateURL(); | 116 default_t_url_ = new TemplateURL(); |
| 113 default_t_url_->SetURL("http://defaultturl/{searchTerms}", 0, 0); | 117 default_t_url_->SetURL("http://defaultturl/{searchTerms}", 0, 0); |
| 114 default_t_url_->SetSuggestionsURL("http://defaultturl2/{searchTerms}", 0, 0); | 118 default_t_url_->SetSuggestionsURL("http://defaultturl2/{searchTerms}", 0, 0); |
| 119 default_t_url_->set_short_name(ASCIIToUTF16("t")); |
| 115 turl_model->Add(default_t_url_); | 120 turl_model->Add(default_t_url_); |
| 116 turl_model->SetDefaultSearchProvider(default_t_url_); | 121 turl_model->SetDefaultSearchProvider(default_t_url_); |
| 117 TemplateURLID default_provider_id = default_t_url_->id(); | 122 TemplateURLID default_provider_id = default_t_url_->id(); |
| 118 ASSERT_NE(0, default_provider_id); | 123 ASSERT_NE(0, default_provider_id); |
| 119 | 124 |
| 120 // Add url1, with search term term1_. | 125 // Add url1, with search term term1_. |
| 121 HistoryService* history = | 126 HistoryService* history = |
| 122 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 127 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 123 term1_url_ = GURL(default_t_url_->url()->ReplaceSearchTerms( | 128 term1_url_ = GURL(default_t_url_->url()->ReplaceSearchTerms( |
| 124 *default_t_url_, term1_, 0, string16())); | 129 *default_t_url_, term1_, 0, string16())); |
| 125 history->AddPageWithDetails(term1_url_, string16(), 1, 1, | 130 history->AddPageWithDetails(term1_url_, string16(), 1, 1, |
| 126 base::Time::Now(), false, | 131 base::Time::Now(), false, |
| 127 history::SOURCE_BROWSED); | 132 history::SOURCE_BROWSED); |
| 128 history->SetKeywordSearchTermsForURL(term1_url_, default_t_url_->id(), | 133 history->SetKeywordSearchTermsForURL(term1_url_, default_t_url_->id(), |
| 129 term1_); | 134 term1_); |
| 130 | 135 |
| 131 // Create another TemplateURL. | 136 // Create another TemplateURL. |
| 132 keyword_t_url_ = new TemplateURL(); | 137 keyword_t_url_ = new TemplateURL(); |
| 133 keyword_t_url_->set_keyword(ASCIIToUTF16("k")); | 138 keyword_t_url_->set_keyword(ASCIIToUTF16("k")); |
| 139 keyword_t_url_->set_short_name(ASCIIToUTF16("k")); |
| 134 keyword_t_url_->SetURL("http://keyword/{searchTerms}", 0, 0); | 140 keyword_t_url_->SetURL("http://keyword/{searchTerms}", 0, 0); |
| 135 keyword_t_url_->SetSuggestionsURL("http://suggest_keyword/{searchTerms}", 0, | 141 keyword_t_url_->SetSuggestionsURL("http://suggest_keyword/{searchTerms}", 0, |
| 136 0); | 142 0); |
| 137 turl_model->Add(keyword_t_url_); | 143 turl_model->Add(keyword_t_url_); |
| 138 ASSERT_NE(0, keyword_t_url_->id()); | 144 ASSERT_NE(0, keyword_t_url_->id()); |
| 139 | 145 |
| 140 // Add a page and search term for keyword_t_url_. | 146 // Add a page and search term for keyword_t_url_. |
| 141 keyword_url_ = GURL(keyword_t_url_->url()->ReplaceSearchTerms( | 147 keyword_url_ = GURL(keyword_t_url_->url()->ReplaceSearchTerms( |
| 142 *keyword_t_url_, keyword_term_, 0, string16())); | 148 *keyword_t_url_, keyword_term_, 0, string16())); |
| 143 history->AddPageWithDetails(keyword_url_, string16(), 1, 1, | 149 history->AddPageWithDetails(keyword_url_, string16(), 1, 1, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 std::string()); | 248 std::string()); |
| 243 fetcher = NULL; | 249 fetcher = NULL; |
| 244 | 250 |
| 245 // Run till the history results complete. | 251 // Run till the history results complete. |
| 246 RunTillProviderDone(); | 252 RunTillProviderDone(); |
| 247 | 253 |
| 248 // The SearchProvider is done. Make sure it has a result for the history | 254 // The SearchProvider is done. Make sure it has a result for the history |
| 249 // term term1. | 255 // term term1. |
| 250 AutocompleteMatch term1_match = FindMatchWithDestination(term1_url_); | 256 AutocompleteMatch term1_match = FindMatchWithDestination(term1_url_); |
| 251 EXPECT_TRUE(!term1_match.destination_url.is_empty()); | 257 EXPECT_TRUE(!term1_match.destination_url.is_empty()); |
| 252 // Term1 should have a description. | 258 // Term1 should not have a description, it's set later. |
| 253 EXPECT_FALSE(term1_match.description.empty()); | 259 EXPECT_TRUE(term1_match.description.empty()); |
| 254 | 260 |
| 255 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 261 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
| 256 *default_t_url_, term, 0, string16())); | 262 *default_t_url_, term, 0, string16())); |
| 257 AutocompleteMatch what_you_typed_match = | 263 AutocompleteMatch what_you_typed_match = |
| 258 FindMatchWithDestination(what_you_typed_url); | 264 FindMatchWithDestination(what_you_typed_url); |
| 259 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); | 265 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); |
| 260 EXPECT_TRUE(what_you_typed_match.description.empty()); | 266 EXPECT_TRUE(what_you_typed_match.description.empty()); |
| 261 | 267 |
| 262 // The match for term1 should be more relevant than the what you typed result. | 268 // The match for term1 should be more relevant than the what you typed result. |
| 263 EXPECT_GT(term1_match.relevance, what_you_typed_match.relevance); | 269 EXPECT_GT(term1_match.relevance, what_you_typed_match.relevance); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 EXPECT_TRUE(provider_->done()); | 377 EXPECT_TRUE(provider_->done()); |
| 372 | 378 |
| 373 // There should be two matches, one for what you typed, the other for | 379 // There should be two matches, one for what you typed, the other for |
| 374 // 'foobar'. | 380 // 'foobar'. |
| 375 EXPECT_EQ(2u, provider_->matches().size()); | 381 EXPECT_EQ(2u, provider_->matches().size()); |
| 376 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 382 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
| 377 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); | 383 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); |
| 378 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); | 384 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); |
| 379 EXPECT_TRUE(!instant_match.destination_url.is_empty()); | 385 EXPECT_TRUE(!instant_match.destination_url.is_empty()); |
| 380 | 386 |
| 381 // And the 'foobar' match should have a description. | 387 // And the 'foobar' match should not have a description, it'll be set later. |
| 382 EXPECT_FALSE(instant_match.description.empty()); | 388 EXPECT_TRUE(instant_match.description.empty()); |
| 383 | 389 |
| 384 // Make sure the what you typed match has no description. | 390 // Make sure the what you typed match has no description. |
| 385 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 391 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
| 386 *default_t_url_, ASCIIToUTF16("foo"), 0, string16())); | 392 *default_t_url_, ASCIIToUTF16("foo"), 0, string16())); |
| 387 AutocompleteMatch what_you_typed_match = | 393 AutocompleteMatch what_you_typed_match = |
| 388 FindMatchWithDestination(what_you_typed_url); | 394 FindMatchWithDestination(what_you_typed_url); |
| 389 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); | 395 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); |
| 390 EXPECT_TRUE(what_you_typed_match.description.empty()); | 396 EXPECT_TRUE(what_you_typed_match.description.empty()); |
| 391 | 397 |
| 392 // The instant search should be more relevant. | 398 // The instant search should be more relevant. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 418 | 424 |
| 419 // Provider should be done. | 425 // Provider should be done. |
| 420 EXPECT_TRUE(provider_->done()); | 426 EXPECT_TRUE(provider_->done()); |
| 421 | 427 |
| 422 // There should be two matches, one for what you typed, the other for | 428 // There should be two matches, one for what you typed, the other for |
| 423 // 'foobar'. | 429 // 'foobar'. |
| 424 EXPECT_EQ(2u, provider_->matches().size()); | 430 EXPECT_EQ(2u, provider_->matches().size()); |
| 425 instant_match = FindMatchWithDestination(instant_url); | 431 instant_match = FindMatchWithDestination(instant_url); |
| 426 EXPECT_FALSE(instant_match.destination_url.is_empty()); | 432 EXPECT_FALSE(instant_match.destination_url.is_empty()); |
| 427 | 433 |
| 428 // And the 'foobar' match should have a description. | 434 // And the 'foobar' match should not have a description, it'll be set later. |
| 429 EXPECT_FALSE(instant_match.description.empty()); | 435 EXPECT_TRUE(instant_match.description.empty()); |
| 430 } | 436 } |
| 431 | 437 |
| 432 // Make sure that if trailing whitespace is added to the text supplied to | 438 // Make sure that if trailing whitespace is added to the text supplied to |
| 433 // AutocompleteInput the default suggest text is cleared. | 439 // AutocompleteInput the default suggest text is cleared. |
| 434 TEST_F(SearchProviderTest, DifferingText) { | 440 TEST_F(SearchProviderTest, DifferingText) { |
| 435 PrefService* service = profile_.GetPrefs(); | 441 PrefService* service = profile_.GetPrefs(); |
| 436 service->SetBoolean(prefs::kInstantEnabled, true); | 442 service->SetBoolean(prefs::kInstantEnabled, true); |
| 437 | 443 |
| 438 QueryForInput(ASCIIToUTF16("foo"), false, false); | 444 QueryForInput(ASCIIToUTF16("foo"), false, false); |
| 439 | 445 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 532 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
| 527 *default_t_url_, ASCIIToUTF16("two "), 0, string16())); | 533 *default_t_url_, ASCIIToUTF16("two "), 0, string16())); |
| 528 AutocompleteMatch what_you_typed_match = | 534 AutocompleteMatch what_you_typed_match = |
| 529 FindMatchWithDestination(what_you_typed_url); | 535 FindMatchWithDestination(what_you_typed_url); |
| 530 EXPECT_FALSE(what_you_typed_match.destination_url.is_empty()); | 536 EXPECT_FALSE(what_you_typed_match.destination_url.is_empty()); |
| 531 // term_match should be autocompleted. | 537 // term_match should be autocompleted. |
| 532 EXPECT_GT(term_match.relevance, what_you_typed_match.relevance); | 538 EXPECT_GT(term_match.relevance, what_you_typed_match.relevance); |
| 533 // And the offset should be at 4. | 539 // And the offset should be at 4. |
| 534 EXPECT_EQ(4u, term_match.inline_autocomplete_offset); | 540 EXPECT_EQ(4u, term_match.inline_autocomplete_offset); |
| 535 } | 541 } |
| 542 |
| 543 // Verifies the SearchProvider sets descriptions for results correctly. |
| 544 TEST_F(SearchProviderTest, PostProcessResults) { |
| 545 // Add an entry that corresponds to a keyword search with 'term2'. |
| 546 string16 term(ASCIIToUTF16("term2")); |
| 547 HistoryService* history = |
| 548 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 549 GURL term_url(keyword_t_url_->url()->ReplaceSearchTerms( |
| 550 *keyword_t_url_, term, 0, string16())); |
| 551 history->AddPageWithDetails(term_url, string16(), 1, 1, |
| 552 base::Time::Now(), false, |
| 553 history::SOURCE_BROWSED); |
| 554 history->SetKeywordSearchTermsForURL(term_url, keyword_t_url_->id(), term); |
| 555 |
| 556 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 557 |
| 558 ACProviders providers; |
| 559 SearchProvider* provider = provider_.release(); |
| 560 providers.push_back(provider); |
| 561 AutocompleteController controller(providers); |
| 562 provider->set_listener(&controller); |
| 563 controller.Start(ASCIIToUTF16("k t"), string16(), false, false, true, |
| 564 AutocompleteInput::ALL_MATCHES); |
| 565 const AutocompleteResult& result = controller.result(); |
| 566 |
| 567 // There should be two matches, one for the keyword one for what you typed. |
| 568 ASSERT_EQ(2u, result.size()); |
| 569 |
| 570 EXPECT_TRUE(result.match_at(0).template_url != NULL); |
| 571 EXPECT_TRUE(result.match_at(1).template_url != NULL); |
| 572 EXPECT_NE(result.match_at(0).template_url, |
| 573 result.match_at(1).template_url); |
| 574 |
| 575 EXPECT_FALSE(result.match_at(0).description.empty()); |
| 576 EXPECT_FALSE(result.match_at(1).description.empty()); |
| 577 EXPECT_NE(result.match_at(0).description, |
| 578 result.match_at(1).description); |
| 579 } |
| OLD | NEW |