Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "base/test/test_mock_time_task_runner.h" | 14 #include "base/test/test_mock_time_task_runner.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "components/ntp_snippets/category_factory.h" | 18 #include "components/ntp_snippets/category_factory.h" |
| 19 #include "components/ntp_snippets/ntp_snippets_constants.h" | 19 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 20 #include "components/ntp_snippets/remote/ntp_snippet.h" | 20 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 21 #include "components/ntp_snippets/user_classifier.h" | |
| 21 #include "components/prefs/testing_pref_service.h" | 22 #include "components/prefs/testing_pref_service.h" |
| 22 #include "components/signin/core/browser/account_tracker_service.h" | 23 #include "components/signin/core/browser/account_tracker_service.h" |
| 23 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 24 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 24 #include "components/signin/core/browser/fake_signin_manager.h" | 25 #include "components/signin/core/browser/fake_signin_manager.h" |
| 25 #include "components/signin/core/browser/test_signin_client.h" | 26 #include "components/signin/core/browser/test_signin_client.h" |
| 26 #include "components/variations/entropy_provider.h" | 27 #include "components/variations/entropy_provider.h" |
| 27 #include "components/variations/variations_associated_data.h" | 28 #include "components/variations/variations_associated_data.h" |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | 29 #include "net/url_request/test_url_fetcher_factory.h" |
| 29 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 const std::map<std::string, std::string>& params) | 150 const std::map<std::string, std::string>& params) |
| 150 : params_manager_(ntp_snippets::kStudyName, params), | 151 : params_manager_(ntp_snippets::kStudyName, params), |
| 151 mock_task_runner_(new base::TestMockTimeTaskRunner()), | 152 mock_task_runner_(new base::TestMockTimeTaskRunner()), |
| 152 mock_task_runner_handle_(mock_task_runner_), | 153 mock_task_runner_handle_(mock_task_runner_), |
| 153 signin_client_(new TestSigninClient(nullptr)), | 154 signin_client_(new TestSigninClient(nullptr)), |
| 154 account_tracker_(new AccountTrackerService()), | 155 account_tracker_(new AccountTrackerService()), |
| 155 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), | 156 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), |
| 156 account_tracker_.get())), | 157 account_tracker_.get())), |
| 157 fake_token_service_(new FakeProfileOAuth2TokenService()), | 158 fake_token_service_(new FakeProfileOAuth2TokenService()), |
| 158 pref_service_(new TestingPrefServiceSimple()), | 159 pref_service_(new TestingPrefServiceSimple()), |
| 160 user_classifier_(nullptr /* pref_service_ */), | |
|
Marc Treib
2016/10/06 14:54:37
Any reason for not passing in a pref service?
jkrcal
2016/10/06 15:18:41
Done.
| |
| 159 test_lang_("en-US"), | 161 test_lang_("en-US"), |
| 160 test_url_(gurl) { | 162 test_url_(gurl) { |
| 161 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); | 163 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); |
| 164 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); | |
| 162 | 165 |
| 163 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 166 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
| 164 fake_signin_manager_.get(), fake_token_service_.get(), | 167 fake_signin_manager_.get(), fake_token_service_.get(), |
| 165 scoped_refptr<net::TestURLRequestContextGetter>( | 168 scoped_refptr<net::TestURLRequestContextGetter>( |
| 166 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 169 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
| 167 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed), | 170 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed), |
| 168 kAPIKey); | 171 kAPIKey, &user_classifier_); |
| 169 | 172 |
| 170 snippets_fetcher_->SetCallback( | 173 snippets_fetcher_->SetCallback( |
| 171 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | 174 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
| 172 base::Unretained(&mock_callback_))); | 175 base::Unretained(&mock_callback_))); |
| 173 snippets_fetcher_->SetTickClockForTesting( | 176 snippets_fetcher_->SetTickClockForTesting( |
| 174 mock_task_runner_->GetMockTickClock()); | 177 mock_task_runner_->GetMockTickClock()); |
| 175 test_hosts_.insert("www.somehost.com"); | 178 test_hosts_.insert("www.somehost.com"); |
| 176 test_excluded_.insert("1234567890"); | 179 test_excluded_.insert("1234567890"); |
| 177 // Increase initial time such that ticks are non-zero. | 180 // Increase initial time such that ticks are non-zero. |
| 178 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 181 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 base::ThreadTaskRunnerHandle mock_task_runner_handle_; | 215 base::ThreadTaskRunnerHandle mock_task_runner_handle_; |
| 213 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 216 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 214 // Initialized lazily in SetFakeResponse(). | 217 // Initialized lazily in SetFakeResponse(). |
| 215 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 218 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
| 216 std::unique_ptr<TestSigninClient> signin_client_; | 219 std::unique_ptr<TestSigninClient> signin_client_; |
| 217 std::unique_ptr<AccountTrackerService> account_tracker_; | 220 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 218 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 221 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 219 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 222 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 220 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 223 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 221 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 224 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 225 UserClassifier user_classifier_; | |
| 222 CategoryFactory category_factory_; | 226 CategoryFactory category_factory_; |
| 223 MockSnippetsAvailableCallback mock_callback_; | 227 MockSnippetsAvailableCallback mock_callback_; |
| 224 const std::string test_lang_; | 228 const std::string test_lang_; |
| 225 const GURL test_url_; | 229 const GURL test_url_; |
| 226 std::set<std::string> test_hosts_; | 230 std::set<std::string> test_hosts_; |
| 227 std::set<std::string> test_excluded_; | 231 std::set<std::string> test_excluded_; |
| 228 base::HistogramTester histogram_tester_; | 232 base::HistogramTester histogram_tester_; |
| 229 | 233 |
| 230 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | 234 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
| 231 }; | 235 }; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 247 | 251 |
| 248 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { | 252 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
| 249 NTPSnippetsFetcher::RequestParams params; | 253 NTPSnippetsFetcher::RequestParams params; |
| 250 params.obfuscated_gaia_id = "0BFUSGAIA"; | 254 params.obfuscated_gaia_id = "0BFUSGAIA"; |
| 251 params.only_return_personalized_results = true; | 255 params.only_return_personalized_results = true; |
| 252 params.user_locale = "en"; | 256 params.user_locale = "en"; |
| 253 params.host_restricts = {"chromium.org"}; | 257 params.host_restricts = {"chromium.org"}; |
| 254 params.excluded_ids = {"1234567890"}; | 258 params.excluded_ids = {"1234567890"}; |
| 255 params.count_to_fetch = 25; | 259 params.count_to_fetch = 25; |
| 256 params.interactive_request = false; | 260 params.interactive_request = false; |
| 261 params.user_class = "ACTIVE_NTP_USER"; | |
| 257 | 262 |
| 258 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 263 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| 259 EXPECT_THAT(params.BuildRequest(), | 264 EXPECT_THAT(params.BuildRequest(), |
| 260 EqualsJSON("{" | 265 EqualsJSON("{" |
| 261 " \"response_detail_level\": \"STANDARD\"," | 266 " \"response_detail_level\": \"STANDARD\"," |
| 262 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," | 267 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," |
| 263 " \"user_locale\": \"en\"," | 268 " \"user_locale\": \"en\"," |
| 264 " \"advanced_options\": {" | 269 " \"advanced_options\": {" |
| 265 " \"local_scoring_params\": {" | 270 " \"local_scoring_params\": {" |
| 266 " \"content_params\": {" | 271 " \"content_params\": {" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 297 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 302 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 298 EXPECT_THAT(params.BuildRequest(), | 303 EXPECT_THAT(params.BuildRequest(), |
| 299 EqualsJSON("{" | 304 EqualsJSON("{" |
| 300 " \"uiLanguage\": \"en\"," | 305 " \"uiLanguage\": \"en\"," |
| 301 " \"priority\": \"BACKGROUND_PREFETCH\"," | 306 " \"priority\": \"BACKGROUND_PREFETCH\"," |
| 302 " \"regularlyVisitedHostNames\": [" | 307 " \"regularlyVisitedHostNames\": [" |
| 303 " \"chromium.org\"" | 308 " \"chromium.org\"" |
| 304 " ]," | 309 " ]," |
| 305 " \"excludedSuggestionIds\": [" | 310 " \"excludedSuggestionIds\": [" |
| 306 " \"1234567890\"" | 311 " \"1234567890\"" |
| 307 " ]" | 312 " ]," |
| 313 " \"user_activeness_class\": \"ACTIVE_NTP_USER\"" | |
| 308 "}")); | 314 "}")); |
| 309 } | 315 } |
| 310 | 316 |
| 311 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { | 317 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { |
| 312 NTPSnippetsFetcher::RequestParams params; | 318 NTPSnippetsFetcher::RequestParams params; |
| 313 params.only_return_personalized_results = false; | 319 params.only_return_personalized_results = false; |
| 314 params.host_restricts = {}; | 320 params.host_restricts = {}; |
| 315 params.count_to_fetch = 10; | 321 params.count_to_fetch = 10; |
| 316 params.excluded_ids = {}; | 322 params.excluded_ids = {}; |
| 317 params.interactive_request = true; | 323 params.interactive_request = true; |
| 324 params.user_class = "ACTIVE_NTP_USER"; | |
| 318 | 325 |
| 319 | 326 |
| 320 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 327 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| 321 EXPECT_THAT(params.BuildRequest(), | 328 EXPECT_THAT(params.BuildRequest(), |
| 322 EqualsJSON("{" | 329 EqualsJSON("{" |
| 323 " \"response_detail_level\": \"STANDARD\"," | 330 " \"response_detail_level\": \"STANDARD\"," |
| 324 " \"advanced_options\": {" | 331 " \"advanced_options\": {" |
| 325 " \"local_scoring_params\": {" | 332 " \"local_scoring_params\": {" |
| 326 " \"content_params\": {" | 333 " \"content_params\": {" |
| 327 " \"only_return_personalized_results\": false" | 334 " \"only_return_personalized_results\": false" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 347 " \"sort_type\": 1" | 354 " \"sort_type\": 1" |
| 348 " }" | 355 " }" |
| 349 " }" | 356 " }" |
| 350 "}")); | 357 "}")); |
| 351 | 358 |
| 352 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 359 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 353 EXPECT_THAT(params.BuildRequest(), | 360 EXPECT_THAT(params.BuildRequest(), |
| 354 EqualsJSON("{" | 361 EqualsJSON("{" |
| 355 " \"regularlyVisitedHostNames\": []," | 362 " \"regularlyVisitedHostNames\": []," |
| 356 " \"priority\": \"USER_ACTION\"," | 363 " \"priority\": \"USER_ACTION\"," |
| 357 " \"excludedSuggestionIds\": []" | 364 " \"excludedSuggestionIds\": []," |
| 365 " \"user_activeness_class\": \"ACTIVE_NTP_USER\"" | |
| 358 "}")); | 366 "}")); |
| 359 } | 367 } |
| 360 | 368 |
| 361 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { | 369 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { |
| 362 NTPSnippetsFetcher::RequestParams params; | 370 NTPSnippetsFetcher::RequestParams params; |
| 363 params.only_return_personalized_results = false; | 371 params.only_return_personalized_results = false; |
| 364 params.host_restricts = {}; | 372 params.host_restricts = {}; |
| 365 params.count_to_fetch = 10; | 373 params.count_to_fetch = 10; |
| 366 params.interactive_request = false; | 374 params.interactive_request = false; |
| 367 for (int i = 0; i < 200; ++i) { | 375 for (int i = 0; i < 200; ++i) { |
| 368 params.excluded_ids.insert(base::StringPrintf("%03d", i)); | 376 params.excluded_ids.insert(base::StringPrintf("%03d", i)); |
| 369 } | 377 } |
| 378 params.user_class = "ACTIVE_NTP_USER"; | |
| 370 | 379 |
| 371 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 380 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 372 EXPECT_THAT(params.BuildRequest(), | 381 EXPECT_THAT(params.BuildRequest(), |
| 373 EqualsJSON("{" | 382 EqualsJSON("{" |
| 374 " \"regularlyVisitedHostNames\": []," | 383 " \"regularlyVisitedHostNames\": []," |
| 375 " \"priority\": \"BACKGROUND_PREFETCH\"," | 384 " \"priority\": \"BACKGROUND_PREFETCH\"," |
| 376 " \"excludedSuggestionIds\": [" | 385 " \"excludedSuggestionIds\": [" |
| 377 " \"000\", \"001\", \"002\", \"003\", \"004\"," | 386 " \"000\", \"001\", \"002\", \"003\", \"004\"," |
| 378 " \"005\", \"006\", \"007\", \"008\", \"009\"," | 387 " \"005\", \"006\", \"007\", \"008\", \"009\"," |
| 379 " \"010\", \"011\", \"012\", \"013\", \"014\"," | 388 " \"010\", \"011\", \"012\", \"013\", \"014\"," |
| 380 " \"015\", \"016\", \"017\", \"018\", \"019\"," | 389 " \"015\", \"016\", \"017\", \"018\", \"019\"," |
| 381 " \"020\", \"021\", \"022\", \"023\", \"024\"," | 390 " \"020\", \"021\", \"022\", \"023\", \"024\"," |
| 382 " \"025\", \"026\", \"027\", \"028\", \"029\"," | 391 " \"025\", \"026\", \"027\", \"028\", \"029\"," |
| 383 " \"030\", \"031\", \"032\", \"033\", \"034\"," | 392 " \"030\", \"031\", \"032\", \"033\", \"034\"," |
| 384 " \"035\", \"036\", \"037\", \"038\", \"039\"," | 393 " \"035\", \"036\", \"037\", \"038\", \"039\"," |
| 385 " \"040\", \"041\", \"042\", \"043\", \"044\"," | 394 " \"040\", \"041\", \"042\", \"043\", \"044\"," |
| 386 " \"045\", \"046\", \"047\", \"048\", \"049\"," | 395 " \"045\", \"046\", \"047\", \"048\", \"049\"," |
| 387 " \"050\", \"051\", \"052\", \"053\", \"054\"," | 396 " \"050\", \"051\", \"052\", \"053\", \"054\"," |
| 388 " \"055\", \"056\", \"057\", \"058\", \"059\"," | 397 " \"055\", \"056\", \"057\", \"058\", \"059\"," |
| 389 " \"060\", \"061\", \"062\", \"063\", \"064\"," | 398 " \"060\", \"061\", \"062\", \"063\", \"064\"," |
| 390 " \"065\", \"066\", \"067\", \"068\", \"069\"," | 399 " \"065\", \"066\", \"067\", \"068\", \"069\"," |
| 391 " \"070\", \"071\", \"072\", \"073\", \"074\"," | 400 " \"070\", \"071\", \"072\", \"073\", \"074\"," |
| 392 " \"075\", \"076\", \"077\", \"078\", \"079\"," | 401 " \"075\", \"076\", \"077\", \"078\", \"079\"," |
| 393 " \"080\", \"081\", \"082\", \"083\", \"084\"," | 402 " \"080\", \"081\", \"082\", \"083\", \"084\"," |
| 394 " \"085\", \"086\", \"087\", \"088\", \"089\"," | 403 " \"085\", \"086\", \"087\", \"088\", \"089\"," |
| 395 " \"090\", \"091\", \"092\", \"093\", \"094\"," | 404 " \"090\", \"091\", \"092\", \"093\", \"094\"," |
| 396 " \"095\", \"096\", \"097\", \"098\", \"099\"" | 405 " \"095\", \"096\", \"097\", \"098\", \"099\"" |
| 397 // Truncated to 100 entries. Currently, they happen to | 406 // Truncated to 100 entries. Currently, they happen to |
| 398 // be those lexically first. | 407 // be those lexically first. |
| 399 " ]" | 408 " ]," |
| 409 " \"user_activeness_class\": \"ACTIVE_NTP_USER\"" | |
| 400 "}")); | 410 "}")); |
| 401 } | 411 } |
| 402 | 412 |
| 403 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { | 413 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { |
| 404 // The lack of registered baked in responses would cause any fetch to fail. | 414 // The lack of registered baked in responses would cause any fetch to fail. |
| 405 FastForwardUntilNoTasksRemain(); | 415 FastForwardUntilNoTasksRemain(); |
| 406 EXPECT_THAT(histogram_tester().GetAllSamples( | 416 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 407 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 417 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 408 IsEmpty()); | 418 IsEmpty()); |
| 409 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 419 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 799 const NTPSnippetsFetcher::OptionalSnippets& snippets) { | 809 const NTPSnippetsFetcher::OptionalSnippets& snippets) { |
| 800 if (snippets) { | 810 if (snippets) { |
| 801 // Matchers above aren't any more precise than this, so this is sufficient | 811 // Matchers above aren't any more precise than this, so this is sufficient |
| 802 // for test-failure diagnostics. | 812 // for test-failure diagnostics. |
| 803 return os << "list with " << snippets->size() << " elements"; | 813 return os << "list with " << snippets->size() << " elements"; |
| 804 } | 814 } |
| 805 return os << "null"; | 815 return os << "null"; |
| 806 } | 816 } |
| 807 | 817 |
| 808 } // namespace ntp_snippets | 818 } // namespace ntp_snippets |
| OLD | NEW |