| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/callback.h" | 5 #include "base/callback.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 TEST_F(CloudPolicyRefreshSchedulerTest, InitialRefreshUnmanaged) { | 123 TEST_F(CloudPolicyRefreshSchedulerTest, InitialRefreshUnmanaged) { |
| 124 store_.policy_->set_state(em::PolicyData::UNMANAGED); | 124 store_.policy_->set_state(em::PolicyData::UNMANAGED); |
| 125 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); | 125 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); |
| 126 CheckTiming(CloudPolicyRefreshScheduler::kUnmanagedRefreshDelayMs); | 126 CheckTiming(CloudPolicyRefreshScheduler::kUnmanagedRefreshDelayMs); |
| 127 ASSERT_FALSE(last_callback_.is_null()); | 127 ASSERT_FALSE(last_callback_.is_null()); |
| 128 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 128 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 129 last_callback_.Run(); | 129 last_callback_.Run(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 TEST_F(CloudPolicyRefreshSchedulerTest, InitialRefreshManaged) { | 132 TEST_F(CloudPolicyRefreshSchedulerTest, InitialRefreshManagedNotYetFetched) { |
| 133 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); | 133 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); |
| 134 EXPECT_EQ(last_delay_, base::TimeDelta()); | 134 EXPECT_EQ(last_delay_, base::TimeDelta()); |
| 135 ASSERT_FALSE(last_callback_.is_null()); | 135 ASSERT_FALSE(last_callback_.is_null()); |
| 136 EXPECT_CALL(client_, FetchPolicy()).Times(1); | 136 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 137 last_callback_.Run(); | 137 last_callback_.Run(); |
| 138 } | 138 } |
| 139 | 139 |
| 140 TEST_F(CloudPolicyRefreshSchedulerTest, InitialRefreshManagedAlreadyFetched) { |
| 141 last_refresh_ = base::Time::NowFromSystemTime(); |
| 142 client_.SetPolicy(em::PolicyFetchResponse()); |
| 143 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); |
| 144 CheckTiming(kPolicyRefreshRate); |
| 145 ASSERT_FALSE(last_callback_.is_null()); |
| 146 EXPECT_CALL(client_, FetchPolicy()).Times(1); |
| 147 last_callback_.Run(); |
| 148 } |
| 149 |
| 140 TEST_F(CloudPolicyRefreshSchedulerTest, Unregistered) { | 150 TEST_F(CloudPolicyRefreshSchedulerTest, Unregistered) { |
| 141 client_.SetDMToken(""); | 151 client_.SetDMToken(""); |
| 142 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).Times(0); | 152 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).Times(0); |
| 143 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); | 153 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); |
| 144 client_.NotifyPolicyFetched(); | 154 client_.NotifyPolicyFetched(); |
| 145 client_.NotifyRegistrationStateChanged(); | 155 client_.NotifyRegistrationStateChanged(); |
| 146 client_.NotifyClientError(); | 156 client_.NotifyClientError(); |
| 147 store_.NotifyStoreLoaded(); | 157 store_.NotifyStoreLoaded(); |
| 148 store_.NotifyStoreError(); | 158 store_.NotifyStoreError(); |
| 149 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, 12 * 60 * 60 * 1000); | 159 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, 12 * 60 * 60 * 1000); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 kPolicyRefreshRate, 1 }, | 259 kPolicyRefreshRate, 1 }, |
| 250 }; | 260 }; |
| 251 | 261 |
| 252 class CloudPolicyRefreshSchedulerClientErrorTest | 262 class CloudPolicyRefreshSchedulerClientErrorTest |
| 253 : public CloudPolicyRefreshSchedulerSteadyStateTest, | 263 : public CloudPolicyRefreshSchedulerSteadyStateTest, |
| 254 public testing::WithParamInterface<ClientErrorTestParam> { | 264 public testing::WithParamInterface<ClientErrorTestParam> { |
| 255 }; | 265 }; |
| 256 | 266 |
| 257 TEST_P(CloudPolicyRefreshSchedulerClientErrorTest, OnClientError) { | 267 TEST_P(CloudPolicyRefreshSchedulerClientErrorTest, OnClientError) { |
| 258 client_.SetStatus(GetParam().client_error); | 268 client_.SetStatus(GetParam().client_error); |
| 269 last_delay_ = base::TimeDelta(); |
| 270 last_callback_.Reset(); |
| 259 | 271 |
| 260 // See whether the error triggers the right refresh delay. | 272 // See whether the error triggers the right refresh delay. |
| 261 int64 expected_delay_ms = GetParam().expected_delay_ms; | 273 int64 expected_delay_ms = GetParam().expected_delay_ms; |
| 262 if (expected_delay_ms == -1) | |
| 263 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).Times(0); | |
| 264 client_.NotifyClientError(); | 274 client_.NotifyClientError(); |
| 265 if (expected_delay_ms >= 0) { | 275 if (expected_delay_ms >= 0) { |
| 266 CheckTiming(expected_delay_ms); | 276 CheckTiming(expected_delay_ms); |
| 267 | 277 |
| 268 // Check whether exponential backoff is working as expected and capped at | 278 // Check whether exponential backoff is working as expected and capped at |
| 269 // the regular refresh rate (if applicable). | 279 // the regular refresh rate (if applicable). |
| 270 do { | 280 do { |
| 271 expected_delay_ms *= GetParam().backoff_factor; | 281 expected_delay_ms *= GetParam().backoff_factor; |
| 272 last_refresh_ = base::Time::NowFromSystemTime(); | 282 last_refresh_ = base::Time::NowFromSystemTime(); |
| 273 client_.NotifyClientError(); | 283 client_.NotifyClientError(); |
| 274 CheckTiming(std::max(std::min(expected_delay_ms, kPolicyRefreshRate), | 284 CheckTiming(std::max(std::min(expected_delay_ms, kPolicyRefreshRate), |
| 275 GetParam().expected_delay_ms)); | 285 GetParam().expected_delay_ms)); |
| 276 } while (GetParam().backoff_factor > 1 && | 286 } while (GetParam().backoff_factor > 1 && |
| 277 expected_delay_ms <= kPolicyRefreshRate); | 287 expected_delay_ms <= kPolicyRefreshRate); |
| 288 } else { |
| 289 EXPECT_EQ(base::TimeDelta(), last_delay_); |
| 290 EXPECT_TRUE(last_callback_.is_null()); |
| 278 } | 291 } |
| 279 } | 292 } |
| 280 | 293 |
| 281 INSTANTIATE_TEST_CASE_P(CloudPolicyRefreshSchedulerClientErrorTest, | 294 INSTANTIATE_TEST_CASE_P(CloudPolicyRefreshSchedulerClientErrorTest, |
| 282 CloudPolicyRefreshSchedulerClientErrorTest, | 295 CloudPolicyRefreshSchedulerClientErrorTest, |
| 283 testing::ValuesIn(kClientErrorTestCases)); | 296 testing::ValuesIn(kClientErrorTestCases)); |
| 284 | 297 |
| 285 } // namespace policy | 298 } // namespace policy |
| OLD | NEW |