Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/zero_suggest_provider.h" | 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
| 6 | 6 |
| 7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 11 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 12 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 12 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
| 13 #include "chrome/browser/history/top_sites.h" | 13 #include "chrome/browser/history/top_sites.h" |
| 14 #include "chrome/browser/history/top_sites_factory.h" | |
| 14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/metrics/proto/omnibox_event.pb.h" | 18 #include "components/metrics/proto/omnibox_event.pb.h" |
| 18 #include "components/omnibox/autocomplete_provider_listener.h" | 19 #include "components/omnibox/autocomplete_provider_listener.h" |
| 19 #include "components/omnibox/omnibox_field_trial.h" | 20 #include "components/omnibox/omnibox_field_trial.h" |
| 20 #include "components/search_engines/template_url.h" | 21 #include "components/search_engines/template_url.h" |
| 21 #include "components/search_engines/template_url_service.h" | 22 #include "components/search_engines/template_url_service.h" |
| 22 #include "components/variations/entropy_provider.h" | 23 #include "components/variations/entropy_provider.h" |
| 23 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 history::MostVisitedURLList GetPrepopulatePages() override { | 88 history::MostVisitedURLList GetPrepopulatePages() override { |
| 88 return history::MostVisitedURLList(); | 89 return history::MostVisitedURLList(); |
| 89 } | 90 } |
| 90 bool AddForcedURL(const GURL& url, const base::Time& time) override { | 91 bool AddForcedURL(const GURL& url, const base::Time& time) override { |
| 91 return false; | 92 return false; |
| 92 } | 93 } |
| 93 // content::NotificationObserver: | 94 // content::NotificationObserver: |
| 94 void Observe(int type, | 95 void Observe(int type, |
| 95 const content::NotificationSource& source, | 96 const content::NotificationSource& source, |
| 96 const content::NotificationDetails& details) override {} | 97 const content::NotificationDetails& details) override {} |
| 97 | 98 |
|
sdefresne
2015/01/21 17:57:23
You need to override "void ShutdownOnUIThread()" i
| |
| 98 // A test-specific field for controlling when most visited callback is run | 99 // A test-specific field for controlling when most visited callback is run |
| 99 // after top sites have been requested. | 100 // after top sites have been requested. |
| 100 GetMostVisitedURLsCallback mv_callback; | 101 GetMostVisitedURLsCallback mv_callback; |
| 101 | 102 |
| 102 protected: | 103 protected: |
| 103 ~FakeEmptyTopSites() override {} | 104 ~FakeEmptyTopSites() override {} |
| 104 }; | 105 }; |
| 105 | 106 |
| 106 void FakeEmptyTopSites::GetMostVisitedURLs( | 107 void FakeEmptyTopSites::GetMostVisitedURLs( |
| 107 const GetMostVisitedURLsCallback& callback, | 108 const GetMostVisitedURLsCallback& callback, |
| 108 bool include_forced_urls) { | 109 bool include_forced_urls) { |
| 109 mv_callback = callback; | 110 mv_callback = callback; |
| 110 } | 111 } |
| 111 | 112 |
| 113 scoped_refptr<RefcountedKeyedService> BuildFakeEmptyTopSites( | |
| 114 content::BrowserContext* profile) { | |
| 115 scoped_refptr<history::TopSites> top_sites = new FakeEmptyTopSites(); | |
| 116 return top_sites; | |
| 117 } | |
| 118 | |
| 112 } // namespace | 119 } // namespace |
| 113 | 120 |
| 114 | 121 |
| 115 class ZeroSuggestProviderTest : public testing::Test, | 122 class ZeroSuggestProviderTest : public testing::Test, |
| 116 public AutocompleteProviderListener { | 123 public AutocompleteProviderListener { |
| 117 public: | 124 public: |
| 118 ZeroSuggestProviderTest(); | 125 ZeroSuggestProviderTest(); |
| 119 | 126 |
| 120 void SetUp() override; | 127 void SetUp() override; |
| 121 void TearDown() override; | 128 void TearDown() override; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 TemplateURLData data; | 175 TemplateURLData data; |
| 169 data.short_name = base::ASCIIToUTF16("t"); | 176 data.short_name = base::ASCIIToUTF16("t"); |
| 170 data.SetURL("https://www.google.com/?q={searchTerms}"); | 177 data.SetURL("https://www.google.com/?q={searchTerms}"); |
| 171 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}"; | 178 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}"; |
| 172 data.instant_url = "https://does/not/exist?strk=1"; | 179 data.instant_url = "https://does/not/exist?strk=1"; |
| 173 data.search_terms_replacement_key = "strk"; | 180 data.search_terms_replacement_key = "strk"; |
| 174 default_t_url_ = new TemplateURL(data); | 181 default_t_url_ = new TemplateURL(data); |
| 175 turl_model->Add(default_t_url_); | 182 turl_model->Add(default_t_url_); |
| 176 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); | 183 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); |
| 177 | 184 |
| 178 profile_.SetTopSites(new FakeEmptyTopSites()); | 185 TopSitesFactory::GetInstance()->SetTestingFactory(profile_, |
|
sdefresne
2015/01/21 17:57:23
You need to use "&profile_" here and below since "
| |
| 179 | 186 BuildFakeEmptyTopSites); |
| 180 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); | 187 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); |
| 181 } | 188 } |
| 182 | 189 |
| 183 void ZeroSuggestProviderTest::TearDown() { | 190 void ZeroSuggestProviderTest::TearDown() { |
| 184 // Shutdown the provider before the profile. | 191 // Shutdown the provider before the profile. |
| 185 provider_ = NULL; | 192 provider_ = NULL; |
| 186 } | 193 } |
| 187 | 194 |
| 188 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) { | 195 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) { |
| 189 } | 196 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 metrics::OmniboxEventProto::OTHER, false, false, | 264 metrics::OmniboxEventProto::OTHER, false, false, |
| 258 true, true, | 265 true, true, |
| 259 ChromeAutocompleteSchemeClassifier(&profile_)); | 266 ChromeAutocompleteSchemeClassifier(&profile_)); |
| 260 history::MostVisitedURLList urls; | 267 history::MostVisitedURLList urls; |
| 261 history::MostVisitedURL url(GURL("http://foo.com/"), | 268 history::MostVisitedURL url(GURL("http://foo.com/"), |
| 262 base::ASCIIToUTF16("Foo")); | 269 base::ASCIIToUTF16("Foo")); |
| 263 urls.push_back(url); | 270 urls.push_back(url); |
| 264 | 271 |
| 265 provider_->Start(input, false, true); | 272 provider_->Start(input, false, true); |
| 266 EXPECT_TRUE(provider_->matches().empty()); | 273 EXPECT_TRUE(provider_->matches().empty()); |
| 267 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 274 scoped_refptr<history::TopSites> top_sites = |
| 268 urls); | 275 TopSitesFactory::GetForProfile(profile_); |
| 276 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); | |
| 269 // Should have verbatim match + most visited url match. | 277 // Should have verbatim match + most visited url match. |
| 270 EXPECT_EQ(2U, provider_->matches().size()); | 278 EXPECT_EQ(2U, provider_->matches().size()); |
| 271 provider_->Stop(false); | 279 provider_->Stop(false); |
| 272 | 280 |
| 273 provider_->Start(input, false, true); | 281 provider_->Start(input, false, true); |
| 274 provider_->Stop(false); | 282 provider_->Stop(false); |
| 275 EXPECT_TRUE(provider_->matches().empty()); | 283 EXPECT_TRUE(provider_->matches().empty()); |
| 276 // Most visited results arriving after Stop() has been called, ensure they | 284 // Most visited results arriving after Stop() has been called, ensure they |
| 277 // are not displayed. | 285 // are not displayed. |
| 278 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 286 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
| 279 urls); | |
| 280 EXPECT_TRUE(provider_->matches().empty()); | 287 EXPECT_TRUE(provider_->matches().empty()); |
| 281 } | 288 } |
| 282 | 289 |
| 283 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { | 290 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { |
| 284 CreateMostVisitedFieldTrial(); | 291 CreateMostVisitedFieldTrial(); |
| 285 | 292 |
| 286 std::string current_url("http://www.foxnews.com/"); | 293 std::string current_url("http://www.foxnews.com/"); |
| 287 std::string input_url("http://www.cnn.com/"); | 294 std::string input_url("http://www.cnn.com/"); |
| 288 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, | 295 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, |
| 289 std::string(), GURL(current_url), | 296 std::string(), GURL(current_url), |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 309 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT, | 316 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT, |
| 310 false, | 317 false, |
| 311 false, | 318 false, |
| 312 true, | 319 true, |
| 313 true, | 320 true, |
| 314 ChromeAutocompleteSchemeClassifier(&profile_)); | 321 ChromeAutocompleteSchemeClassifier(&profile_)); |
| 315 | 322 |
| 316 provider_->Start(srp_input, false, true); | 323 provider_->Start(srp_input, false, true); |
| 317 EXPECT_TRUE(provider_->matches().empty()); | 324 EXPECT_TRUE(provider_->matches().empty()); |
| 318 // Most visited results arriving after a new request has been started. | 325 // Most visited results arriving after a new request has been started. |
| 319 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 326 scoped_refptr<history::TopSites> top_sites = |
| 320 urls); | 327 TopSitesFactory::GetForProfile(profile_); |
| 328 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); | |
| 321 EXPECT_TRUE(provider_->matches().empty()); | 329 EXPECT_TRUE(provider_->matches().empty()); |
| 322 } | 330 } |
| 323 | 331 |
| 324 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { | 332 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { |
| 325 CreatePersonalizedFieldTrial(); | 333 CreatePersonalizedFieldTrial(); |
| 326 | 334 |
| 327 // Ensure the cache is empty. | 335 // Ensure the cache is empty. |
| 328 PrefService* prefs = profile_.GetPrefs(); | 336 PrefService* prefs = profile_.GetPrefs(); |
| 329 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); | 337 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); |
| 330 | 338 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 436 | 444 |
| 437 base::RunLoop().RunUntilIdle(); | 445 base::RunLoop().RunUntilIdle(); |
| 438 | 446 |
| 439 // Expect that the matches have been cleared. | 447 // Expect that the matches have been cleared. |
| 440 ASSERT_TRUE(provider_->matches().empty()); | 448 ASSERT_TRUE(provider_->matches().empty()); |
| 441 | 449 |
| 442 // Expect the new results have been stored. | 450 // Expect the new results have been stored. |
| 443 EXPECT_EQ(empty_response, | 451 EXPECT_EQ(empty_response, |
| 444 prefs->GetString(prefs::kZeroSuggestCachedResults)); | 452 prefs->GetString(prefs::kZeroSuggestCachedResults)); |
| 445 } | 453 } |
| OLD | NEW |