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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 174 |
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 TopSitesFactory::GetInstance()->SetTestingFactory(profile_, |
178 profile_.SetTopSites(new FakeEmptyTopSites()); | 185 BuildFakeEmptyTopSites); |
179 | 186 |
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) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 metrics::OmniboxEventProto::OTHER, false, false, | 236 metrics::OmniboxEventProto::OTHER, false, false, |
230 true, true, | 237 true, true, |
231 ChromeAutocompleteSchemeClassifier(&profile_)); | 238 ChromeAutocompleteSchemeClassifier(&profile_)); |
232 history::MostVisitedURLList urls; | 239 history::MostVisitedURLList urls; |
233 history::MostVisitedURL url(GURL("http://foo.com/"), | 240 history::MostVisitedURL url(GURL("http://foo.com/"), |
234 base::ASCIIToUTF16("Foo")); | 241 base::ASCIIToUTF16("Foo")); |
235 urls.push_back(url); | 242 urls.push_back(url); |
236 | 243 |
237 provider_->Start(input, false); | 244 provider_->Start(input, false); |
238 EXPECT_TRUE(provider_->matches().empty()); | 245 EXPECT_TRUE(provider_->matches().empty()); |
239 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 246 scoped_refptr<history::TopSites> top_sites = |
240 urls); | 247 TopSitesFactory::GetForProfile(profile_); |
| 248 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
241 // Should have verbatim match + most visited url match. | 249 // Should have verbatim match + most visited url match. |
242 EXPECT_EQ(2U, provider_->matches().size()); | 250 EXPECT_EQ(2U, provider_->matches().size()); |
243 provider_->Stop(false); | 251 provider_->Stop(false); |
244 | 252 |
245 provider_->Start(input, false); | 253 provider_->Start(input, false); |
246 provider_->Stop(false); | 254 provider_->Stop(false); |
247 EXPECT_TRUE(provider_->matches().empty()); | 255 EXPECT_TRUE(provider_->matches().empty()); |
248 // Most visited results arriving after Stop() has been called, ensure they | 256 // Most visited results arriving after Stop() has been called, ensure they |
249 // are not displayed. | 257 // are not displayed. |
250 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 258 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
251 urls); | |
252 EXPECT_TRUE(provider_->matches().empty()); | 259 EXPECT_TRUE(provider_->matches().empty()); |
253 } | 260 } |
254 | 261 |
255 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { | 262 TEST_F(ZeroSuggestProviderTest, TestMostVisitedNavigateToSearchPage) { |
256 CreateMostVisitedFieldTrial(); | 263 CreateMostVisitedFieldTrial(); |
257 | 264 |
258 std::string current_url("http://www.foxnews.com/"); | 265 std::string current_url("http://www.foxnews.com/"); |
259 std::string input_url("http://www.cnn.com/"); | 266 std::string input_url("http://www.cnn.com/"); |
260 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, | 267 AutocompleteInput input(base::ASCIIToUTF16(input_url), base::string16::npos, |
261 std::string(), GURL(current_url), | 268 std::string(), GURL(current_url), |
(...skipping 19 matching lines...) Expand all Loading... |
281 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT, | 288 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT, |
282 false, | 289 false, |
283 false, | 290 false, |
284 true, | 291 true, |
285 true, | 292 true, |
286 ChromeAutocompleteSchemeClassifier(&profile_)); | 293 ChromeAutocompleteSchemeClassifier(&profile_)); |
287 | 294 |
288 provider_->Start(srp_input, false); | 295 provider_->Start(srp_input, false); |
289 EXPECT_TRUE(provider_->matches().empty()); | 296 EXPECT_TRUE(provider_->matches().empty()); |
290 // Most visited results arriving after a new request has been started. | 297 // Most visited results arriving after a new request has been started. |
291 static_cast<FakeEmptyTopSites*>(profile_.GetTopSites())->mv_callback.Run( | 298 scoped_refptr<history::TopSites> top_sites = |
292 urls); | 299 TopSitesFactory::GetForProfile(profile_); |
| 300 static_cast<FakeEmptyTopSites*>(top_sites.get())->mv_callback.Run(urls); |
293 EXPECT_TRUE(provider_->matches().empty()); | 301 EXPECT_TRUE(provider_->matches().empty()); |
294 } | 302 } |
295 | 303 |
296 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { | 304 TEST_F(ZeroSuggestProviderTest, TestPsuggestZeroSuggestCachingFirstRun) { |
297 CreatePersonalizedFieldTrial(); | 305 CreatePersonalizedFieldTrial(); |
298 | 306 |
299 // Ensure the cache is empty. | 307 // Ensure the cache is empty. |
300 PrefService* prefs = profile_.GetPrefs(); | 308 PrefService* prefs = profile_.GetPrefs(); |
301 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); | 309 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string()); |
302 | 310 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 | 416 |
409 base::RunLoop().RunUntilIdle(); | 417 base::RunLoop().RunUntilIdle(); |
410 | 418 |
411 // Expect that the matches have been cleared. | 419 // Expect that the matches have been cleared. |
412 ASSERT_TRUE(provider_->matches().empty()); | 420 ASSERT_TRUE(provider_->matches().empty()); |
413 | 421 |
414 // Expect the new results have been stored. | 422 // Expect the new results have been stored. |
415 EXPECT_EQ(empty_response, | 423 EXPECT_EQ(empty_response, |
416 prefs->GetString(prefs::kZeroSuggestCachedResults)); | 424 prefs->GetString(prefs::kZeroSuggestCachedResults)); |
417 } | 425 } |
OLD | NEW |