| 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/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/test/test_browser_thread.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 17 |
| 15 using ::testing::AnyNumber; | 18 using ::testing::AnyNumber; |
| 16 using ::testing::Mock; | 19 using ::testing::Mock; |
| 17 using ::testing::_; | 20 using ::testing::_; |
| 18 | 21 |
| 19 namespace policy { | 22 namespace policy { |
| 20 | 23 |
| 21 namespace { | 24 namespace { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 PolicyServiceImpl::Providers providers; | 58 PolicyServiceImpl::Providers providers; |
| 56 providers.push_back(&provider0_); | 59 providers.push_back(&provider0_); |
| 57 providers.push_back(&provider1_); | 60 providers.push_back(&provider1_); |
| 58 providers.push_back(&provider2_); | 61 providers.push_back(&provider2_); |
| 59 policy_service_.reset(new PolicyServiceImpl(providers)); | 62 policy_service_.reset(new PolicyServiceImpl(providers)); |
| 60 } | 63 } |
| 61 | 64 |
| 62 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, | 65 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, |
| 63 const base::Value*)); | 66 const base::Value*)); |
| 64 | 67 |
| 68 MOCK_METHOD0(OnPolicyRefresh, void()); |
| 69 |
| 65 // Returns true if the policies for |domain|, |component_id| match |expected|. | 70 // Returns true if the policies for |domain|, |component_id| match |expected|. |
| 66 bool VerifyPolicies(PolicyDomain domain, | 71 bool VerifyPolicies(PolicyDomain domain, |
| 67 const std::string& component_id, | 72 const std::string& component_id, |
| 68 const PolicyMap& expected) { | 73 const PolicyMap& expected) { |
| 69 const PolicyMap* policies = | 74 const PolicyMap* policies = |
| 70 policy_service_->GetPolicies(domain, component_id); | 75 policy_service_->GetPolicies(domain, component_id); |
| 71 return policies && policies->Equals(expected); | 76 return policies && policies->Equals(expected); |
| 72 } | 77 } |
| 73 | 78 |
| 74 protected: | 79 protected: |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 | 230 |
| 226 // No more notifications after destroying the registrar. | 231 // No more notifications after destroying the registrar. |
| 227 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 232 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
| 228 registrar.reset(); | 233 registrar.reset(); |
| 229 provider0_.AddMandatoryPolicy("aaa", kValue1.DeepCopy()); | 234 provider0_.AddMandatoryPolicy("aaa", kValue1.DeepCopy()); |
| 230 provider0_.AddMandatoryPolicy("pre", kValue1.DeepCopy()); | 235 provider0_.AddMandatoryPolicy("pre", kValue1.DeepCopy()); |
| 231 provider0_.RefreshPolicies(); | 236 provider0_.RefreshPolicies(); |
| 232 Mock::VerifyAndClearExpectations(this); | 237 Mock::VerifyAndClearExpectations(this); |
| 233 } | 238 } |
| 234 | 239 |
| 240 TEST_F(PolicyServiceTest, RefreshPolicies) { |
| 241 MessageLoop loop; |
| 242 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); |
| 243 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); |
| 244 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop); |
| 245 |
| 246 provider0_.SetNotifyOnRefresh(false); |
| 247 provider1_.SetNotifyOnRefresh(false); |
| 248 provider2_.SetNotifyOnRefresh(false); |
| 249 |
| 250 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 251 policy_service_->RefreshPolicies(base::Bind( |
| 252 &PolicyServiceTest::OnPolicyRefresh, |
| 253 base::Unretained(this))); |
| 254 loop.RunAllPending(); |
| 255 Mock::VerifyAndClearExpectations(this); |
| 256 |
| 257 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 258 base::FundamentalValue kValue0(0); |
| 259 provider0_.AddMandatoryPolicy("aaa", kValue0.DeepCopy()); |
| 260 provider0_.NotifyPolicyUpdated(); |
| 261 loop.RunAllPending(); |
| 262 Mock::VerifyAndClearExpectations(this); |
| 263 |
| 264 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 265 base::FundamentalValue kValue1(1); |
| 266 provider1_.AddRecommendedPolicy("aaa", kValue1.DeepCopy()); |
| 267 provider1_.NotifyPolicyUpdated(); |
| 268 loop.RunAllPending(); |
| 269 Mock::VerifyAndClearExpectations(this); |
| 270 |
| 271 // A provider can refresh more than once after a RefreshPolicies call, but |
| 272 // OnPolicyRefresh should be triggered only after all providers are |
| 273 // refreshed. |
| 274 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 275 provider1_.AddRecommendedPolicy("bbb", kValue1.DeepCopy()); |
| 276 provider1_.NotifyPolicyUpdated(); |
| 277 loop.RunAllPending(); |
| 278 Mock::VerifyAndClearExpectations(this); |
| 279 |
| 280 // If another RefreshPolicies() call happens while waiting for a previous |
| 281 // one to complete, then all providers must refresh again. |
| 282 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 283 policy_service_->RefreshPolicies(base::Bind( |
| 284 &PolicyServiceTest::OnPolicyRefresh, |
| 285 base::Unretained(this))); |
| 286 loop.RunAllPending(); |
| 287 Mock::VerifyAndClearExpectations(this); |
| 288 |
| 289 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
| 290 provider2_.AddMandatoryPolicy("bbb", kValue0.DeepCopy()); |
| 291 provider2_.NotifyPolicyUpdated(); |
| 292 loop.RunAllPending(); |
| 293 Mock::VerifyAndClearExpectations(this); |
| 294 |
| 295 // Providers 0 and 1 must reload again. |
| 296 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); |
| 297 base::FundamentalValue kValue2(2); |
| 298 provider0_.AddMandatoryPolicy("aaa", kValue2.DeepCopy()); |
| 299 provider0_.NotifyPolicyUpdated(); |
| 300 provider1_.NotifyPolicyUpdated(); |
| 301 loop.RunAllPending(); |
| 302 Mock::VerifyAndClearExpectations(this); |
| 303 |
| 304 const PolicyMap* policies = policy_service_->GetPolicies( |
| 305 POLICY_DOMAIN_CHROME, ""); |
| 306 ASSERT_TRUE(policies); |
| 307 EXPECT_TRUE(base::Value::Equals(&kValue2, policies->GetValue("aaa"))); |
| 308 EXPECT_TRUE(base::Value::Equals(&kValue0, policies->GetValue("bbb"))); |
| 309 } |
| 310 |
| 235 } // namespace policy | 311 } // namespace policy |
| OLD | NEW |