Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1533)

Side by Side Diff: chrome/browser/policy/policy_service_impl_unittest.cc

Issue 11667006: Create a list of policy changes before notifying observers (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Another ToT merge Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/policy/policy_service_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/run_loop.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 13 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
13 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 using ::testing::AnyNumber; 19 using ::testing::AnyNumber;
19 using ::testing::Mock; 20 using ::testing::Mock;
20 using ::testing::Return; 21 using ::testing::Return;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 base::Value::CreateStringValue(value)); 60 base::Value::CreateStringValue(value));
60 policy_map->Set(kDiffLevelPolicy, level, scope, 61 policy_map->Set(kDiffLevelPolicy, level, scope,
61 base::Value::CreateStringValue(value)); 62 base::Value::CreateStringValue(value));
62 policy_map = &bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension); 63 policy_map = &bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension);
63 policy_map->Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY, 64 policy_map->Set(kSameLevelPolicy, POLICY_LEVEL_MANDATORY,
64 POLICY_SCOPE_USER, base::Value::CreateStringValue(value)); 65 POLICY_SCOPE_USER, base::Value::CreateStringValue(value));
65 policy_map->Set(kDiffLevelPolicy, level, scope, 66 policy_map->Set(kDiffLevelPolicy, level, scope,
66 base::Value::CreateStringValue(value)); 67 base::Value::CreateStringValue(value));
67 } 68 }
68 69
70 // Observer class that changes the policy in the passed provider when the
71 // callback is invoked.
72 class ChangePolicyObserver : public PolicyService::Observer {
73 public:
74 explicit ChangePolicyObserver(MockConfigurationPolicyProvider* provider)
75 : provider_(provider),
76 observer_invoked_(false) {}
77
78 virtual void OnPolicyUpdated(PolicyDomain domain,
79 const std::string& ns,
80 const PolicyMap& previous,
81 const PolicyMap& current) OVERRIDE {
82 PolicyMap new_policy;
83 new_policy.Set("foo", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
84 base::Value::CreateIntegerValue(14));
85 provider_->UpdateChromePolicy(new_policy);
86 observer_invoked_ = true;
87 }
88
89 bool observer_invoked() const { return observer_invoked_; }
90
91 private:
92 MockConfigurationPolicyProvider* provider_;
93 bool observer_invoked_;
94 };
95
69 } // namespace 96 } // namespace
70 97
71 class PolicyServiceTest : public testing::Test { 98 class PolicyServiceTest : public testing::Test {
72 public: 99 public:
73 PolicyServiceTest() {} 100 PolicyServiceTest() {}
74
75 virtual void SetUp() OVERRIDE { 101 virtual void SetUp() OVERRIDE {
76 EXPECT_CALL(provider0_, IsInitializationComplete()) 102 EXPECT_CALL(provider0_, IsInitializationComplete())
77 .WillRepeatedly(Return(true)); 103 .WillRepeatedly(Return(true));
78 EXPECT_CALL(provider1_, IsInitializationComplete()) 104 EXPECT_CALL(provider1_, IsInitializationComplete())
79 .WillRepeatedly(Return(true)); 105 .WillRepeatedly(Return(true));
80 EXPECT_CALL(provider2_, IsInitializationComplete()) 106 EXPECT_CALL(provider2_, IsInitializationComplete())
81 .WillRepeatedly(Return(true)); 107 .WillRepeatedly(Return(true));
82 108
83 provider0_.Init(); 109 provider0_.Init();
84 provider1_.Init(); 110 provider1_.Init();
(...skipping 21 matching lines...) Expand all
106 132
107 MOCK_METHOD0(OnPolicyRefresh, void()); 133 MOCK_METHOD0(OnPolicyRefresh, void());
108 134
109 // Returns true if the policies for |domain|, |component_id| match |expected|. 135 // Returns true if the policies for |domain|, |component_id| match |expected|.
110 bool VerifyPolicies(PolicyDomain domain, 136 bool VerifyPolicies(PolicyDomain domain,
111 const std::string& component_id, 137 const std::string& component_id,
112 const PolicyMap& expected) { 138 const PolicyMap& expected) {
113 return policy_service_->GetPolicies(domain, component_id).Equals(expected); 139 return policy_service_->GetPolicies(domain, component_id).Equals(expected);
114 } 140 }
115 141
142 void UpdateProviderPolicy(const PolicyMap& policy) {
143 provider0_.UpdateChromePolicy(policy);
144 RunUntilIdle();
145 }
146
147 void RunUntilIdle() {
148 base::RunLoop loop;
149 loop.RunUntilIdle();
150 }
151
116 protected: 152 protected:
117 MockConfigurationPolicyProvider provider0_; 153 MockConfigurationPolicyProvider provider0_;
118 MockConfigurationPolicyProvider provider1_; 154 MockConfigurationPolicyProvider provider1_;
119 MockConfigurationPolicyProvider provider2_; 155 MockConfigurationPolicyProvider provider2_;
120 PolicyMap policy0_; 156 PolicyMap policy0_;
121 PolicyMap policy1_; 157 PolicyMap policy1_;
122 PolicyMap policy2_; 158 PolicyMap policy2_;
123 scoped_ptr<PolicyServiceImpl> policy_service_; 159 scoped_ptr<PolicyServiceImpl> policy_service_;
160 MessageLoop loop_;
124 161
125 private: 162 private:
126 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest); 163 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest);
127 }; 164 };
128 165
129 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) { 166 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) {
130 PolicyMap expected; 167 PolicyMap expected;
131 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 168 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
132 base::Value::CreateIntegerValue(13)); 169 base::Value::CreateIntegerValue(13));
133 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); 170 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected));
134 } 171 }
135 172
136 TEST_F(PolicyServiceTest, NotifyObservers) { 173 TEST_F(PolicyServiceTest, NotifyObservers) {
137 MockPolicyServiceObserver observer; 174 MockPolicyServiceObserver observer;
138 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer); 175 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
139 176
140 PolicyMap expectedPrevious; 177 PolicyMap expectedPrevious;
141 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 178 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
142 base::Value::CreateIntegerValue(13)); 179 base::Value::CreateIntegerValue(13));
143 180
144 PolicyMap expectedCurrent; 181 PolicyMap expectedCurrent;
145 expectedCurrent.CopyFrom(expectedPrevious); 182 expectedCurrent.CopyFrom(expectedPrevious);
146 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 183 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
147 base::Value::CreateIntegerValue(123)); 184 base::Value::CreateIntegerValue(123));
148 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 185 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
149 base::Value::CreateIntegerValue(123)); 186 base::Value::CreateIntegerValue(123));
150 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 187 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
151 PolicyEquals(&expectedPrevious), 188 PolicyEquals(&expectedPrevious),
152 PolicyEquals(&expectedCurrent))); 189 PolicyEquals(&expectedCurrent)));
153 provider0_.UpdateChromePolicy(policy0_); 190 UpdateProviderPolicy(policy0_);
154 Mock::VerifyAndClearExpectations(&observer); 191 Mock::VerifyAndClearExpectations(&observer);
155 192
156 // No changes. 193 // No changes.
157 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); 194 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0);
158 provider0_.UpdateChromePolicy(policy0_); 195 UpdateProviderPolicy(policy0_);
159 Mock::VerifyAndClearExpectations(&observer); 196 Mock::VerifyAndClearExpectations(&observer);
160 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); 197 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent));
161 198
162 // New policy. 199 // New policy.
163 expectedPrevious.CopyFrom(expectedCurrent); 200 expectedPrevious.CopyFrom(expectedCurrent);
164 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 201 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
165 base::Value::CreateIntegerValue(456)); 202 base::Value::CreateIntegerValue(456));
166 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 203 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
167 base::Value::CreateIntegerValue(456)); 204 base::Value::CreateIntegerValue(456));
168 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 205 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
169 PolicyEquals(&expectedPrevious), 206 PolicyEquals(&expectedPrevious),
170 PolicyEquals(&expectedCurrent))); 207 PolicyEquals(&expectedCurrent)));
171 provider0_.UpdateChromePolicy(policy0_); 208 UpdateProviderPolicy(policy0_);
172 Mock::VerifyAndClearExpectations(&observer); 209 Mock::VerifyAndClearExpectations(&observer);
173 210
174 // Removed policy. 211 // Removed policy.
175 expectedPrevious.CopyFrom(expectedCurrent); 212 expectedPrevious.CopyFrom(expectedCurrent);
176 expectedCurrent.Erase("bbb"); 213 expectedCurrent.Erase("bbb");
177 policy0_.Erase("bbb"); 214 policy0_.Erase("bbb");
178 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 215 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
179 PolicyEquals(&expectedPrevious), 216 PolicyEquals(&expectedPrevious),
180 PolicyEquals(&expectedCurrent))); 217 PolicyEquals(&expectedCurrent)));
181 provider0_.UpdateChromePolicy(policy0_); 218 UpdateProviderPolicy(policy0_);
182 Mock::VerifyAndClearExpectations(&observer); 219 Mock::VerifyAndClearExpectations(&observer);
183 220
184 // Changed policy. 221 // Changed policy.
185 expectedPrevious.CopyFrom(expectedCurrent); 222 expectedPrevious.CopyFrom(expectedCurrent);
186 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 223 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
187 base::Value::CreateIntegerValue(789)); 224 base::Value::CreateIntegerValue(789));
188 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 225 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
189 base::Value::CreateIntegerValue(789)); 226 base::Value::CreateIntegerValue(789));
190 227
191 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 228 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
192 PolicyEquals(&expectedPrevious), 229 PolicyEquals(&expectedPrevious),
193 PolicyEquals(&expectedCurrent))); 230 PolicyEquals(&expectedCurrent)));
194 provider0_.UpdateChromePolicy(policy0_); 231 UpdateProviderPolicy(policy0_);
195 Mock::VerifyAndClearExpectations(&observer); 232 Mock::VerifyAndClearExpectations(&observer);
196 233
197 // No changes again. 234 // No changes again.
198 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); 235 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0);
199 provider0_.UpdateChromePolicy(policy0_); 236 UpdateProviderPolicy(policy0_);
200 Mock::VerifyAndClearExpectations(&observer); 237 Mock::VerifyAndClearExpectations(&observer);
201 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); 238 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent));
202 239
203 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer); 240 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
204 } 241 }
205 242
206 TEST_F(PolicyServiceTest, NotifyObserversInMultipleNamespaces) { 243 TEST_F(PolicyServiceTest, NotifyObserversInMultipleNamespaces) {
207 const std::string kExtension0("extension-0"); 244 const std::string kExtension0("extension-0");
208 const std::string kExtension1("extension-1"); 245 const std::string kExtension1("extension-1");
209 const std::string kExtension2("extension-2"); 246 const std::string kExtension2("extension-2");
(...skipping 23 matching lines...) Expand all
233 PolicyEquals(&policy_map))); 270 PolicyEquals(&policy_map)));
234 EXPECT_CALL(extension_observer, 271 EXPECT_CALL(extension_observer,
235 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0, 272 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0,
236 PolicyEquals(&kEmptyPolicyMap), 273 PolicyEquals(&kEmptyPolicyMap),
237 PolicyEquals(&policy_map))); 274 PolicyEquals(&policy_map)));
238 EXPECT_CALL(extension_observer, 275 EXPECT_CALL(extension_observer,
239 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1, 276 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1,
240 PolicyEquals(&kEmptyPolicyMap), 277 PolicyEquals(&kEmptyPolicyMap),
241 PolicyEquals(&policy_map))); 278 PolicyEquals(&policy_map)));
242 provider0_.UpdatePolicy(bundle.Pass()); 279 provider0_.UpdatePolicy(bundle.Pass());
280 RunUntilIdle();
243 Mock::VerifyAndClearExpectations(&chrome_observer); 281 Mock::VerifyAndClearExpectations(&chrome_observer);
244 Mock::VerifyAndClearExpectations(&extension_observer); 282 Mock::VerifyAndClearExpectations(&extension_observer);
245 283
246 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes, 284 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes,
247 // and kExtension2 is new. 285 // and kExtension2 is new.
248 previous_policy_map.CopyFrom(policy_map); 286 previous_policy_map.CopyFrom(policy_map);
249 bundle.reset(new PolicyBundle()); 287 bundle.reset(new PolicyBundle());
250 bundle->Get(POLICY_DOMAIN_CHROME, "").CopyFrom(policy_map); 288 bundle->Get(POLICY_DOMAIN_CHROME, "").CopyFrom(policy_map);
251 policy_map.Set("policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 289 policy_map.Set("policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
252 base::Value::CreateStringValue("another value")); 290 base::Value::CreateStringValue("another value"));
253 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension1).CopyFrom(policy_map); 291 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension1).CopyFrom(policy_map);
254 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension2).CopyFrom(policy_map); 292 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension2).CopyFrom(policy_map);
255 293
256 EXPECT_CALL(chrome_observer, OnPolicyUpdated(_, _, _, _)).Times(0); 294 EXPECT_CALL(chrome_observer, OnPolicyUpdated(_, _, _, _)).Times(0);
257 EXPECT_CALL(extension_observer, 295 EXPECT_CALL(extension_observer,
258 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0, 296 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0,
259 PolicyEquals(&previous_policy_map), 297 PolicyEquals(&previous_policy_map),
260 PolicyEquals(&kEmptyPolicyMap))); 298 PolicyEquals(&kEmptyPolicyMap)));
261 EXPECT_CALL(extension_observer, 299 EXPECT_CALL(extension_observer,
262 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1, 300 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1,
263 PolicyEquals(&previous_policy_map), 301 PolicyEquals(&previous_policy_map),
264 PolicyEquals(&policy_map))); 302 PolicyEquals(&policy_map)));
265 EXPECT_CALL(extension_observer, 303 EXPECT_CALL(extension_observer,
266 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension2, 304 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension2,
267 PolicyEquals(&kEmptyPolicyMap), 305 PolicyEquals(&kEmptyPolicyMap),
268 PolicyEquals(&policy_map))); 306 PolicyEquals(&policy_map)));
269 provider0_.UpdatePolicy(bundle.Pass()); 307 provider0_.UpdatePolicy(bundle.Pass());
308 RunUntilIdle();
270 Mock::VerifyAndClearExpectations(&chrome_observer); 309 Mock::VerifyAndClearExpectations(&chrome_observer);
271 Mock::VerifyAndClearExpectations(&extension_observer); 310 Mock::VerifyAndClearExpectations(&extension_observer);
272 311
273 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &chrome_observer); 312 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &chrome_observer);
274 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS, 313 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS,
275 &extension_observer); 314 &extension_observer);
276 } 315 }
277 316
317 TEST_F(PolicyServiceTest, ObserverChangesPolicy) {
318 ChangePolicyObserver observer(&provider0_);
319 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
320 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
321 base::Value::CreateIntegerValue(123));
322 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
323 base::Value::CreateIntegerValue(1234));
324 // Should not crash.
325 UpdateProviderPolicy(policy0_);
326 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
327 EXPECT_TRUE(observer.observer_invoked());
328 }
329
278 TEST_F(PolicyServiceTest, Priorities) { 330 TEST_F(PolicyServiceTest, Priorities) {
279 PolicyMap expected; 331 PolicyMap expected;
280 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 332 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
281 base::Value::CreateIntegerValue(13)); 333 base::Value::CreateIntegerValue(13));
282 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 334 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
283 base::Value::CreateIntegerValue(0)); 335 base::Value::CreateIntegerValue(0));
284 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 336 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
285 base::Value::CreateIntegerValue(0)); 337 base::Value::CreateIntegerValue(0));
286 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 338 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
287 base::Value::CreateIntegerValue(1)); 339 base::Value::CreateIntegerValue(1));
(...skipping 24 matching lines...) Expand all
312 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); 364 policy_service_.get(), POLICY_DOMAIN_CHROME, ""));
313 365
314 // Starting to observe existing policies doesn't trigger a notification. 366 // Starting to observe existing policies doesn't trigger a notification.
315 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); 367 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
316 registrar->Observe("pre", base::Bind( 368 registrar->Observe("pre", base::Bind(
317 &PolicyServiceTest::OnPolicyValueUpdated, 369 &PolicyServiceTest::OnPolicyValueUpdated,
318 base::Unretained(this))); 370 base::Unretained(this)));
319 registrar->Observe("aaa", base::Bind( 371 registrar->Observe("aaa", base::Bind(
320 &PolicyServiceTest::OnPolicyValueUpdated, 372 &PolicyServiceTest::OnPolicyValueUpdated,
321 base::Unretained(this))); 373 base::Unretained(this)));
374 RunUntilIdle();
322 Mock::VerifyAndClearExpectations(this); 375 Mock::VerifyAndClearExpectations(this);
323 376
324 // Changing it now triggers a notification. 377 // Changing it now triggers a notification.
325 base::FundamentalValue kValue0(0); 378 base::FundamentalValue kValue0(0);
326 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); 379 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0)));
327 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 380 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
328 kValue0.DeepCopy()); 381 kValue0.DeepCopy());
329 provider0_.UpdateChromePolicy(policy0_); 382 UpdateProviderPolicy(policy0_);
330 Mock::VerifyAndClearExpectations(this); 383 Mock::VerifyAndClearExpectations(this);
331 384
332 // Changing other values doesn't trigger a notification. 385 // Changing other values doesn't trigger a notification.
333 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); 386 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
334 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 387 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
335 kValue0.DeepCopy()); 388 kValue0.DeepCopy());
336 provider0_.UpdateChromePolicy(policy0_); 389 UpdateProviderPolicy(policy0_);
337 Mock::VerifyAndClearExpectations(this); 390 Mock::VerifyAndClearExpectations(this);
338 391
339 // Modifying the value triggers a notification. 392 // Modifying the value triggers a notification.
340 base::FundamentalValue kValue1(1); 393 base::FundamentalValue kValue1(1);
341 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), 394 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0),
342 ValueEquals(&kValue1))); 395 ValueEquals(&kValue1)));
343 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 396 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
344 kValue1.DeepCopy()); 397 kValue1.DeepCopy());
345 provider0_.UpdateChromePolicy(policy0_); 398 UpdateProviderPolicy(policy0_);
346 Mock::VerifyAndClearExpectations(this); 399 Mock::VerifyAndClearExpectations(this);
347 400
348 // Removing the value triggers a notification. 401 // Removing the value triggers a notification.
349 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); 402 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL));
350 policy0_.Erase("aaa"); 403 policy0_.Erase("aaa");
351 provider0_.UpdateChromePolicy(policy0_); 404 UpdateProviderPolicy(policy0_);
352 Mock::VerifyAndClearExpectations(this); 405 Mock::VerifyAndClearExpectations(this);
353 406
354 // No more notifications after destroying the registrar. 407 // No more notifications after destroying the registrar.
355 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); 408 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
356 registrar.reset(); 409 registrar.reset();
357 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 410 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
358 kValue1.DeepCopy()); 411 kValue1.DeepCopy());
359 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 412 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
360 kValue1.DeepCopy()); 413 kValue1.DeepCopy());
361 provider0_.UpdateChromePolicy(policy0_); 414 UpdateProviderPolicy(policy0_);
362 Mock::VerifyAndClearExpectations(this); 415 Mock::VerifyAndClearExpectations(this);
363 } 416 }
364 417
365 TEST_F(PolicyServiceTest, RefreshPolicies) { 418 TEST_F(PolicyServiceTest, RefreshPolicies) {
366 MessageLoop loop; 419 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop_);
367 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); 420 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop_);
368 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); 421 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop_);
369 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop);
370 422
371 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); 423 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber());
372 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); 424 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber());
373 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); 425 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber());
374 426
375 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 427 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
376 policy_service_->RefreshPolicies(base::Bind( 428 policy_service_->RefreshPolicies(base::Bind(
377 &PolicyServiceTest::OnPolicyRefresh, 429 &PolicyServiceTest::OnPolicyRefresh,
378 base::Unretained(this))); 430 base::Unretained(this)));
379 loop.RunUntilIdle(); 431 // Let any queued observer tasks run.
432 RunUntilIdle();
380 Mock::VerifyAndClearExpectations(this); 433 Mock::VerifyAndClearExpectations(this);
381 434
382 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 435 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
383 base::FundamentalValue kValue0(0); 436 base::FundamentalValue kValue0(0);
384 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 437 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
385 kValue0.DeepCopy()); 438 kValue0.DeepCopy());
386 provider0_.UpdateChromePolicy(policy0_); 439 UpdateProviderPolicy(policy0_);
387 loop.RunUntilIdle();
388 Mock::VerifyAndClearExpectations(this); 440 Mock::VerifyAndClearExpectations(this);
389 441
390 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 442 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
391 base::FundamentalValue kValue1(1); 443 base::FundamentalValue kValue1(1);
392 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 444 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
393 kValue1.DeepCopy()); 445 kValue1.DeepCopy());
394 provider1_.UpdateChromePolicy(policy1_); 446 provider1_.UpdateChromePolicy(policy1_);
395 loop.RunUntilIdle(); 447 RunUntilIdle();
396 Mock::VerifyAndClearExpectations(this); 448 Mock::VerifyAndClearExpectations(this);
397 449
398 // A provider can refresh more than once after a RefreshPolicies call, but 450 // A provider can refresh more than once after a RefreshPolicies call, but
399 // OnPolicyRefresh should be triggered only after all providers are 451 // OnPolicyRefresh should be triggered only after all providers are
400 // refreshed. 452 // refreshed.
401 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 453 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
402 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 454 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
403 kValue1.DeepCopy()); 455 kValue1.DeepCopy());
404 provider1_.UpdateChromePolicy(policy1_); 456 provider1_.UpdateChromePolicy(policy1_);
405 loop.RunUntilIdle(); 457 RunUntilIdle();
406 Mock::VerifyAndClearExpectations(this); 458 Mock::VerifyAndClearExpectations(this);
407 459
408 // If another RefreshPolicies() call happens while waiting for a previous 460 // If another RefreshPolicies() call happens while waiting for a previous
409 // one to complete, then all providers must refresh again. 461 // one to complete, then all providers must refresh again.
410 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 462 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
411 policy_service_->RefreshPolicies(base::Bind( 463 policy_service_->RefreshPolicies(base::Bind(
412 &PolicyServiceTest::OnPolicyRefresh, 464 &PolicyServiceTest::OnPolicyRefresh,
413 base::Unretained(this))); 465 base::Unretained(this)));
414 loop.RunUntilIdle(); 466 RunUntilIdle();
415 Mock::VerifyAndClearExpectations(this); 467 Mock::VerifyAndClearExpectations(this);
416 468
417 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 469 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
418 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 470 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
419 kValue0.DeepCopy()); 471 kValue0.DeepCopy());
420 provider2_.UpdateChromePolicy(policy2_); 472 provider2_.UpdateChromePolicy(policy2_);
421 loop.RunUntilIdle(); 473 RunUntilIdle();
422 Mock::VerifyAndClearExpectations(this); 474 Mock::VerifyAndClearExpectations(this);
423 475
424 // Providers 0 and 1 must reload again. 476 // Providers 0 and 1 must reload again.
425 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); 477 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2);
426 base::FundamentalValue kValue2(2); 478 base::FundamentalValue kValue2(2);
427 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 479 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
428 kValue2.DeepCopy()); 480 kValue2.DeepCopy());
429 provider0_.UpdateChromePolicy(policy0_); 481 provider0_.UpdateChromePolicy(policy0_);
430 provider1_.UpdateChromePolicy(policy1_); 482 provider1_.UpdateChromePolicy(policy1_);
431 loop.RunUntilIdle(); 483 RunUntilIdle();
432 Mock::VerifyAndClearExpectations(this); 484 Mock::VerifyAndClearExpectations(this);
433 485
434 const PolicyMap& policies = policy_service_->GetPolicies( 486 const PolicyMap& policies = policy_service_->GetPolicies(
435 POLICY_DOMAIN_CHROME, ""); 487 POLICY_DOMAIN_CHROME, "");
436 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa"))); 488 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa")));
437 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb"))); 489 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb")));
438 } 490 }
439 491
440 TEST_F(PolicyServiceTest, NamespaceMerge) { 492 TEST_F(PolicyServiceTest, NamespaceMerge) {
441 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle()); 493 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle());
(...skipping 20 matching lines...) Expand all
462 // level/scope combination takes precedence, on every namespace. 514 // level/scope combination takes precedence, on every namespace.
463 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 515 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
464 base::Value::CreateStringValue("bundle2")); 516 base::Value::CreateStringValue("bundle2"));
465 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, "") 517 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, "")
466 .Equals(expected)); 518 .Equals(expected));
467 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_EXTENSIONS, kExtension) 519 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_EXTENSIONS, kExtension)
468 .Equals(expected)); 520 .Equals(expected));
469 } 521 }
470 522
471 } // namespace policy 523 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/policy_service_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698