| 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 "chrome/browser/policy/policy_service_impl.h" | 5 #include "chrome/browser/policy/policy_service_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 return base::Value::Equals(arg, expected); | 45 return base::Value::Equals(arg, expected); |
| 46 } | 46 } |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 class PolicyServiceTest : public testing::Test { | 50 class PolicyServiceTest : public testing::Test { |
| 51 public: | 51 public: |
| 52 PolicyServiceTest() {} | 52 PolicyServiceTest() {} |
| 53 | 53 |
| 54 void SetUp() OVERRIDE { | 54 void SetUp() OVERRIDE { |
| 55 EXPECT_CALL(provider0_, ProvideInternal(_)) | |
| 56 .WillRepeatedly(CopyPolicyMap(&policy0_)); | |
| 57 EXPECT_CALL(provider1_, ProvideInternal(_)) | |
| 58 .WillRepeatedly(CopyPolicyMap(&policy1_)); | |
| 59 EXPECT_CALL(provider2_, ProvideInternal(_)) | |
| 60 .WillRepeatedly(CopyPolicyMap(&policy2_)); | |
| 61 | |
| 62 EXPECT_CALL(provider0_, IsInitializationComplete()) | 55 EXPECT_CALL(provider0_, IsInitializationComplete()) |
| 63 .WillRepeatedly(Return(true)); | 56 .WillRepeatedly(Return(true)); |
| 64 EXPECT_CALL(provider1_, IsInitializationComplete()) | 57 EXPECT_CALL(provider1_, IsInitializationComplete()) |
| 65 .WillRepeatedly(Return(true)); | 58 .WillRepeatedly(Return(true)); |
| 66 EXPECT_CALL(provider2_, IsInitializationComplete()) | 59 EXPECT_CALL(provider2_, IsInitializationComplete()) |
| 67 .WillRepeatedly(Return(true)); | 60 .WillRepeatedly(Return(true)); |
| 68 | 61 |
| 69 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 62 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 70 base::Value::CreateIntegerValue(13)); | 63 base::Value::CreateIntegerValue(13)); |
| 64 provider0_.UpdateProviderChromePolicy(policy0_); |
| 71 | 65 |
| 72 PolicyServiceImpl::Providers providers; | 66 PolicyServiceImpl::Providers providers; |
| 73 providers.push_back(&provider0_); | 67 providers.push_back(&provider0_); |
| 74 providers.push_back(&provider1_); | 68 providers.push_back(&provider1_); |
| 75 providers.push_back(&provider2_); | 69 providers.push_back(&provider2_); |
| 76 policy_service_.reset(new PolicyServiceImpl(providers)); | 70 policy_service_.reset(new PolicyServiceImpl(providers)); |
| 77 } | 71 } |
| 78 | 72 |
| 79 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, | 73 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, |
| 80 const base::Value*)); | 74 const base::Value*)); |
| 81 | 75 |
| 82 MOCK_METHOD0(OnPolicyRefresh, void()); | 76 MOCK_METHOD0(OnPolicyRefresh, void()); |
| 83 | 77 |
| 84 // Returns true if the policies for |domain|, |component_id| match |expected|. | 78 // Returns true if the policies for |domain|, |component_id| match |expected|. |
| 85 bool VerifyPolicies(PolicyDomain domain, | 79 bool VerifyPolicies(PolicyDomain domain, |
| 86 const std::string& component_id, | 80 const std::string& component_id, |
| 87 const PolicyMap& expected) { | 81 const PolicyMap& expected) { |
| 88 const PolicyMap* policies = | 82 return policy_service_->GetPolicies(domain, component_id).Equals(expected); |
| 89 policy_service_->GetPolicies(domain, component_id); | |
| 90 return policies && policies->Equals(expected); | |
| 91 } | 83 } |
| 92 | 84 |
| 93 protected: | 85 protected: |
| 94 MockConfigurationPolicyProvider provider0_; | 86 MockConfigurationPolicyProvider provider0_; |
| 95 MockConfigurationPolicyProvider provider1_; | 87 MockConfigurationPolicyProvider provider1_; |
| 96 MockConfigurationPolicyProvider provider2_; | 88 MockConfigurationPolicyProvider provider2_; |
| 97 PolicyMap policy0_; | 89 PolicyMap policy0_; |
| 98 PolicyMap policy1_; | 90 PolicyMap policy1_; |
| 99 PolicyMap policy2_; | 91 PolicyMap policy2_; |
| 100 scoped_ptr<PolicyServiceImpl> policy_service_; | 92 scoped_ptr<PolicyServiceImpl> policy_service_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 120 | 112 |
| 121 PolicyMap expectedCurrent; | 113 PolicyMap expectedCurrent; |
| 122 expectedCurrent.CopyFrom(expectedPrevious); | 114 expectedCurrent.CopyFrom(expectedPrevious); |
| 123 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 115 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 124 base::Value::CreateIntegerValue(123)); | 116 base::Value::CreateIntegerValue(123)); |
| 125 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 117 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 126 base::Value::CreateIntegerValue(123)); | 118 base::Value::CreateIntegerValue(123)); |
| 127 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 119 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 128 PolicyEquals(&expectedPrevious), | 120 PolicyEquals(&expectedPrevious), |
| 129 PolicyEquals(&expectedCurrent))); | 121 PolicyEquals(&expectedCurrent))); |
| 130 provider0_.NotifyPolicyUpdated(); | 122 provider0_.UpdateProviderChromePolicy(policy0_); |
| 131 Mock::VerifyAndClearExpectations(&observer); | 123 Mock::VerifyAndClearExpectations(&observer); |
| 132 | 124 |
| 133 // No changes. | 125 // No changes. |
| 134 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); | 126 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
| 135 provider0_.NotifyPolicyUpdated(); | 127 provider0_.UpdateProviderChromePolicy(policy0_); |
| 136 Mock::VerifyAndClearExpectations(&observer); | 128 Mock::VerifyAndClearExpectations(&observer); |
| 137 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 129 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
| 138 | 130 |
| 139 // New policy. | 131 // New policy. |
| 140 expectedPrevious.CopyFrom(expectedCurrent); | 132 expectedPrevious.CopyFrom(expectedCurrent); |
| 141 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 133 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 142 base::Value::CreateIntegerValue(456)); | 134 base::Value::CreateIntegerValue(456)); |
| 143 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 135 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 144 base::Value::CreateIntegerValue(456)); | 136 base::Value::CreateIntegerValue(456)); |
| 145 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 137 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 146 PolicyEquals(&expectedPrevious), | 138 PolicyEquals(&expectedPrevious), |
| 147 PolicyEquals(&expectedCurrent))); | 139 PolicyEquals(&expectedCurrent))); |
| 148 provider0_.NotifyPolicyUpdated(); | 140 provider0_.UpdateProviderChromePolicy(policy0_); |
| 149 Mock::VerifyAndClearExpectations(&observer); | 141 Mock::VerifyAndClearExpectations(&observer); |
| 150 | 142 |
| 151 // Removed policy. | 143 // Removed policy. |
| 152 expectedPrevious.CopyFrom(expectedCurrent); | 144 expectedPrevious.CopyFrom(expectedCurrent); |
| 153 expectedCurrent.Erase("bbb"); | 145 expectedCurrent.Erase("bbb"); |
| 154 policy0_.Erase("bbb"); | 146 policy0_.Erase("bbb"); |
| 155 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 147 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 156 PolicyEquals(&expectedPrevious), | 148 PolicyEquals(&expectedPrevious), |
| 157 PolicyEquals(&expectedCurrent))); | 149 PolicyEquals(&expectedCurrent))); |
| 158 provider0_.NotifyPolicyUpdated(); | 150 provider0_.UpdateProviderChromePolicy(policy0_); |
| 159 Mock::VerifyAndClearExpectations(&observer); | 151 Mock::VerifyAndClearExpectations(&observer); |
| 160 | 152 |
| 161 // Changed policy. | 153 // Changed policy. |
| 162 expectedPrevious.CopyFrom(expectedCurrent); | 154 expectedPrevious.CopyFrom(expectedCurrent); |
| 163 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 155 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 164 base::Value::CreateIntegerValue(789)); | 156 base::Value::CreateIntegerValue(789)); |
| 165 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 157 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 166 base::Value::CreateIntegerValue(789)); | 158 base::Value::CreateIntegerValue(789)); |
| 167 | 159 |
| 168 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 160 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
| 169 PolicyEquals(&expectedPrevious), | 161 PolicyEquals(&expectedPrevious), |
| 170 PolicyEquals(&expectedCurrent))); | 162 PolicyEquals(&expectedCurrent))); |
| 171 provider0_.NotifyPolicyUpdated(); | 163 provider0_.UpdateProviderChromePolicy(policy0_); |
| 172 Mock::VerifyAndClearExpectations(&observer); | 164 Mock::VerifyAndClearExpectations(&observer); |
| 173 | 165 |
| 174 // No changes again. | 166 // No changes again. |
| 175 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); | 167 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
| 176 provider0_.NotifyPolicyUpdated(); | 168 provider0_.UpdateProviderChromePolicy(policy0_); |
| 177 Mock::VerifyAndClearExpectations(&observer); | 169 Mock::VerifyAndClearExpectations(&observer); |
| 178 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 170 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
| 179 | 171 |
| 180 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); | 172 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); |
| 181 } | 173 } |
| 182 | 174 |
| 183 TEST_F(PolicyServiceTest, Priorities) { | 175 TEST_F(PolicyServiceTest, Priorities) { |
| 184 PolicyMap expected; | 176 PolicyMap expected; |
| 185 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 177 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 186 base::Value::CreateIntegerValue(13)); | 178 base::Value::CreateIntegerValue(13)); |
| 187 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 179 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 188 base::Value::CreateIntegerValue(0)); | 180 base::Value::CreateIntegerValue(0)); |
| 189 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 181 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 190 base::Value::CreateIntegerValue(0)); | 182 base::Value::CreateIntegerValue(0)); |
| 191 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 183 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 192 base::Value::CreateIntegerValue(1)); | 184 base::Value::CreateIntegerValue(1)); |
| 193 policy2_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 185 policy2_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 194 base::Value::CreateIntegerValue(2)); | 186 base::Value::CreateIntegerValue(2)); |
| 195 provider0_.NotifyPolicyUpdated(); | 187 provider0_.UpdateProviderChromePolicy(policy0_); |
| 196 provider1_.NotifyPolicyUpdated(); | 188 provider1_.UpdateProviderChromePolicy(policy1_); |
| 197 provider2_.NotifyPolicyUpdated(); | 189 provider2_.UpdateProviderChromePolicy(policy2_); |
| 198 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 190 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 199 | 191 |
| 200 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 192 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 201 base::Value::CreateIntegerValue(1)); | 193 base::Value::CreateIntegerValue(1)); |
| 202 policy0_.Erase("aaa"); | 194 policy0_.Erase("aaa"); |
| 203 provider0_.NotifyPolicyUpdated(); | 195 provider0_.UpdateProviderChromePolicy(policy0_); |
| 204 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 196 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 205 | 197 |
| 206 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 198 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 207 base::Value::CreateIntegerValue(2)); | 199 base::Value::CreateIntegerValue(2)); |
| 208 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 200 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 209 base::Value::CreateIntegerValue(1)); | 201 base::Value::CreateIntegerValue(1)); |
| 210 provider1_.NotifyPolicyUpdated(); | 202 provider1_.UpdateProviderChromePolicy(policy1_); |
| 211 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 203 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
| 212 } | 204 } |
| 213 | 205 |
| 214 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { | 206 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { |
| 215 scoped_ptr<PolicyChangeRegistrar> registrar( | 207 scoped_ptr<PolicyChangeRegistrar> registrar( |
| 216 new PolicyChangeRegistrar( | 208 new PolicyChangeRegistrar( |
| 217 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); | 209 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); |
| 218 | 210 |
| 219 // Starting to observe existing policies doesn't trigger a notification. | 211 // Starting to observe existing policies doesn't trigger a notification. |
| 220 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 212 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 221 registrar->Observe("pre", base::Bind( | 213 registrar->Observe("pre", base::Bind( |
| 222 &PolicyServiceTest::OnPolicyValueUpdated, | 214 &PolicyServiceTest::OnPolicyValueUpdated, |
| 223 base::Unretained(this))); | 215 base::Unretained(this))); |
| 224 registrar->Observe("aaa", base::Bind( | 216 registrar->Observe("aaa", base::Bind( |
| 225 &PolicyServiceTest::OnPolicyValueUpdated, | 217 &PolicyServiceTest::OnPolicyValueUpdated, |
| 226 base::Unretained(this))); | 218 base::Unretained(this))); |
| 227 Mock::VerifyAndClearExpectations(this); | 219 Mock::VerifyAndClearExpectations(this); |
| 228 | 220 |
| 229 // Changing it now triggers a notification. | 221 // Changing it now triggers a notification. |
| 230 base::FundamentalValue kValue0(0); | 222 base::FundamentalValue kValue0(0); |
| 231 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); | 223 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); |
| 232 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 224 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 233 kValue0.DeepCopy()); | 225 kValue0.DeepCopy()); |
| 234 provider0_.NotifyPolicyUpdated(); | 226 provider0_.UpdateProviderChromePolicy(policy0_); |
| 235 Mock::VerifyAndClearExpectations(this); | 227 Mock::VerifyAndClearExpectations(this); |
| 236 | 228 |
| 237 // Changing other values doesn't trigger a notification. | 229 // Changing other values doesn't trigger a notification. |
| 238 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 230 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 239 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 231 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 240 kValue0.DeepCopy()); | 232 kValue0.DeepCopy()); |
| 241 provider0_.NotifyPolicyUpdated(); | 233 provider0_.UpdateProviderChromePolicy(policy0_); |
| 242 Mock::VerifyAndClearExpectations(this); | 234 Mock::VerifyAndClearExpectations(this); |
| 243 | 235 |
| 244 // Modifying the value triggers a notification. | 236 // Modifying the value triggers a notification. |
| 245 base::FundamentalValue kValue1(1); | 237 base::FundamentalValue kValue1(1); |
| 246 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), | 238 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), |
| 247 ValueEquals(&kValue1))); | 239 ValueEquals(&kValue1))); |
| 248 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 240 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 249 kValue1.DeepCopy()); | 241 kValue1.DeepCopy()); |
| 250 provider0_.NotifyPolicyUpdated(); | 242 provider0_.UpdateProviderChromePolicy(policy0_); |
| 251 Mock::VerifyAndClearExpectations(this); | 243 Mock::VerifyAndClearExpectations(this); |
| 252 | 244 |
| 253 // Removing the value triggers a notification. | 245 // Removing the value triggers a notification. |
| 254 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); | 246 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); |
| 255 policy0_.Erase("aaa"); | 247 policy0_.Erase("aaa"); |
| 256 provider0_.NotifyPolicyUpdated(); | 248 provider0_.UpdateProviderChromePolicy(policy0_); |
| 257 Mock::VerifyAndClearExpectations(this); | 249 Mock::VerifyAndClearExpectations(this); |
| 258 | 250 |
| 259 // No more notifications after destroying the registrar. | 251 // No more notifications after destroying the registrar. |
| 260 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 252 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 261 registrar.reset(); | 253 registrar.reset(); |
| 262 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 254 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 263 kValue1.DeepCopy()); | 255 kValue1.DeepCopy()); |
| 264 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 256 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 265 kValue1.DeepCopy()); | 257 kValue1.DeepCopy()); |
| 266 provider0_.NotifyPolicyUpdated(); | 258 provider0_.UpdateProviderChromePolicy(policy0_); |
| 267 Mock::VerifyAndClearExpectations(this); | 259 Mock::VerifyAndClearExpectations(this); |
| 268 } | 260 } |
| 269 | 261 |
| 270 TEST_F(PolicyServiceTest, RefreshPolicies) { | 262 TEST_F(PolicyServiceTest, RefreshPolicies) { |
| 271 MessageLoop loop; | 263 MessageLoop loop; |
| 272 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); | 264 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); |
| 273 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); | 265 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); |
| 274 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop); | 266 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop); |
| 275 | 267 |
| 276 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); | 268 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); |
| 277 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); | 269 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); |
| 278 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); | 270 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); |
| 279 | 271 |
| 280 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 272 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 281 policy_service_->RefreshPolicies(base::Bind( | 273 policy_service_->RefreshPolicies(base::Bind( |
| 282 &PolicyServiceTest::OnPolicyRefresh, | 274 &PolicyServiceTest::OnPolicyRefresh, |
| 283 base::Unretained(this))); | 275 base::Unretained(this))); |
| 284 loop.RunAllPending(); | 276 loop.RunAllPending(); |
| 285 Mock::VerifyAndClearExpectations(this); | 277 Mock::VerifyAndClearExpectations(this); |
| 286 | 278 |
| 287 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 279 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 288 base::FundamentalValue kValue0(0); | 280 base::FundamentalValue kValue0(0); |
| 289 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 281 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 290 kValue0.DeepCopy()); | 282 kValue0.DeepCopy()); |
| 291 provider0_.NotifyPolicyUpdated(); | 283 provider0_.UpdateProviderChromePolicy(policy0_); |
| 292 loop.RunAllPending(); | 284 loop.RunAllPending(); |
| 293 Mock::VerifyAndClearExpectations(this); | 285 Mock::VerifyAndClearExpectations(this); |
| 294 | 286 |
| 295 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 287 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 296 base::FundamentalValue kValue1(1); | 288 base::FundamentalValue kValue1(1); |
| 297 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 289 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 298 kValue1.DeepCopy()); | 290 kValue1.DeepCopy()); |
| 299 provider1_.NotifyPolicyUpdated(); | 291 provider1_.UpdateProviderChromePolicy(policy1_); |
| 300 loop.RunAllPending(); | 292 loop.RunAllPending(); |
| 301 Mock::VerifyAndClearExpectations(this); | 293 Mock::VerifyAndClearExpectations(this); |
| 302 | 294 |
| 303 // A provider can refresh more than once after a RefreshPolicies call, but | 295 // A provider can refresh more than once after a RefreshPolicies call, but |
| 304 // OnPolicyRefresh should be triggered only after all providers are | 296 // OnPolicyRefresh should be triggered only after all providers are |
| 305 // refreshed. | 297 // refreshed. |
| 306 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 298 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 307 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 299 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 308 kValue1.DeepCopy()); | 300 kValue1.DeepCopy()); |
| 309 provider1_.NotifyPolicyUpdated(); | 301 provider1_.UpdateProviderChromePolicy(policy1_); |
| 310 loop.RunAllPending(); | 302 loop.RunAllPending(); |
| 311 Mock::VerifyAndClearExpectations(this); | 303 Mock::VerifyAndClearExpectations(this); |
| 312 | 304 |
| 313 // If another RefreshPolicies() call happens while waiting for a previous | 305 // If another RefreshPolicies() call happens while waiting for a previous |
| 314 // one to complete, then all providers must refresh again. | 306 // one to complete, then all providers must refresh again. |
| 315 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 307 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 316 policy_service_->RefreshPolicies(base::Bind( | 308 policy_service_->RefreshPolicies(base::Bind( |
| 317 &PolicyServiceTest::OnPolicyRefresh, | 309 &PolicyServiceTest::OnPolicyRefresh, |
| 318 base::Unretained(this))); | 310 base::Unretained(this))); |
| 319 loop.RunAllPending(); | 311 loop.RunAllPending(); |
| 320 Mock::VerifyAndClearExpectations(this); | 312 Mock::VerifyAndClearExpectations(this); |
| 321 | 313 |
| 322 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 314 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 323 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 315 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 324 kValue0.DeepCopy()); | 316 kValue0.DeepCopy()); |
| 325 provider2_.NotifyPolicyUpdated(); | 317 provider2_.UpdateProviderChromePolicy(policy2_); |
| 326 loop.RunAllPending(); | 318 loop.RunAllPending(); |
| 327 Mock::VerifyAndClearExpectations(this); | 319 Mock::VerifyAndClearExpectations(this); |
| 328 | 320 |
| 329 // Providers 0 and 1 must reload again. | 321 // Providers 0 and 1 must reload again. |
| 330 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); | 322 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); |
| 331 base::FundamentalValue kValue2(2); | 323 base::FundamentalValue kValue2(2); |
| 332 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 324 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 333 kValue2.DeepCopy()); | 325 kValue2.DeepCopy()); |
| 334 provider0_.NotifyPolicyUpdated(); | 326 provider0_.UpdateProviderChromePolicy(policy0_); |
| 335 provider1_.NotifyPolicyUpdated(); | 327 provider1_.UpdateProviderChromePolicy(policy1_); |
| 336 loop.RunAllPending(); | 328 loop.RunAllPending(); |
| 337 Mock::VerifyAndClearExpectations(this); | 329 Mock::VerifyAndClearExpectations(this); |
| 338 | 330 |
| 339 const PolicyMap* policies = policy_service_->GetPolicies( | 331 const PolicyMap& policies = policy_service_->GetPolicies( |
| 340 POLICY_DOMAIN_CHROME, ""); | 332 POLICY_DOMAIN_CHROME, ""); |
| 341 ASSERT_TRUE(policies); | 333 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa"))); |
| 342 EXPECT_TRUE(base::Value::Equals(&kValue2, policies->GetValue("aaa"))); | 334 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb"))); |
| 343 EXPECT_TRUE(base::Value::Equals(&kValue0, policies->GetValue("bbb"))); | |
| 344 } | 335 } |
| 345 | 336 |
| 346 } // namespace policy | 337 } // namespace policy |
| OLD | NEW |