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

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: Casting fix Created 6 years, 1 month 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 CHECK(false);
76 return *(new std::string());
77 }
78 bool IsNonForcedFull() override {
79 return false;
80 }
81 bool IsForcedFull() override {
82 return false;
83 }
84 bool loaded() const override {
85 return false;
86 }
87 history::MostVisitedURLList GetPrepopulatePages() override {
88 return history::MostVisitedURLList();
89 }
90 bool AddForcedURL(const GURL& url, const base::Time& time) override {
91 return false;
92 }
93 // content::NotificationObserver:
94 void Observe(int type,
95 const content::NotificationSource& source,
96 const content::NotificationDetails& details) override {}
97
98 // A test-specific field for controlling when most visited callback is run
99 // after top sites have been requested.
100 GetMostVisitedURLsCallback mv_callback;
101 protected:
102 virtual ~FakeEmptyTopSites() {
103 }
104 };
105
106 void FakeEmptyTopSites::GetMostVisitedURLs(
107 const GetMostVisitedURLsCallback& callback,
108 bool include_forced_urls) {
109 mv_callback = callback;
110 }
111
112 } // namespace
113
114
27 class ZeroSuggestProviderTest : public testing::Test, 115 class ZeroSuggestProviderTest : public testing::Test,
28 public AutocompleteProviderListener { 116 public AutocompleteProviderListener {
29 public: 117 public:
30 ZeroSuggestProviderTest(); 118 ZeroSuggestProviderTest();
31 119
32 virtual void SetUp() override; 120 virtual void SetUp() override;
33 virtual void TearDown() override; 121 virtual void TearDown() override;
34 122
35 protected: 123 protected:
36 // AutocompleteProviderListener: 124 // AutocompleteProviderListener:
37 void OnProviderUpdate(bool updated_matches) override; 125 void OnProviderUpdate(bool updated_matches) override;
38 126
39 void ResetFieldTrialList(); 127 void ResetFieldTrialList();
40 128
41 void CreatePersonalizedFieldTrial(); 129 void CreatePersonalizedFieldTrial();
130 void CreateMostVisitedFieldTrial();
42 131
43 // Set up threads for testing; this needs to be instantiated before 132 // Set up threads for testing; this needs to be instantiated before
44 // |profile_|. 133 // |profile_|.
45 content::TestBrowserThreadBundle thread_bundle_; 134 content::TestBrowserThreadBundle thread_bundle_;
46 135
47 // Needed for OmniboxFieldTrial::ActivateStaticTrials(). 136 // Needed for OmniboxFieldTrial::ActivateStaticTrials().
48 scoped_ptr<base::FieldTrialList> field_trial_list_; 137 scoped_ptr<base::FieldTrialList> field_trial_list_;
49 138
50 // URLFetcherFactory implementation registered. 139 // URLFetcherFactory implementation registered.
51 net::TestURLFetcherFactory test_factory_; 140 net::TestURLFetcherFactory test_factory_;
(...skipping 27 matching lines...) Expand all
79 TemplateURLData data; 168 TemplateURLData data;
80 data.short_name = base::ASCIIToUTF16("t"); 169 data.short_name = base::ASCIIToUTF16("t");
81 data.SetURL("https://www.google.com/?q={searchTerms}"); 170 data.SetURL("https://www.google.com/?q={searchTerms}");
82 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}"; 171 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}";
83 data.instant_url = "https://does/not/exist?strk=1"; 172 data.instant_url = "https://does/not/exist?strk=1";
84 data.search_terms_replacement_key = "strk"; 173 data.search_terms_replacement_key = "strk";
85 default_t_url_ = new TemplateURL(data); 174 default_t_url_ = new TemplateURL(data);
86 turl_model->Add(default_t_url_); 175 turl_model->Add(default_t_url_);
87 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); 176 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_);
88 177
178 profile_.SetTopSites(new FakeEmptyTopSites());
179
89 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); 180 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_);
90 } 181 }
91 182
92 void ZeroSuggestProviderTest::TearDown() { 183 void ZeroSuggestProviderTest::TearDown() {
93 // Shutdown the provider before the profile. 184 // Shutdown the provider before the profile.
94 provider_ = NULL; 185 provider_ = NULL;
95 } 186 }
96 187
97 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) { 188 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) {
98 } 189 }
(...skipping 11 matching lines...) Expand all
110 std::map<std::string, std::string> params; 201 std::map<std::string, std::string> params;
111 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true"; 202 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true";
112 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] = 203 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] =
113 "Personalized"; 204 "Personalized";
114 variations::AssociateVariationParams( 205 variations::AssociateVariationParams(
115 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params); 206 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params);
116 base::FieldTrialList::CreateFieldTrial( 207 base::FieldTrialList::CreateFieldTrial(
117 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); 208 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A");
118 } 209 }
119 210
211 void ZeroSuggestProviderTest::CreateMostVisitedFieldTrial() {
212 std::map<std::string, std::string> params;
213 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true";
214 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] =
215 "MostVisitedWithoutSERP";
216 variations::AssociateVariationParams(
217 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params);
218 base::FieldTrialList::CreateFieldTrial(
219 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A");
220 }
221
222 TEST_F(ZeroSuggestProviderTest, TestMostVisitedCallback) {
223 CreateMostVisitedFieldTrial();
224
225 std::string current_url("http://www.foxnews.com/");
226 std::string input_url("http://www.cnn.com/");
227 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos,
228 std::string(), GURL(current_url),
229 metrics::OmniboxEventProto::OTHER, false, false,
230 true, true,
231 ChromeAutocompleteSchemeClassifier(&profile_));
232 history::MostVisitedURLList urls;
233 history::MostVisitedURL url(GURL("http://foo.com/"),
234 base::ASCIIToUTF16(std::string("Foo")));
235 urls.push_back(url);
236
237 provider_->Start(input, false);
238 EXPECT_TRUE(provider_->matches().empty());
239 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run(
240 urls);
241 // Should have verbatim match + most visited url match.
242 EXPECT_EQ(2U, provider_->matches().size());
243 provider_->Stop(false);
244
245 provider_->Start(input, false);
246 provider_->Stop(false);
247 EXPECT_TRUE(provider_->matches().empty());
248 // Most visited results arriving after Stop() has been called, ensure they
249 // are not displayed.
250 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run(
251 urls);
252 EXPECT_TRUE(provider_->matches().empty());
253 }
254
255 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) {
256 CreateMostVisitedFieldTrial();
257
258 std::string current_url("http://www.foxnews.com/");
259 std::string input_url("http://www.cnn.com/");
260 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos,
261 std::string(), GURL(current_url),
262 metrics::OmniboxEventProto::OTHER, false, false,
263 true, true,
264 ChromeAutocompleteSchemeClassifier(&profile_));
265 history::MostVisitedURLList urls;
266 history::MostVisitedURL url(GURL("http://foo.com/"),
267 base::ASCIIToUTF16(std::string("Foo")));
268 urls.push_back(url);
269
270 provider_->Start(input, false);
271 EXPECT_TRUE(provider_->matches().empty());
272 // Stop() doesn't always get called.
273
274 std::string search_url("https://www.google.com/?q=flowers");
275 AutocompleteInput srp_input(
276 base::ASCIIToUTF16(search_url),
277 base::string16::npos,
278 std::string(),
279 GURL(search_url),
280 metrics::OmniboxEventProto::
281 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT,
282 false,
283 false,
284 true,
285 true,
286 ChromeAutocompleteSchemeClassifier(&profile_));
287
288 provider_->Start(srp_input, false);
289 EXPECT_TRUE(provider_->matches().empty());
290 // Most visited results arriving after a new request has been started.
291 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run(
292 urls);
293 EXPECT_TRUE(provider_->matches().empty());
294 }
295
120 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { 296 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) {
121 CreatePersonalizedFieldTrial(); 297 CreatePersonalizedFieldTrial();
122 298
123 // Ensure the cache is empty. 299 // Ensure the cache is empty.
124 PrefService* prefs = profile_.GetPrefs(); 300 PrefService* prefs = profile_.GetPrefs();
125 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); 301 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string());
126 302
127 std::string url("http://www.cnn.com"); 303 std::string url("http://www.cnn.com/");
128 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, 304 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
129 std::string(), GURL(url), 305 std::string(), GURL(url),
130 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 306 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
131 true, true, 307 true, true,
132 ChromeAutocompleteSchemeClassifier(&profile_)); 308 ChromeAutocompleteSchemeClassifier(&profile_));
133 309
134 provider_->Start(input, false); 310 provider_->Start(input, false);
135 311
136 EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty()); 312 EXPECT_TRUE(prefs->GetString(prefs::kZeroSuggestCachedResults).empty());
137 EXPECT_TRUE(provider_->matches().empty()); 313 EXPECT_TRUE(provider_->matches().empty());
138 314
139 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1); 315 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(1);
140 ASSERT_TRUE(fetcher); 316 ASSERT_TRUE(fetcher);
141 fetcher->set_response_code(200); 317 fetcher->set_response_code(200);
142 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," 318 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"],"
143 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," 319 "[],[],{\"google:suggestrelevance\":[602, 601, 600],"
144 "\"google:verbatimrelevance\":1300}]"); 320 "\"google:verbatimrelevance\":1300}]");
145 fetcher->SetResponseString(json_response); 321 fetcher->SetResponseString(json_response);
146 fetcher->delegate()->OnURLFetchComplete(fetcher); 322 fetcher->delegate()->OnURLFetchComplete(fetcher);
147 323
148 base::RunLoop().RunUntilIdle(); 324 base::RunLoop().RunUntilIdle();
149 325
150 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim 326 EXPECT_EQ(4U, provider_->matches().size()); // 3 results + verbatim
151 EXPECT_EQ(json_response, prefs->GetString(prefs::kZeroSuggestCachedResults)); 327 EXPECT_EQ(json_response, prefs->GetString(prefs::kZeroSuggestCachedResults));
152 } 328 }
153 329
154 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) { 330 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestHasCachedResults) {
155 CreatePersonalizedFieldTrial(); 331 CreatePersonalizedFieldTrial();
156 332
157 std::string url("http://www.cnn.com"); 333 std::string url("http://www.cnn.com/");
158 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, 334 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
159 std::string(), GURL(url), 335 std::string(), GURL(url),
160 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 336 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
161 true, true, 337 true, true,
162 ChromeAutocompleteSchemeClassifier(&profile_)); 338 ChromeAutocompleteSchemeClassifier(&profile_));
163 339
164 // Set up the pref to cache the response from the previous run. 340 // Set up the pref to cache the response from the previous run.
165 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," 341 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"],"
166 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," 342 "[],[],{\"google:suggestrelevance\":[602, 601, 600],"
167 "\"google:verbatimrelevance\":1300}]"); 343 "\"google:verbatimrelevance\":1300}]");
(...skipping 26 matching lines...) Expand all
194 EXPECT_EQ(base::ASCIIToUTF16("search3"), provider_->matches()[3].contents); 370 EXPECT_EQ(base::ASCIIToUTF16("search3"), provider_->matches()[3].contents);
195 371
196 // Expect the new results have been stored. 372 // Expect the new results have been stored.
197 EXPECT_EQ(json_response2, 373 EXPECT_EQ(json_response2,
198 prefs->GetString(prefs::kZeroSuggestCachedResults)); 374 prefs->GetString(prefs::kZeroSuggestCachedResults));
199 } 375 }
200 376
201 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) { 377 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestReceivedEmptyResults) {
202 CreatePersonalizedFieldTrial(); 378 CreatePersonalizedFieldTrial();
203 379
204 std::string url("http://www.cnn.com"); 380 std::string url("http://www.cnn.com/");
205 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, 381 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
206 std::string(), GURL(url), 382 std::string(), GURL(url),
207 metrics::OmniboxEventProto::INVALID_SPEC, true, false, 383 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
208 true, true, 384 true, true,
209 ChromeAutocompleteSchemeClassifier(&profile_)); 385 ChromeAutocompleteSchemeClassifier(&profile_));
210 386
211 // Set up the pref to cache the response from the previous run. 387 // Set up the pref to cache the response from the previous run.
212 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"]," 388 std::string json_response("[\"\",[\"search1\", \"search2\", \"search3\"],"
213 "[],[],{\"google:suggestrelevance\":[602, 601, 600]," 389 "[],[],{\"google:suggestrelevance\":[602, 601, 600],"
214 "\"google:verbatimrelevance\":1300}]"); 390 "\"google:verbatimrelevance\":1300}]");
(...skipping 17 matching lines...) Expand all
232 408
233 base::RunLoop().RunUntilIdle(); 409 base::RunLoop().RunUntilIdle();
234 410
235 // Expect that the matches have been cleared. 411 // Expect that the matches have been cleared.
236 ASSERT_TRUE(provider_->matches().empty()); 412 ASSERT_TRUE(provider_->matches().empty());
237 413
238 // Expect the new results have been stored. 414 // Expect the new results have been stored.
239 EXPECT_EQ(empty_response, 415 EXPECT_EQ(empty_response,
240 prefs->GetString(prefs::kZeroSuggestCachedResults)); 416 prefs->GetString(prefs::kZeroSuggestCachedResults));
241 } 417 }
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