Chromium Code Reviews| 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/values.h" | |
|
pastarmovj
2012/12/04 12:20:38
Do you need this include?
Mattias Nissler (ping if slow)
2012/12/04 13:20:40
No, removed.
| |
| 10 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" | 11 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" |
| 11 #include "chrome/browser/policy/mock_cloud_policy_client.h" | 12 #include "chrome/browser/policy/mock_cloud_policy_client.h" |
| 12 #include "chrome/browser/policy/mock_cloud_policy_store.h" | 13 #include "chrome/browser/policy/mock_cloud_policy_store.h" |
| 13 #include "chrome/browser/policy/test_task_runner.h" | 14 #include "chrome/browser/policy/test_task_runner.h" |
| 14 #include "chrome/browser/prefs/browser_prefs.h" | 15 #include "chrome/browser/prefs/browser_prefs.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "policy/policy_constants.h" |
| 16 #include "chrome/test/base/testing_pref_service.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace em = enterprise_management; | 20 namespace em = enterprise_management; |
| 21 | 21 |
| 22 using testing::DoAll; | 22 using testing::DoAll; |
| 23 using testing::Return; | 23 using testing::Return; |
| 24 using testing::SaveArg; | 24 using testing::SaveArg; |
| 25 using testing::_; | 25 using testing::_; |
| 26 | 26 |
| 27 namespace policy { | 27 namespace policy { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 const int64 kPolicyRefreshRate = 4 * 60 * 60 * 1000; | 30 const int64 kPolicyRefreshRate = 4 * 60 * 60 * 1000; |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 class CloudPolicyRefreshSchedulerTest : public testing::Test { | 33 class CloudPolicyRefreshSchedulerTest : public testing::Test { |
| 34 protected: | 34 protected: |
| 35 CloudPolicyRefreshSchedulerTest() | 35 CloudPolicyRefreshSchedulerTest() |
| 36 : task_runner_(new TestTaskRunner()), | 36 : task_runner_(new TestTaskRunner()), |
| 37 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) { | 37 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) {} |
| 38 chrome::RegisterLocalState(&prefs_); | |
| 39 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, kPolicyRefreshRate); | |
| 40 } | |
| 41 | 38 |
| 42 virtual void SetUp() OVERRIDE { | 39 virtual void SetUp() OVERRIDE { |
| 43 client_.SetDMToken("token"); | 40 client_.SetDMToken("token"); |
| 44 | 41 |
| 45 // Set up the protobuf timestamp to be one minute in the past. Since the | 42 // Set up the protobuf timestamp to be one minute in the past. Since the |
| 46 // protobuf field only has millisecond precision, we convert the actual | 43 // protobuf field only has millisecond precision, we convert the actual |
| 47 // value back to get a millisecond-clamped time stamp for the checks below. | 44 // value back to get a millisecond-clamped time stamp for the checks below. |
| 48 store_.policy_.reset(new em::PolicyData()); | 45 store_.policy_.reset(new em::PolicyData()); |
| 49 store_.policy_->set_timestamp( | 46 store_.policy_->set_timestamp( |
| 50 ((base::Time::NowFromSystemTime() - base::TimeDelta::FromMinutes(1)) - | 47 ((base::Time::NowFromSystemTime() - base::TimeDelta::FromMinutes(1)) - |
| 51 base::Time::UnixEpoch()).InMilliseconds()); | 48 base::Time::UnixEpoch()).InMilliseconds()); |
| 52 last_refresh_ = | 49 last_refresh_ = |
| 53 base::Time::UnixEpoch() + | 50 base::Time::UnixEpoch() + |
| 54 base::TimeDelta::FromMilliseconds(store_.policy_->timestamp()); | 51 base::TimeDelta::FromMilliseconds(store_.policy_->timestamp()); |
| 55 | 52 |
| 56 // Keep track of when the last task was posted. | 53 // Keep track of when the last task was posted. |
| 57 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)) | 54 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)) |
| 58 .WillRepeatedly(DoAll(SaveArg<1>(&last_callback_), | 55 .WillRepeatedly(DoAll(SaveArg<1>(&last_callback_), |
| 59 SaveArg<2>(&last_delay_), | 56 SaveArg<2>(&last_delay_), |
| 60 Return(true))); | 57 Return(true))); |
| 61 } | 58 } |
| 62 | 59 |
| 63 CloudPolicyRefreshScheduler* CreateRefreshScheduler() { | 60 CloudPolicyRefreshScheduler* CreateRefreshScheduler() { |
| 64 return new CloudPolicyRefreshScheduler(&client_, &store_, &prefs_, | 61 CloudPolicyRefreshScheduler* scheduler = |
| 65 prefs::kUserPolicyRefreshRate, | 62 new CloudPolicyRefreshScheduler(&client_, &store_, task_runner_); |
| 66 task_runner_); | 63 scheduler->SetRefreshDelay(kPolicyRefreshRate); |
| 64 return scheduler; | |
| 67 } | 65 } |
| 68 | 66 |
| 69 void NotifyIPAddressChanged() { | 67 void NotifyIPAddressChanged() { |
| 70 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | 68 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 71 loop_.RunUntilIdle(); | 69 loop_.RunUntilIdle(); |
| 72 } | 70 } |
| 73 | 71 |
| 74 void CheckTiming(int64 expected_delay_ms) { | 72 void CheckTiming(int64 expected_delay_ms) { |
| 75 base::Time now(base::Time::NowFromSystemTime()); | 73 base::Time now(base::Time::NowFromSystemTime()); |
| 76 base::TimeDelta expected_delay( | 74 base::TimeDelta expected_delay( |
| 77 base::TimeDelta::FromMilliseconds(expected_delay_ms)); | 75 base::TimeDelta::FromMilliseconds(expected_delay_ms)); |
| 78 EXPECT_GE(last_delay_, expected_delay - (now - last_refresh_)); | 76 EXPECT_GE(last_delay_, expected_delay - (now - last_refresh_)); |
| 79 EXPECT_LE(last_delay_, expected_delay); | 77 EXPECT_LE(last_delay_, expected_delay); |
| 80 } | 78 } |
| 81 | 79 |
| 82 MessageLoop loop_; | 80 MessageLoop loop_; |
| 83 MockCloudPolicyClient client_; | 81 MockCloudPolicyClient client_; |
| 84 MockCloudPolicyStore store_; | 82 MockCloudPolicyStore store_; |
| 85 TestingPrefService prefs_; | |
| 86 scoped_refptr<TestTaskRunner> task_runner_; | 83 scoped_refptr<TestTaskRunner> task_runner_; |
| 87 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 84 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 88 | 85 |
| 89 // Base time for the refresh that the scheduler should be using. | 86 // Base time for the refresh that the scheduler should be using. |
| 90 base::Time last_refresh_; | 87 base::Time last_refresh_; |
| 91 | 88 |
| 92 base::Closure last_callback_; | 89 base::Closure last_callback_; |
| 93 base::TimeDelta last_delay_; | 90 base::TimeDelta last_delay_; |
| 94 }; | 91 }; |
| 95 | 92 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 last_callback_.Run(); | 126 last_callback_.Run(); |
| 130 } | 127 } |
| 131 | 128 |
| 132 TEST_F(CloudPolicyRefreshSchedulerTest, Unregistered) { | 129 TEST_F(CloudPolicyRefreshSchedulerTest, Unregistered) { |
| 133 client_.SetDMToken(""); | 130 client_.SetDMToken(""); |
| 134 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).Times(0); | 131 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).Times(0); |
| 135 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); | 132 scoped_ptr<CloudPolicyRefreshScheduler> scheduler(CreateRefreshScheduler()); |
| 136 client_.NotifyPolicyFetched(); | 133 client_.NotifyPolicyFetched(); |
| 137 client_.NotifyRegistrationStateChanged(); | 134 client_.NotifyRegistrationStateChanged(); |
| 138 client_.NotifyClientError(); | 135 client_.NotifyClientError(); |
| 136 scheduler->SetRefreshDelay(12 * 60 * 60 * 1000); | |
| 139 store_.NotifyStoreLoaded(); | 137 store_.NotifyStoreLoaded(); |
| 140 store_.NotifyStoreError(); | 138 store_.NotifyStoreError(); |
| 141 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, 12 * 60 * 60 * 1000); | |
| 142 } | 139 } |
| 143 | 140 |
| 144 class CloudPolicyRefreshSchedulerSteadyStateTest | 141 class CloudPolicyRefreshSchedulerSteadyStateTest |
| 145 : public CloudPolicyRefreshSchedulerTest { | 142 : public CloudPolicyRefreshSchedulerTest { |
| 146 protected: | 143 protected: |
| 147 CloudPolicyRefreshSchedulerSteadyStateTest() | 144 CloudPolicyRefreshSchedulerSteadyStateTest() |
| 148 : refresh_scheduler_(&client_, &store_, &prefs_, | 145 : refresh_scheduler_(&client_, &store_, task_runner_) {} |
| 149 prefs::kUserPolicyRefreshRate, task_runner_) {} | |
| 150 | 146 |
| 151 virtual void SetUp() OVERRIDE { | 147 virtual void SetUp() OVERRIDE { |
| 148 refresh_scheduler_.SetRefreshDelay(kPolicyRefreshRate); | |
| 152 CloudPolicyRefreshSchedulerTest::SetUp(); | 149 CloudPolicyRefreshSchedulerTest::SetUp(); |
| 153 last_refresh_ = base::Time::NowFromSystemTime(); | 150 last_refresh_ = base::Time::NowFromSystemTime(); |
| 154 client_.NotifyPolicyFetched(); | 151 client_.NotifyPolicyFetched(); |
| 155 CheckTiming(kPolicyRefreshRate); | 152 CheckTiming(kPolicyRefreshRate); |
| 156 } | 153 } |
| 157 | 154 |
| 158 CloudPolicyRefreshScheduler refresh_scheduler_; | 155 CloudPolicyRefreshScheduler refresh_scheduler_; |
| 159 }; | 156 }; |
| 160 | 157 |
| 161 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnPolicyFetched) { | 158 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnPolicyFetched) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 178 CheckTiming(kPolicyRefreshRate); | 175 CheckTiming(kPolicyRefreshRate); |
| 179 } | 176 } |
| 180 | 177 |
| 181 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnStoreError) { | 178 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnStoreError) { |
| 182 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).Times(0); | 179 EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)).Times(0); |
| 183 store_.NotifyStoreError(); | 180 store_.NotifyStoreError(); |
| 184 } | 181 } |
| 185 | 182 |
| 186 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, RefreshDelayChange) { | 183 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, RefreshDelayChange) { |
| 187 const int delay_short_ms = 5 * 60 * 1000; | 184 const int delay_short_ms = 5 * 60 * 1000; |
| 188 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, delay_short_ms); | 185 refresh_scheduler_.SetRefreshDelay(delay_short_ms); |
| 189 CheckTiming(CloudPolicyRefreshScheduler::kRefreshDelayMinMs); | 186 CheckTiming(CloudPolicyRefreshScheduler::kRefreshDelayMinMs); |
| 190 | 187 |
| 191 const int delay_ms = 12 * 60 * 60 * 1000; | 188 const int delay_ms = 12 * 60 * 60 * 1000; |
| 192 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, delay_ms); | 189 refresh_scheduler_.SetRefreshDelay(delay_ms); |
| 193 CheckTiming(delay_ms); | 190 CheckTiming(delay_ms); |
| 194 | 191 |
| 195 const int delay_long_ms = 2 * 24 * 60 * 60 * 1000; | 192 const int delay_long_ms = 2 * 24 * 60 * 60 * 1000; |
| 196 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, delay_long_ms); | 193 refresh_scheduler_.SetRefreshDelay(delay_long_ms); |
| 197 CheckTiming(CloudPolicyRefreshScheduler::kRefreshDelayMaxMs); | 194 CheckTiming(CloudPolicyRefreshScheduler::kRefreshDelayMaxMs); |
| 198 } | 195 } |
| 199 | 196 |
| 200 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnIPAddressChanged) { | 197 TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnIPAddressChanged) { |
| 201 NotifyIPAddressChanged(); | 198 NotifyIPAddressChanged(); |
| 202 CheckTiming(kPolicyRefreshRate); | 199 CheckTiming(kPolicyRefreshRate); |
| 203 | 200 |
| 204 client_.SetStatus(DM_STATUS_REQUEST_FAILED); | 201 client_.SetStatus(DM_STATUS_REQUEST_FAILED); |
| 205 NotifyIPAddressChanged(); | 202 NotifyIPAddressChanged(); |
| 206 EXPECT_EQ(last_delay_, base::TimeDelta()); | 203 EXPECT_EQ(last_delay_, base::TimeDelta()); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 EXPECT_EQ(base::TimeDelta(), last_delay_); | 268 EXPECT_EQ(base::TimeDelta(), last_delay_); |
| 272 EXPECT_TRUE(last_callback_.is_null()); | 269 EXPECT_TRUE(last_callback_.is_null()); |
| 273 } | 270 } |
| 274 } | 271 } |
| 275 | 272 |
| 276 INSTANTIATE_TEST_CASE_P(CloudPolicyRefreshSchedulerClientErrorTest, | 273 INSTANTIATE_TEST_CASE_P(CloudPolicyRefreshSchedulerClientErrorTest, |
| 277 CloudPolicyRefreshSchedulerClientErrorTest, | 274 CloudPolicyRefreshSchedulerClientErrorTest, |
| 278 testing::ValuesIn(kClientErrorTestCases)); | 275 testing::ValuesIn(kClientErrorTestCases)); |
| 279 | 276 |
| 280 } // namespace policy | 277 } // namespace policy |
| OLD | NEW |