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