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/remote_suggestions_scheduler_impl.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" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 MOCK_METHOD1(DismissSuggestion, void(const ContentSuggestion::ID&)); | 108 MOCK_METHOD1(DismissSuggestion, void(const ContentSuggestion::ID&)); |
| 109 MOCK_METHOD2(FetchSuggestionImage, | 109 MOCK_METHOD2(FetchSuggestionImage, |
| 110 void(const ContentSuggestion::ID&, const ImageFetchedCallback&)); | 110 void(const ContentSuggestion::ID&, const ImageFetchedCallback&)); |
| 111 MOCK_METHOD2(GetDismissedSuggestionsForDebugging, | 111 MOCK_METHOD2(GetDismissedSuggestionsForDebugging, |
| 112 void(Category, const DismissedSuggestionsCallback&)); | 112 void(Category, const DismissedSuggestionsCallback&)); |
| 113 MOCK_METHOD0(OnSignInStateChanged, void()); | 113 MOCK_METHOD0(OnSignInStateChanged, void()); |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 } // namespace | 116 } // namespace |
| 117 | 117 |
| 118 class SchedulingRemoteSuggestionsProviderTest : public ::testing::Test { | 118 class RemoteSuggestionsSchedulerImplTest : public ::testing::Test { |
| 119 public: | 119 public: |
| 120 SchedulingRemoteSuggestionsProviderTest() | 120 RemoteSuggestionsSchedulerImplTest() |
| 121 : // For the test we enabled all trigger types. | 121 : // For the test we enabled all trigger types. |
| 122 default_variation_params_{{"scheduler_trigger_types", | 122 default_variation_params_{{"scheduler_trigger_types", |
| 123 "persistent_scheduler_wake_up,ntp_opened," | 123 "persistent_scheduler_wake_up,ntp_opened," |
| 124 "browser_foregrounded,browser_cold_start"}}, | 124 "browser_foregrounded,browser_cold_start"}}, |
| 125 params_manager_(ntp_snippets::kStudyName, | 125 params_manager_(ntp_snippets::kStudyName, |
| 126 default_variation_params_, | 126 default_variation_params_, |
| 127 {kArticleSuggestionsFeature.name}), | 127 {kArticleSuggestionsFeature.name}), |
| 128 underlying_provider_(nullptr), | |
| 129 scheduling_provider_(nullptr), | |
| 130 user_classifier_(/*pref_service=*/nullptr, | 128 user_classifier_(/*pref_service=*/nullptr, |
| 131 base::MakeUnique<base::DefaultClock>()) { | 129 base::MakeUnique<base::DefaultClock>()) { |
| 132 SchedulingRemoteSuggestionsProvider::RegisterProfilePrefs( | 130 RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( |
| 133 utils_.pref_service()->registry()); | 131 utils_.pref_service()->registry()); |
| 134 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); | 132 RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry()); |
| 135 // TODO(jkrcal) Create a static function in EulaAcceptedNotifier that | 133 // TODO(jkrcal) Create a static function in EulaAcceptedNotifier that |
| 136 // registers this pref and replace the call in browser_process_impl.cc & in | 134 // registers this pref and replace the call in browser_process_impl.cc & in |
| 137 // eula_accepted_notifier_unittest.cc with the new static function. | 135 // eula_accepted_notifier_unittest.cc with the new static function. |
| 138 local_state_.registry()->RegisterBooleanPref(::prefs::kEulaAccepted, false); | 136 local_state_.registry()->RegisterBooleanPref(::prefs::kEulaAccepted, false); |
| 139 ResetProvider(); | 137 ResetProvider(); |
| 140 } | 138 } |
| 141 | 139 |
| 142 void ResetProvider() { | 140 void ResetProvider() { |
| 143 auto underlying_provider = | 141 provider_ = base::MakeUnique<StrictMock<MockRemoteSuggestionsProvider>>( |
| 144 base::MakeUnique<StrictMock<MockRemoteSuggestionsProvider>>( | 142 /*observer=*/nullptr); |
| 145 /*observer=*/nullptr); | |
| 146 underlying_provider_ = underlying_provider.get(); | |
| 147 | 143 |
| 148 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | 144 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 149 test_clock_ = test_clock.get(); | 145 test_clock_ = test_clock.get(); |
| 150 test_clock_->SetNow(base::Time::Now()); | 146 test_clock_->SetNow(base::Time::Now()); |
| 151 | 147 |
| 152 scheduling_provider_ = | 148 scheduler_ = base::MakeUnique<RemoteSuggestionsSchedulerImpl>( |
| 153 base::MakeUnique<SchedulingRemoteSuggestionsProvider>( | 149 &persistent_scheduler_, &user_classifier_, utils_.pref_service(), |
| 154 /*observer=*/nullptr, std::move(underlying_provider), | 150 &local_state_, std::move(test_clock)); |
| 155 &persistent_scheduler_, &user_classifier_, utils_.pref_service(), | 151 scheduler_->SetProvider(provider_.get()); |
| 156 &local_state_, std::move(test_clock)); | |
| 157 } | 152 } |
| 158 | 153 |
| 159 void SetVariationParameter(const std::string& param_name, | 154 void SetVariationParameter(const std::string& param_name, |
| 160 const std::string& param_value) { | 155 const std::string& param_value) { |
| 161 std::map<std::string, std::string> params = default_variation_params_; | 156 std::map<std::string, std::string> params = default_variation_params_; |
| 162 params[param_name] = param_value; | 157 params[param_name] = param_value; |
| 163 | 158 |
| 164 params_manager_.ClearAllVariationParams(); | 159 params_manager_.ClearAllVariationParams(); |
| 165 params_manager_.SetVariationParamsWithFeatureAssociations( | 160 params_manager_.SetVariationParamsWithFeatureAssociations( |
| 166 ntp_snippets::kStudyName, params, | 161 ntp_snippets::kStudyName, params, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 183 const std::vector<ContentSuggestion>& suggestions)>* | 178 const std::vector<ContentSuggestion>& suggestions)>* |
| 184 fetch_done, | 179 fetch_done, |
| 185 Status status_code, | 180 Status status_code, |
| 186 std::vector<ContentSuggestion> suggestions) { | 181 std::vector<ContentSuggestion> suggestions) { |
| 187 fetch_done->Call(status_code, suggestions); | 182 fetch_done->Call(status_code, suggestions); |
| 188 } | 183 } |
| 189 | 184 |
| 190 protected: | 185 protected: |
| 191 std::map<std::string, std::string> default_variation_params_; | 186 std::map<std::string, std::string> default_variation_params_; |
| 192 variations::testing::VariationParamsManager params_manager_; | 187 variations::testing::VariationParamsManager params_manager_; |
| 193 StrictMock<MockPersistentScheduler> persistent_scheduler_; | |
| 194 StrictMock<MockRemoteSuggestionsProvider>* underlying_provider_; | |
| 195 std::unique_ptr<SchedulingRemoteSuggestionsProvider> scheduling_provider_; | |
| 196 base::SimpleTestClock* test_clock_; | |
| 197 | 188 |
| 198 void ActivateUnderlyingProvider() { | 189 void ActivateProvider() { |
| 199 SetEulaAcceptedPref(); | 190 SetEulaAcceptedPref(); |
| 200 scheduling_provider_->OnProviderActivated(); | 191 scheduler_->OnProviderActivated(); |
| 201 } | 192 } |
| 202 | 193 |
| 203 void InactivateUnderlyingProvider() { | 194 void DeactivateProvider() { scheduler_->OnProviderDeactivated(); } |
| 204 scheduling_provider_->OnProviderDeactivated(); | 195 |
| 196 MockPersistentScheduler* persistent_scheduler() { | |
| 197 return &persistent_scheduler_; | |
| 205 } | 198 } |
| 199 base::SimpleTestClock* test_clock() { return test_clock_; } | |
| 200 MockRemoteSuggestionsProvider* provider() { return provider_.get(); } | |
| 201 RemoteSuggestionsSchedulerImpl* scheduler() { return scheduler_.get(); } | |
| 206 | 202 |
| 207 private: | 203 private: |
| 208 test::RemoteSuggestionsTestUtils utils_; | 204 test::RemoteSuggestionsTestUtils utils_; |
| 209 UserClassifier user_classifier_; | 205 UserClassifier user_classifier_; |
| 210 TestingPrefServiceSimple local_state_; | 206 TestingPrefServiceSimple local_state_; |
| 207 StrictMock<MockPersistentScheduler> persistent_scheduler_; | |
|
jkrcal
2017/03/27 14:09:32
I reordered the members to reflect how they depend
| |
| 208 base::SimpleTestClock* test_clock_; | |
| 209 std::unique_ptr<MockRemoteSuggestionsProvider> provider_; | |
| 210 std::unique_ptr<RemoteSuggestionsSchedulerImpl> scheduler_; | |
| 211 | 211 |
| 212 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProviderTest); | 212 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsSchedulerImplTest); |
| 213 }; | 213 }; |
| 214 | 214 |
| 215 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 215 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldIgnoreSignalsWhenNotEnabled) { |
| 216 ShouldIgnoreSignalsWhenNotEnabled) { | 216 scheduler()->OnPersistentSchedulerWakeUp(); |
| 217 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 217 scheduler()->OnNTPOpened(); |
| 218 scheduling_provider_->OnNTPOpened(); | 218 scheduler()->OnBrowserForegrounded(); |
| 219 scheduling_provider_->OnBrowserForegrounded(); | 219 scheduler()->OnBrowserColdStart(); |
| 220 scheduling_provider_->OnBrowserColdStart(); | |
| 221 } | 220 } |
| 222 | 221 |
| 223 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 222 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 224 ShouldIgnoreEulaStateOnPlatformsWhereNotAvaiable) { | 223 ShouldIgnoreEulaStateOnPlatformsWhereNotAvaiable) { |
| 225 // Only run this tests on platforms that don't support Eula. | 224 // Only run this tests on platforms that don't support Eula. |
| 226 if (IsEulaNotifierAvailable()) { | 225 if (IsEulaNotifierAvailable()) { |
| 227 return; | 226 return; |
| 228 } | 227 } |
| 229 | 228 |
| 230 // Activating the provider should schedule the persistent background fetches. | 229 // Activating the provider should schedule the persistent background fetches. |
| 231 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 230 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 232 scheduling_provider_->OnProviderActivated(); | 231 scheduler()->OnProviderActivated(); |
| 233 | 232 |
| 234 // Verify fetches get triggered. | 233 // Verify fetches get triggered. |
| 235 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 234 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 236 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 235 scheduler()->OnPersistentSchedulerWakeUp(); |
| 237 } | 236 } |
| 238 | 237 |
| 239 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 238 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 240 ShouldIgnoreSignalsWhenEulaNotAccepted) { | 239 ShouldIgnoreSignalsWhenEulaNotAccepted) { |
| 241 // Only run this tests on platforms supporting Eula. | 240 // Only run this tests on platforms supporting Eula. |
| 242 if (!IsEulaNotifierAvailable()) { | 241 if (!IsEulaNotifierAvailable()) { |
| 243 return; | 242 return; |
| 244 } | 243 } |
| 245 // Activating the provider should schedule the persistent background fetches. | 244 // Activating the provider should schedule the persistent background fetches. |
| 246 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 245 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 247 scheduling_provider_->OnProviderActivated(); | 246 scheduler()->OnProviderActivated(); |
| 248 | 247 |
| 249 // All signals are ignored because of Eula not being accepted. | 248 // All signals are ignored because of Eula not being accepted. |
| 250 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 249 scheduler()->OnPersistentSchedulerWakeUp(); |
| 251 scheduling_provider_->OnNTPOpened(); | 250 scheduler()->OnNTPOpened(); |
| 252 scheduling_provider_->OnBrowserForegrounded(); | 251 scheduler()->OnBrowserForegrounded(); |
| 253 scheduling_provider_->OnBrowserColdStart(); | 252 scheduler()->OnBrowserColdStart(); |
| 254 } | 253 } |
| 255 | 254 |
| 256 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 255 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldFetchWhenEulaGetsAccepted) { |
| 257 ShouldFetchWhenEulaGetsAccepted) { | |
| 258 // Only run this tests on platforms supporting Eula. | 256 // Only run this tests on platforms supporting Eula. |
| 259 if (!IsEulaNotifierAvailable()) { | 257 if (!IsEulaNotifierAvailable()) { |
| 260 return; | 258 return; |
| 261 } | 259 } |
| 262 // Activating the provider should schedule the persistent background fetches. | 260 // Activating the provider should schedule the persistent background fetches. |
| 263 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 261 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 264 scheduling_provider_->OnProviderActivated(); | 262 scheduler()->OnProviderActivated(); |
| 265 | 263 |
| 266 // Make one (ignored) call to make sure we are interested in eula state. | 264 // Make one (ignored) call to make sure we are interested in eula state. |
| 267 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 265 scheduler()->OnPersistentSchedulerWakeUp(); |
| 268 | 266 |
| 269 // Accepting Eula afterwards results in a background fetch. | 267 // Accepting Eula afterwards results in a background fetch. |
| 270 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 268 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 271 SetEulaAcceptedPref(); | 269 SetEulaAcceptedPref(); |
| 272 } | 270 } |
| 273 | 271 |
| 274 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 272 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 275 ShouldIgnoreSignalsWhenDisabledByParam) { | 273 ShouldIgnoreSignalsWhenDisabledByParam) { |
| 276 // First set an empty list of allowed trigger types. | 274 // First set an empty list of allowed trigger types. |
| 277 SetVariationParameter("scheduler_trigger_types", "-"); | 275 SetVariationParameter("scheduler_trigger_types", "-"); |
| 278 ResetProvider(); | 276 ResetProvider(); |
| 279 | 277 |
| 280 // Then enable the scheduler. | 278 // Then enable the scheduler. |
| 281 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 279 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 282 ActivateUnderlyingProvider(); | 280 ActivateProvider(); |
| 283 | 281 |
| 284 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 282 scheduler()->OnPersistentSchedulerWakeUp(); |
| 285 scheduling_provider_->OnNTPOpened(); | 283 scheduler()->OnNTPOpened(); |
| 286 scheduling_provider_->OnBrowserForegrounded(); | 284 scheduler()->OnBrowserForegrounded(); |
| 287 scheduling_provider_->OnBrowserColdStart(); | 285 scheduler()->OnBrowserColdStart(); |
| 288 } | 286 } |
| 289 | 287 |
| 290 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 288 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 291 ShouldHandleEmptyParamForTriggerTypes) { | 289 ShouldHandleEmptyParamForTriggerTypes) { |
| 292 // First set an empty param for allowed trigger types -> should result in the | 290 // First set an empty param for allowed trigger types -> should result in the |
| 293 // default list. | 291 // default list. |
| 294 SetVariationParameter("scheduler_trigger_types", ""); | 292 SetVariationParameter("scheduler_trigger_types", ""); |
| 295 ResetProvider(); | 293 ResetProvider(); |
| 296 | 294 |
| 297 // Then enable the scheduler. | 295 // Then enable the scheduler. |
| 298 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 296 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 299 ActivateUnderlyingProvider(); | 297 ActivateProvider(); |
| 300 | 298 |
| 301 // For instance, persistent scheduler wake up should be enabled by default. | 299 // For instance, persistent scheduler wake up should be enabled by default. |
| 302 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 300 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 303 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 301 scheduler()->OnPersistentSchedulerWakeUp(); |
| 304 } | 302 } |
| 305 | 303 |
| 306 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 304 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 307 ShouldHandleIncorrentParamForTriggerTypes) { | 305 ShouldHandleIncorrentParamForTriggerTypes) { |
| 308 // First set an invalid list of allowed trigger types. | 306 // First set an invalid list of allowed trigger types. |
| 309 SetVariationParameter("scheduler_trigger_types", "ntp_opened,foo;"); | 307 SetVariationParameter("scheduler_trigger_types", "ntp_opened,foo;"); |
| 310 ResetProvider(); | 308 ResetProvider(); |
| 311 | 309 |
| 312 // Then enable the scheduler. | 310 // Then enable the scheduler. |
| 313 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 311 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 314 ActivateUnderlyingProvider(); | 312 ActivateProvider(); |
| 315 | 313 |
| 316 // For instance, persistent scheduler wake up should be enabled by default. | 314 // For instance, persistent scheduler wake up should be enabled by default. |
| 317 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 315 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 318 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 316 scheduler()->OnPersistentSchedulerWakeUp(); |
| 319 } | 317 } |
| 320 | 318 |
| 321 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 319 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 322 ShouldFetchOnPersistentSchedulerWakeUp) { | 320 ShouldFetchOnPersistentSchedulerWakeUp) { |
| 323 // First set only this type to be allowed. | 321 // First set only this type to be allowed. |
| 324 SetVariationParameter("scheduler_trigger_types", | 322 SetVariationParameter("scheduler_trigger_types", |
| 325 "persistent_scheduler_wake_up"); | 323 "persistent_scheduler_wake_up"); |
| 326 ResetProvider(); | 324 ResetProvider(); |
| 327 | 325 |
| 328 // Then enable the scheduler. | 326 // Then enable the scheduler. |
| 329 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 327 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 330 ActivateUnderlyingProvider(); | 328 ActivateProvider(); |
| 331 | 329 |
| 332 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 330 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 333 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 331 scheduler()->OnPersistentSchedulerWakeUp(); |
| 334 } | 332 } |
| 335 | 333 |
| 336 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 334 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 337 ShouldFetchOnPersistentSchedulerWakeUpRepeated) { | 335 ShouldFetchOnPersistentSchedulerWakeUpRepeated) { |
| 338 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 336 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 339 { | 337 { |
| 340 InSequence s; | 338 InSequence s; |
| 341 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 339 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 342 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 340 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 343 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 341 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 344 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 342 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 345 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 343 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 346 } | 344 } |
| 347 // First enable the scheduler -- calling Schedule() for the first time. | 345 // First enable the scheduler -- calling Schedule() for the first time. |
| 348 ActivateUnderlyingProvider(); | 346 ActivateProvider(); |
| 349 // Make the first persistent fetch successful -- calling Schedule() again. | 347 // Make the first persistent fetch successful -- calling Schedule() again. |
| 350 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 348 scheduler()->OnPersistentSchedulerWakeUp(); |
| 351 signal_fetch_done.Run(Status::Success()); | 349 signal_fetch_done.Run(Status::Success()); |
| 352 // Make the second fetch. | 350 // Make the second fetch. |
| 353 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 351 scheduler()->OnPersistentSchedulerWakeUp(); |
| 354 } | 352 } |
| 355 | 353 |
| 356 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 354 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 357 ShouldNotTriggerBackgroundFetchIfAlreadyInProgess) { | 355 ShouldNotTriggerBackgroundFetchIfAlreadyInProgess) { |
| 358 { | 356 { |
| 359 InSequence s; | 357 InSequence s; |
| 360 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 358 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 361 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 359 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 362 // RefetchInTheBackground is not called after the second trigger. | 360 // RefetchInTheBackground is not called after the second trigger. |
| 363 } | 361 } |
| 364 // First enable the scheduler -- calling Schedule() for the first time. | 362 // First enable the scheduler -- calling Schedule() for the first time. |
| 365 ActivateUnderlyingProvider(); | 363 ActivateProvider(); |
| 366 // Make the first persistent fetch never finish. | 364 // Make the first persistent fetch never finish. |
| 367 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 365 scheduler()->OnPersistentSchedulerWakeUp(); |
| 368 // Make the second fetch. | 366 // Make the second fetch. |
| 369 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 367 scheduler()->OnPersistentSchedulerWakeUp(); |
| 370 } | 368 } |
| 371 | 369 |
| 372 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 370 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 373 ShouldFetchOnNTPOpenedForTheFirstTime) { | 371 ShouldFetchOnNTPOpenedForTheFirstTime) { |
| 374 // First set only this type to be allowed. | 372 // First set only this type to be allowed. |
| 375 SetVariationParameter("scheduler_trigger_types", "ntp_opened"); | 373 SetVariationParameter("scheduler_trigger_types", "ntp_opened"); |
| 376 ResetProvider(); | 374 ResetProvider(); |
| 377 | 375 |
| 378 // Then enable the scheduler. | 376 // Then enable the scheduler. |
| 379 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 377 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 380 ActivateUnderlyingProvider(); | 378 ActivateProvider(); |
| 381 | 379 |
| 382 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 380 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 383 scheduling_provider_->OnNTPOpened(); | 381 scheduler()->OnNTPOpened(); |
| 384 } | 382 } |
| 385 | 383 |
| 386 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 384 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 387 ShouldFetchOnBrowserForegroundedForTheFirstTime) { | 385 ShouldFetchOnBrowserForegroundedForTheFirstTime) { |
| 388 // First set only this type to be allowed. | 386 // First set only this type to be allowed. |
| 389 SetVariationParameter("scheduler_trigger_types", "browser_foregrounded"); | 387 SetVariationParameter("scheduler_trigger_types", "browser_foregrounded"); |
| 390 ResetProvider(); | 388 ResetProvider(); |
| 391 | 389 |
| 392 // Then enable the scheduler. | 390 // Then enable the scheduler. |
| 393 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 391 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 394 ActivateUnderlyingProvider(); | 392 ActivateProvider(); |
| 395 | 393 |
| 396 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 394 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 397 scheduling_provider_->OnBrowserForegrounded(); | 395 scheduler()->OnBrowserForegrounded(); |
| 398 } | 396 } |
| 399 | 397 |
| 400 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 398 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 401 ShouldFetchOnBrowserColdStartForTheFirstTime) { | 399 ShouldFetchOnBrowserColdStartForTheFirstTime) { |
| 402 // First set only this type to be allowed. | 400 // First set only this type to be allowed. |
| 403 SetVariationParameter("scheduler_trigger_types", "browser_cold_start"); | 401 SetVariationParameter("scheduler_trigger_types", "browser_cold_start"); |
| 404 ResetProvider(); | 402 ResetProvider(); |
| 405 | 403 |
| 406 // Then enable the scheduler. | 404 // Then enable the scheduler. |
| 407 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 405 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 408 ActivateUnderlyingProvider(); | 406 ActivateProvider(); |
| 409 | 407 |
| 410 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 408 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 411 scheduling_provider_->OnBrowserColdStart(); | 409 scheduler()->OnBrowserColdStart(); |
| 412 } | 410 } |
| 413 | 411 |
| 414 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 412 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 415 ShouldNotFetchOnNTPOpenedAfterSuccessfulSoftFetch) { | 413 ShouldNotFetchOnNTPOpenedAfterSuccessfulSoftFetch) { |
| 416 // First enable the scheduler; the second Schedule is called after the | 414 // First enable the scheduler; the second Schedule is called after the |
| 417 // successful fetch. | 415 // successful fetch. |
| 418 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(2); | 416 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 419 ActivateUnderlyingProvider(); | 417 ActivateProvider(); |
| 420 | 418 |
| 421 // Make the first soft fetch successful. | 419 // Make the first soft fetch successful. |
| 422 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 420 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 423 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 421 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 424 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 422 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 425 scheduling_provider_->OnNTPOpened(); | 423 scheduler()->OnNTPOpened(); |
| 426 signal_fetch_done.Run(Status::Success()); | 424 signal_fetch_done.Run(Status::Success()); |
| 427 // The second call is ignored if it happens right after the first one. | 425 // The second call is ignored if it happens right after the first one. |
| 428 scheduling_provider_->OnNTPOpened(); | 426 scheduler()->OnNTPOpened(); |
| 429 } | 427 } |
| 430 | 428 |
| 431 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 429 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 432 ShouldNotFetchOnNTPOpenedAfterSuccessfulPersistentFetch) { | 430 ShouldNotFetchOnNTPOpenedAfterSuccessfulPersistentFetch) { |
| 433 // First enable the scheduler; the second Schedule is called after the | 431 // First enable the scheduler; the second Schedule is called after the |
| 434 // successful fetch. | 432 // successful fetch. |
| 435 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(2); | 433 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 436 ActivateUnderlyingProvider(); | 434 ActivateProvider(); |
| 437 | 435 |
| 438 // Make the first persistent fetch successful. | 436 // Make the first persistent fetch successful. |
| 439 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 437 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 440 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 438 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 441 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 439 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 442 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 440 scheduler()->OnPersistentSchedulerWakeUp(); |
| 443 signal_fetch_done.Run(Status::Success()); | 441 signal_fetch_done.Run(Status::Success()); |
| 444 // The second call is ignored if it happens right after the first one. | 442 // The second call is ignored if it happens right after the first one. |
| 445 scheduling_provider_->OnNTPOpened(); | 443 scheduler()->OnNTPOpened(); |
| 446 } | 444 } |
| 447 | 445 |
| 448 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 446 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 449 ShouldNotFetchOnNTPOpenedAfterFailedSoftFetch) { | 447 ShouldNotFetchOnNTPOpenedAfterFailedSoftFetch) { |
| 450 // First enable the scheduler. | 448 // First enable the scheduler. |
| 451 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 449 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 452 ActivateUnderlyingProvider(); | 450 ActivateProvider(); |
| 453 | 451 |
| 454 // Make the first soft fetch failed. | 452 // Make the first soft fetch failed. |
| 455 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 453 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 456 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 454 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 457 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 455 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 458 scheduling_provider_->OnNTPOpened(); | 456 scheduler()->OnNTPOpened(); |
| 459 signal_fetch_done.Run(Status(StatusCode::PERMANENT_ERROR, "")); | 457 signal_fetch_done.Run(Status(StatusCode::PERMANENT_ERROR, "")); |
| 460 | 458 |
| 461 // The second call is ignored if it happens right after the first one. | 459 // The second call is ignored if it happens right after the first one. |
| 462 scheduling_provider_->OnNTPOpened(); | 460 scheduler()->OnNTPOpened(); |
| 463 } | 461 } |
| 464 | 462 |
| 465 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 463 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 466 ShouldNotFetchOnNTPOpenedAfterFailedPersistentFetch) { | 464 ShouldNotFetchOnNTPOpenedAfterFailedPersistentFetch) { |
| 467 // First enable the scheduler. | 465 // First enable the scheduler. |
| 468 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 466 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 469 ActivateUnderlyingProvider(); | 467 ActivateProvider(); |
| 470 | 468 |
| 471 // Make the first persistent fetch failed. | 469 // Make the first persistent fetch failed. |
| 472 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 470 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 473 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 471 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 474 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 472 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 475 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 473 scheduler()->OnPersistentSchedulerWakeUp(); |
| 476 signal_fetch_done.Run(Status(StatusCode::PERMANENT_ERROR, "")); | 474 signal_fetch_done.Run(Status(StatusCode::PERMANENT_ERROR, "")); |
| 477 | 475 |
| 478 // The second call is ignored if it happens right after the first one. | 476 // The second call is ignored if it happens right after the first one. |
| 479 scheduling_provider_->OnNTPOpened(); | 477 scheduler()->OnNTPOpened(); |
| 480 } | 478 } |
| 481 | 479 |
| 482 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 480 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 483 ShouldFetchAgainOnBrowserForgroundLaterAgain) { | 481 ShouldFetchAgainOnBrowserForgroundLaterAgain) { |
| 484 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 482 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 485 { | 483 { |
| 486 InSequence s; | 484 InSequence s; |
| 487 // Initial scheduling after being enabled. | 485 // Initial scheduling after being enabled. |
| 488 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 486 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 489 // The first call to NTPOpened results in a fetch. | 487 // The first call to NTPOpened results in a fetch. |
| 490 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 488 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 491 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 489 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 492 // Rescheduling after a succesful fetch. | 490 // Rescheduling after a succesful fetch. |
| 493 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 491 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 494 // The second call to NTPOpened 2hrs later again results in a fetch. | 492 // The second call to NTPOpened 2hrs later again results in a fetch. |
| 495 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 493 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 496 } | 494 } |
| 497 | 495 |
| 498 // First enable the scheduler. | 496 // First enable the scheduler. |
| 499 ActivateUnderlyingProvider(); | 497 ActivateProvider(); |
| 500 // Make the first soft fetch successful. | 498 // Make the first soft fetch successful. |
| 501 scheduling_provider_->OnBrowserForegrounded(); | 499 scheduler()->OnBrowserForegrounded(); |
| 502 signal_fetch_done.Run(Status::Success()); | 500 signal_fetch_done.Run(Status::Success()); |
| 503 // Open NTP again after 2hrs. | 501 // Open NTP again after 2hrs. |
| 504 test_clock_->Advance(base::TimeDelta::FromHours(2)); | 502 test_clock()->Advance(base::TimeDelta::FromHours(2)); |
| 505 scheduling_provider_->OnBrowserForegrounded(); | 503 scheduler()->OnBrowserForegrounded(); |
| 506 } | 504 } |
| 507 | 505 |
| 508 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 506 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 509 ShouldRescheduleOnRescheduleFetching) { | 507 ShouldRescheduleOnRescheduleFetching) { |
| 510 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 508 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 511 scheduling_provider_->RescheduleFetching(); | 509 scheduler()->RescheduleFetching(); |
| 512 } | 510 } |
| 513 | 511 |
| 514 TEST_F(SchedulingRemoteSuggestionsProviderTest, ShouldScheduleOnActivation) { | 512 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldScheduleOnActivation) { |
| 515 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 513 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 516 ActivateUnderlyingProvider(); | 514 ActivateProvider(); |
| 517 } | 515 } |
| 518 | 516 |
| 519 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 517 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 520 ShouldUnscheduleOnLaterInactivation) { | 518 ShouldUnscheduleOnLaterInactivation) { |
| 521 { | 519 { |
| 522 InSequence s; | 520 InSequence s; |
| 523 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 521 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 524 EXPECT_CALL(persistent_scheduler_, Unschedule()); | 522 EXPECT_CALL(*persistent_scheduler(), Unschedule()); |
| 525 } | 523 } |
| 526 ActivateUnderlyingProvider(); | 524 ActivateProvider(); |
| 527 InactivateUnderlyingProvider(); | 525 DeactivateProvider(); |
| 528 } | 526 } |
| 529 | 527 |
| 530 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 528 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldScheduleOnLaterActivation) { |
| 531 ShouldScheduleOnLaterActivation) { | 529 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 532 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | |
| 533 // There is no schedule yet, so inactivation does not trigger unschedule. | 530 // There is no schedule yet, so inactivation does not trigger unschedule. |
| 534 InactivateUnderlyingProvider(); | 531 DeactivateProvider(); |
| 535 ActivateUnderlyingProvider(); | 532 ActivateProvider(); |
| 536 } | 533 } |
| 537 | 534 |
| 538 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 535 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 539 ShouldRescheduleAfterSuccessfulFetch) { | 536 ShouldRescheduleAfterSuccessfulFetch) { |
| 540 // First reschedule on becoming active. | 537 // First reschedule on becoming active. |
| 541 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(2); | 538 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 542 ActivateUnderlyingProvider(); | 539 ActivateProvider(); |
| 543 | 540 |
| 544 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 541 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 545 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 542 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 546 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 543 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 547 | 544 |
| 548 // Trigger a fetch. | 545 // Trigger a fetch. |
| 549 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 546 scheduler()->OnPersistentSchedulerWakeUp(); |
| 550 // Second reschedule after a successful fetch. | 547 // Second reschedule after a successful fetch. |
| 551 signal_fetch_done.Run(Status::Success()); | 548 signal_fetch_done.Run(Status::Success()); |
| 552 } | 549 } |
| 553 | 550 |
| 554 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 551 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 555 ShouldNotRescheduleAfterFailedFetch) { | 552 ShouldNotRescheduleAfterFailedFetch) { |
| 556 // Only reschedule on becoming active. | 553 // Only reschedule on becoming active. |
| 557 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 554 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 558 ActivateUnderlyingProvider(); | 555 ActivateProvider(); |
| 559 | 556 |
| 560 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 557 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 561 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 558 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 562 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 559 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 563 | 560 |
| 564 // Trigger a fetch. | 561 // Trigger a fetch. |
| 565 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 562 scheduler()->OnPersistentSchedulerWakeUp(); |
| 566 // No furter reschedule after a failure. | 563 // No furter reschedule after a failure. |
| 567 signal_fetch_done.Run(Status(StatusCode::PERMANENT_ERROR, "")); | 564 signal_fetch_done.Run(Status(StatusCode::PERMANENT_ERROR, "")); |
| 568 } | 565 } |
| 569 | 566 |
| 570 TEST_F(SchedulingRemoteSuggestionsProviderTest, ShouldScheduleOnlyOnce) { | 567 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldScheduleOnlyOnce) { |
| 571 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 568 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 572 ActivateUnderlyingProvider(); | 569 ActivateProvider(); |
| 573 // No further call to Schedule on a second status callback. | 570 // No further call to Schedule on a second status callback. |
| 574 ActivateUnderlyingProvider(); | 571 ActivateProvider(); |
| 575 } | 572 } |
| 576 | 573 |
| 577 TEST_F(SchedulingRemoteSuggestionsProviderTest, ShouldUnscheduleOnlyOnce) { | 574 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldUnscheduleOnlyOnce) { |
| 578 { | 575 { |
| 579 InSequence s; | 576 InSequence s; |
| 580 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 577 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 581 EXPECT_CALL(persistent_scheduler_, Unschedule()); | 578 EXPECT_CALL(*persistent_scheduler(), Unschedule()); |
| 582 } | 579 } |
| 583 // First schedule so that later we really unschedule. | 580 // First schedule so that later we really unschedule. |
| 584 ActivateUnderlyingProvider(); | 581 ActivateProvider(); |
| 585 InactivateUnderlyingProvider(); | 582 DeactivateProvider(); |
| 586 // No further call to Unschedule on second status callback. | 583 // No further call to Unschedule on second status callback. |
| 587 InactivateUnderlyingProvider(); | 584 DeactivateProvider(); |
| 588 } | 585 } |
| 589 | 586 |
| 590 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 587 TEST_F(RemoteSuggestionsSchedulerImplTest, ReschedulesWhenWifiParamChanges) { |
| 591 ReschedulesWhenWifiParamChanges) { | 588 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 592 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(2); | 589 ActivateProvider(); |
| 593 ActivateUnderlyingProvider(); | |
| 594 | 590 |
| 595 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 591 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 596 // null. Change the wifi interval for this class. | 592 // null. Change the wifi interval for this class. |
| 597 SetVariationParameter("fetching_interval_hours-wifi-active_ntp_user", "1.5"); | 593 SetVariationParameter("fetching_interval_hours-wifi-active_ntp_user", "1.5"); |
| 598 | 594 |
| 599 // Schedule() should get called for the second time after params have changed. | 595 // Schedule() should get called for the second time after params have changed. |
| 600 ActivateUnderlyingProvider(); | 596 ActivateProvider(); |
| 601 } | 597 } |
| 602 | 598 |
| 603 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 599 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 604 ReschedulesWhenFallbackParamChanges) { | 600 ReschedulesWhenFallbackParamChanges) { |
| 605 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(2); | 601 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 606 ActivateUnderlyingProvider(); | 602 ActivateProvider(); |
| 607 | 603 |
| 608 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 604 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 609 // null. Change the fallback interval for this class. | 605 // null. Change the fallback interval for this class. |
| 610 SetVariationParameter("fetching_interval_hours-fallback-active_ntp_user", | 606 SetVariationParameter("fetching_interval_hours-fallback-active_ntp_user", |
| 611 "1.5"); | 607 "1.5"); |
| 612 | 608 |
| 613 // Schedule() should get called for the second time after params have changed. | 609 // Schedule() should get called for the second time after params have changed. |
| 614 ActivateUnderlyingProvider(); | 610 ActivateProvider(); |
| 615 } | 611 } |
| 616 | 612 |
| 617 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 613 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 618 ReschedulesWhenOnUsageEventParamChanges) { | 614 ReschedulesWhenOnUsageEventParamChanges) { |
| 619 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(2); | 615 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 620 ActivateUnderlyingProvider(); | 616 ActivateProvider(); |
| 621 | 617 |
| 622 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 618 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 623 // null. Change the on usage interval for this class. | 619 // null. Change the on usage interval for this class. |
| 624 SetVariationParameter("soft_fetching_interval_hours-active-active_ntp_user", | 620 SetVariationParameter("soft_fetching_interval_hours-active-active_ntp_user", |
| 625 "1.5"); | 621 "1.5"); |
| 626 | 622 |
| 627 // Schedule() should get called for the second time after params have changed. | 623 // Schedule() should get called for the second time after params have changed. |
| 628 ActivateUnderlyingProvider(); | 624 ActivateProvider(); |
| 629 } | 625 } |
| 630 | 626 |
| 631 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 627 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 632 ReschedulesWhenOnNtpOpenedParamChanges) { | 628 ReschedulesWhenOnNtpOpenedParamChanges) { |
| 633 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(2); | 629 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 634 ActivateUnderlyingProvider(); | 630 ActivateProvider(); |
| 635 | 631 |
| 636 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 632 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 637 // null. Change the fallback interval for this class. | 633 // null. Change the fallback interval for this class. |
| 638 SetVariationParameter("soft_on_ntp_opened_interval_hours-active_ntp_user", | 634 SetVariationParameter("soft_on_ntp_opened_interval_hours-active_ntp_user", |
| 639 "1.5"); | 635 "1.5"); |
| 640 | 636 |
| 641 // Schedule() should get called for the second time after params have changed. | 637 // Schedule() should get called for the second time after params have changed. |
| 642 ActivateUnderlyingProvider(); | 638 ActivateProvider(); |
| 643 } | 639 } |
| 644 | 640 |
| 645 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 641 TEST_F(RemoteSuggestionsSchedulerImplTest, FetchIntervalForNtpOpenedTrigger) { |
| 646 FetchIntervalForNtpOpenedTrigger) { | |
| 647 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 642 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 648 { | 643 { |
| 649 InSequence s; | 644 InSequence s; |
| 650 // Initial scheduling after being enabled. | 645 // Initial scheduling after being enabled. |
| 651 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 646 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 652 // The first call to NTPOpened results in a fetch. | 647 // The first call to NTPOpened results in a fetch. |
| 653 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 648 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 654 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 649 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 655 // Rescheduling after a succesful fetch. | 650 // Rescheduling after a succesful fetch. |
| 656 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 651 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 657 // The third call to NTPOpened 35min later again results in a fetch. | 652 // The third call to NTPOpened 35min later again results in a fetch. |
| 658 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 653 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 659 } | 654 } |
| 660 | 655 |
| 661 ActivateUnderlyingProvider(); | 656 ActivateProvider(); |
| 662 | 657 |
| 663 scheduling_provider_->OnNTPOpened(); | 658 scheduler()->OnNTPOpened(); |
| 664 signal_fetch_done.Run(Status::Success()); | 659 signal_fetch_done.Run(Status::Success()); |
| 665 | 660 |
| 666 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER which uses a 2h time | 661 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER which uses a 2h time |
| 667 // interval by default for soft backgroudn fetches on ntp open events. | 662 // interval by default for soft backgroudn fetches on ntp open events. |
| 668 | 663 |
| 669 // Open NTP again after 20min. This time no fetch is executed. | 664 // Open NTP again after 20min. This time no fetch is executed. |
| 670 test_clock_->Advance(base::TimeDelta::FromMinutes(20)); | 665 test_clock()->Advance(base::TimeDelta::FromMinutes(20)); |
| 671 scheduling_provider_->OnNTPOpened(); | 666 scheduler()->OnNTPOpened(); |
| 672 | 667 |
| 673 // Open NTP again after 101min (121min since first opened). Since the default | 668 // Open NTP again after 101min (121min since first opened). Since the default |
| 674 // time interval has passed refetch again. | 669 // time interval has passed refetch again. |
| 675 test_clock_->Advance(base::TimeDelta::FromMinutes(101)); | 670 test_clock()->Advance(base::TimeDelta::FromMinutes(101)); |
| 676 scheduling_provider_->OnNTPOpened(); | 671 scheduler()->OnNTPOpened(); |
| 677 } | 672 } |
| 678 | 673 |
| 679 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 674 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 680 OverrideFetchIntervalForNtpOpenedTrigger) { | 675 OverrideFetchIntervalForNtpOpenedTrigger) { |
| 681 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 676 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 682 // null. Change the on usage interval for this class from 2h to 30min. | 677 // null. Change the on usage interval for this class from 2h to 30min. |
| 683 SetVariationParameter("soft_on_ntp_opened_interval_hours-active_ntp_user", | 678 SetVariationParameter("soft_on_ntp_opened_interval_hours-active_ntp_user", |
| 684 "0.5"); | 679 "0.5"); |
| 685 | 680 |
| 686 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 681 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 687 { | 682 { |
| 688 InSequence s; | 683 InSequence s; |
| 689 // Initial scheduling after being enabled. | 684 // Initial scheduling after being enabled. |
| 690 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 685 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 691 // The first call to NTPOpened results in a fetch. | 686 // The first call to NTPOpened results in a fetch. |
| 692 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 687 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 693 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 688 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 694 // Rescheduling after a succesful fetch. | 689 // Rescheduling after a succesful fetch. |
| 695 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 690 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 696 // The third call to NTPOpened 35min later again results in a fetch. | 691 // The third call to NTPOpened 35min later again results in a fetch. |
| 697 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 692 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 698 } | 693 } |
| 699 | 694 |
| 700 ActivateUnderlyingProvider(); | 695 ActivateProvider(); |
| 701 | 696 |
| 702 scheduling_provider_->OnNTPOpened(); | 697 scheduler()->OnNTPOpened(); |
| 703 signal_fetch_done.Run(Status::Success()); | 698 signal_fetch_done.Run(Status::Success()); |
| 704 | 699 |
| 705 // Open NTP again after 20min. No fetch request is issues since the 30 min | 700 // Open NTP again after 20min. No fetch request is issues since the 30 min |
| 706 // time interval has not passed yet. | 701 // time interval has not passed yet. |
| 707 test_clock_->Advance(base::TimeDelta::FromMinutes(20)); | 702 test_clock()->Advance(base::TimeDelta::FromMinutes(20)); |
| 708 scheduling_provider_->OnNTPOpened(); | 703 scheduler()->OnNTPOpened(); |
| 709 | 704 |
| 710 // Open NTP again after 15min (35min since first opened) | 705 // Open NTP again after 15min (35min since first opened) |
| 711 test_clock_->Advance(base::TimeDelta::FromMinutes(15)); | 706 test_clock()->Advance(base::TimeDelta::FromMinutes(15)); |
| 712 scheduling_provider_->OnNTPOpened(); | 707 scheduler()->OnNTPOpened(); |
| 713 } | 708 } |
| 714 | 709 |
| 715 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 710 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 716 ShouldBlockFetchingForSomeTimeAfterHistoryCleared) { | 711 ShouldBlockFetchingForSomeTimeAfterHistoryCleared) { |
| 717 // First enable the scheduler -- this will trigger the persistent scheduling. | 712 // First enable the scheduler -- this will trigger the persistent scheduling. |
| 718 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 713 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 719 ActivateUnderlyingProvider(); | 714 ActivateProvider(); |
| 720 // Clear the history. | 715 // Clear the history. |
| 721 scheduling_provider_->OnHistoryCleared(); | 716 scheduler()->OnHistoryCleared(); |
| 722 | 717 |
| 723 // A trigger after 15 minutes is ignored. | 718 // A trigger after 15 minutes is ignored. |
| 724 test_clock_->Advance(base::TimeDelta::FromMinutes(15)); | 719 test_clock()->Advance(base::TimeDelta::FromMinutes(15)); |
| 725 scheduling_provider_->OnBrowserForegrounded(); | 720 scheduler()->OnBrowserForegrounded(); |
| 726 | 721 |
| 727 // A trigger after another 16 minutes is performed (more than 30m after | 722 // A trigger after another 16 minutes is performed (more than 30m after |
| 728 // clearing the history). | 723 // clearing the history). |
| 729 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)); | 724 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 730 test_clock_->Advance(base::TimeDelta::FromMinutes(16)); | 725 test_clock()->Advance(base::TimeDelta::FromMinutes(16)); |
| 731 scheduling_provider_->OnBrowserForegrounded(); | 726 scheduler()->OnBrowserForegrounded(); |
| 732 } | 727 } |
| 733 | 728 |
| 734 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 729 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 735 ShouldImmediatelyFetchAfterSuggestionsCleared) { | 730 ShouldImmediatelyFetchAfterSuggestionsCleared) { |
| 736 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 731 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 737 | 732 |
| 738 // First enable the scheduler -- this will trigger the persistent scheduling. | 733 // First enable the scheduler -- this will trigger the persistent scheduling. |
| 739 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 734 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 740 ActivateUnderlyingProvider(); | 735 ActivateProvider(); |
| 741 | 736 |
| 742 // The first trigger results in a fetch. | 737 // The first trigger results in a fetch. |
| 743 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 738 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 744 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 739 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 745 scheduling_provider_->OnBrowserForegrounded(); | 740 scheduler()->OnBrowserForegrounded(); |
| 746 // Make the fetch successful -- this results in rescheduling. | 741 // Make the fetch successful -- this results in rescheduling. |
| 747 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)); | 742 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 748 signal_fetch_done.Run(Status::Success()); | 743 signal_fetch_done.Run(Status::Success()); |
| 749 | 744 |
| 750 // Clear the suggestions - results in an immediate fetch. | 745 // Clear the suggestions - results in an immediate fetch. |
| 751 EXPECT_CALL(*underlying_provider_, ReloadSuggestions()); | 746 EXPECT_CALL(*provider(), ReloadSuggestions()); |
| 752 scheduling_provider_->OnSuggestionsCleared(); | 747 scheduler()->OnSuggestionsCleared(); |
| 753 } | 748 } |
| 754 | 749 |
| 755 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 750 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldThrottleInteractiveRequests) { |
| 756 ShouldThrottleInteractiveRequests) { | |
| 757 // Change the quota for interactive requests ("active NTP user" is the default | 751 // Change the quota for interactive requests ("active NTP user" is the default |
| 758 // class in tests). | 752 // class in tests). |
| 759 SetVariationParameter("interactive_quota_SuggestionFetcherActiveNTPUser", | 753 SetVariationParameter("interactive_quota_SuggestionFetcherActiveNTPUser", |
| 760 "10"); | 754 "10"); |
| 761 ResetProvider(); | 755 ResetProvider(); |
| 762 | 756 |
| 763 Category category = Category::FromKnownCategory(KnownCategories::ARTICLES); | 757 for (int x = 0; x < 10; ++x) { |
| 764 std::set<std::string> known_suggestions; | 758 EXPECT_THAT(scheduler()->AcquireQuotaForInteractiveFetch(), Eq(true)); |
| 765 | |
| 766 // Both Fetch(..) and ReloadSuggestions() consume the same quota. As long as | |
| 767 // the quota suffices, the call gets through. | |
| 768 EXPECT_CALL(*underlying_provider_, ReloadSuggestions()).Times(5); | |
| 769 for (int x = 0; x < 5; ++x) { | |
| 770 scheduling_provider_->ReloadSuggestions(); | |
| 771 } | 759 } |
| 772 | 760 |
| 773 // Expect underlying provider being called and store the callback to inform | 761 // Now the quota is over. |
| 774 // scheduling provider. | 762 EXPECT_THAT(scheduler()->AcquireQuotaForInteractiveFetch(), Eq(false)); |
| 775 FetchDoneCallback signal_fetch_done_from_underlying_provider; | |
| 776 EXPECT_CALL(*underlying_provider_, Fetch(_, _, _)) | |
| 777 .Times(5) | |
| 778 .WillRepeatedly(SaveArg<2>(&signal_fetch_done_from_underlying_provider)); | |
| 779 // Expect scheduling provider to pass the information through. | |
| 780 MockFunction<void(Status status_code, | |
| 781 const std::vector<ContentSuggestion>& suggestions)> | |
| 782 fetch_done_from_scheduling_provider; | |
| 783 EXPECT_CALL(fetch_done_from_scheduling_provider, | |
| 784 Call(Field(&Status::code, StatusCode::SUCCESS), _)) | |
| 785 .Times(5); | |
| 786 // Scheduling is not activated, each successful fetch results in Unschedule(). | |
| 787 EXPECT_CALL(persistent_scheduler_, Unschedule()).Times(5); | |
| 788 for (int x = 0; x < 5; ++x) { | |
| 789 scheduling_provider_->Fetch( | |
| 790 category, known_suggestions, | |
| 791 base::Bind(&SchedulingRemoteSuggestionsProviderTest::FetchDoneWrapper, | |
| 792 base::Unretained(this), | |
| 793 &fetch_done_from_scheduling_provider)); | |
| 794 // Inform scheduling provider the fetc is successful (with no suggestions). | |
| 795 signal_fetch_done_from_underlying_provider.Run( | |
| 796 Status::Success(), std::vector<ContentSuggestion>{}); | |
| 797 } | |
| 798 | |
| 799 // When the quota expires, it is blocked by the scheduling provider, directly | |
| 800 // calling the callback. | |
| 801 EXPECT_CALL(fetch_done_from_scheduling_provider, | |
| 802 Call(Field(&Status::code, StatusCode::TEMPORARY_ERROR), _)); | |
| 803 scheduling_provider_->ReloadSuggestions(); | |
| 804 scheduling_provider_->Fetch( | |
| 805 category, known_suggestions, | |
| 806 base::Bind(&SchedulingRemoteSuggestionsProviderTest::FetchDoneWrapper, | |
| 807 base::Unretained(this), &fetch_done_from_scheduling_provider)); | |
| 808 } | 763 } |
| 809 | 764 |
| 810 TEST_F(SchedulingRemoteSuggestionsProviderTest, | 765 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 811 ShouldThrottleNonInteractiveRequests) { | 766 ShouldThrottleNonInteractiveRequests) { |
| 812 // Change the quota for interactive requests ("active NTP user" is the default | 767 // Change the quota for interactive requests ("active NTP user" is the default |
| 813 // class in tests). | 768 // class in tests). |
| 814 SetVariationParameter("quota_SuggestionFetcherActiveNTPUser", "5"); | 769 SetVariationParameter("quota_SuggestionFetcherActiveNTPUser", "5"); |
| 815 ResetProvider(); | 770 ResetProvider(); |
| 816 | 771 |
| 817 // One scheduling on start, 5 times after successful fetches. | 772 // One scheduling on start, 5 times after successful fetches. |
| 818 EXPECT_CALL(persistent_scheduler_, Schedule(_, _)).Times(6); | 773 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(6); |
| 819 | 774 |
| 820 // First enable the scheduler -- this will trigger the persistent scheduling. | 775 // First enable the scheduler -- this will trigger the persistent scheduling. |
| 821 ActivateUnderlyingProvider(); | 776 ActivateProvider(); |
| 822 | 777 |
| 823 // As long as the quota suffices, the call gets through. | 778 // As long as the quota suffices, the call gets through. |
| 824 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 779 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 825 EXPECT_CALL(*underlying_provider_, RefetchInTheBackground(_)) | 780 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 826 .Times(5) | 781 .Times(5) |
| 827 .WillRepeatedly(SaveArg<0>(&signal_fetch_done)); | 782 .WillRepeatedly(SaveArg<0>(&signal_fetch_done)); |
| 828 for (int x = 0; x < 5; ++x) { | 783 for (int x = 0; x < 5; ++x) { |
| 829 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 784 scheduler()->OnPersistentSchedulerWakeUp(); |
| 830 signal_fetch_done.Run(Status::Success()); | 785 signal_fetch_done.Run(Status::Success()); |
| 831 } | 786 } |
| 832 | 787 |
| 833 // For the 6th time, it is blocked by the scheduling provider. | 788 // For the 6th time, it is blocked by the scheduling provider. |
| 834 scheduling_provider_->OnPersistentSchedulerWakeUp(); | 789 scheduler()->OnPersistentSchedulerWakeUp(); |
| 835 } | 790 } |
| 836 | 791 |
| 837 } // namespace ntp_snippets | 792 } // namespace ntp_snippets |
| OLD | NEW |