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

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: Rebase to ToT 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
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() : loop_(MessageLoop::TYPE_UI) {}
74 101
75 virtual void SetUp() OVERRIDE { 102 virtual void SetUp() OVERRIDE {
76 EXPECT_CALL(provider0_, IsInitializationComplete()) 103 EXPECT_CALL(provider0_, IsInitializationComplete())
77 .WillRepeatedly(Return(true)); 104 .WillRepeatedly(Return(true));
78 EXPECT_CALL(provider1_, IsInitializationComplete()) 105 EXPECT_CALL(provider1_, IsInitializationComplete())
79 .WillRepeatedly(Return(true)); 106 .WillRepeatedly(Return(true));
80 EXPECT_CALL(provider2_, IsInitializationComplete()) 107 EXPECT_CALL(provider2_, IsInitializationComplete())
81 .WillRepeatedly(Return(true)); 108 .WillRepeatedly(Return(true));
82 109
83 provider0_.Init(); 110 provider0_.Init();
(...skipping 22 matching lines...) Expand all
106 133
107 MOCK_METHOD0(OnPolicyRefresh, void()); 134 MOCK_METHOD0(OnPolicyRefresh, void());
108 135
109 // Returns true if the policies for |domain|, |component_id| match |expected|. 136 // Returns true if the policies for |domain|, |component_id| match |expected|.
110 bool VerifyPolicies(PolicyDomain domain, 137 bool VerifyPolicies(PolicyDomain domain,
111 const std::string& component_id, 138 const std::string& component_id,
112 const PolicyMap& expected) { 139 const PolicyMap& expected) {
113 return policy_service_->GetPolicies(domain, component_id).Equals(expected); 140 return policy_service_->GetPolicies(domain, component_id).Equals(expected);
114 } 141 }
115 142
143 void UpdateProviderPolicy(const PolicyMap& policy) {
144 provider0_.UpdateChromePolicy(policy);
145 RunUntilIdle();
146 }
147
148 void RunUntilIdle() {
149 base::RunLoop loop;
150 loop.RunUntilIdle();
151 }
152
116 protected: 153 protected:
117 MockConfigurationPolicyProvider provider0_; 154 MockConfigurationPolicyProvider provider0_;
118 MockConfigurationPolicyProvider provider1_; 155 MockConfigurationPolicyProvider provider1_;
119 MockConfigurationPolicyProvider provider2_; 156 MockConfigurationPolicyProvider provider2_;
120 PolicyMap policy0_; 157 PolicyMap policy0_;
121 PolicyMap policy1_; 158 PolicyMap policy1_;
122 PolicyMap policy2_; 159 PolicyMap policy2_;
123 scoped_ptr<PolicyServiceImpl> policy_service_; 160 scoped_ptr<PolicyServiceImpl> policy_service_;
161 MessageLoop loop_;
124 162
125 private: 163 private:
126 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest); 164 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest);
127 }; 165 };
128 166
129 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) { 167 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) {
130 PolicyMap expected; 168 PolicyMap expected;
131 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 169 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
132 base::Value::CreateIntegerValue(13)); 170 base::Value::CreateIntegerValue(13));
133 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); 171 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected));
134 } 172 }
135 173
136 TEST_F(PolicyServiceTest, NotifyObservers) { 174 TEST_F(PolicyServiceTest, NotifyObservers) {
137 MockPolicyServiceObserver observer; 175 MockPolicyServiceObserver observer;
138 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer); 176 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
139 177
140 PolicyMap expectedPrevious; 178 PolicyMap expectedPrevious;
141 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 179 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
142 base::Value::CreateIntegerValue(13)); 180 base::Value::CreateIntegerValue(13));
143 181
144 PolicyMap expectedCurrent; 182 PolicyMap expectedCurrent;
145 expectedCurrent.CopyFrom(expectedPrevious); 183 expectedCurrent.CopyFrom(expectedPrevious);
146 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 184 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
147 base::Value::CreateIntegerValue(123)); 185 base::Value::CreateIntegerValue(123));
148 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 186 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
149 base::Value::CreateIntegerValue(123)); 187 base::Value::CreateIntegerValue(123));
150 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 188 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
151 PolicyEquals(&expectedPrevious), 189 PolicyEquals(&expectedPrevious),
152 PolicyEquals(&expectedCurrent))); 190 PolicyEquals(&expectedCurrent)));
153 provider0_.UpdateChromePolicy(policy0_); 191 UpdateProviderPolicy(policy0_);
154 Mock::VerifyAndClearExpectations(&observer); 192 Mock::VerifyAndClearExpectations(&observer);
155 193
156 // No changes. 194 // No changes.
157 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); 195 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0);
158 provider0_.UpdateChromePolicy(policy0_); 196 UpdateProviderPolicy(policy0_);
159 Mock::VerifyAndClearExpectations(&observer); 197 Mock::VerifyAndClearExpectations(&observer);
160 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); 198 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent));
161 199
162 // New policy. 200 // New policy.
163 expectedPrevious.CopyFrom(expectedCurrent); 201 expectedPrevious.CopyFrom(expectedCurrent);
164 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 202 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
165 base::Value::CreateIntegerValue(456)); 203 base::Value::CreateIntegerValue(456));
166 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 204 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
167 base::Value::CreateIntegerValue(456)); 205 base::Value::CreateIntegerValue(456));
168 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 206 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
169 PolicyEquals(&expectedPrevious), 207 PolicyEquals(&expectedPrevious),
170 PolicyEquals(&expectedCurrent))); 208 PolicyEquals(&expectedCurrent)));
171 provider0_.UpdateChromePolicy(policy0_); 209 UpdateProviderPolicy(policy0_);
172 Mock::VerifyAndClearExpectations(&observer); 210 Mock::VerifyAndClearExpectations(&observer);
173 211
174 // Removed policy. 212 // Removed policy.
175 expectedPrevious.CopyFrom(expectedCurrent); 213 expectedPrevious.CopyFrom(expectedCurrent);
176 expectedCurrent.Erase("bbb"); 214 expectedCurrent.Erase("bbb");
177 policy0_.Erase("bbb"); 215 policy0_.Erase("bbb");
178 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 216 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
179 PolicyEquals(&expectedPrevious), 217 PolicyEquals(&expectedPrevious),
180 PolicyEquals(&expectedCurrent))); 218 PolicyEquals(&expectedCurrent)));
181 provider0_.UpdateChromePolicy(policy0_); 219 UpdateProviderPolicy(policy0_);
182 Mock::VerifyAndClearExpectations(&observer); 220 Mock::VerifyAndClearExpectations(&observer);
183 221
184 // Changed policy. 222 // Changed policy.
185 expectedPrevious.CopyFrom(expectedCurrent); 223 expectedPrevious.CopyFrom(expectedCurrent);
186 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 224 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
187 base::Value::CreateIntegerValue(789)); 225 base::Value::CreateIntegerValue(789));
188 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 226 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
189 base::Value::CreateIntegerValue(789)); 227 base::Value::CreateIntegerValue(789));
190 228
191 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", 229 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "",
192 PolicyEquals(&expectedPrevious), 230 PolicyEquals(&expectedPrevious),
193 PolicyEquals(&expectedCurrent))); 231 PolicyEquals(&expectedCurrent)));
194 provider0_.UpdateChromePolicy(policy0_); 232 UpdateProviderPolicy(policy0_);
195 Mock::VerifyAndClearExpectations(&observer); 233 Mock::VerifyAndClearExpectations(&observer);
196 234
197 // No changes again. 235 // No changes again.
198 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); 236 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0);
199 provider0_.UpdateChromePolicy(policy0_); 237 UpdateProviderPolicy(policy0_);
200 Mock::VerifyAndClearExpectations(&observer); 238 Mock::VerifyAndClearExpectations(&observer);
201 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); 239 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent));
202 240
203 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer); 241 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
204 } 242 }
205 243
206 TEST_F(PolicyServiceTest, NotifyObserversInMultipleNamespaces) { 244 TEST_F(PolicyServiceTest, NotifyObserversInMultipleNamespaces) {
207 const std::string kExtension0("extension-0"); 245 const std::string kExtension0("extension-0");
208 const std::string kExtension1("extension-1"); 246 const std::string kExtension1("extension-1");
209 const std::string kExtension2("extension-2"); 247 const std::string kExtension2("extension-2");
(...skipping 23 matching lines...) Expand all
233 PolicyEquals(&policy_map))); 271 PolicyEquals(&policy_map)));
234 EXPECT_CALL(extension_observer, 272 EXPECT_CALL(extension_observer,
235 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0, 273 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0,
236 PolicyEquals(&kEmptyPolicyMap), 274 PolicyEquals(&kEmptyPolicyMap),
237 PolicyEquals(&policy_map))); 275 PolicyEquals(&policy_map)));
238 EXPECT_CALL(extension_observer, 276 EXPECT_CALL(extension_observer,
239 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1, 277 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1,
240 PolicyEquals(&kEmptyPolicyMap), 278 PolicyEquals(&kEmptyPolicyMap),
241 PolicyEquals(&policy_map))); 279 PolicyEquals(&policy_map)));
242 provider0_.UpdatePolicy(bundle.Pass()); 280 provider0_.UpdatePolicy(bundle.Pass());
281 RunUntilIdle();
243 Mock::VerifyAndClearExpectations(&chrome_observer); 282 Mock::VerifyAndClearExpectations(&chrome_observer);
244 Mock::VerifyAndClearExpectations(&extension_observer); 283 Mock::VerifyAndClearExpectations(&extension_observer);
245 284
246 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes, 285 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes,
247 // and kExtension2 is new. 286 // and kExtension2 is new.
248 previous_policy_map.CopyFrom(policy_map); 287 previous_policy_map.CopyFrom(policy_map);
249 bundle.reset(new PolicyBundle()); 288 bundle.reset(new PolicyBundle());
250 bundle->Get(POLICY_DOMAIN_CHROME, "").CopyFrom(policy_map); 289 bundle->Get(POLICY_DOMAIN_CHROME, "").CopyFrom(policy_map);
251 policy_map.Set("policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 290 policy_map.Set("policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
252 base::Value::CreateStringValue("another value")); 291 base::Value::CreateStringValue("another value"));
253 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension1).CopyFrom(policy_map); 292 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension1).CopyFrom(policy_map);
254 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension2).CopyFrom(policy_map); 293 bundle->Get(POLICY_DOMAIN_EXTENSIONS, kExtension2).CopyFrom(policy_map);
255 294
256 EXPECT_CALL(chrome_observer, OnPolicyUpdated(_, _, _, _)).Times(0); 295 EXPECT_CALL(chrome_observer, OnPolicyUpdated(_, _, _, _)).Times(0);
257 EXPECT_CALL(extension_observer, 296 EXPECT_CALL(extension_observer,
258 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0, 297 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension0,
259 PolicyEquals(&previous_policy_map), 298 PolicyEquals(&previous_policy_map),
260 PolicyEquals(&kEmptyPolicyMap))); 299 PolicyEquals(&kEmptyPolicyMap)));
261 EXPECT_CALL(extension_observer, 300 EXPECT_CALL(extension_observer,
262 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1, 301 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension1,
263 PolicyEquals(&previous_policy_map), 302 PolicyEquals(&previous_policy_map),
264 PolicyEquals(&policy_map))); 303 PolicyEquals(&policy_map)));
265 EXPECT_CALL(extension_observer, 304 EXPECT_CALL(extension_observer,
266 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension2, 305 OnPolicyUpdated(POLICY_DOMAIN_EXTENSIONS, kExtension2,
267 PolicyEquals(&kEmptyPolicyMap), 306 PolicyEquals(&kEmptyPolicyMap),
268 PolicyEquals(&policy_map))); 307 PolicyEquals(&policy_map)));
269 provider0_.UpdatePolicy(bundle.Pass()); 308 provider0_.UpdatePolicy(bundle.Pass());
309 RunUntilIdle();
270 Mock::VerifyAndClearExpectations(&chrome_observer); 310 Mock::VerifyAndClearExpectations(&chrome_observer);
271 Mock::VerifyAndClearExpectations(&extension_observer); 311 Mock::VerifyAndClearExpectations(&extension_observer);
272 312
273 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &chrome_observer); 313 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &chrome_observer);
274 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS, 314 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS,
275 &extension_observer); 315 &extension_observer);
276 } 316 }
277 317
318 TEST_F(PolicyServiceTest, ObserverChangesPolicy) {
319 ChangePolicyObserver observer(&provider0_);
320 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
321 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
322 base::Value::CreateIntegerValue(123));
323 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
324 base::Value::CreateIntegerValue(1234));
325 // Should not crash.
326 UpdateProviderPolicy(policy0_);
327 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
328 EXPECT_TRUE(observer.observer_invoked());
329 }
330
278 TEST_F(PolicyServiceTest, Priorities) { 331 TEST_F(PolicyServiceTest, Priorities) {
279 PolicyMap expected; 332 PolicyMap expected;
280 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 333 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
281 base::Value::CreateIntegerValue(13)); 334 base::Value::CreateIntegerValue(13));
282 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 335 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
283 base::Value::CreateIntegerValue(0)); 336 base::Value::CreateIntegerValue(0));
284 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 337 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
285 base::Value::CreateIntegerValue(0)); 338 base::Value::CreateIntegerValue(0));
286 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 339 policy1_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
287 base::Value::CreateIntegerValue(1)); 340 base::Value::CreateIntegerValue(1));
(...skipping 24 matching lines...) Expand all
312 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); 365 policy_service_.get(), POLICY_DOMAIN_CHROME, ""));
313 366
314 // Starting to observe existing policies doesn't trigger a notification. 367 // Starting to observe existing policies doesn't trigger a notification.
315 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); 368 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
316 registrar->Observe("pre", base::Bind( 369 registrar->Observe("pre", base::Bind(
317 &PolicyServiceTest::OnPolicyValueUpdated, 370 &PolicyServiceTest::OnPolicyValueUpdated,
318 base::Unretained(this))); 371 base::Unretained(this)));
319 registrar->Observe("aaa", base::Bind( 372 registrar->Observe("aaa", base::Bind(
320 &PolicyServiceTest::OnPolicyValueUpdated, 373 &PolicyServiceTest::OnPolicyValueUpdated,
321 base::Unretained(this))); 374 base::Unretained(this)));
375 {
376 // Let any queued up notifications fire.
377 base::RunLoop loop;
378 loop.RunUntilIdle();
379 }
Joao da Silva 2013/01/07 09:23:52 RunUntilIdle()?
Andrew T Wilson (Slow) 2013/01/07 14:11:32 Done.
322 Mock::VerifyAndClearExpectations(this); 380 Mock::VerifyAndClearExpectations(this);
323 381
324 // Changing it now triggers a notification. 382 // Changing it now triggers a notification.
325 base::FundamentalValue kValue0(0); 383 base::FundamentalValue kValue0(0);
326 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); 384 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0)));
327 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 385 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
328 kValue0.DeepCopy()); 386 kValue0.DeepCopy());
329 provider0_.UpdateChromePolicy(policy0_); 387 UpdateProviderPolicy(policy0_);
330 Mock::VerifyAndClearExpectations(this); 388 Mock::VerifyAndClearExpectations(this);
331 389
332 // Changing other values doesn't trigger a notification. 390 // Changing other values doesn't trigger a notification.
333 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); 391 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
334 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 392 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
335 kValue0.DeepCopy()); 393 kValue0.DeepCopy());
336 provider0_.UpdateChromePolicy(policy0_); 394 UpdateProviderPolicy(policy0_);
337 Mock::VerifyAndClearExpectations(this); 395 Mock::VerifyAndClearExpectations(this);
338 396
339 // Modifying the value triggers a notification. 397 // Modifying the value triggers a notification.
340 base::FundamentalValue kValue1(1); 398 base::FundamentalValue kValue1(1);
341 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0), 399 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0),
342 ValueEquals(&kValue1))); 400 ValueEquals(&kValue1)));
343 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 401 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
344 kValue1.DeepCopy()); 402 kValue1.DeepCopy());
345 provider0_.UpdateChromePolicy(policy0_); 403 UpdateProviderPolicy(policy0_);
346 Mock::VerifyAndClearExpectations(this); 404 Mock::VerifyAndClearExpectations(this);
347 405
348 // Removing the value triggers a notification. 406 // Removing the value triggers a notification.
349 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); 407 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL));
350 policy0_.Erase("aaa"); 408 policy0_.Erase("aaa");
351 provider0_.UpdateChromePolicy(policy0_); 409 UpdateProviderPolicy(policy0_);
352 Mock::VerifyAndClearExpectations(this); 410 Mock::VerifyAndClearExpectations(this);
353 411
354 // No more notifications after destroying the registrar. 412 // No more notifications after destroying the registrar.
355 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0); 413 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
356 registrar.reset(); 414 registrar.reset();
357 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 415 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
358 kValue1.DeepCopy()); 416 kValue1.DeepCopy());
359 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 417 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
360 kValue1.DeepCopy()); 418 kValue1.DeepCopy());
361 provider0_.UpdateChromePolicy(policy0_); 419 UpdateProviderPolicy(policy0_);
362 Mock::VerifyAndClearExpectations(this); 420 Mock::VerifyAndClearExpectations(this);
363 } 421 }
364 422
365 TEST_F(PolicyServiceTest, RefreshPolicies) { 423 TEST_F(PolicyServiceTest, RefreshPolicies) {
366 MessageLoop loop; 424 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop_);
367 content::TestBrowserThread ui_thread(content::BrowserThread::UI, &loop); 425 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop_);
368 content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); 426 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop_);
369 content::TestBrowserThread io_thread(content::BrowserThread::IO, &loop);
370 427
371 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber()); 428 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber());
372 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber()); 429 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber());
373 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber()); 430 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber());
374 431
375 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 432 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
376 policy_service_->RefreshPolicies(base::Bind( 433 policy_service_->RefreshPolicies(base::Bind(
377 &PolicyServiceTest::OnPolicyRefresh, 434 &PolicyServiceTest::OnPolicyRefresh,
378 base::Unretained(this))); 435 base::Unretained(this)));
379 loop.RunUntilIdle(); 436 // Let any queued observer tasks run.
437 RunUntilIdle();
380 Mock::VerifyAndClearExpectations(this); 438 Mock::VerifyAndClearExpectations(this);
381 439
382 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 440 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
383 base::FundamentalValue kValue0(0); 441 base::FundamentalValue kValue0(0);
384 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 442 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
385 kValue0.DeepCopy()); 443 kValue0.DeepCopy());
386 provider0_.UpdateChromePolicy(policy0_); 444 UpdateProviderPolicy(policy0_);
387 loop.RunUntilIdle();
388 Mock::VerifyAndClearExpectations(this); 445 Mock::VerifyAndClearExpectations(this);
389 446
390 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 447 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
391 base::FundamentalValue kValue1(1); 448 base::FundamentalValue kValue1(1);
392 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 449 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
393 kValue1.DeepCopy()); 450 kValue1.DeepCopy());
394 provider1_.UpdateChromePolicy(policy1_); 451 provider1_.UpdateChromePolicy(policy1_);
395 loop.RunUntilIdle(); 452 RunUntilIdle();
396 Mock::VerifyAndClearExpectations(this); 453 Mock::VerifyAndClearExpectations(this);
397 454
398 // A provider can refresh more than once after a RefreshPolicies call, but 455 // A provider can refresh more than once after a RefreshPolicies call, but
399 // OnPolicyRefresh should be triggered only after all providers are 456 // OnPolicyRefresh should be triggered only after all providers are
400 // refreshed. 457 // refreshed.
401 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 458 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
402 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 459 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
403 kValue1.DeepCopy()); 460 kValue1.DeepCopy());
404 provider1_.UpdateChromePolicy(policy1_); 461 provider1_.UpdateChromePolicy(policy1_);
405 loop.RunUntilIdle(); 462 RunUntilIdle();
406 Mock::VerifyAndClearExpectations(this); 463 Mock::VerifyAndClearExpectations(this);
407 464
408 // If another RefreshPolicies() call happens while waiting for a previous 465 // If another RefreshPolicies() call happens while waiting for a previous
409 // one to complete, then all providers must refresh again. 466 // one to complete, then all providers must refresh again.
410 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 467 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
411 policy_service_->RefreshPolicies(base::Bind( 468 policy_service_->RefreshPolicies(base::Bind(
412 &PolicyServiceTest::OnPolicyRefresh, 469 &PolicyServiceTest::OnPolicyRefresh,
413 base::Unretained(this))); 470 base::Unretained(this)));
414 loop.RunUntilIdle(); 471 RunUntilIdle();
415 Mock::VerifyAndClearExpectations(this); 472 Mock::VerifyAndClearExpectations(this);
416 473
417 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0); 474 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
418 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 475 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
419 kValue0.DeepCopy()); 476 kValue0.DeepCopy());
420 provider2_.UpdateChromePolicy(policy2_); 477 provider2_.UpdateChromePolicy(policy2_);
421 loop.RunUntilIdle(); 478 RunUntilIdle();
422 Mock::VerifyAndClearExpectations(this); 479 Mock::VerifyAndClearExpectations(this);
423 480
424 // Providers 0 and 1 must reload again. 481 // Providers 0 and 1 must reload again.
425 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2); 482 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2);
426 base::FundamentalValue kValue2(2); 483 base::FundamentalValue kValue2(2);
427 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 484 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
428 kValue2.DeepCopy()); 485 kValue2.DeepCopy());
429 provider0_.UpdateChromePolicy(policy0_); 486 provider0_.UpdateChromePolicy(policy0_);
430 provider1_.UpdateChromePolicy(policy1_); 487 provider1_.UpdateChromePolicy(policy1_);
431 loop.RunUntilIdle(); 488 RunUntilIdle();
432 Mock::VerifyAndClearExpectations(this); 489 Mock::VerifyAndClearExpectations(this);
433 490
434 const PolicyMap& policies = policy_service_->GetPolicies( 491 const PolicyMap& policies = policy_service_->GetPolicies(
435 POLICY_DOMAIN_CHROME, ""); 492 POLICY_DOMAIN_CHROME, "");
436 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa"))); 493 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa")));
437 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb"))); 494 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb")));
438 } 495 }
439 496
440 TEST_F(PolicyServiceTest, NamespaceMerge) { 497 TEST_F(PolicyServiceTest, NamespaceMerge) {
441 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle()); 498 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle());
(...skipping 20 matching lines...) Expand all
462 // level/scope combination takes precedence, on every namespace. 519 // level/scope combination takes precedence, on every namespace.
463 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, 520 expected.Set(kDiffLevelPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
464 base::Value::CreateStringValue("bundle2")); 521 base::Value::CreateStringValue("bundle2"));
465 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, "") 522 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, "")
466 .Equals(expected)); 523 .Equals(expected));
467 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_EXTENSIONS, kExtension) 524 EXPECT_TRUE(policy_service_->GetPolicies(POLICY_DOMAIN_EXTENSIONS, kExtension)
468 .Equals(expected)); 525 .Equals(expected));
469 } 526 }
470 527
471 } // namespace policy 528 } // namespace policy
OLDNEW
« chrome/browser/policy/policy_service_impl.cc ('K') | « 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