| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 void SyncWithHistory() override {} | 58 void SyncWithHistory() override {} |
| 58 bool HasBlacklistedItems() const override { | 59 bool HasBlacklistedItems() const override { |
| 59 return false; | 60 return false; |
| 60 } | 61 } |
| 61 void AddBlacklistedURL(const GURL& url) override {} | 62 void AddBlacklistedURL(const GURL& url) override {} |
| 62 void RemoveBlacklistedURL(const GURL& url) override {} | 63 void RemoveBlacklistedURL(const GURL& url) override {} |
| 63 bool IsBlacklisted(const GURL& url) override { | 64 bool IsBlacklisted(const GURL& url) override { |
| 64 return false; | 65 return false; |
| 65 } | 66 } |
| 66 void ClearBlacklistedURLs() override {} | 67 void ClearBlacklistedURLs() override {} |
| 67 void Shutdown() override {} | |
| 68 base::CancelableTaskTracker::TaskId StartQueryForMostVisited() override { | 68 base::CancelableTaskTracker::TaskId StartQueryForMostVisited() override { |
| 69 return 0; | 69 return 0; |
| 70 } | 70 } |
| 71 bool IsKnownURL(const GURL& url) override { | 71 bool IsKnownURL(const GURL& url) override { |
| 72 return false; | 72 return false; |
| 73 } | 73 } |
| 74 const std::string& GetCanonicalURLString(const GURL& url) const override { | 74 const std::string& GetCanonicalURLString(const GURL& url) const override { |
| 75 CHECK(false); | 75 CHECK(false); |
| 76 return *(new std::string()); | 76 return *(new std::string()); |
| 77 } | 77 } |
| 78 bool IsNonForcedFull() override { | 78 bool IsNonForcedFull() override { |
| 79 return false; | 79 return false; |
| 80 } | 80 } |
| 81 bool IsForcedFull() override { | 81 bool IsForcedFull() override { |
| 82 return false; | 82 return false; |
| 83 } | 83 } |
| 84 bool loaded() const override { | 84 bool loaded() const override { |
| 85 return false; | 85 return false; |
| 86 } | 86 } |
| 87 history::MostVisitedURLList GetPrepopulatePages() override { | 87 history::MostVisitedURLList GetPrepopulatePages() override { |
| 88 return history::MostVisitedURLList(); | 88 return history::MostVisitedURLList(); |
| 89 } | 89 } |
| 90 bool AddForcedURL(const GURL& url, const base::Time& time) override { | 90 bool AddForcedURL(const GURL& url, const base::Time& time) override { |
| 91 return false; | 91 return false; |
| 92 } | 92 } |
| 93 |
| 94 // RefcountedKeyedService: |
| 95 void ShutdownOnUIThread() override {} |
| 96 |
| 93 // content::NotificationObserver: | 97 // content::NotificationObserver: |
| 94 void Observe(int type, | 98 void Observe(int type, |
| 95 const content::NotificationSource& source, | 99 const content::NotificationSource& source, |
| 96 const content::NotificationDetails& details) override {} | 100 const content::NotificationDetails& details) override {} |
| 97 | 101 |
| 98 // A test-specific field for controlling when most visited callback is run | 102 // A test-specific field for controlling when most visited callback is run |
| 99 // after top sites have been requested. | 103 // after top sites have been requested. |
| 100 GetMostVisitedURLsCallback mv_callback; | 104 GetMostVisitedURLsCallback mv_callback; |
| 101 | 105 |
| 102 protected: | 106 protected: |
| 103 ~FakeEmptyTopSites() override {} | 107 ~FakeEmptyTopSites() override {} |
| 104 }; | 108 }; |
| 105 | 109 |
| 106 void FakeEmptyTopSites::GetMostVisitedURLs( | 110 void FakeEmptyTopSites::GetMostVisitedURLs( |
| 107 const GetMostVisitedURLsCallback& callback, | 111 const GetMostVisitedURLsCallback& callback, |
| 108 bool include_forced_urls) { | 112 bool include_forced_urls) { |
| 109 mv_callback = callback; | 113 mv_callback = callback; |
| 110 } | 114 } |
| 111 | 115 |
| 116 scoped_refptr<RefcountedKeyedService> BuildFakeEmptyTopSites( |
| 117 content::BrowserContext* profile) { |
| 118 scoped_refptr<history::TopSites> top_sites = new FakeEmptyTopSites(); |
| 119 return top_sites; |
| 120 } |
| 121 |
| 112 } // namespace | 122 } // namespace |
| 113 | 123 |
| 114 | 124 |
| 115 class ZeroSuggestProviderTest : public testing::Test, | 125 class ZeroSuggestProviderTest : public testing::Test, |
| 116 public AutocompleteProviderListener { | 126 public AutocompleteProviderListener { |
| 117 public: | 127 public: |
| 118 ZeroSuggestProviderTest(); | 128 ZeroSuggestProviderTest(); |
| 119 | 129 |
| 120 void SetUp() override; | 130 void SetUp() override; |
| 121 void TearDown() override; | 131 void TearDown() override; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 TemplateURLData data; | 178 TemplateURLData data; |
| 169 data.short_name = base::ASCIIToUTF16("t"); | 179 data.short_name = base::ASCIIToUTF16("t"); |
| 170 data.SetURL("https://www.google.com/?q={searchTerms}"); | 180 data.SetURL("https://www.google.com/?q={searchTerms}"); |
| 171 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}"; | 181 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}"; |
| 172 data.instant_url = "https://does/not/exist?strk=1"; | 182 data.instant_url = "https://does/not/exist?strk=1"; |
| 173 data.search_terms_replacement_key = "strk"; | 183 data.search_terms_replacement_key = "strk"; |
| 174 default_t_url_ = new TemplateURL(data); | 184 default_t_url_ = new TemplateURL(data); |
| 175 turl_model->Add(default_t_url_); | 185 turl_model->Add(default_t_url_); |
| 176 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); | 186 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); |
| 177 | 187 |
| 178 profile_.SetTopSites(new FakeEmptyTopSites()); | 188 profile_.DestroyTopSites(); |
| 179 | 189 TopSitesFactory::GetInstance()->SetTestingFactory(&profile_, |
| 190 BuildFakeEmptyTopSites); |
| 180 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); | 191 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); |
| 181 } | 192 } |
| 182 | 193 |
| 183 void ZeroSuggestProviderTest::TearDown() { | 194 void ZeroSuggestProviderTest::TearDown() { |
| 184 // Shutdown the provider before the profile. | 195 // Shutdown the provider before the profile. |
| 185 provider_ = NULL; | 196 provider_ = NULL; |
| 186 } | 197 } |
| 187 | 198 |
| 188 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) { | 199 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) { |
| 189 } | 200 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 metrics::OmniboxEventProto::OTHER, false, false, | 268 metrics::OmniboxEventProto::OTHER, false, false, |
| 258 true, true, | 269 true, true, |
| 259 ChromeAutocompleteSchemeClassifier(&profile_)); | 270 ChromeAutocompleteSchemeClassifier(&profile_)); |
| 260 history::MostVisitedURLList urls; | 271 history::MostVisitedURLList urls; |
| 261 history::MostVisitedURL url(GURL("http://foo.com/"), | 272 history::MostVisitedURL url(GURL("http://foo.com/"), |
| 262 base::ASCIIToUTF16("Foo")); | 273 base::ASCIIToUTF16("Foo")); |
| 263 urls.push_back(url); | 274 urls.push_back(url); |
| 264 | 275 |
| 265 provider_->Start(input, false, true); | 276 provider_->Start(input, false, true); |
| 266 EXPECT_TRUE(provider_->matches().empty()); | 277 EXPECT_TRUE(provider_->matches().empty()); |
| 267 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 278 scoped_refptr<history::TopSites> top_sites = |
| 268 urls); | 279 TopSitesFactory::GetForProfile(&profile_); |
| 280 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
| 269 // Should have verbatim match + most visited url match. | 281 // Should have verbatim match + most visited url match. |
| 270 EXPECT_EQ(2U, provider_->matches().size()); | 282 EXPECT_EQ(2U, provider_->matches().size()); |
| 271 provider_->Stop(false); | 283 provider_->Stop(false); |
| 272 | 284 |
| 273 provider_->Start(input, false, true); | 285 provider_->Start(input, false, true); |
| 274 provider_->Stop(false); | 286 provider_->Stop(false); |
| 275 EXPECT_TRUE(provider_->matches().empty()); | 287 EXPECT_TRUE(provider_->matches().empty()); |
| 276 // Most visited results arriving after Stop() has been called, ensure they | 288 // Most visited results arriving after Stop() has been called, ensure they |
| 277 // are not displayed. | 289 // are not displayed. |
| 278 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 290 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
| 279 urls); | |
| 280 EXPECT_TRUE(provider_->matches().empty()); | 291 EXPECT_TRUE(provider_->matches().empty()); |
| 281 } | 292 } |
| 282 | 293 |
| 283 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { | 294 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { |
| 284 CreateMostVisitedFieldTrial(); | 295 CreateMostVisitedFieldTrial(); |
| 285 | 296 |
| 286 std::string current_url("http://www.foxnews.com/"); | 297 std::string current_url("http://www.foxnews.com/"); |
| 287 std::string input_url("http://www.cnn.com/"); | 298 std::string input_url("http://www.cnn.com/"); |
| 288 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, | 299 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, |
| 289 std::string(), GURL(current_url), | 300 std::string(), GURL(current_url), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 309 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT, | 320 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT, |
| 310 false, | 321 false, |
| 311 false, | 322 false, |
| 312 true, | 323 true, |
| 313 true, | 324 true, |
| 314 ChromeAutocompleteSchemeClassifier(&profile_)); | 325 ChromeAutocompleteSchemeClassifier(&profile_)); |
| 315 | 326 |
| 316 provider_->Start(srp_input, false, true); | 327 provider_->Start(srp_input, false, true); |
| 317 EXPECT_TRUE(provider_->matches().empty()); | 328 EXPECT_TRUE(provider_->matches().empty()); |
| 318 // Most visited results arriving after a new request has been started. | 329 // Most visited results arriving after a new request has been started. |
| 319 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 330 scoped_refptr<history::TopSites> top_sites = |
| 320 urls); | 331 TopSitesFactory::GetForProfile(&profile_); |
| 332 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
| 321 EXPECT_TRUE(provider_->matches().empty()); | 333 EXPECT_TRUE(provider_->matches().empty()); |
| 322 } | 334 } |
| 323 | 335 |
| 324 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { | 336 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { |
| 325 CreatePersonalizedFieldTrial(); | 337 CreatePersonalizedFieldTrial(); |
| 326 | 338 |
| 327 // Ensure the cache is empty. | 339 // Ensure the cache is empty. |
| 328 PrefService* prefs = profile_.GetPrefs(); | 340 PrefService* prefs = profile_.GetPrefs(); |
| 329 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); | 341 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); |
| 330 | 342 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 | 448 |
| 437 base::RunLoop().RunUntilIdle(); | 449 base::RunLoop().RunUntilIdle(); |
| 438 | 450 |
| 439 // Expect that the matches have been cleared. | 451 // Expect that the matches have been cleared. |
| 440 ASSERT_TRUE(provider_->matches().empty()); | 452 ASSERT_TRUE(provider_->matches().empty()); |
| 441 | 453 |
| 442 // Expect the new results have been stored. | 454 // Expect the new results have been stored. |
| 443 EXPECT_EQ(empty_response, | 455 EXPECT_EQ(empty_response, |
| 444 prefs->GetString(prefs::kZeroSuggestCachedResults)); | 456 prefs->GetString(prefs::kZeroSuggestCachedResults)); |
| 445 } | 457 } |
| OLD | NEW |