Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: chrome/browser/autocomplete/zero_suggest_provider_unittest.cc

Issue 815983002: Topsites become keyedService based. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased and fixed unit test fail Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 void ShutdownOnUIThread() override {}
sdefresne 2015/01/22 19:05:05 style: add a comment before this method saying //
Jitu( very slow this week) 2015/01/23 11:36:52 Done.
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
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
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/22 19:05:05 Please add a call to "profile_.DestroyTopSites();"
Jitu( very slow this week) 2015/01/23 11:36:52 Done.
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
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
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
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698