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

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

Issue 671593002: Do not delay most visited results. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit test, addressed comments Created 6 years, 2 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/search_engines/template_url_service_factory.h" 14 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
16 #include "components/metrics/proto/omnibox_event.pb.h" 17 #include "components/metrics/proto/omnibox_event.pb.h"
17 #include "components/omnibox/autocomplete_provider_listener.h" 18 #include "components/omnibox/autocomplete_provider_listener.h"
18 #include "components/omnibox/omnibox_field_trial.h" 19 #include "components/omnibox/omnibox_field_trial.h"
19 #include "components/search_engines/template_url.h" 20 #include "components/search_engines/template_url.h"
20 #include "components/search_engines/template_url_service.h" 21 #include "components/search_engines/template_url_service.h"
21 #include "components/variations/entropy_provider.h" 22 #include "components/variations/entropy_provider.h"
22 #include "components/variations/variations_associated_data.h" 23 #include "components/variations/variations_associated_data.h"
23 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "net/url_request/test_url_fetcher_factory.h" 25 #include "net/url_request/test_url_fetcher_factory.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
28 namespace {
29 class FakeEmptyTopSites : public history::TopSites {
30 public:
31 FakeEmptyTopSites() {
32 }
33
34 // history::TopSites:
35 bool SetPageThumbnail(const GURL& url, const gfx::Image& thumbnail,
36 const ThumbnailScore& score) override {
37 return false;
38 }
39 bool SetPageThumbnailToJPEGBytes(const GURL& url,
40 const base::RefCountedMemory* memory,
41 const ThumbnailScore& score) override {
42 return false;
43 }
44 void GetMostVisitedURLs(const GetMostVisitedURLsCallback& callback,
45 bool include_forced_urls) override;
46 bool GetPageThumbnail(const GURL& url, bool prefix_match,
47 scoped_refptr<base::RefCountedMemory>* bytes) override {
48 return false;
49 }
50 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override {
51 return false;
52 }
53 bool GetTemporaryPageThumbnailScore(const GURL& url, ThumbnailScore* score)
54 override {
55 return false;
56 }
57 void SyncWithHistory() override {}
58 bool HasBlacklistedItems() const override {
59 return false;
60 }
61 void AddBlacklistedURL(const GURL& url) override {}
62 void RemoveBlacklistedURL(const GURL& url) override {}
63 bool IsBlacklisted(const GURL& url) override {
64 return false;
65 }
66 void ClearBlacklistedURLs() override {}
67 void Shutdown() override {}
68 base::CancelableTaskTracker::TaskId StartQueryForMostVisited() override {
69 return 0;
70 }
71 bool IsKnownURL(const GURL& url) override {
72 return false;
73 }
74 const std::string& GetCanonicalURLString(const GURL& url) const override {
75 return *(new std::string());
Mark P 2014/10/23 21:33:52 Can you CHECK(false) here to make sure this is nev
Maria 2014/10/23 21:53:20 Done.
76 }
77 bool IsNonForcedFull() override {
78 return false;
79 }
80 bool IsForcedFull() override {
81 return false;
82 }
83 bool loaded() const override {
84 return false;
85 }
86 history::MostVisitedURLList GetPrepopulatePages() override {
87 return history::MostVisitedURLList();
88 }
89 bool AddForcedURL(const GURL& url, const base::Time& time) override {
90 return false;
91 }
92 // content::NotificationObserver:
93 void Observe(int type,
94 const content::NotificationSource& source,
95 const content::NotificationDetails& details) override {}
96
97 // A test-specific field for controlling when most visited callback is run
98 // after top sites have been requested.
99 GetMostVisitedURLsCallback mv_callback;
100 protected:
101 virtual ~FakeEmptyTopSites() {
102 }
103 };
104
105 void FakeEmptyTopSites::GetMostVisitedURLs(
106 const GetMostVisitedURLsCallback& callback,
107 bool include_forced_urls) {
108 mv_callback = callback;
109 }
110
111 } // namespace
112
113
27 class ZeroSuggestProviderTest : public testing::Test, 114 class ZeroSuggestProviderTest : public testing::Test,
28 public AutocompleteProviderListener { 115 public AutocompleteProviderListener {
29 public: 116 public:
30 ZeroSuggestProviderTest(); 117 ZeroSuggestProviderTest();
31 118
32 virtual void SetUp() override; 119 virtual void SetUp() override;
33 virtual void TearDown() override; 120 virtual void TearDown() override;
34 121
35 protected: 122 protected:
36 // AutocompleteProviderListener: 123 // AutocompleteProviderListener:
37 void OnProviderUpdate(bool updated_matches) override; 124 void OnProviderUpdate(bool updated_matches) override;
38 125
39 void ResetFieldTrialList(); 126 void ResetFieldTrialList();
40 127
41 void CreatePersonalizedFieldTrial(); 128 void CreatePersonalizedFieldTrial();
129 void CreateMostVisitedFieldTrial();
42 130
43 // Set up threads for testing; this needs to be instantiated before 131 // Set up threads for testing; this needs to be instantiated before
44 // |profile_|. 132 // |profile_|.
45 content::TestBrowserThreadBundle thread_bundle_; 133 content::TestBrowserThreadBundle thread_bundle_;
46 134
47 // Needed for OmniboxFieldTrial::ActivateStaticTrials(). 135 // Needed for OmniboxFieldTrial::ActivateStaticTrials().
48 scoped_ptr<base::FieldTrialList> field_trial_list_; 136 scoped_ptr<base::FieldTrialList> field_trial_list_;
49 137
50 // URLFetcherFactory implementation registered. 138 // URLFetcherFactory implementation registered.
51 net::TestURLFetcherFactory test_factory_; 139 net::TestURLFetcherFactory test_factory_;
(...skipping 27 matching lines...) Expand all
79 TemplateURLData data; 167 TemplateURLData data;
80 data.short_name = base::ASCIIToUTF16("t"); 168 data.short_name = base::ASCIIToUTF16("t");
81 data.SetURL("https://www.google.com/?q={searchTerms}"); 169 data.SetURL("https://www.google.com/?q={searchTerms}");
82 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}"; 170 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}";
83 data.instant_url = "https://does/not/exist?strk=1"; 171 data.instant_url = "https://does/not/exist?strk=1";
84 data.search_terms_replacement_key = "strk"; 172 data.search_terms_replacement_key = "strk";
85 default_t_url_ = new TemplateURL(data); 173 default_t_url_ = new TemplateURL(data);
86 turl_model->Add(default_t_url_); 174 turl_model->Add(default_t_url_);
87 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); 175 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_);
88 176
177 profile_.SetTopSites(new FakeEmptyTopSites());
178
89 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); 179 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_);
90 } 180 }
91 181
92 void ZeroSuggestProviderTest::TearDown() { 182 void ZeroSuggestProviderTest::TearDown() {
93 // Shutdown the provider before the profile. 183 // Shutdown the provider before the profile.
94 provider_ = NULL; 184 provider_ = NULL;
95 } 185 }
96 186
97 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) { 187 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) {
98 } 188 }
(...skipping 11 matching lines...) Expand all
110 std::map<std::string, std::string> params; 200 std::map<std::string, std::string> params;
111 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true"; 201 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true";
112 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] = 202 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] =
113 "Personalized"; 203 "Personalized";
114 variations::AssociateVariationParams( 204 variations::AssociateVariationParams(
115 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params); 205 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params);
116 base::FieldTrialList::CreateFieldTrial( 206 base::FieldTrialList::CreateFieldTrial(
117 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); 207 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A");
118 } 208 }
119 209
210 void ZeroSuggestProviderTest::CreateMostVisitedFieldTrial() {
211 std::map<std::string, std::string> params;
212 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true";
213 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] =
214 "MostVisitedWithoutSERP";
215 variations::AssociateVariationParams(
216 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params);
217 base::FieldTrialList::CreateFieldTrial(
218 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A");
219 }
220
221 TEST_F(ZeroSuggestProviderTest, TestMostVisitedCallback) {
222 CreateMostVisitedFieldTrial();
223
224 std::string current_url("http://www.foxnews.com/");
225 std::string input_url("http://www.cnn.com/");
226 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos,
227 std::string(), GURL(current_url),
228 metrics::OmniboxEventProto::OTHER, false, false,
229 true, true,
230 ChromeAutocompleteSchemeClassifier(&profile_));
231 history::MostVisitedURLList urls;
232 history::MostVisitedURL url(GURL("http://foo.com/"),
233 base::ASCIIToUTF16(std::string("Foo")));
234 urls.push_back(url);
235
236 provider_->Start(input, false);
237 EXPECT_TRUE(provider_->matches().empty());
238 ((FakeEmptyTopSites*) profile_.GetTopSites())->mv_callback.Run(urls);
239 // Should have verbatim match + most visited url match.
240 EXPECT_EQ(2U, provider_->matches().size());
241 provider_->Stop(false);
242
243 provider_->Start(input, false);
244 provider_->Stop(false);
245 EXPECT_TRUE(provider_->matches().empty());
246 // Most visited results arriving after Stop() has been called, ensure they
247 // are not displayed.
248 ((FakeEmptyTopSites*) profile_.GetTopSites())->mv_callback.Run(urls);
249 EXPECT_TRUE(provider_->matches().empty());
250 }
251
252 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) {
253 CreateMostVisitedFieldTrial();
254
255 std::string current_url("http://www.foxnews.com/");
256 std::string input_url("http://www.cnn.com/");
257 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos,
258 std::string(), GURL(current_url),
259 metrics::OmniboxEventProto::OTHER, false, false,
260 true, true,
261 ChromeAutocompleteSchemeClassifier(&profile_));
262 history::MostVisitedURLList urls;
263 history::MostVisitedURL url(GURL("http://foo.com/"),
264 base::ASCIIToUTF16(std::string("Foo")));
265 urls.push_back(url);
266
267 provider_->Start(input, false);
268 EXPECT_TRUE(provider_->matches().empty());
269 // Stop() doesn't always get called.
270
271 std::string search_url("https://www.google.com/?q=flowers");
272 AutocompleteInput srp_input(
273 base::ASCIIToUTF16(search_url),
274 base::string16::npos,
275 std::string(),
276 GURL(search_url),
277 metrics::OmniboxEventProto::
278 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT,
279 false,
280 false,
281 true,
282 true,
283 ChromeAutocompleteSchemeClassifier(&profile_));
284
285 provider_->Start(srp_input, false);
286 EXPECT_TRUE(provider_->matches().empty());
287 // Most visited results arriving after a new request has been started.
288 ((FakeEmptyTopSites*) profile_.GetTopSites())->mv_callback.Run(urls);
289 EXPECT_TRUE(provider_->matches().empty());
290 }
291
120 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { 292 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) {
121 CreatePersonalizedFieldTrial(); 293 CreatePersonalizedFieldTrial();
122 294
123 // Ensure the cache is empty. 295 // Ensure the cache is empty.
124 PrefService* prefs = profile_.GetPrefs(); 296 PrefService* prefs = profile_.GetPrefs();
125 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); 297 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string());
126 298
127 std::string url("http://www.cnn.com"); 299 std::string url("http://www.cnn.com/");
128 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, 300 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
129 std::string(), GURL(url), 301 std::string(), GURL(url),
130 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 302 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
131 true, true, 303 true, true,
132 ChromeAutocompleteSchemeClassifier(&profile_)); 304 ChromeAutocompleteSchemeClassifier(&profile_));
133 305
134 provider_->Start(input, false); 306 provider_->Start(input, false);
135 307
136 EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty()); 308 EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty());
137 EXPECT_TRUE(provider_->matches().empty()); 309 EXPECT_TRUE(provider_->matches().empty());
138 310
139 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); 311 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1);
140 ASSERT_TRUE(fetcher); 312 ASSERT_TRUE(fetcher);
141 fetcher->set_response_code(200); 313 fetcher->set_response_code(200);
142 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," 314 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"],"
143 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," 315 "[],[],{\"google:suggestrelevance\":[602, 601, 600],"
144 "\"google:verbatimrelevance\":1300}]"); 316 "\"google:verbatimrelevance\":1300}]");
145 fetcher->SetResponseString(json_response); 317 fetcher->SetResponseString(json_response);
146 fetcher->delegate()->OnURLFetchComplete(fetcher); 318 fetcher->delegate()->OnURLFetchComplete(fetcher);
147 319
148 base::RunLoop().RunUntilIdle(); 320 base::RunLoop().RunUntilIdle();
149 321
150 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim 322 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim
151 EXPECT_EQ(json_response, prefs->GetString(prefs::kZeroSuggestCachedResults)); 323 EXPECT_EQ(json_response, prefs->GetString(prefs::kZeroSuggestCachedResults));
152 } 324 }
153 325
154 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) { 326 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) {
155 CreatePersonalizedFieldTrial(); 327 CreatePersonalizedFieldTrial();
156 328
157 std::string url("http://www.cnn.com"); 329 std::string url("http://www.cnn.com/");
158 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, 330 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
159 std::string(), GURL(url), 331 std::string(), GURL(url),
160 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 332 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
161 true, true, 333 true, true,
162 ChromeAutocompleteSchemeClassifier(&profile_)); 334 ChromeAutocompleteSchemeClassifier(&profile_));
163 335
164 // Set up the pref to cache the response from the previous run. 336 // Set up the pref to cache the response from the previous run.
165 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," 337 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"],"
166 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," 338 "[],[],{\"google:suggestrelevance\":[602, 601, 600],"
167 "\"google:verbatimrelevance\":1300}]"); 339 "\"google:verbatimrelevance\":1300}]");
(...skipping 26 matching lines...) Expand all
194 EXPECT_EQ(base::ASCIIToUTF16("search3"), provider_->matches()[3].contents); 366 EXPECT_EQ(base::ASCIIToUTF16("search3"), provider_->matches()[3].contents);
195 367
196 // Expect the new results have been stored. 368 // Expect the new results have been stored.
197 EXPECT_EQ(json_response2, 369 EXPECT_EQ(json_response2,
198 prefs->GetString(prefs::kZeroSuggestCachedResults)); 370 prefs->GetString(prefs::kZeroSuggestCachedResults));
199 } 371 }
200 372
201 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) { 373 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) {
202 CreatePersonalizedFieldTrial(); 374 CreatePersonalizedFieldTrial();
203 375
204 std::string url("http://www.cnn.com"); 376 std::string url("http://www.cnn.com/");
205 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, 377 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
206 std::string(), GURL(url), 378 std::string(), GURL(url),
207 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 379 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
208 true, true, 380 true, true,
209 ChromeAutocompleteSchemeClassifier(&profile_)); 381 ChromeAutocompleteSchemeClassifier(&profile_));
210 382
211 // Set up the pref to cache the response from the previous run. 383 // Set up the pref to cache the response from the previous run.
212 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," 384 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"],"
213 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," 385 "[],[],{\"google:suggestrelevance\":[602, 601, 600],"
214 "\"google:verbatimrelevance\":1300}]"); 386 "\"google:verbatimrelevance\":1300}]");
(...skipping 17 matching lines...) Expand all
232 404
233 base::RunLoop().RunUntilIdle(); 405 base::RunLoop().RunUntilIdle();
234 406
235 // Expect that the matches have been cleared. 407 // Expect that the matches have been cleared.
236 ASSERT_TRUE(provider_->matches().empty()); 408 ASSERT_TRUE(provider_->matches().empty());
237 409
238 // Expect the new results have been stored. 410 // Expect the new results have been stored.
239 EXPECT_EQ(empty_response, 411 EXPECT_EQ(empty_response,
240 prefs->GetString(prefs::kZeroSuggestCachedResults)); 412 prefs->GetString(prefs::kZeroSuggestCachedResults));
241 } 413 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/zero_suggest_provider.cc ('k') | chrome/test/base/testing_profile.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698