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/scheduling_remote_suggestions_provider. h" | 5 #include "components/ntp_snippets/remote/scheduling_remote_suggestions_provider. h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 MOCK_METHOD1(GetCategoryStatus, CategoryStatus(Category)); | 91 MOCK_METHOD1(GetCategoryStatus, CategoryStatus(Category)); |
| 92 MOCK_METHOD1(GetCategoryInfo, CategoryInfo(Category)); | 92 MOCK_METHOD1(GetCategoryInfo, CategoryInfo(Category)); |
| 93 MOCK_METHOD3(ClearHistory, | 93 MOCK_METHOD3(ClearHistory, |
| 94 void(base::Time begin, | 94 void(base::Time begin, |
| 95 base::Time end, | 95 base::Time end, |
| 96 const base::Callback<bool(const GURL& url)>& filter)); | 96 const base::Callback<bool(const GURL& url)>& filter)); |
| 97 MOCK_METHOD3(Fetch, | 97 MOCK_METHOD3(Fetch, |
| 98 void(const Category&, | 98 void(const Category&, |
| 99 const std::set<std::string>&, | 99 const std::set<std::string>&, |
| 100 const FetchDoneCallback&)); | 100 const FetchDoneCallback&)); |
| 101 MOCK_METHOD0(ReloadSuggestions, void()); | |
| 101 MOCK_METHOD1(ClearCachedSuggestions, void(Category)); | 102 MOCK_METHOD1(ClearCachedSuggestions, void(Category)); |
| 102 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category)); | 103 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category)); |
| 103 MOCK_METHOD1(DismissSuggestion, void(const ContentSuggestion::ID&)); | 104 MOCK_METHOD1(DismissSuggestion, void(const ContentSuggestion::ID&)); |
| 104 MOCK_METHOD2(FetchSuggestionImage, | 105 MOCK_METHOD2(FetchSuggestionImage, |
| 105 void(const ContentSuggestion::ID&, const ImageFetchedCallback&)); | 106 void(const ContentSuggestion::ID&, const ImageFetchedCallback&)); |
| 106 MOCK_METHOD2(GetDismissedSuggestionsForDebugging, | 107 MOCK_METHOD2(GetDismissedSuggestionsForDebugging, |
| 107 void(Category, const DismissedSuggestionsCallback&)); | 108 void(Category, const DismissedSuggestionsCallback&)); |
| 108 MOCK_METHOD0(OnSignInStateChanged, void()); | 109 MOCK_METHOD0(OnSignInStateChanged, void()); |
| 109 }; | 110 }; |
| 110 | 111 |
| 111 } // namespace | 112 } // namespace |
| 112 | 113 |
| 113 class SchedulingRemoteSuggestionsProviderTest | 114 class SchedulingRemoteSuggestionsProviderTest |
| 114 : public ::testing::Test { | 115 : public ::testing::Test { |
| 115 public: | 116 public: |
| 116 SchedulingRemoteSuggestionsProviderTest() | 117 SchedulingRemoteSuggestionsProviderTest() |
| 117 : // For the test we enabled all trigger types. | 118 : // For the test we enabled all trigger types. |
| 118 default_variation_params_{{"scheduler_trigger_types", | 119 default_variation_params_{{"scheduler_trigger_types", |
| 119 "persistent_scheduler_wake_up,ntp_opened," | 120 "persistent_scheduler_wake_up,ntp_opened," |
| 120 "browser_foregrounded,browser_cold_start"}}, | 121 "browser_foregrounded,browser_cold_start"}}, |
| 121 params_manager_(ntp_snippets::kStudyName, | 122 params_manager_(ntp_snippets::kStudyName, |
| 122 default_variation_params_, | 123 default_variation_params_, |
| 123 {kArticleSuggestionsFeature.name}), | 124 {kArticleSuggestionsFeature.name}), |
| 124 underlying_provider_(nullptr), | 125 underlying_provider_(nullptr), |
| 125 scheduling_provider_(nullptr), | 126 scheduling_provider_(nullptr), |
| 126 user_classifier_(/*pref_service=*/nullptr) { | 127 user_classifier_(/*pref_service=*/nullptr) { |
| 127 SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( | 128 SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( |
| 128 utils_.pref_service()->registry()); | 129 utils_.pref_service()->registry()); |
| 130 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | |
| 129 ResetProvider(); | 131 ResetProvider(); |
| 130 } | 132 } |
| 131 | 133 |
| 132 void ResetProvider() { | 134 void ResetProvider() { |
| 133 auto underlying_provider = | 135 auto underlying_provider = |
| 134 base::MakeUnique<StrictMock<MockRemoteSuggestionsProvider>>( | 136 base::MakeUnique<StrictMock<MockRemoteSuggestionsProvider>>( |
| 135 /*observer=*/nullptr); | 137 /*observer=*/nullptr); |
| 136 underlying_provider_ = underlying_provider.get(); | 138 underlying_provider_ = underlying_provider.get(); |
| 137 | 139 |
| 138 // SchedulingRemoteSuggestionsProvider calls | 140 // SchedulingRemoteSuggestionsProvider calls |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 670 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 672 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); |
| 671 signal_fetch_done.Run(Status::Success()); | 673 signal_fetch_done.Run(Status::Success()); |
| 672 | 674 |
| 673 // Clear the suggestions. | 675 // Clear the suggestions. |
| 674 scheduling_provider_->OnSuggestionsCleared(); | 676 scheduling_provider_->OnSuggestionsCleared(); |
| 675 // Another trigger right after results in a fetch again. | 677 // Another trigger right after results in a fetch again. |
| 676 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 678 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); |
| 677 scheduling_provider_->OnBrowserForegrounded(); | 679 scheduling_provider_->OnBrowserForegrounded(); |
| 678 } | 680 } |
| 679 | 681 |
| 682 TEST_F(SchedulingRemoteSuggestionsProviderTest, | |
| 683 ShouldThrottleInteractiveRequests) { | |
| 684 // Change the quota for interactive requests ("active NTP user" is the default | |
| 685 // class in tests). | |
| 686 SetVariationParameter("interactive_quota_SuggestionFetcherActiveNTPUser", | |
| 687 "10"); | |
| 688 ResetProvider(); | |
| 689 | |
| 690 Category category = Category::FromKnownCategory(KnownCategories::ARTICLES); | |
| 691 std::set<std::string> known_suggestions; | |
| 692 | |
| 693 // As long as the quota suffices, the call gets through. | |
|
Marc Treib
2017/02/24 15:50:35
Maybe mention that both ReloadSuggestions and Fetc
jkrcal
2017/02/27 10:26:23
Added a comment. The reason to mix these two is to
| |
| 694 EXPECT_CALL(*underlying_provider_, ReloadSuggestions()).Times(5); | |
| 695 EXPECT_CALL(*underlying_provider_, Fetch(_, _, _)).Times(5); | |
| 696 for (int x = 0; x < 5; ++x) { | |
| 697 scheduling_provider_->ReloadSuggestions(); | |
| 698 scheduling_provider_->Fetch(category, known_suggestions, | |
| 699 FetchDoneCallback()); | |
| 700 } | |
| 701 | |
| 702 // When the quota expires, it is blocked by the scheduling provider. | |
| 703 scheduling_provider_->ReloadSuggestions(); | |
| 704 scheduling_provider_->Fetch(category, known_suggestions, FetchDoneCallback()); | |
|
tschumann
2017/02/24 10:18:08
shouldn't we verify the result codes?
jkrcal
2017/02/27 10:26:23
Done.
| |
| 705 } | |
| 706 | |
| 707 TEST_F(SchedulingRemoteSuggestionsProviderTest, | |
| 708 ShouldThrottleNonInteractiveRequests) { | |
| 709 // Change the quota for interactive requests ("active NTP user" is the default | |
| 710 // class in tests). | |
| 711 SetVariationParameter("quota_SuggestionFetcherActiveNTPUser", "5"); | |
| 712 ResetProvider(); | |
| 713 | |
| 714 // One scheduling on start, 5 times after successful fetches. | |
| 715 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(6); | |
| 716 | |
| 717 // First enable the scheduler -- this will trigger the persistent scheduling. | |
| 718 ActivateUnderlyingProvider(); | |
| 719 | |
| 720 // As long as the quota suffices, the call gets through. | |
| 721 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | |
| 722 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | |
| 723 .Times(5) | |
| 724 .WillRepeatedly(SaveArg<0>(&signal_fetch_done)); | |
| 725 for (int x = 0; x < 5; ++x) { | |
| 726 scheduling_provider_->OnPersistentSchedulerWakeUp(); | |
| 727 signal_fetch_done.Run(Status::Success()); | |
| 728 } | |
| 729 | |
| 730 // For the 6th time, it is blocked by the scheduling provider. | |
| 731 scheduling_provider_->OnPersistentSchedulerWakeUp(); | |
| 732 } | |
| 733 | |
| 680 } // namespace ntp_snippets | 734 } // namespace ntp_snippets |
| OLD | NEW |