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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/test/simple_test_clock.h" | 18 #include "base/test/simple_test_clock.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/clock.h" | 20 #include "base/time/clock.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "components/ntp_snippets/features.h" | 22 #include "components/ntp_snippets/features.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_constants.h" | 23 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 24 #include "components/ntp_snippets/pref_names.h" | 24 #include "components/ntp_snippets/pref_names.h" |
| 25 #include "components/ntp_snippets/remote/persistent_scheduler.h" | 25 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
| 26 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" | 26 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 27 #include "components/ntp_snippets/remote/test_utils.h" | 27 #include "components/ntp_snippets/remote/test_utils.h" |
| 28 #include "components/ntp_snippets/status.h" | 28 #include "components/ntp_snippets/status.h" |
| 29 #include "components/ntp_snippets/user_classifier.h" | 29 #include "components/ntp_snippets/user_classifier.h" |
| 30 #include "components/prefs/pref_registry_simple.h" | |
| 30 #include "components/prefs/testing_pref_service.h" | 31 #include "components/prefs/testing_pref_service.h" |
| 31 #include "components/variations/variations_params_manager.h" | 32 #include "components/variations/variations_params_manager.h" |
| 33 #include "components/web_resource/web_resource_pref_names.h" | |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 36 |
| 35 using testing::ElementsAre; | 37 using testing::ElementsAre; |
| 36 using testing::Eq; | 38 using testing::Eq; |
| 37 using testing::Field; | 39 using testing::Field; |
| 38 using testing::InSequence; | 40 using testing::InSequence; |
| 39 using testing::Invoke; | 41 using testing::Invoke; |
| 40 using testing::IsEmpty; | 42 using testing::IsEmpty; |
| 41 using testing::Mock; | 43 using testing::Mock; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 "browser_foregrounded,browser_cold_start"}}, | 124 "browser_foregrounded,browser_cold_start"}}, |
| 123 params_manager_(ntp_snippets::kStudyName, | 125 params_manager_(ntp_snippets::kStudyName, |
| 124 default_variation_params_, | 126 default_variation_params_, |
| 125 {kArticleSuggestionsFeature.name}), | 127 {kArticleSuggestionsFeature.name}), |
| 126 underlying_provider_(nullptr), | 128 underlying_provider_(nullptr), |
| 127 scheduling_provider_(nullptr), | 129 scheduling_provider_(nullptr), |
| 128 user_classifier_(/*pref_service=*/nullptr) { | 130 user_classifier_(/*pref_service=*/nullptr) { |
| 129 SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( | 131 SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( |
| 130 utils_.pref_service()->registry()); | 132 utils_.pref_service()->registry()); |
| 131 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | 133 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 134 // TODO(jkrcal) Create a static function in EulaAcceptedNotifier that | |
| 135 // registers this pref and replace the call in browser_process_impl.cc & in | |
| 136 // eula_accepted_notifier_unittest.cc with the new static function. | |
| 137 local_state_.registry()->RegisterBooleanPref(::prefs::kEulaAccepted, false); | |
| 132 ResetProvider(); | 138 ResetProvider(); |
| 133 } | 139 } |
| 134 | 140 |
| 135 void ResetProvider() { | 141 void ResetProvider() { |
| 136 auto underlying_provider = | 142 auto underlying_provider = |
| 137 base::MakeUnique<StrictMock<MockRemoteSuggestionsProvider>>( | 143 base::MakeUnique<StrictMock<MockRemoteSuggestionsProvider>>( |
| 138 /*observer=*/nullptr); | 144 /*observer=*/nullptr); |
| 139 underlying_provider_ = underlying_provider.get(); | 145 underlying_provider_ = underlying_provider.get(); |
| 140 | 146 |
| 141 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 147 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 142 test_clock_ = test_clock.get(); | 148 test_clock_ = test_clock.get(); |
| 143 test_clock_->SetNow(base::Time::Now()); | 149 test_clock_->SetNow(base::Time::Now()); |
| 144 | 150 |
| 145 scheduling_provider_ = | 151 scheduling_provider_ = |
| 146 base::MakeUnique<SchedulingRemoteSuggestionsProvider>( | 152 base::MakeUnique<SchedulingRemoteSuggestionsProvider>( |
| 147 /*observer=*/nullptr, std::move(underlying_provider), | 153 /*observer=*/nullptr, std::move(underlying_provider), |
| 148 &persistent_scheduler_, &user_classifier_, utils_.pref_service(), | 154 &persistent_scheduler_, &user_classifier_, utils_.pref_service(), |
| 149 std::move(test_clock)); | 155 &local_state_, std::move(test_clock)); |
| 150 } | 156 } |
| 151 | 157 |
| 152 void SetVariationParameter(const std::string& param_name, | 158 void SetVariationParameter(const std::string& param_name, |
| 153 const std::string& param_value) { | 159 const std::string& param_value) { |
| 154 std::map<std::string, std::string> params = default_variation_params_; | 160 std::map<std::string, std::string> params = default_variation_params_; |
| 155 params[param_name] = param_value; | 161 params[param_name] = param_value; |
| 156 | 162 |
| 157 params_manager_.ClearAllVariationParams(); | 163 params_manager_.ClearAllVariationParams(); |
| 158 params_manager_.SetVariationParamsWithFeatureAssociations( | 164 params_manager_.SetVariationParamsWithFeatureAssociations( |
| 159 ntp_snippets::kStudyName, params, | 165 ntp_snippets::kStudyName, params, |
| 160 {ntp_snippets::kArticleSuggestionsFeature.name}); | 166 {ntp_snippets::kArticleSuggestionsFeature.name}); |
| 161 } | 167 } |
| 162 | 168 |
| 169 bool IsEulaNotifierAvailable() { | |
| 170 // Just check that the constructer does not return null unique_ptr. The | |
| 171 // notifier will be again desctructed right away. | |
| 172 return web_resource::EulaAcceptedNotifier::Create(&local_state_); | |
|
tschumann
2017/03/22 17:56:41
let's make this more specific and add != nullptr
y
jkrcal
2017/03/23 09:29:42
Done.
| |
| 173 } | |
| 174 | |
| 175 void SetEulaAcceptedPref() { | |
| 176 local_state_.SetBoolean(::prefs::kEulaAccepted, true); | |
| 177 } | |
| 178 | |
| 163 // GMock cannot deal with move-only types. We need to pass the vector to the | 179 // GMock cannot deal with move-only types. We need to pass the vector to the |
| 164 // mock function as const ref using this wrapper callback. | 180 // mock function as const ref using this wrapper callback. |
| 165 void FetchDoneWrapper( | 181 void FetchDoneWrapper( |
| 166 MockFunction<void(Status status_code, | 182 MockFunction<void(Status status_code, |
| 167 const std::vector<ContentSuggestion>& suggestions)>* | 183 const std::vector<ContentSuggestion>& suggestions)>* |
| 168 fetch_done, | 184 fetch_done, |
| 169 Status status_code, | 185 Status status_code, |
| 170 std::vector<ContentSuggestion> suggestions) { | 186 std::vector<ContentSuggestion> suggestions) { |
| 171 fetch_done->Call(status_code, suggestions); | 187 fetch_done->Call(status_code, suggestions); |
| 172 } | 188 } |
| 173 | 189 |
| 174 protected: | 190 protected: |
| 175 std::map<std::string, std::string> default_variation_params_; | 191 std::map<std::string, std::string> default_variation_params_; |
| 176 variations::testing::VariationParamsManager params_manager_; | 192 variations::testing::VariationParamsManager params_manager_; |
| 177 StrictMock<MockPersistentScheduler> persistent_scheduler_; | 193 StrictMock<MockPersistentScheduler> persistent_scheduler_; |
| 178 StrictMock<MockRemoteSuggestionsProvider>* underlying_provider_; | 194 StrictMock<MockRemoteSuggestionsProvider>* underlying_provider_; |
| 179 std::unique_ptr<SchedulingRemoteSuggestionsProvider> scheduling_provider_; | 195 std::unique_ptr<SchedulingRemoteSuggestionsProvider> scheduling_provider_; |
| 180 base::SimpleTestClock* test_clock_; | 196 base::SimpleTestClock* test_clock_; |
| 181 | 197 |
| 182 void ActivateUnderlyingProvider() { | 198 void ActivateUnderlyingProvider() { |
| 199 SetEulaAcceptedPref(); | |
| 183 scheduling_provider_->OnProviderActivated(); | 200 scheduling_provider_->OnProviderActivated(); |
| 184 } | 201 } |
| 185 | 202 |
| 203 void ActivateUnderlyingProviderWithoutEula() { | |
|
tschumann
2017/03/22 17:56:41
I'd get rid of this function as it's misleading. T
jkrcal
2017/03/23 09:29:43
Done.
| |
| 204 scheduling_provider_->OnProviderActivated(); | |
| 205 } | |
| 206 | |
| 186 void InactivateUnderlyingProvider() { | 207 void InactivateUnderlyingProvider() { |
| 187 scheduling_provider_->OnProviderDeactivated(); | 208 scheduling_provider_->OnProviderDeactivated(); |
| 188 } | 209 } |
| 189 | 210 |
| 190 private: | 211 private: |
| 191 test::RemoteSuggestionsTestUtils utils_; | 212 test::RemoteSuggestionsTestUtils utils_; |
| 192 UserClassifier user_classifier_; | 213 UserClassifier user_classifier_; |
| 214 TestingPrefServiceSimple local_state_; | |
| 193 | 215 |
| 194 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProviderTest); | 216 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProviderTest); |
| 195 }; | 217 }; |
| 196 | 218 |
| 197 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 219 TEST_F(SchedulingRemoteSuggestionsProviderTest, |
| 198 ShouldIgnoreSignalsWhenNotEnabled) { | 220 ShouldIgnoreSignalsWhenNotEnabled) { |
| 199 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 221 scheduling_provider_->OnPersistentSchedulerWakeUp(); |
| 200 scheduling_provider_->OnNTPOpened(); | 222 scheduling_provider_->OnNTPOpened(); |
| 201 scheduling_provider_->OnBrowserForegrounded(); | 223 scheduling_provider_->OnBrowserForegrounded(); |
| 202 scheduling_provider_->OnBrowserColdStart(); | 224 scheduling_provider_->OnBrowserColdStart(); |
| 203 } | 225 } |
| 204 | 226 |
| 205 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 227 TEST_F(SchedulingRemoteSuggestionsProviderTest, |
| 228 ShouldIgnoreEulaStateOnPlatformsWhereNotAvaiable) { | |
| 229 // Do not run the test if Eula notifier is available | |
|
tschumann
2017/03/22 17:56:41
// Only run this tests on platforms that don't sup
jkrcal
2017/03/23 09:29:43
Done.
| |
| 230 if (IsEulaNotifierAvailable()) { | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 // First enable the scheduler. | |
|
tschumann
2017/03/22 17:56:41
together with the comment about inlining ActivateU
jkrcal
2017/03/23 09:29:42
Done.
| |
| 235 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | |
| 236 ActivateUnderlyingProviderWithoutEula(); | |
| 237 | |
| 238 // A trigger results in a fetch even though Eula is not accepted (because it | |
| 239 // is not needed on this platform). | |
| 240 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | |
| 241 scheduling_provider_->OnPersistentSchedulerWakeUp(); | |
| 242 } | |
| 243 | |
| 244 TEST_F(SchedulingRemoteSuggestionsProviderTest, | |
| 245 ShouldIgnoreSignalsWhenEulaNotAccepted) { | |
| 246 // Do not run the test if Eula notifier is not available. | |
|
tschumann
2017/03/22 17:56:41
// Only run this tests on platforms supporting Eul
jkrcal
2017/03/23 09:29:42
Done.
| |
| 247 if (!IsEulaNotifierAvailable()) { | |
| 248 return; | |
| 249 } | |
| 250 // First enable the scheduler. | |
|
tschumann
2017/03/22 17:56:41
// Activating the provider should schedule the bac
jkrcal
2017/03/23 09:29:43
Done.
| |
| 251 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | |
| 252 ActivateUnderlyingProviderWithoutEula(); | |
| 253 | |
| 254 // All signals are ignored because of Eula not being accepted. | |
| 255 scheduling_provider_->OnPersistentSchedulerWakeUp(); | |
| 256 scheduling_provider_->OnNTPOpened(); | |
| 257 scheduling_provider_->OnBrowserForegrounded(); | |
| 258 scheduling_provider_->OnBrowserColdStart(); | |
| 259 } | |
| 260 | |
| 261 TEST_F(SchedulingRemoteSuggestionsProviderTest, | |
| 262 ShouldFetchWhenEulaGetsAccepted) { | |
| 263 // Do not run the test if Eula notifier is not available. | |
|
tschumann
2017/03/22 17:56:41
Only run this tests on platforms supporting Eula.
jkrcal
2017/03/23 09:29:42
Done.
| |
| 264 if (!IsEulaNotifierAvailable()) { | |
| 265 return; | |
| 266 } | |
| 267 // First enable the scheduler. | |
| 268 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | |
| 269 ActivateUnderlyingProviderWithoutEula(); | |
| 270 | |
| 271 // Make one (ignored) call to make sure we are interested in eula state. | |
| 272 scheduling_provider_->OnPersistentSchedulerWakeUp(); | |
| 273 | |
| 274 // Accepting Eula afterwards results in a background fetch. | |
| 275 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | |
| 276 SetEulaAcceptedPref(); | |
| 277 } | |
| 278 | |
| 279 TEST_F(SchedulingRemoteSuggestionsProviderTest, | |
| 206 ShouldIgnoreSignalsWhenDisabledByParam) { | 280 ShouldIgnoreSignalsWhenDisabledByParam) { |
| 207 // First set an empty list of allowed trigger types. | 281 // First set an empty list of allowed trigger types. |
| 208 SetVariationParameter("scheduler_trigger_types", "-"); | 282 SetVariationParameter("scheduler_trigger_types", "-"); |
| 209 ResetProvider(); | 283 ResetProvider(); |
| 210 | 284 |
| 211 // Then enable the scheduler. | 285 // Then enable the scheduler. |
| 212 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 286 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); |
| 213 ActivateUnderlyingProvider(); | 287 ActivateUnderlyingProvider(); |
| 214 | 288 |
| 215 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 289 scheduling_provider_->OnPersistentSchedulerWakeUp(); |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 759 for (int x = 0; x < 5; ++x) { | 833 for (int x = 0; x < 5; ++x) { |
| 760 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 834 scheduling_provider_->OnPersistentSchedulerWakeUp(); |
| 761 signal_fetch_done.Run(Status::Success()); | 835 signal_fetch_done.Run(Status::Success()); |
| 762 } | 836 } |
| 763 | 837 |
| 764 // For the 6th time, it is blocked by the scheduling provider. | 838 // For the 6th time, it is blocked by the scheduling provider. |
| 765 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 839 scheduling_provider_->OnPersistentSchedulerWakeUp(); |
| 766 } | 840 } |
| 767 | 841 |
| 768 } // namespace ntp_snippets | 842 } // namespace ntp_snippets |
| OLD | NEW |