| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 19 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 20 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 20 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 21 #include "chrome/browser/autocomplete/autocomplete_input.h" | 21 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 22 #include "chrome/browser/autocomplete/autocomplete_match.h" | 22 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 23 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 23 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 24 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 24 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 25 #include "chrome/browser/autocomplete/base_search_provider.h" |
| 25 #include "chrome/browser/autocomplete/history_url_provider.h" | 26 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 26 #include "chrome/browser/history/history_service.h" | 27 #include "chrome/browser/history/history_service.h" |
| 27 #include "chrome/browser/history/history_service_factory.h" | 28 #include "chrome/browser/history/history_service_factory.h" |
| 28 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 29 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 29 #include "chrome/browser/search_engines/search_engine_type.h" | 30 #include "chrome/browser/search_engines/search_engine_type.h" |
| 30 #include "chrome/browser/search_engines/template_url.h" | 31 #include "chrome/browser/search_engines/template_url.h" |
| 31 #include "chrome/browser/search_engines/template_url_service.h" | 32 #include "chrome/browser/search_engines/template_url_service.h" |
| 32 #include "chrome/browser/search_engines/template_url_service_factory.h" | 33 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 33 #include "chrome/browser/signin/signin_manager.h" | 34 #include "chrome/browser/signin/signin_manager.h" |
| 34 #include "chrome/browser/signin/signin_manager_factory.h" | 35 #include "chrome/browser/signin/signin_manager_factory.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 148 |
| 148 protected: | 149 protected: |
| 149 // Needed for AutocompleteFieldTrial::ActivateStaticTrials(); | 150 // Needed for AutocompleteFieldTrial::ActivateStaticTrials(); |
| 150 scoped_ptr<base::FieldTrialList> field_trial_list_; | 151 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 151 | 152 |
| 152 // Default value used for testing. | 153 // Default value used for testing. |
| 153 static const std::string kNotApplicable; | 154 static const std::string kNotApplicable; |
| 154 | 155 |
| 155 // Adds a search for |term|, using the engine |t_url| to the history, and | 156 // Adds a search for |term|, using the engine |t_url| to the history, and |
| 156 // returns the URL for that search. | 157 // returns the URL for that search. |
| 157 GURL AddSearchToHistory(TemplateURL* t_url, base::string16 term, int visit_cou
nt); | 158 GURL AddSearchToHistory(TemplateURL* t_url, |
| 159 base::string16 term, |
| 160 int visit_count); |
| 158 | 161 |
| 159 // Looks for a match in |provider_| with |contents| equal to |contents|. | 162 // Looks for a match in |provider_| with |contents| equal to |contents|. |
| 160 // Sets |match| to it if found. Returns whether |match| was set. | 163 // Sets |match| to it if found. Returns whether |match| was set. |
| 161 bool FindMatchWithContents(const base::string16& contents, | 164 bool FindMatchWithContents(const base::string16& contents, |
| 162 AutocompleteMatch* match); | 165 AutocompleteMatch* match); |
| 163 | 166 |
| 164 // Looks for a match in |provider_| with destination |url|. Sets |match| to | 167 // Looks for a match in |provider_| with destination |url|. Sets |match| to |
| 165 // it if found. Returns whether |match| was set. | 168 // it if found. Returns whether |match| was set. |
| 166 bool FindMatchWithDestination(const GURL& url, AutocompleteMatch* match); | 169 bool FindMatchWithDestination(const GURL& url, AutocompleteMatch* match); |
| 167 | 170 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 data.SetURL("http://keyword/{searchTerms}"); | 269 data.SetURL("http://keyword/{searchTerms}"); |
| 267 data.suggestions_url = "http://suggest_keyword/{searchTerms}"; | 270 data.suggestions_url = "http://suggest_keyword/{searchTerms}"; |
| 268 keyword_t_url_ = new TemplateURL(&profile_, data); | 271 keyword_t_url_ = new TemplateURL(&profile_, data); |
| 269 turl_model->Add(keyword_t_url_); | 272 turl_model->Add(keyword_t_url_); |
| 270 ASSERT_NE(0, keyword_t_url_->id()); | 273 ASSERT_NE(0, keyword_t_url_->id()); |
| 271 | 274 |
| 272 // Add a page and search term for keyword_t_url_. | 275 // Add a page and search term for keyword_t_url_. |
| 273 keyword_url_ = AddSearchToHistory(keyword_t_url_, keyword_term_, 1); | 276 keyword_url_ = AddSearchToHistory(keyword_t_url_, keyword_term_, 1); |
| 274 | 277 |
| 275 // Keywords are updated by the InMemoryHistoryBackend only after the message | 278 // Keywords are updated by the InMemoryHistoryBackend only after the message |
| 276 // has been processed on the history thread. Block until history processes all | 279 // has been processed on the history thread. Block until history processes |
| 277 // requests to ensure the InMemoryDatabase is the state we expect it. | 280 // all requests to ensure the InMemoryDatabase is the state we expect it. |
| 278 profile_.BlockUntilHistoryProcessesPendingRequests(); | 281 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 279 | 282 |
| 280 provider_ = new SearchProviderForTest(this, &profile_); | 283 provider_ = new SearchProviderForTest(this, &profile_); |
| 281 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; | 284 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; |
| 282 } | 285 } |
| 283 | 286 |
| 284 void SearchProviderTest::TearDown() { | 287 void SearchProviderTest::TearDown() { |
| 285 base::RunLoop().RunUntilIdle(); | 288 base::RunLoop().RunUntilIdle(); |
| 286 | 289 |
| 287 // Shutdown the provider before the profile. | 290 // Shutdown the provider before the profile. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 base::string16 term, | 374 base::string16 term, |
| 372 int visit_count) { | 375 int visit_count) { |
| 373 HistoryService* history = | 376 HistoryService* history = |
| 374 HistoryServiceFactory::GetForProfile(&profile_, | 377 HistoryServiceFactory::GetForProfile(&profile_, |
| 375 Profile::EXPLICIT_ACCESS); | 378 Profile::EXPLICIT_ACCESS); |
| 376 GURL search(t_url->url_ref().ReplaceSearchTerms( | 379 GURL search(t_url->url_ref().ReplaceSearchTerms( |
| 377 TemplateURLRef::SearchTermsArgs(term))); | 380 TemplateURLRef::SearchTermsArgs(term))); |
| 378 static base::Time last_added_time; | 381 static base::Time last_added_time; |
| 379 last_added_time = std::max(base::Time::Now(), | 382 last_added_time = std::max(base::Time::Now(), |
| 380 last_added_time + base::TimeDelta::FromMicroseconds(1)); | 383 last_added_time + base::TimeDelta::FromMicroseconds(1)); |
| 381 history->AddPageWithDetails(search, base::string16(), visit_count, visit_count
, | 384 history->AddPageWithDetails(search, |
| 382 last_added_time, false, history::SOURCE_BROWSED); | 385 base::string16(), |
| 386 visit_count, |
| 387 visit_count, |
| 388 last_added_time, |
| 389 false, |
| 390 history::SOURCE_BROWSED); |
| 383 history->SetKeywordSearchTermsForURL(search, t_url->id(), term); | 391 history->SetKeywordSearchTermsForURL(search, t_url->id(), term); |
| 384 return search; | 392 return search; |
| 385 } | 393 } |
| 386 | 394 |
| 387 bool SearchProviderTest::FindMatchWithContents(const base::string16& contents, | 395 bool SearchProviderTest::FindMatchWithContents(const base::string16& contents, |
| 388 AutocompleteMatch* match) { | 396 AutocompleteMatch* match) { |
| 389 for (ACMatches::const_iterator i = provider_->matches().begin(); | 397 for (ACMatches::const_iterator i = provider_->matches().begin(); |
| 390 i != provider_->matches().end(); ++i) { | 398 i != provider_->matches().end(); ++i) { |
| 391 if (i->contents == contents) { | 399 if (i->contents == contents) { |
| 392 *match = *i; | 400 *match = *i; |
| (...skipping 3259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3652 ASSERT_FALSE(matches.empty()); | 3660 ASSERT_FALSE(matches.empty()); |
| 3653 EXPECT_GE(matches[0].relevance, 1300); | 3661 EXPECT_GE(matches[0].relevance, 1300); |
| 3654 | 3662 |
| 3655 ASSERT_LE(matches.size(), ARRAYSIZE_UNSAFE(cases[i].matches)); | 3663 ASSERT_LE(matches.size(), ARRAYSIZE_UNSAFE(cases[i].matches)); |
| 3656 // Ensure that the returned matches equal the expectations. | 3664 // Ensure that the returned matches equal the expectations. |
| 3657 for (size_t j = 0; j < matches.size(); ++j) { | 3665 for (size_t j = 0; j < matches.size(); ++j) { |
| 3658 SCOPED_TRACE(description); | 3666 SCOPED_TRACE(description); |
| 3659 EXPECT_EQ(cases[i].matches[j].contents, | 3667 EXPECT_EQ(cases[i].matches[j].contents, |
| 3660 base::UTF16ToUTF8(matches[j].contents)); | 3668 base::UTF16ToUTF8(matches[j].contents)); |
| 3661 EXPECT_EQ(cases[i].matches[j].allowed_to_be_prefetched, | 3669 EXPECT_EQ(cases[i].matches[j].allowed_to_be_prefetched, |
| 3662 SearchProvider::ShouldPrefetch(matches[j])); | 3670 BaseSearchProvider::ShouldPrefetch(matches[j])); |
| 3663 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); | 3671 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); |
| 3664 EXPECT_EQ(cases[i].matches[j].from_keyword, | 3672 EXPECT_EQ(cases[i].matches[j].from_keyword, |
| 3665 matches[j].keyword == ASCIIToUTF16("k")); | 3673 matches[j].keyword == ASCIIToUTF16("k")); |
| 3666 } | 3674 } |
| 3667 } | 3675 } |
| 3668 } | 3676 } |
| 3669 | 3677 |
| 3670 TEST_F(SearchProviderTest, XSSIGuardedJSONParsing_InvalidResponse) { | 3678 TEST_F(SearchProviderTest, XSSIGuardedJSONParsing_InvalidResponse) { |
| 3671 ClearAllResults(); | 3679 ClearAllResults(); |
| 3672 | 3680 |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4032 EXPECT_FALSE(provider_->deletion_handlers_.empty()); | 4040 EXPECT_FALSE(provider_->deletion_handlers_.empty()); |
| 4033 // Set up a default fetcher with provided results. | 4041 // Set up a default fetcher with provided results. |
| 4034 fetcher = test_factory_.GetFetcherByID( | 4042 fetcher = test_factory_.GetFetcherByID( |
| 4035 SearchProvider::kDeletionURLFetcherID); | 4043 SearchProvider::kDeletionURLFetcherID); |
| 4036 ASSERT_TRUE(fetcher); | 4044 ASSERT_TRUE(fetcher); |
| 4037 fetcher->set_response_code(500); | 4045 fetcher->set_response_code(500); |
| 4038 fetcher->delegate()->OnURLFetchComplete(fetcher); | 4046 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 4039 EXPECT_TRUE(provider_->deletion_handlers_.empty()); | 4047 EXPECT_TRUE(provider_->deletion_handlers_.empty()); |
| 4040 EXPECT_FALSE(provider_->is_success()); | 4048 EXPECT_FALSE(provider_->is_success()); |
| 4041 } | 4049 } |
| OLD | NEW |