| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/remote_suggestions_provider_impl.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 26 #include "components/image_fetcher/image_decoder.h" | 26 #include "components/image_fetcher/image_decoder.h" |
| 27 #include "components/image_fetcher/image_fetcher.h" | 27 #include "components/image_fetcher/image_fetcher.h" |
| 28 #include "components/image_fetcher/image_fetcher_delegate.h" | 28 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 29 #include "components/ntp_snippets/category.h" | 29 #include "components/ntp_snippets/category.h" |
| 30 #include "components/ntp_snippets/category_info.h" | 30 #include "components/ntp_snippets/category_info.h" |
| 31 #include "components/ntp_snippets/category_rankers/category_ranker.h" | 31 #include "components/ntp_snippets/category_rankers/category_ranker.h" |
| 32 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h" | 32 #include "components/ntp_snippets/category_rankers/constant_category_ranker.h" |
| 33 #include "components/ntp_snippets/category_rankers/mock_category_ranker.h" | 33 #include "components/ntp_snippets/category_rankers/mock_category_ranker.h" |
| 34 #include "components/ntp_snippets/fake_content_suggestions_provider_observer.h" | 34 #include "components/ntp_snippets/fake_content_suggestions_provider_observer.h" |
| 35 #include "components/ntp_snippets/features.h" |
| 35 #include "components/ntp_snippets/ntp_snippets_constants.h" | 36 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 36 #include "components/ntp_snippets/pref_names.h" | 37 #include "components/ntp_snippets/pref_names.h" |
| 37 #include "components/ntp_snippets/remote/persistent_scheduler.h" | 38 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
| 38 #include "components/ntp_snippets/remote/remote_suggestion.h" | 39 #include "components/ntp_snippets/remote/remote_suggestion.h" |
| 39 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 40 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
| 40 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" | 41 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" |
| 41 #include "components/ntp_snippets/remote/test_utils.h" | 42 #include "components/ntp_snippets/remote/test_utils.h" |
| 42 #include "components/ntp_snippets/user_classifier.h" | 43 #include "components/ntp_snippets/user_classifier.h" |
| 43 #include "components/prefs/testing_pref_service.h" | 44 #include "components/prefs/testing_pref_service.h" |
| 44 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 45 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 MATCHER_P(IsCategory, id, "") { | 85 MATCHER_P(IsCategory, id, "") { |
| 85 return arg.id() == static_cast<int>(id); | 86 return arg.id() == static_cast<int>(id); |
| 86 } | 87 } |
| 87 | 88 |
| 88 MATCHER_P(HasCode, code, "") { | 89 MATCHER_P(HasCode, code, "") { |
| 89 return arg.code == code; | 90 return arg.code == code; |
| 90 } | 91 } |
| 91 | 92 |
| 92 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; | 93 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; |
| 93 const char kTestContentSuggestionsServerEndpoint[] = | 94 const char kTestContentSuggestionsServerEndpoint[] = |
| 94 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | 95 "https://alpha-chromecontentsuggestions-pa.sandbox.googleapis.com/v1/" |
| 95 "suggestions/fetch"; | 96 "suggestions/fetch"; |
| 96 const char kAPIKey[] = "fakeAPIkey"; | 97 const char kAPIKey[] = "fakeAPIkey"; |
| 97 const char kTestContentSuggestionsServerWithAPIKey[] = | 98 const char kTestContentSuggestionsServerWithAPIKey[] = |
| 98 "https://localunittest-chromecontentsuggestions-pa.googleapis.com/v1/" | 99 "https://alpha-chromecontentsuggestions-pa.sandbox.googleapis.com/v1/" |
| 99 "suggestions/fetch?key=fakeAPIkey"; | 100 "suggestions/fetch?key=fakeAPIkey"; |
| 100 | 101 |
| 101 const char kSuggestionUrl[] = "http://localhost/foobar"; | 102 const char kSuggestionUrl[] = "http://localhost/foobar"; |
| 102 const char kSuggestionTitle[] = "Title"; | 103 const char kSuggestionTitle[] = "Title"; |
| 103 const char kSuggestionText[] = "Suggestion"; | 104 const char kSuggestionText[] = "Suggestion"; |
| 104 const char kSuggestionSalientImage[] = "http://localhost/salient_image"; | 105 const char kSuggestionSalientImage[] = "http://localhost/salient_image"; |
| 105 const char kSuggestionPublisherName[] = "Foo News"; | 106 const char kSuggestionPublisherName[] = "Foo News"; |
| 106 const char kSuggestionAmpUrl[] = "http://localhost/amp"; | 107 const char kSuggestionAmpUrl[] = "http://localhost/amp"; |
| 107 | 108 |
| 108 const char kSuggestionUrl2[] = "http://foo.com/bar"; | 109 const char kSuggestionUrl2[] = "http://foo.com/bar"; |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 gfx::Image decoded_image_; | 362 gfx::Image decoded_image_; |
| 362 }; | 363 }; |
| 363 | 364 |
| 364 } // namespace | 365 } // namespace |
| 365 | 366 |
| 366 class RemoteSuggestionsProviderImplTest : public ::testing::Test { | 367 class RemoteSuggestionsProviderImplTest : public ::testing::Test { |
| 367 public: | 368 public: |
| 368 RemoteSuggestionsProviderImplTest() | 369 RemoteSuggestionsProviderImplTest() |
| 369 : params_manager_(ntp_snippets::kStudyName, | 370 : params_manager_(ntp_snippets::kStudyName, |
| 370 {{"content_suggestions_backend", | 371 {{"content_suggestions_backend", |
| 371 kTestContentSuggestionsServerEndpoint}, | 372 kTestContentSuggestionsServerEndpoint}}, |
| 372 {"fetching_personalization", "non_personal"}}), | 373 {ntp_snippets::kArticleSuggestionsFeature.name}), |
| 373 fake_url_fetcher_factory_( | 374 fake_url_fetcher_factory_( |
| 374 /*default_factory=*/&failing_url_fetcher_factory_), | 375 /*default_factory=*/&failing_url_fetcher_factory_), |
| 375 test_url_(kTestContentSuggestionsServerWithAPIKey), | 376 test_url_(kTestContentSuggestionsServerWithAPIKey), |
| 376 category_ranker_(base::MakeUnique<ConstantCategoryRanker>()), | 377 category_ranker_(base::MakeUnique<ConstantCategoryRanker>()), |
| 377 user_classifier_(/*pref_service=*/nullptr), | 378 user_classifier_(/*pref_service=*/nullptr), |
| 378 suggestions_fetcher_(nullptr), | 379 suggestions_fetcher_(nullptr), |
| 379 image_fetcher_(nullptr), | 380 image_fetcher_(nullptr), |
| 380 image_decoder_(nullptr), | 381 image_decoder_(nullptr), |
| 381 database_(nullptr) { | 382 database_(nullptr) { |
| 382 RemoteSuggestionsProviderImpl::RegisterProfilePrefs( | 383 RemoteSuggestionsProviderImpl::RegisterProfilePrefs( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 404 | 405 |
| 405 std::unique_ptr<RemoteSuggestionsProviderImpl> | 406 std::unique_ptr<RemoteSuggestionsProviderImpl> |
| 406 MakeSuggestionsProviderWithoutInitialization() { | 407 MakeSuggestionsProviderWithoutInitialization() { |
| 407 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 408 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 408 base::ThreadTaskRunnerHandle::Get()); | 409 base::ThreadTaskRunnerHandle::Get()); |
| 409 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 410 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 410 new net::TestURLRequestContextGetter(task_runner.get()); | 411 new net::TestURLRequestContextGetter(task_runner.get()); |
| 411 | 412 |
| 412 utils_.ResetSigninManager(); | 413 utils_.ResetSigninManager(); |
| 413 auto suggestions_fetcher = base::MakeUnique<RemoteSuggestionsFetcher>( | 414 auto suggestions_fetcher = base::MakeUnique<RemoteSuggestionsFetcher>( |
| 414 utils_.fake_signin_manager(), fake_token_service_.get(), | 415 utils_.fake_signin_manager(), /*token_service=*/nullptr, |
| 415 std::move(request_context_getter), utils_.pref_service(), nullptr, | 416 std::move(request_context_getter), utils_.pref_service(), nullptr, |
| 416 base::Bind(&ParseJson), kAPIKey, &user_classifier_); | 417 base::Bind(&ParseJson), kAPIKey, &user_classifier_); |
| 417 suggestions_fetcher_ = suggestions_fetcher.get(); | 418 suggestions_fetcher_ = suggestions_fetcher.get(); |
| 418 | 419 |
| 419 utils_.fake_signin_manager()->SignIn("foo@bar.com"); | |
| 420 | |
| 421 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); | 420 auto image_fetcher = base::MakeUnique<NiceMock<MockImageFetcher>>(); |
| 422 | 421 |
| 423 image_fetcher_ = image_fetcher.get(); | 422 image_fetcher_ = image_fetcher.get(); |
| 424 EXPECT_CALL(*image_fetcher, SetImageFetcherDelegate(_)); | 423 EXPECT_CALL(*image_fetcher, SetImageFetcherDelegate(_)); |
| 425 auto image_decoder = base::MakeUnique<FakeImageDecoder>(); | 424 auto image_decoder = base::MakeUnique<FakeImageDecoder>(); |
| 426 image_decoder_ = image_decoder.get(); | 425 image_decoder_ = image_decoder.get(); |
| 427 EXPECT_FALSE(observer_); | 426 EXPECT_FALSE(observer_); |
| 428 observer_ = base::MakeUnique<FakeContentSuggestionsProviderObserver>(); | 427 observer_ = base::MakeUnique<FakeContentSuggestionsProviderObserver>(); |
| 429 auto database = base::MakeUnique<RemoteSuggestionsDatabase>( | 428 auto database = base::MakeUnique<RemoteSuggestionsDatabase>( |
| 430 database_dir_.GetPath(), task_runner); | 429 database_dir_.GetPath(), task_runner); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 } | 531 } |
| 533 | 532 |
| 534 private: | 533 private: |
| 535 variations::testing::VariationParamsManager params_manager_; | 534 variations::testing::VariationParamsManager params_manager_; |
| 536 test::RemoteSuggestionsTestUtils utils_; | 535 test::RemoteSuggestionsTestUtils utils_; |
| 537 base::MessageLoop message_loop_; | 536 base::MessageLoop message_loop_; |
| 538 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 537 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
| 539 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 538 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
| 540 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 539 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
| 541 const GURL test_url_; | 540 const GURL test_url_; |
| 542 std::unique_ptr<OAuth2TokenService> fake_token_service_; | |
| 543 std::unique_ptr<CategoryRanker> category_ranker_; | 541 std::unique_ptr<CategoryRanker> category_ranker_; |
| 544 UserClassifier user_classifier_; | 542 UserClassifier user_classifier_; |
| 545 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; | 543 std::unique_ptr<FakeContentSuggestionsProviderObserver> observer_; |
| 546 RemoteSuggestionsFetcher* suggestions_fetcher_; | 544 RemoteSuggestionsFetcher* suggestions_fetcher_; |
| 547 NiceMock<MockImageFetcher>* image_fetcher_; | 545 NiceMock<MockImageFetcher>* image_fetcher_; |
| 548 FakeImageDecoder* image_decoder_; | 546 FakeImageDecoder* image_decoder_; |
| 549 | 547 |
| 550 base::ScopedTempDir database_dir_; | 548 base::ScopedTempDir database_dir_; |
| 551 RemoteSuggestionsDatabase* database_; | 549 RemoteSuggestionsDatabase* database_; |
| 552 | 550 |
| (...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1816 RemoteSuggestionsProvider::ProviderStatus)>::Call, | 1814 RemoteSuggestionsProvider::ProviderStatus)>::Call, |
| 1817 base::Unretained(&status_callback)))); | 1815 base::Unretained(&status_callback)))); |
| 1818 | 1816 |
| 1819 // Should be called when becoming disabled. | 1817 // Should be called when becoming disabled. |
| 1820 EXPECT_CALL(status_callback, | 1818 EXPECT_CALL(status_callback, |
| 1821 Call(RemoteSuggestionsProvider::ProviderStatus::INACTIVE)); | 1819 Call(RemoteSuggestionsProvider::ProviderStatus::INACTIVE)); |
| 1822 service->EnterState(RemoteSuggestionsProviderImpl::State::DISABLED); | 1820 service->EnterState(RemoteSuggestionsProviderImpl::State::DISABLED); |
| 1823 } | 1821 } |
| 1824 | 1822 |
| 1825 } // namespace ntp_snippets | 1823 } // namespace ntp_snippets |
| OLD | NEW |