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

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: Fixed slow mv return condition and added unit tests 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 FakeTopSites : public history::TopSites {
Mark P 2014/10/22 19:11:24 optional nit: FakeTopSite implies that at minimum
Maria 2014/10/23 20:34:26 Done.
30 public:
31 FakeTopSites() {
32 }
33 virtual ~FakeTopSites() {
34 }
35 // history::TopSites:
36 bool SetPageThumbnail(const GURL& url, const gfx::Image& thumbnail,
37 const ThumbnailScore& score) override {
38 return false;
39 }
40 bool SetPageThumbnailToJPEGBytes(const GURL& url,
41 const base::RefCountedMemory* memory,
42 const ThumbnailScore& score) override {
43 return false;
44 }
45 void GetMostVisitedURLs(const GetMostVisitedURLsCallback& callback,
46 bool include_forced_urls) override;
47 bool GetPageThumbnail(const GURL& url, bool prefix_match,
48 scoped_refptr<base::RefCountedMemory>* bytes) override {
49 return false;
50 }
51 bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) override {
52 return false;
53 }
54 bool GetTemporaryPageThumbnailScore(const GURL& url, ThumbnailScore* score)
55 override {
56 return false;
57 }
58 void SyncWithHistory() override {}
59 bool HasBlacklistedItems() const override {
60 return false;
61 }
62 void AddBlacklistedURL(const GURL& url) override {}
63 void RemoveBlacklistedURL(const GURL& url) override {}
64 bool IsBlacklisted(const GURL& url) override {
65 return false;
66 }
67 void ClearBlacklistedURLs() override {}
68 void Shutdown() override {}
69 base::CancelableTaskTracker::TaskId StartQueryForMostVisited() override {
70 return 0;
71 }
72 bool IsKnownURL(const GURL& url) override {
73 return false;
74 }
75 const std::string& GetCanonicalURLString(const GURL& url) const override {
76 return NULL;
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 GetMostVisitedURLsCallback mv_callback;
Mark P 2014/10/22 19:11:23 optional nit: comment this new local variable abou
Maria 2014/10/23 20:34:26 Done.
98 };
99
100 void FakeTopSites::GetMostVisitedURLs(
101 const GetMostVisitedURLsCallback& callback,
102 bool include_forced_urls) {
103 mv_callback = callback;
104 }
105
106 } // namespace
107
108
27 class ZeroSuggestProviderTest : public testing::Test, 109 class ZeroSuggestProviderTest : public testing::Test,
28 public AutocompleteProviderListener { 110 public AutocompleteProviderListener {
29 public: 111 public:
30 ZeroSuggestProviderTest(); 112 ZeroSuggestProviderTest();
31 113
32 virtual void SetUp() override; 114 virtual void SetUp() override;
33 virtual void TearDown() override; 115 virtual void TearDown() override;
34 116
35 protected: 117 protected:
36 // AutocompleteProviderListener: 118 // AutocompleteProviderListener:
37 void OnProviderUpdate(bool updated_matches) override; 119 void OnProviderUpdate(bool updated_matches) override;
38 120
39 void ResetFieldTrialList(); 121 void ResetFieldTrialList();
40 122
41 void CreatePersonalizedFieldTrial(); 123 void CreatePersonalizedFieldTrial();
124 void CreateMostVisitedFieldTrial();
42 125
43 // Set up threads for testing; this needs to be instantiated before 126 // Set up threads for testing; this needs to be instantiated before
44 // |profile_|. 127 // |profile_|.
45 content::TestBrowserThreadBundle thread_bundle_; 128 content::TestBrowserThreadBundle thread_bundle_;
46 129
47 // Needed for OmniboxFieldTrial::ActivateStaticTrials(). 130 // Needed for OmniboxFieldTrial::ActivateStaticTrials().
48 scoped_ptr<base::FieldTrialList> field_trial_list_; 131 scoped_ptr<base::FieldTrialList> field_trial_list_;
49 132
50 // URLFetcherFactory implementation registered. 133 // URLFetcherFactory implementation registered.
51 net::TestURLFetcherFactory test_factory_; 134 net::TestURLFetcherFactory test_factory_;
(...skipping 27 matching lines...) Expand all
79 TemplateURLData data; 162 TemplateURLData data;
80 data.short_name = base::ASCIIToUTF16("t"); 163 data.short_name = base::ASCIIToUTF16("t");
81 data.SetURL("https://www.google.com/?q={searchTerms}"); 164 data.SetURL("https://www.google.com/?q={searchTerms}");
82 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}"; 165 data.suggestions_url = "https://www.google.com/complete/?q={searchTerms}";
83 data.instant_url = "https://does/not/exist?strk=1"; 166 data.instant_url = "https://does/not/exist?strk=1";
84 data.search_terms_replacement_key = "strk"; 167 data.search_terms_replacement_key = "strk";
85 default_t_url_ = new TemplateURL(data); 168 default_t_url_ = new TemplateURL(data);
86 turl_model->Add(default_t_url_); 169 turl_model->Add(default_t_url_);
87 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_); 170 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_);
88 171
172 profile_.SetTopSites(new FakeTopSites());
173
89 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_); 174 provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_);
90 } 175 }
91 176
92 void ZeroSuggestProviderTest::TearDown() { 177 void ZeroSuggestProviderTest::TearDown() {
93 // Shutdown the provider before the profile. 178 // Shutdown the provider before the profile.
94 provider_ = NULL; 179 provider_ = NULL;
95 } 180 }
96 181
97 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) { 182 void ZeroSuggestProviderTest::OnProviderUpdate(bool updated_matches) {
98 } 183 }
(...skipping 11 matching lines...) Expand all
110 std::map<std::string, std::string> params; 195 std::map<std::string, std::string> params;
111 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true"; 196 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true";
112 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] = 197 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] =
113 "Personalized"; 198 "Personalized";
114 variations::AssociateVariationParams( 199 variations::AssociateVariationParams(
115 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params); 200 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params);
116 base::FieldTrialList::CreateFieldTrial( 201 base::FieldTrialList::CreateFieldTrial(
117 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); 202 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A");
118 } 203 }
119 204
205 void ZeroSuggestProviderTest::CreateMostVisitedFieldTrial() {
206 std::map<std::string, std::string> params;
207 params[std::string(OmniboxFieldTrial::kZeroSuggestRule)] = "true";
208 params[std::string(OmniboxFieldTrial::kZeroSuggestVariantRule)] =
209 "MostVisited";
210 variations::AssociateVariationParams(
211 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params);
212 base::FieldTrialList::CreateFieldTrial(
213 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A");
214 }
215
216 TEST_F(ZeroSuggestProviderTest, TestMostVisitedCallback) {
217 CreateMostVisitedFieldTrial();
218
219 std::string input_url("http://www.cnn.com");
Mark P 2014/10/22 19:11:24 nit: add trailing slash to make a valid URL, not r
Maria 2014/10/23 20:34:27 Done.
220 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos,
221 std::string(), GURL(input_url),
Mark P 2014/10/22 19:11:24 optional nit: This GURL doesn't have to be the sam
222 metrics::OmniboxEventProto::INVALID_SPEC, true, false,
Mark P 2014/10/22 19:11:24 Typically prevent_inline_autocomplete is false, no
Maria 2014/10/23 20:34:26 Done.
223 true, true,
224 ChromeAutocompleteSchemeClassifier(&profile_));
225 history::MostVisitedURLList urls;
226 history::MostVisitedURL url;
227 url.url = GURL("http://foo.com");
Mark P 2014/10/22 19:11:23 Please use one of the MostVisitedURL constructors.
Maria 2014/10/23 20:34:26 Done.
228 urls.push_back(url);
229
230 provider_->Start(input, false);
231 EXPECT_TRUE(provider_->matches().empty());
232 ((FakeTopSites*) profile_.GetTopSites())->mv_callback.Run(urls);
Mark P 2014/10/22 19:11:23 style guide request: I think we're supposed to exp
Maria 2014/10/23 20:34:26 That's what I had here at first, but it refuses to
Mark P 2014/10/23 21:33:52 From the style guide, I guess we're supposed to us
Maria 2014/10/23 21:53:20 It looks to me like safe conversions is used for n
233 // Should have verbatim match + most visited url match
Mark P 2014/10/22 19:11:23 nit: period at end of comment
Maria 2014/10/23 20:34:26 Done.
Maria 2014/10/23 20:34:27 Done.
234 EXPECT_EQ(2U, provider_->matches().size());
235 provider_->Stop(false);
Mark P 2014/10/22 19:11:24 Oftentimes the autocomplete system Start()s a new
Maria 2014/10/23 20:34:26 I think this will be taken care of by the new test
236
237 provider_->Start(input, false);
238 provider_->Stop(false);
239 EXPECT_TRUE(provider_->matches().empty());
240 // Most visited results arriving after Stop() has been called, ensure they
241 // are not displayed.
242 ((FakeTopSites*) profile_.GetTopSites())->mv_callback.Run(urls);
243 EXPECT_TRUE(provider_->matches().empty());
244 }
Mark P 2014/10/22 19:11:23 I'd also like to see a test for the case I mention
Maria 2014/10/23 20:34:26 Done.
245
120 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { 246 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) {
121 CreatePersonalizedFieldTrial(); 247 CreatePersonalizedFieldTrial();
122 248
123 // Ensure the cache is empty. 249 // Ensure the cache is empty.
124 PrefService* prefs = profile_.GetPrefs(); 250 PrefService* prefs = profile_.GetPrefs();
125 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); 251 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string());
126 252
127 std::string url("http://www.cnn.com"); 253 std::string url("http://www.cnn.com");
128 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, 254 AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
129 std::string(), GURL(url), 255 std::string(), GURL(url),
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 358
233 base::RunLoop().RunUntilIdle(); 359 base::RunLoop().RunUntilIdle();
234 360
235 // Expect that the matches have been cleared. 361 // Expect that the matches have been cleared.
236 ASSERT_TRUE(provider_->matches().empty()); 362 ASSERT_TRUE(provider_->matches().empty());
237 363
238 // Expect the new results have been stored. 364 // Expect the new results have been stored.
239 EXPECT_EQ(empty_response, 365 EXPECT_EQ(empty_response,
240 prefs->GetString(prefs::kZeroSuggestCachedResults)); 366 prefs->GetString(prefs::kZeroSuggestCachedResults));
241 } 367 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698