| 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 <deque> | 7 #include <deque> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); | 265 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); |
| 266 } | 266 } |
| 267 | 267 |
| 268 } // namespace | 268 } // namespace |
| 269 | 269 |
| 270 class NTPSnippetsFetcherTestBase : public testing::Test { | 270 class NTPSnippetsFetcherTestBase : public testing::Test { |
| 271 public: | 271 public: |
| 272 explicit NTPSnippetsFetcherTestBase(const GURL& gurl) | 272 explicit NTPSnippetsFetcherTestBase(const GURL& gurl) |
| 273 : default_variation_params_( | 273 : default_variation_params_( |
| 274 {{"send_top_languages", "true"}, {"send_user_class", "true"}}), | 274 {{"send_top_languages", "true"}, {"send_user_class", "true"}}), |
| 275 params_manager_( | 275 params_manager_(ntp_snippets::kStudyName, |
| 276 base::MakeUnique<variations::testing::VariationParamsManager>( | 276 default_variation_params_, |
| 277 ntp_snippets::kStudyName, | 277 {ntp_snippets::kArticleSuggestionsFeature.name}), |
| 278 default_variation_params_, | |
| 279 std::set<std::string>{ | |
| 280 ntp_snippets::kArticleSuggestionsFeature.name})), | |
| 281 mock_task_runner_(new base::TestMockTimeTaskRunner()), | 278 mock_task_runner_(new base::TestMockTimeTaskRunner()), |
| 282 mock_task_runner_handle_(mock_task_runner_), | 279 mock_task_runner_handle_(mock_task_runner_), |
| 283 signin_client_(base::MakeUnique<TestSigninClient>(nullptr)), | 280 signin_client_(base::MakeUnique<TestSigninClient>(nullptr)), |
| 284 account_tracker_(base::MakeUnique<AccountTrackerService>()), | 281 account_tracker_(base::MakeUnique<AccountTrackerService>()), |
| 285 fake_signin_manager_( | 282 fake_signin_manager_( |
| 286 base::MakeUnique<FakeSigninManagerBase>(signin_client_.get(), | 283 base::MakeUnique<FakeSigninManagerBase>(signin_client_.get(), |
| 287 account_tracker_.get())), | 284 account_tracker_.get())), |
| 288 fake_token_service_(base::MakeUnique<FakeProfileOAuth2TokenService>()), | 285 fake_token_service_(base::MakeUnique<FakeProfileOAuth2TokenService>()), |
| 289 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()), | 286 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()), |
| 290 test_url_(gurl) { | 287 test_url_(gurl) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 void InitFakeURLFetcherFactory() { | 328 void InitFakeURLFetcherFactory() { |
| 332 if (fake_url_fetcher_factory_) { | 329 if (fake_url_fetcher_factory_) { |
| 333 return; | 330 return; |
| 334 } | 331 } |
| 335 // Instantiation of factory automatically sets itself as URLFetcher's | 332 // Instantiation of factory automatically sets itself as URLFetcher's |
| 336 // factory. | 333 // factory. |
| 337 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( | 334 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( |
| 338 /*default_factory=*/&failing_url_fetcher_factory_)); | 335 /*default_factory=*/&failing_url_fetcher_factory_)); |
| 339 } | 336 } |
| 340 | 337 |
| 341 void SetDefaultVariationParam(std::string param_name, std::string value) { | |
| 342 default_variation_params_[param_name] = value; | |
| 343 SetVariationParam(param_name, value); | |
| 344 } | |
| 345 | |
| 346 void SetVariationParam(std::string param_name, std::string value) { | 338 void SetVariationParam(std::string param_name, std::string value) { |
| 347 params_manager_.reset(); | |
| 348 | |
| 349 std::map<std::string, std::string> params = default_variation_params_; | 339 std::map<std::string, std::string> params = default_variation_params_; |
| 350 params[param_name] = value; | 340 params[param_name] = value; |
| 351 | 341 |
| 352 params_manager_ = | 342 params_manager_.ClearAllVariationParams(); |
| 353 base::MakeUnique<variations::testing::VariationParamsManager>( | 343 params_manager_.SetVariationParamsWithFeatureAssociations( |
| 354 ntp_snippets::kStudyName, params, | 344 ntp_snippets::kStudyName, params, |
| 355 std::set<std::string>{ | 345 {ntp_snippets::kArticleSuggestionsFeature.name}); |
| 356 ntp_snippets::kArticleSuggestionsFeature.name}); | |
| 357 } | |
| 358 | |
| 359 void SetVariationParametersForFeatures( | |
| 360 const std::map<std::string, std::string>& params, | |
| 361 const std::set<std::string>& features) { | |
| 362 params_manager_.reset(); | |
| 363 params_manager_ = | |
| 364 base::MakeUnique<variations::testing::VariationParamsManager>( | |
| 365 ntp_snippets::kStudyName, params, features); | |
| 366 } | 346 } |
| 367 | 347 |
| 368 void SetFakeResponse(const std::string& response_data, | 348 void SetFakeResponse(const std::string& response_data, |
| 369 net::HttpStatusCode response_code, | 349 net::HttpStatusCode response_code, |
| 370 net::URLRequestStatus::Status status) { | 350 net::URLRequestStatus::Status status) { |
| 371 InitFakeURLFetcherFactory(); | 351 InitFakeURLFetcherFactory(); |
| 372 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data, | 352 fake_url_fetcher_factory_->SetFakeResponse(test_url_, response_data, |
| 373 response_code, status); | 353 response_code, status); |
| 374 } | 354 } |
| 375 | 355 |
| 376 TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); } | 356 TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); } |
| 377 | 357 |
| 358 protected: |
| 359 std::map<std::string, std::string> default_variation_params_; |
| 360 |
| 378 private: | 361 private: |
| 379 std::map<std::string, std::string> default_variation_params_; | 362 variations::testing::VariationParamsManager params_manager_; |
| 380 // TODO(fhorschig): Make it a simple member when crbug.com/672010 is resolved. | |
| 381 std::unique_ptr<variations::testing::VariationParamsManager> params_manager_; | |
| 382 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; | 363 scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; |
| 383 base::ThreadTaskRunnerHandle mock_task_runner_handle_; | 364 base::ThreadTaskRunnerHandle mock_task_runner_handle_; |
| 384 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 365 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 385 // Initialized lazily in SetFakeResponse(). | 366 // Initialized lazily in SetFakeResponse(). |
| 386 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 367 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
| 387 std::unique_ptr<TestSigninClient> signin_client_; | 368 std::unique_ptr<TestSigninClient> signin_client_; |
| 388 std::unique_ptr<AccountTrackerService> account_tracker_; | 369 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 389 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 370 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 390 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 371 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 391 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 372 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 402 public: | 383 public: |
| 403 ChromeReaderSnippetsFetcherTest() | 384 ChromeReaderSnippetsFetcherTest() |
| 404 : NTPSnippetsFetcherTestBase(GURL(kTestChromeReaderUrl)) {} | 385 : NTPSnippetsFetcherTestBase(GURL(kTestChromeReaderUrl)) {} |
| 405 }; | 386 }; |
| 406 | 387 |
| 407 class NTPSnippetsContentSuggestionsFetcherTest | 388 class NTPSnippetsContentSuggestionsFetcherTest |
| 408 : public NTPSnippetsFetcherTestBase { | 389 : public NTPSnippetsFetcherTestBase { |
| 409 public: | 390 public: |
| 410 NTPSnippetsContentSuggestionsFetcherTest() | 391 NTPSnippetsContentSuggestionsFetcherTest() |
| 411 : NTPSnippetsFetcherTestBase(GURL(kTestChromeContentSuggestionsUrl)) { | 392 : NTPSnippetsFetcherTestBase(GURL(kTestChromeContentSuggestionsUrl)) { |
| 412 SetDefaultVariationParam("content_suggestions_backend", | 393 default_variation_params_["content_suggestions_backend"] = |
| 413 kContentSuggestionsServer); | 394 kContentSuggestionsServer; |
| 395 SetVariationParam("content_suggestions_backend", kContentSuggestionsServer); |
| 414 ResetSnippetsFetcher(); | 396 ResetSnippetsFetcher(); |
| 415 } | 397 } |
| 416 }; | 398 }; |
| 417 | 399 |
| 418 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldNotFetchOnCreation) { | 400 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldNotFetchOnCreation) { |
| 419 // The lack of registered baked in responses would cause any fetch to fail. | 401 // The lack of registered baked in responses would cause any fetch to fail. |
| 420 FastForwardUntilNoTasksRemain(); | 402 FastForwardUntilNoTasksRemain(); |
| 421 EXPECT_THAT(histogram_tester().GetAllSamples( | 403 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 422 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 404 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 423 IsEmpty()); | 405 IsEmpty()); |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 {"", 0, "Do not retry by default"}, | 742 {"", 0, "Do not retry by default"}, |
| 761 {"0", 0, "Do not retry on param value 0"}, | 743 {"0", 0, "Do not retry on param value 0"}, |
| 762 {"-1", 0, "Do not retry on negative param values."}, | 744 {"-1", 0, "Do not retry on negative param values."}, |
| 763 {"4", 4, "Retry as set in param value."}}; | 745 {"4", 4, "Retry as set in param value."}}; |
| 764 | 746 |
| 765 NTPSnippetsRequestParams params = test_params(); | 747 NTPSnippetsRequestParams params = test_params(); |
| 766 params.interactive_request = false; | 748 params.interactive_request = false; |
| 767 | 749 |
| 768 for (const auto& retry_config : retry_config_expectation) { | 750 for (const auto& retry_config : retry_config_expectation) { |
| 769 DelegateCallingTestURLFetcherFactory fetcher_factory; | 751 DelegateCallingTestURLFetcherFactory fetcher_factory; |
| 770 SetVariationParametersForFeatures( | 752 SetVariationParam("background_5xx_retries_count", retry_config.param_value); |
| 771 {{"background_5xx_retries_count", retry_config.param_value}}, | |
| 772 {ntp_snippets::kArticleSuggestionsFeature.name}); | |
| 773 | 753 |
| 774 snippets_fetcher().FetchSnippets( | 754 snippets_fetcher().FetchSnippets( |
| 775 params, ToSnippetsAvailableCallback(&mock_callback())); | 755 params, ToSnippetsAvailableCallback(&mock_callback())); |
| 776 | 756 |
| 777 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher(); | 757 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher(); |
| 778 ASSERT_THAT(fetcher, NotNull()); | 758 ASSERT_THAT(fetcher, NotNull()); |
| 779 EXPECT_THAT(fetcher->GetMaxRetriesOn5xx(), Eq(retry_config.expected_value)) | 759 EXPECT_THAT(fetcher->GetMaxRetriesOn5xx(), Eq(retry_config.expected_value)) |
| 780 << retry_config.description; | 760 << retry_config.description; |
| 781 } | 761 } |
| 782 } | 762 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 914 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
| 935 if (fetched_categories) { | 915 if (fetched_categories) { |
| 936 // Matchers above aren't any more precise than this, so this is sufficient | 916 // Matchers above aren't any more precise than this, so this is sufficient |
| 937 // for test-failure diagnostics. | 917 // for test-failure diagnostics. |
| 938 return os << "list with " << fetched_categories->size() << " elements"; | 918 return os << "list with " << fetched_categories->size() << " elements"; |
| 939 } | 919 } |
| 940 return os << "null"; | 920 return os << "null"; |
| 941 } | 921 } |
| 942 | 922 |
| 943 } // namespace ntp_snippets | 923 } // namespace ntp_snippets |
| OLD | NEW |