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" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #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" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/test/test_browser_thread.h" | 14 #include "content/test/test_browser_thread.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using ::testing::AnyNumber; | 18 using ::testing::AnyNumber; |
19 using ::testing::Mock; | 19 using ::testing::Mock; |
20 using ::testing::Return; | 20 using ::testing::Return; |
21 using ::testing::_; | 21 using ::testing::_; |
22 | 22 |
23 namespace policy { | 23 namespace policy { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kExtension[] = "extension-id"; | |
28 const char kSameLevelPolicy[] = "policy-same-level-and-scope"; | |
29 const char kDiffLevelPolicy[] = "chrome-diff-level-and-scope"; | |
30 | |
27 class MockPolicyServiceObserver : public PolicyService::Observer { | 31 class MockPolicyServiceObserver : public PolicyService::Observer { |
28 public: | 32 public: |
29 virtual ~MockPolicyServiceObserver() {} | 33 virtual ~MockPolicyServiceObserver() {} |
30 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, | 34 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, |
31 const std::string&, | 35 const std::string&, |
32 const PolicyMap& previous, | 36 const PolicyMap& previous, |
33 const PolicyMap& current)); | 37 const PolicyMap& current)); |
34 }; | 38 }; |
35 | 39 |
36 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with | 40 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with |
37 // their expected values. | 41 // their expected values. |
38 MATCHER_P(PolicyEquals, expected, "") { | 42 MATCHER_P(PolicyEquals, expected, "") { |
39 return arg.Equals(*expected); | 43 return arg.Equals(*expected); |
40 } | 44 } |
41 | 45 |
42 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() | 46 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() |
43 // with their expected values. | 47 // with their expected values. |
44 MATCHER_P(ValueEquals, expected, "") { | 48 MATCHER_P(ValueEquals, expected, "") { |
45 return base::Value::Equals(arg, expected); | 49 return base::Value::Equals(arg, expected); |
46 } | 50 } |
47 | 51 |
52 // Helper that fills |bundle| with test policies. | |
53 void AddTestPolicies(PolicyBundle* bundle, | |
54 const char* value, | |
55 PolicyLevel level, | |
56 PolicyScope scope) { | |
57 PolicyMap* policy_map = &bundle->Get(POLICY_DOMAIN_CHROME, ""); | |
58 policy_map->Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
59 base::Value::CreateStringValue(value)); | |
60 policy_map->Set(kDiffLevelPolicy, level, scope, | |
61 base::Value::CreateStringValue(value)); | |
62 policy_map = &bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension); | |
63 policy_map->Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY, | |
64 POLICY_SCOPE_USER, base::Value::CreateStringValue(value)); | |
65 policy_map->Set(kDiffLevelPolicy, level, scope, | |
66 base::Value::CreateStringValue(value)); | |
67 } | |
68 | |
48 } // namespace | 69 } // namespace |
49 | 70 |
50 class PolicyServiceTest : public testing::Test { | 71 class PolicyServiceTest : public testing::Test { |
51 public: | 72 public: |
52 PolicyServiceTest() {} | 73 PolicyServiceTest() {} |
53 | 74 |
54 void SetUp() OVERRIDE { | 75 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()) | 76 EXPECT_CALL(provider0_, IsInitializationComplete()) |
63 .WillRepeatedly(Return(true)); | 77 .WillRepeatedly(Return(true)); |
64 EXPECT_CALL(provider1_, IsInitializationComplete()) | 78 EXPECT_CALL(provider1_, IsInitializationComplete()) |
65 .WillRepeatedly(Return(true)); | 79 .WillRepeatedly(Return(true)); |
66 EXPECT_CALL(provider2_, IsInitializationComplete()) | 80 EXPECT_CALL(provider2_, IsInitializationComplete()) |
67 .WillRepeatedly(Return(true)); | 81 .WillRepeatedly(Return(true)); |
68 | 82 |
69 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 83 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
70 base::Value::CreateIntegerValue(13)); | 84 base::Value::CreateIntegerValue(13)); |
85 provider0_.UpdateChromePolicy(policy0_); | |
71 | 86 |
72 PolicyServiceImpl::Providers providers; | 87 PolicyServiceImpl::Providers providers; |
73 providers.push_back(&provider0_); | 88 providers.push_back(&provider0_); |
74 providers.push_back(&provider1_); | 89 providers.push_back(&provider1_); |
75 providers.push_back(&provider2_); | 90 providers.push_back(&provider2_); |
76 policy_service_.reset(new PolicyServiceImpl(providers)); | 91 policy_service_.reset(new PolicyServiceImpl(providers)); |
77 } | 92 } |
78 | 93 |
79 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, | 94 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, |
80 const base::Value*)); | 95 const base::Value*)); |
81 | 96 |
82 MOCK_METHOD0(OnPolicyRefresh, void()); | 97 MOCK_METHOD0(OnPolicyRefresh, void()); |
83 | 98 |
84 // Returns true if the policies for |domain|, |component_id| match |expected|. | 99 // Returns true if the policies for |domain|, |component_id| match |expected|. |
85 bool VerifyPolicies(PolicyDomain domain, | 100 bool VerifyPolicies(PolicyDomain domain, |
86 const std::string& component_id, | 101 const std::string& component_id, |
87 const PolicyMap& expected) { | 102 const PolicyMap& expected) { |
88 const PolicyMap* policies = | 103 return policy_service_->GetPolicies(domain, component_id).Equals(expected); |
89 policy_service_->GetPolicies(domain, component_id); | |
90 return policies && policies->Equals(expected); | |
91 } | 104 } |
92 | 105 |
93 protected: | 106 protected: |
94 MockConfigurationPolicyProvider provider0_; | 107 MockConfigurationPolicyProvider provider0_; |
95 MockConfigurationPolicyProvider provider1_; | 108 MockConfigurationPolicyProvider provider1_; |
96 MockConfigurationPolicyProvider provider2_; | 109 MockConfigurationPolicyProvider provider2_; |
97 PolicyMap policy0_; | 110 PolicyMap policy0_; |
98 PolicyMap policy1_; | 111 PolicyMap policy1_; |
99 PolicyMap policy2_; | 112 PolicyMap policy2_; |
100 scoped_ptr<PolicyServiceImpl> policy_service_; | 113 scoped_ptr<PolicyServiceImpl> policy_service_; |
(...skipping 19 matching lines...) Expand all Loading... | |
120 | 133 |
121 PolicyMap expectedCurrent; | 134 PolicyMap expectedCurrent; |
122 expectedCurrent.CopyFrom(expectedPrevious); | 135 expectedCurrent.CopyFrom(expectedPrevious); |
123 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 136 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
124 base::Value::CreateIntegerValue(123)); | 137 base::Value::CreateIntegerValue(123)); |
125 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 138 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
126 base::Value::CreateIntegerValue(123)); | 139 base::Value::CreateIntegerValue(123)); |
127 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 140 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
128 PolicyEquals(&expectedPrevious), | 141 PolicyEquals(&expectedPrevious), |
129 PolicyEquals(&expectedCurrent))); | 142 PolicyEquals(&expectedCurrent))); |
130 provider0_.NotifyPolicyUpdated(); | 143 provider0_.UpdateChromePolicy(policy0_); |
131 Mock::VerifyAndClearExpectations(&observer); | 144 Mock::VerifyAndClearExpectations(&observer); |
132 | 145 |
133 // No changes. | 146 // No changes. |
134 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); | 147 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
135 provider0_.NotifyPolicyUpdated(); | 148 provider0_.UpdateChromePolicy(policy0_); |
136 Mock::VerifyAndClearExpectations(&observer); | 149 Mock::VerifyAndClearExpectations(&observer); |
137 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 150 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
138 | 151 |
139 // New policy. | 152 // New policy. |
140 expectedPrevious.CopyFrom(expectedCurrent); | 153 expectedPrevious.CopyFrom(expectedCurrent); |
141 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 154 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
142 base::Value::CreateIntegerValue(456)); | 155 base::Value::CreateIntegerValue(456)); |
143 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 156 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
144 base::Value::CreateIntegerValue(456)); | 157 base::Value::CreateIntegerValue(456)); |
145 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 158 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
146 PolicyEquals(&expectedPrevious), | 159 PolicyEquals(&expectedPrevious), |
147 PolicyEquals(&expectedCurrent))); | 160 PolicyEquals(&expectedCurrent))); |
148 provider0_.NotifyPolicyUpdated(); | 161 provider0_.UpdateChromePolicy(policy0_); |
149 Mock::VerifyAndClearExpectations(&observer); | 162 Mock::VerifyAndClearExpectations(&observer); |
150 | 163 |
151 // Removed policy. | 164 // Removed policy. |
152 expectedPrevious.CopyFrom(expectedCurrent); | 165 expectedPrevious.CopyFrom(expectedCurrent); |
153 expectedCurrent.Erase("bbb"); | 166 expectedCurrent.Erase("bbb"); |
154 policy0_.Erase("bbb"); | 167 policy0_.Erase("bbb"); |
155 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 168 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
156 PolicyEquals(&expectedPrevious), | 169 PolicyEquals(&expectedPrevious), |
157 PolicyEquals(&expectedCurrent))); | 170 PolicyEquals(&expectedCurrent))); |
158 provider0_.NotifyPolicyUpdated(); | 171 provider0_.UpdateChromePolicy(policy0_); |
159 Mock::VerifyAndClearExpectations(&observer); | 172 Mock::VerifyAndClearExpectations(&observer); |
160 | 173 |
161 // Changed policy. | 174 // Changed policy. |
162 expectedPrevious.CopyFrom(expectedCurrent); | 175 expectedPrevious.CopyFrom(expectedCurrent); |
163 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 176 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
164 base::Value::CreateIntegerValue(789)); | 177 base::Value::CreateIntegerValue(789)); |
165 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 178 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
166 base::Value::CreateIntegerValue(789)); | 179 base::Value::CreateIntegerValue(789)); |
167 | 180 |
168 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 181 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
169 PolicyEquals(&expectedPrevious), | 182 PolicyEquals(&expectedPrevious), |
170 PolicyEquals(&expectedCurrent))); | 183 PolicyEquals(&expectedCurrent))); |
171 provider0_.NotifyPolicyUpdated(); | 184 provider0_.UpdateChromePolicy(policy0_); |
172 Mock::VerifyAndClearExpectations(&observer); | 185 Mock::VerifyAndClearExpectations(&observer); |
173 | 186 |
174 // No changes again. | 187 // No changes again. |
175 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); | 188 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
176 provider0_.NotifyPolicyUpdated(); | 189 provider0_.UpdateChromePolicy(policy0_); |
177 Mock::VerifyAndClearExpectations(&observer); | 190 Mock::VerifyAndClearExpectations(&observer); |
178 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 191 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
179 | 192 |
180 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); | 193 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); |
181 } | 194 } |
182 | 195 |
196 TEST_F(PolicyServiceTest, NotifyObserversInMultipleNamespaces) { | |
197 const std::string kExtension0("extension-0"); | |
198 const std::string kExtension1("extension-1"); | |
199 const std::string kExtension2("extension-2"); | |
200 MockPolicyServiceObserver chrome_observer; | |
201 MockPolicyServiceObserver extension0_observer; | |
202 MockPolicyServiceObserver extension1_observer; | |
203 MockPolicyServiceObserver extension2_observer; | |
204 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, "", &chrome_observer); | |
205 policy_service_->AddObserver( | |
206 POLICY_DOMAIN_EXTENSIONS, kExtension0, &extension0_observer); | |
Mattias Nissler (ping if slow)
2012/05/15 09:40:33
I think this would become more readable if you put
Joao da Silva
2012/05/15 13:06:31
Done.
| |
207 policy_service_->AddObserver( | |
208 POLICY_DOMAIN_EXTENSIONS, kExtension1, &extension1_observer); | |
209 policy_service_->AddObserver( | |
210 POLICY_DOMAIN_EXTENSIONS, kExtension2, &extension2_observer); | |
211 | |
212 PolicyMap policy_map; | |
213 policy_map.Set("policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
214 base::Value::CreateStringValue("value")); | |
215 | |
216 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | |
217 // The initial setup includes a policy for chrome that is now changing. | |
218 bundle->Get(POLICY_DOMAIN_CHROME, "").CopyFrom(policy_map); | |
219 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension0).CopyFrom(policy_map); | |
220 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension1).CopyFrom(policy_map); | |
221 | |
222 EXPECT_CALL(chrome_observer, | |
223 OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", _, _)); | |
Mattias Nissler (ping if slow)
2012/05/15 09:40:33
add a matcher for checking the correct PolicyMap g
Joao da Silva
2012/05/15 13:06:31
Done.
| |
224 EXPECT_CALL(extension0_observer, | |
225 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0, _, _)); | |
226 EXPECT_CALL(extension1_observer, | |
227 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1, _, _)); | |
228 EXPECT_CALL(extension2_observer, OnPolicyUpdated(_, _, _, _)).Times(0); | |
229 provider0_.UpdatePolicy(bundle.Pass()); | |
230 Mock::VerifyAndClearExpectations(&chrome_observer); | |
231 Mock::VerifyAndClearExpectations(&extension0_observer); | |
232 Mock::VerifyAndClearExpectations(&extension1_observer); | |
233 Mock::VerifyAndClearExpectations(&extension2_observer); | |
234 | |
235 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes, | |
236 // and kExtension2 is new. | |
237 bundle.reset(new PolicyBundle()); | |
238 bundle->Get(POLICY_DOMAIN_CHROME, "").CopyFrom(policy_map); | |
239 policy_map.Set("policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
240 base::Value::CreateStringValue("another value")); | |
241 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension1).CopyFrom(policy_map); | |
242 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension2).CopyFrom(policy_map); | |
243 | |
244 EXPECT_CALL(chrome_observer, OnPolicyUpdated(_, _, _, _)).Times(0); | |
245 EXPECT_CALL(extension0_observer, | |
246 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0, _, _)); | |
247 EXPECT_CALL(extension1_observer, | |
248 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1, _, _)); | |
249 EXPECT_CALL(extension2_observer, | |
250 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension2, _, _)); | |
251 provider0_.UpdatePolicy(bundle.Pass()); | |
252 Mock::VerifyAndClearExpectations(&chrome_observer); | |
253 Mock::VerifyAndClearExpectations(&extension0_observer); | |
254 Mock::VerifyAndClearExpectations(&extension1_observer); | |
255 Mock::VerifyAndClearExpectations(&extension2_observer); | |
256 | |
257 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &chrome_observer); | |
258 policy_service_->RemoveObserver( | |
Mattias Nissler (ping if slow)
2012/05/15 09:40:33
same comment regarding line breaking.
Joao da Silva
2012/05/15 13:06:31
Done.
| |
259 POLICY_DOMAIN_EXTENSIONS, kExtension0, &extension0_observer); | |
260 policy_service_->RemoveObserver( | |
261 POLICY_DOMAIN_EXTENSIONS, kExtension1, &extension1_observer); | |
262 policy_service_->RemoveObserver( | |
263 POLICY_DOMAIN_EXTENSIONS, kExtension2, &extension2_observer); | |
264 } | |
265 | |
183 TEST_F(PolicyServiceTest, Priorities) { | 266 TEST_F(PolicyServiceTest, Priorities) { |
184 PolicyMap expected; | 267 PolicyMap expected; |
185 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 268 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
186 base::Value::CreateIntegerValue(13)); | 269 base::Value::CreateIntegerValue(13)); |
187 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 270 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
188 base::Value::CreateIntegerValue(0)); | 271 base::Value::CreateIntegerValue(0)); |
189 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 272 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
190 base::Value::CreateIntegerValue(0)); | 273 base::Value::CreateIntegerValue(0)); |
191 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 274 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
192 base::Value::CreateIntegerValue(1)); | 275 base::Value::CreateIntegerValue(1)); |
193 policy2_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 276 policy2_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
194 base::Value::CreateIntegerValue(2)); | 277 base::Value::CreateIntegerValue(2)); |
195 provider0_.NotifyPolicyUpdated(); | 278 provider0_.UpdateChromePolicy(policy0_); |
196 provider1_.NotifyPolicyUpdated(); | 279 provider1_.UpdateChromePolicy(policy1_); |
197 provider2_.NotifyPolicyUpdated(); | 280 provider2_.UpdateChromePolicy(policy2_); |
198 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 281 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
199 | 282 |
200 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 283 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
201 base::Value::CreateIntegerValue(1)); | 284 base::Value::CreateIntegerValue(1)); |
202 policy0_.Erase("aaa"); | 285 policy0_.Erase("aaa"); |
203 provider0_.NotifyPolicyUpdated(); | 286 provider0_.UpdateChromePolicy(policy0_); |
204 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 287 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
205 | 288 |
206 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 289 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
207 base::Value::CreateIntegerValue(2)); | 290 base::Value::CreateIntegerValue(2)); |
208 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 291 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
209 base::Value::CreateIntegerValue(1)); | 292 base::Value::CreateIntegerValue(1)); |
210 provider1_.NotifyPolicyUpdated(); | 293 provider1_.UpdateChromePolicy(policy1_); |
211 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 294 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
212 } | 295 } |
213 | 296 |
214 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { | 297 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { |
215 scoped_ptr<PolicyChangeRegistrar> registrar( | 298 scoped_ptr<PolicyChangeRegistrar> registrar( |
216 new PolicyChangeRegistrar( | 299 new PolicyChangeRegistrar( |
217 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); | 300 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); |
218 | 301 |
219 // Starting to observe existing policies doesn't trigger a notification. | 302 // Starting to observe existing policies doesn't trigger a notification. |
220 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 303 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
221 registrar->Observe("pre", base::Bind( | 304 registrar->Observe("pre", base::Bind( |
222 &PolicyServiceTest::OnPolicyValueUpdated, | 305 &PolicyServiceTest::OnPolicyValueUpdated, |
223 base::Unretained(this))); | 306 base::Unretained(this))); |
224 registrar->Observe("aaa", base::Bind( | 307 registrar->Observe("aaa", base::Bind( |
225 &PolicyServiceTest::OnPolicyValueUpdated, | 308 &PolicyServiceTest::OnPolicyValueUpdated, |
226 base::Unretained(this))); | 309 base::Unretained(this))); |
227 Mock::VerifyAndClearExpectations(this); | 310 Mock::VerifyAndClearExpectations(this); |
228 | 311 |
229 // Changing it now triggers a notification. | 312 // Changing it now triggers a notification. |
230 base::FundamentalValue kValue0(0); | 313 base::FundamentalValue kValue0(0); |
231 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); | 314 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); |
232 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 315 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
233 kValue0.DeepCopy()); | 316 kValue0.DeepCopy()); |
234 provider0_.NotifyPolicyUpdated(); | 317 provider0_.UpdateChromePolicy(policy0_); |
235 Mock::VerifyAndClearExpectations(this); | 318 Mock::VerifyAndClearExpectations(this); |
236 | 319 |
237 // Changing other values doesn't trigger a notification. | 320 // Changing other values doesn't trigger a notification. |
238 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 321 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
239 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 322 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
240 kValue0.DeepCopy()); | 323 kValue0.DeepCopy()); |
241 provider0_.NotifyPolicyUpdated(); | 324 provider0_.UpdateChromePolicy(policy0_); |
242 Mock::VerifyAndClearExpectations(this); | 325 Mock::VerifyAndClearExpectations(this); |
243 | 326 |
244 // Modifying the value triggers a notification. | 327 // Modifying the value triggers a notification. |
245 base::FundamentalValue kValue1(1); | 328 base::FundamentalValue kValue1(1); |
246 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), | 329 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), |
247 ValueEquals(&kValue1))); | 330 ValueEquals(&kValue1))); |
248 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 331 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
249 kValue1.DeepCopy()); | 332 kValue1.DeepCopy()); |
250 provider0_.NotifyPolicyUpdated(); | 333 provider0_.UpdateChromePolicy(policy0_); |
251 Mock::VerifyAndClearExpectations(this); | 334 Mock::VerifyAndClearExpectations(this); |
252 | 335 |
253 // Removing the value triggers a notification. | 336 // Removing the value triggers a notification. |
254 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); | 337 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); |
255 policy0_.Erase("aaa"); | 338 policy0_.Erase("aaa"); |
256 provider0_.NotifyPolicyUpdated(); | 339 provider0_.UpdateChromePolicy(policy0_); |
257 Mock::VerifyAndClearExpectations(this); | 340 Mock::VerifyAndClearExpectations(this); |
258 | 341 |
259 // No more notifications after destroying the registrar. | 342 // No more notifications after destroying the registrar. |
260 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); | 343 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); |
261 registrar.reset(); | 344 registrar.reset(); |
262 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 345 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
263 kValue1.DeepCopy()); | 346 kValue1.DeepCopy()); |
264 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 347 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
265 kValue1.DeepCopy()); | 348 kValue1.DeepCopy()); |
266 provider0_.NotifyPolicyUpdated(); | 349 provider0_.UpdateChromePolicy(policy0_); |
267 Mock::VerifyAndClearExpectations(this); | 350 Mock::VerifyAndClearExpectations(this); |
268 } | 351 } |
269 | 352 |
270 TEST_F(PolicyServiceTest, RefreshPolicies) { | 353 TEST_F(PolicyServiceTest, RefreshPolicies) { |
271 MessageLoop loop; | 354 MessageLoop loop; |
272 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); | 355 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); |
273 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); | 356 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); |
274 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop); | 357 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop); |
275 | 358 |
276 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); | 359 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); |
277 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); | 360 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); |
278 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); | 361 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); |
279 | 362 |
280 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 363 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
281 policy_service_->RefreshPolicies(base::Bind( | 364 policy_service_->RefreshPolicies(base::Bind( |
282 &PolicyServiceTest::OnPolicyRefresh, | 365 &PolicyServiceTest::OnPolicyRefresh, |
283 base::Unretained(this))); | 366 base::Unretained(this))); |
284 loop.RunAllPending(); | 367 loop.RunAllPending(); |
285 Mock::VerifyAndClearExpectations(this); | 368 Mock::VerifyAndClearExpectations(this); |
286 | 369 |
287 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 370 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
288 base::FundamentalValue kValue0(0); | 371 base::FundamentalValue kValue0(0); |
289 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 372 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
290 kValue0.DeepCopy()); | 373 kValue0.DeepCopy()); |
291 provider0_.NotifyPolicyUpdated(); | 374 provider0_.UpdateChromePolicy(policy0_); |
292 loop.RunAllPending(); | 375 loop.RunAllPending(); |
293 Mock::VerifyAndClearExpectations(this); | 376 Mock::VerifyAndClearExpectations(this); |
294 | 377 |
295 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 378 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
296 base::FundamentalValue kValue1(1); | 379 base::FundamentalValue kValue1(1); |
297 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 380 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
298 kValue1.DeepCopy()); | 381 kValue1.DeepCopy()); |
299 provider1_.NotifyPolicyUpdated(); | 382 provider1_.UpdateChromePolicy(policy1_); |
300 loop.RunAllPending(); | 383 loop.RunAllPending(); |
301 Mock::VerifyAndClearExpectations(this); | 384 Mock::VerifyAndClearExpectations(this); |
302 | 385 |
303 // A provider can refresh more than once after a RefreshPolicies call, but | 386 // A provider can refresh more than once after a RefreshPolicies call, but |
304 // OnPolicyRefresh should be triggered only after all providers are | 387 // OnPolicyRefresh should be triggered only after all providers are |
305 // refreshed. | 388 // refreshed. |
306 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 389 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
307 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 390 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
308 kValue1.DeepCopy()); | 391 kValue1.DeepCopy()); |
309 provider1_.NotifyPolicyUpdated(); | 392 provider1_.UpdateChromePolicy(policy1_); |
310 loop.RunAllPending(); | 393 loop.RunAllPending(); |
311 Mock::VerifyAndClearExpectations(this); | 394 Mock::VerifyAndClearExpectations(this); |
312 | 395 |
313 // If another RefreshPolicies() call happens while waiting for a previous | 396 // If another RefreshPolicies() call happens while waiting for a previous |
314 // one to complete, then all providers must refresh again. | 397 // one to complete, then all providers must refresh again. |
315 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 398 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
316 policy_service_->RefreshPolicies(base::Bind( | 399 policy_service_->RefreshPolicies(base::Bind( |
317 &PolicyServiceTest::OnPolicyRefresh, | 400 &PolicyServiceTest::OnPolicyRefresh, |
318 base::Unretained(this))); | 401 base::Unretained(this))); |
319 loop.RunAllPending(); | 402 loop.RunAllPending(); |
320 Mock::VerifyAndClearExpectations(this); | 403 Mock::VerifyAndClearExpectations(this); |
321 | 404 |
322 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); | 405 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); |
323 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 406 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
324 kValue0.DeepCopy()); | 407 kValue0.DeepCopy()); |
325 provider2_.NotifyPolicyUpdated(); | 408 provider2_.UpdateChromePolicy(policy2_); |
326 loop.RunAllPending(); | 409 loop.RunAllPending(); |
327 Mock::VerifyAndClearExpectations(this); | 410 Mock::VerifyAndClearExpectations(this); |
328 | 411 |
329 // Providers 0 and 1 must reload again. | 412 // Providers 0 and 1 must reload again. |
330 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); | 413 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); |
331 base::FundamentalValue kValue2(2); | 414 base::FundamentalValue kValue2(2); |
332 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 415 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
333 kValue2.DeepCopy()); | 416 kValue2.DeepCopy()); |
334 provider0_.NotifyPolicyUpdated(); | 417 provider0_.UpdateChromePolicy(policy0_); |
335 provider1_.NotifyPolicyUpdated(); | 418 provider1_.UpdateChromePolicy(policy1_); |
336 loop.RunAllPending(); | 419 loop.RunAllPending(); |
337 Mock::VerifyAndClearExpectations(this); | 420 Mock::VerifyAndClearExpectations(this); |
338 | 421 |
339 const PolicyMap* policies = policy_service_->GetPolicies( | 422 const PolicyMap& policies = policy_service_->GetPolicies( |
340 POLICY_DOMAIN_CHROME, ""); | 423 POLICY_DOMAIN_CHROME, ""); |
341 ASSERT_TRUE(policies); | 424 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa"))); |
342 EXPECT_TRUE(base::Value::Equals(&kValue2, policies->GetValue("aaa"))); | 425 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb"))); |
343 EXPECT_TRUE(base::Value::Equals(&kValue0, policies->GetValue("bbb"))); | 426 } |
427 | |
428 TEST_F(PolicyServiceTest, NamespaceMerge) { | |
429 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle()); | |
430 scoped_ptr<PolicyBundle> bundle1(new PolicyBundle()); | |
431 scoped_ptr<PolicyBundle> bundle2(new PolicyBundle()); | |
432 | |
433 AddTestPolicies( | |
434 bundle0.get(), "bundle0", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | |
Mattias Nissler (ping if slow)
2012/05/15 09:40:33
break line after second parameter?
Joao da Silva
2012/05/15 13:06:31
Done.
| |
435 AddTestPolicies( | |
436 bundle1.get(), "bundle1", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | |
437 AddTestPolicies( | |
438 bundle2.get(), "bundle2", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE); | |
439 | |
440 provider0_.UpdatePolicy(bundle0.Pass()); | |
441 provider1_.UpdatePolicy(bundle1.Pass()); | |
442 provider2_.UpdatePolicy(bundle2.Pass()); | |
443 | |
444 PolicyMap expected; | |
445 // For policies of the same level and scope, the first provider takes | |
446 // precedence, on every namespace. | |
447 expected.Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
448 base::Value::CreateStringValue("bundle0")); | |
449 // For policies with different levels and scopes, the highest priority | |
450 // level/scope combination takes precedence, on every namespace. | |
451 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | |
452 base::Value::CreateStringValue("bundle2")); | |
453 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, "") | |
454 .Equals(expected)); | |
455 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_EXTENSIONS, kExtension) | |
456 .Equals(expected)); | |
344 } | 457 } |
345 | 458 |
346 } // namespace policy | 459 } // namespace policy |
OLD | NEW |