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