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 21 matching lines...) Expand all Loading... |
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/features.h" |
36 #include "components/ntp_snippets/ntp_snippets_constants.h" | 36 #include "components/ntp_snippets/ntp_snippets_constants.h" |
37 #include "components/ntp_snippets/pref_names.h" | 37 #include "components/ntp_snippets/pref_names.h" |
38 #include "components/ntp_snippets/remote/persistent_scheduler.h" | 38 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
39 #include "components/ntp_snippets/remote/remote_suggestion.h" | 39 #include "components/ntp_snippets/remote/remote_suggestion.h" |
40 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 40 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
41 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" | 41 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" |
| 42 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
42 #include "components/ntp_snippets/remote/test_utils.h" | 43 #include "components/ntp_snippets/remote/test_utils.h" |
43 #include "components/ntp_snippets/user_classifier.h" | 44 #include "components/ntp_snippets/user_classifier.h" |
44 #include "components/prefs/testing_pref_service.h" | 45 #include "components/prefs/testing_pref_service.h" |
45 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 46 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
46 #include "components/signin/core/browser/fake_signin_manager.h" | 47 #include "components/signin/core/browser/fake_signin_manager.h" |
47 #include "components/variations/variations_params_manager.h" | 48 #include "components/variations/variations_params_manager.h" |
48 #include "net/url_request/test_url_fetcher_factory.h" | 49 #include "net/url_request/test_url_fetcher_factory.h" |
49 #include "net/url_request/url_request_test_util.h" | 50 #include "net/url_request/url_request_test_util.h" |
50 #include "testing/gmock/include/gmock/gmock.h" | 51 #include "testing/gmock/include/gmock/gmock.h" |
51 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 const image_fetcher::ImageDecodedCallback& callback) override { | 356 const image_fetcher::ImageDecodedCallback& callback) override { |
356 callback.Run(decoded_image_); | 357 callback.Run(decoded_image_); |
357 } | 358 } |
358 | 359 |
359 void SetDecodedImage(const gfx::Image& image) { decoded_image_ = image; } | 360 void SetDecodedImage(const gfx::Image& image) { decoded_image_ = image; } |
360 | 361 |
361 private: | 362 private: |
362 gfx::Image decoded_image_; | 363 gfx::Image decoded_image_; |
363 }; | 364 }; |
364 | 365 |
| 366 class MockScheduler : public RemoteSuggestionsScheduler { |
| 367 public: |
| 368 MOCK_METHOD0(OnProviderActivated, void()); |
| 369 MOCK_METHOD0(OnProviderInactivated, void()); |
| 370 MOCK_METHOD0(OnSuggestionsCleared, void()); |
| 371 MOCK_METHOD0(OnHistoryCleared, void()); |
| 372 MOCK_METHOD0(OnBrowserForegrounded, void()); |
| 373 MOCK_METHOD0(OnBrowserColdStart, void()); |
| 374 MOCK_METHOD0(OnNTPOpened, void()); |
| 375 MOCK_METHOD0(OnPersistentSchedulerWakeUp, void()); |
| 376 MOCK_METHOD0(RescheduleFetching, void()); |
| 377 }; |
| 378 |
365 } // namespace | 379 } // namespace |
366 | 380 |
367 class RemoteSuggestionsProviderImplTest : public ::testing::Test { | 381 class RemoteSuggestionsProviderImplTest : public ::testing::Test { |
368 public: | 382 public: |
369 RemoteSuggestionsProviderImplTest() | 383 RemoteSuggestionsProviderImplTest() |
370 : params_manager_(ntp_snippets::kStudyName, | 384 : params_manager_(ntp_snippets::kStudyName, |
371 {{"content_suggestions_backend", | 385 {{"content_suggestions_backend", |
372 kTestContentSuggestionsServerEndpoint}}, | 386 kTestContentSuggestionsServerEndpoint}}, |
373 {ntp_snippets::kArticleSuggestionsFeature.name}), | 387 {ntp_snippets::kArticleSuggestionsFeature.name}), |
374 fake_url_fetcher_factory_( | 388 fake_url_fetcher_factory_( |
(...skipping 1356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1731 service->RefetchInTheBackground(/*callback=*/nullptr); | 1745 service->RefetchInTheBackground(/*callback=*/nullptr); |
1732 base::RunLoop().RunUntilIdle(); | 1746 base::RunLoop().RunUntilIdle(); |
1733 // TODO(jkrcal): Move together with the pref storage into the scheduler. | 1747 // TODO(jkrcal): Move together with the pref storage into the scheduler. |
1734 EXPECT_EQ( | 1748 EXPECT_EQ( |
1735 simple_test_clock_ptr->Now().ToInternalValue(), | 1749 simple_test_clock_ptr->Now().ToInternalValue(), |
1736 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); | 1750 pref_service()->GetInt64(prefs::kLastSuccessfulBackgroundFetchTime)); |
1737 // TODO(markusheintz): Add a test that simulates a browser restart once the | 1751 // TODO(markusheintz): Add a test that simulates a browser restart once the |
1738 // scheduler refactoring is done (crbug.com/672434). | 1752 // scheduler refactoring is done (crbug.com/672434). |
1739 } | 1753 } |
1740 | 1754 |
1741 TEST_F(RemoteSuggestionsProviderImplTest, CallsProviderStatusCallbackIfReady) { | 1755 TEST_F(RemoteSuggestionsProviderImplTest, CallsSchedulerIfInited) { |
1742 // Initiate the service so that it is already READY. | 1756 // Initiate the service so that it is already READY. |
1743 auto service = MakeSuggestionsProvider(); | 1757 auto service = MakeSuggestionsProvider(); |
1744 | 1758 StrictMock<MockScheduler> scheduler; |
1745 StrictMock<MockFunction<void(RemoteSuggestionsProvider::ProviderStatus)>> | 1759 // The scheduler should be notified of activation of the provider. |
1746 status_callback; | 1760 EXPECT_CALL(scheduler, OnProviderActivated()); |
1747 // The callback should be called on registering. | 1761 service->SetRemoteSuggestionsScheduler(&scheduler); |
1748 EXPECT_CALL(status_callback, | |
1749 Call(RemoteSuggestionsProvider::ProviderStatus::ACTIVE)); | |
1750 service->SetProviderStatusCallback( | |
1751 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( | |
1752 base::Bind(&MockFunction<void( | |
1753 RemoteSuggestionsProvider::ProviderStatus)>::Call, | |
1754 base::Unretained(&status_callback)))); | |
1755 } | 1762 } |
1756 | 1763 |
1757 TEST_F(RemoteSuggestionsProviderImplTest, | 1764 TEST_F(RemoteSuggestionsProviderImplTest, DoesNotCallSchedulerIfNotInited) { |
1758 DoesNotCallProviderStatusCallbackIfNotInited) { | |
1759 auto service = MakeSuggestionsProviderWithoutInitialization(); | 1765 auto service = MakeSuggestionsProviderWithoutInitialization(); |
1760 | 1766 StrictMock<MockScheduler> scheduler; |
1761 StrictMock<MockFunction<void(RemoteSuggestionsProvider::ProviderStatus)>> | |
1762 status_callback; | |
1763 // The provider is not initialized yet, no callback should be called on | 1767 // The provider is not initialized yet, no callback should be called on |
1764 // registering. | 1768 // registering. |
1765 service->SetProviderStatusCallback( | 1769 service->SetRemoteSuggestionsScheduler(&scheduler); |
1766 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( | |
1767 base::Bind(&MockFunction<void( | |
1768 RemoteSuggestionsProvider::ProviderStatus)>::Call, | |
1769 base::Unretained(&status_callback)))); | |
1770 } | 1770 } |
1771 | 1771 |
1772 TEST_F(RemoteSuggestionsProviderImplTest, | 1772 TEST_F(RemoteSuggestionsProviderImplTest, CallsSchedulerWhenReady) { |
1773 CallsProviderStatusCallbackWhenReady) { | |
1774 auto service = MakeSuggestionsProviderWithoutInitialization(); | 1773 auto service = MakeSuggestionsProviderWithoutInitialization(); |
1775 StrictMock<MockFunction<void(RemoteSuggestionsProvider::ProviderStatus)>> | 1774 StrictMock<MockScheduler> scheduler; |
1776 status_callback; | 1775 // The provider is not initialized yet, no callback should be called yet. |
1777 service->SetProviderStatusCallback( | 1776 service->SetRemoteSuggestionsScheduler(&scheduler); |
1778 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( | |
1779 base::Bind(&MockFunction<void( | |
1780 RemoteSuggestionsProvider::ProviderStatus)>::Call, | |
1781 base::Unretained(&status_callback)))); | |
1782 | 1777 |
1783 // Should be called when becoming ready. | 1778 // Should be called when becoming ready. |
1784 EXPECT_CALL(status_callback, | 1779 EXPECT_CALL(scheduler, OnProviderActivated()); |
1785 Call(RemoteSuggestionsProvider::ProviderStatus::ACTIVE)); | |
1786 WaitForSuggestionsProviderInitialization(service.get(), | 1780 WaitForSuggestionsProviderInitialization(service.get(), |
1787 /*set_empty_response=*/true); | 1781 /*set_empty_response=*/true); |
1788 } | 1782 } |
1789 | 1783 |
1790 TEST_F(RemoteSuggestionsProviderImplTest, CallsProviderStatusCallbackOnError) { | 1784 TEST_F(RemoteSuggestionsProviderImplTest, CallsSchedulerOnError) { |
1791 auto service = MakeSuggestionsProviderWithoutInitialization(); | 1785 auto service = MakeSuggestionsProviderWithoutInitialization(); |
1792 StrictMock<MockFunction<void(RemoteSuggestionsProvider::ProviderStatus)>> | 1786 StrictMock<MockScheduler> scheduler; |
1793 status_callback; | 1787 // The provider is not initialized yet, no callback should be called yet. |
1794 service->SetProviderStatusCallback( | 1788 service->SetRemoteSuggestionsScheduler(&scheduler); |
1795 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( | |
1796 base::Bind(&MockFunction<void( | |
1797 RemoteSuggestionsProvider::ProviderStatus)>::Call, | |
1798 base::Unretained(&status_callback)))); | |
1799 | 1789 |
1800 // Should be called on error. | 1790 // Should be called on error. |
1801 EXPECT_CALL(status_callback, | 1791 EXPECT_CALL(scheduler, OnProviderInactivated()); |
1802 Call(RemoteSuggestionsProvider::ProviderStatus::INACTIVE)); | |
1803 service->EnterState(RemoteSuggestionsProviderImpl::State::ERROR_OCCURRED); | 1792 service->EnterState(RemoteSuggestionsProviderImpl::State::ERROR_OCCURRED); |
1804 } | 1793 } |
1805 | 1794 |
1806 TEST_F(RemoteSuggestionsProviderImplTest, | 1795 TEST_F(RemoteSuggestionsProviderImplTest, CallsSchedulerWhenDisabled) { |
1807 CallsProviderStatusCallbackWhenDisabled) { | |
1808 auto service = MakeSuggestionsProviderWithoutInitialization(); | 1796 auto service = MakeSuggestionsProviderWithoutInitialization(); |
1809 StrictMock<MockFunction<void(RemoteSuggestionsProvider::ProviderStatus)>> | 1797 StrictMock<MockScheduler> scheduler; |
1810 status_callback; | 1798 // The provider is not initialized yet, no callback should be called yet. |
1811 service->SetProviderStatusCallback( | 1799 service->SetRemoteSuggestionsScheduler(&scheduler); |
1812 base::MakeUnique<RemoteSuggestionsProvider::ProviderStatusCallback>( | |
1813 base::Bind(&MockFunction<void( | |
1814 RemoteSuggestionsProvider::ProviderStatus)>::Call, | |
1815 base::Unretained(&status_callback)))); | |
1816 | 1800 |
1817 // Should be called when becoming disabled. | 1801 // Should be called when becoming disabled. |
1818 EXPECT_CALL(status_callback, | 1802 EXPECT_CALL(scheduler, OnProviderInactivated()); |
1819 Call(RemoteSuggestionsProvider::ProviderStatus::INACTIVE)); | |
1820 service->EnterState(RemoteSuggestionsProviderImpl::State::DISABLED); | 1803 service->EnterState(RemoteSuggestionsProviderImpl::State::DISABLED); |
1821 } | 1804 } |
1822 | 1805 |
1823 } // namespace ntp_snippets | 1806 } // namespace ntp_snippets |
OLD | NEW |