cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / policy / core / common / policy_service_impl_unittest.cc
blobae51237aac6932f4b0a5bf402f14cc78f8b2569f
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
3 // found in the LICENSE file.
5 #include "components/policy/core/common/policy_service_impl.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/values.h"
13 #include "components/policy/core/common/external_data_fetcher.h"
14 #include "components/policy/core/common/mock_configuration_policy_provider.h"
15 #include "components/policy/core/common/mock_policy_service.h"
16 #include "policy/policy_constants.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::AnyNumber;
21 using ::testing::Mock;
22 using ::testing::Return;
23 using ::testing::_;
25 namespace policy {
27 namespace {
29 const char kExtension[] = "extension-id";
30 const char kSameLevelPolicy[] = "policy-same-level-and-scope";
31 const char kDiffLevelPolicy[] = "chrome-diff-level-and-scope";
33 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with
34 // their expected values.
35 MATCHER_P(PolicyEquals, expected, "") {
36 return arg.Equals(*expected);
39 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated()
40 // with their expected values.
41 MATCHER_P(ValueEquals, expected, "") {
42 return base::Value::Equals(arg, expected);
45 // Helper that fills |bundle| with test policies.
46 void AddTestPolicies(PolicyBundle* bundle,
47 const char* value,
48 PolicyLevel level,
49 PolicyScope scope) {
50 PolicyMap* policy_map =
51 &bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
52 policy_map->Set(kSameLevelPolicy,
53 POLICY_LEVEL_MANDATORY,
54 POLICY_SCOPE_USER,
55 new base::StringValue(value),
56 NULL);
57 policy_map->Set(
58 kDiffLevelPolicy, level, scope, new base::StringValue(value), NULL);
59 policy_map =
60 &bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension));
61 policy_map->Set(kSameLevelPolicy,
62 POLICY_LEVEL_MANDATORY,
63 POLICY_SCOPE_USER,
64 new base::StringValue(value),
65 NULL);
66 policy_map->Set(
67 kDiffLevelPolicy, level, scope, new base::StringValue(value), NULL);
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) {}
78 void OnPolicyUpdated(const PolicyNamespace&,
79 const PolicyMap& previous,
80 const PolicyMap& current) override {
81 PolicyMap new_policy;
82 new_policy.Set("foo",
83 POLICY_LEVEL_MANDATORY,
84 POLICY_SCOPE_USER,
85 new base::FundamentalValue(14),
86 NULL);
87 provider_->UpdateChromePolicy(new_policy);
88 observer_invoked_ = true;
91 bool observer_invoked() const { return observer_invoked_; }
93 private:
94 MockConfigurationPolicyProvider* provider_;
95 bool observer_invoked_;
98 } // namespace
100 class PolicyServiceTest : public testing::Test {
101 public:
102 PolicyServiceTest() {}
103 void SetUp() override {
104 EXPECT_CALL(provider0_, IsInitializationComplete(_))
105 .WillRepeatedly(Return(true));
106 EXPECT_CALL(provider1_, IsInitializationComplete(_))
107 .WillRepeatedly(Return(true));
108 EXPECT_CALL(provider2_, IsInitializationComplete(_))
109 .WillRepeatedly(Return(true));
111 provider0_.Init();
112 provider1_.Init();
113 provider2_.Init();
115 policy0_.Set("pre",
116 POLICY_LEVEL_MANDATORY,
117 POLICY_SCOPE_USER,
118 new base::FundamentalValue(13),
119 NULL);
120 provider0_.UpdateChromePolicy(policy0_);
122 PolicyServiceImpl::Providers providers;
123 providers.push_back(&provider0_);
124 providers.push_back(&provider1_);
125 providers.push_back(&provider2_);
126 policy_service_.reset(new PolicyServiceImpl(providers));
129 void TearDown() override {
130 provider0_.Shutdown();
131 provider1_.Shutdown();
132 provider2_.Shutdown();
135 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*,
136 const base::Value*));
138 MOCK_METHOD0(OnPolicyRefresh, void());
140 // Returns true if the policies for namespace |ns| match |expected|.
141 bool VerifyPolicies(const PolicyNamespace& ns,
142 const PolicyMap& expected) {
143 return policy_service_->GetPolicies(ns).Equals(expected);
146 void RunUntilIdle() {
147 base::RunLoop loop;
148 loop.RunUntilIdle();
151 protected:
152 base::MessageLoop loop_;
153 MockConfigurationPolicyProvider provider0_;
154 MockConfigurationPolicyProvider provider1_;
155 MockConfigurationPolicyProvider provider2_;
156 PolicyMap policy0_;
157 PolicyMap policy1_;
158 PolicyMap policy2_;
159 scoped_ptr<PolicyServiceImpl> policy_service_;
161 private:
162 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest);
165 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) {
166 PolicyMap expected;
167 expected.Set("pre",
168 POLICY_LEVEL_MANDATORY,
169 POLICY_SCOPE_USER,
170 new base::FundamentalValue(13),
171 NULL);
172 EXPECT_TRUE(VerifyPolicies(
173 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
176 TEST_F(PolicyServiceTest, NotifyObservers) {
177 MockPolicyServiceObserver observer;
178 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
180 PolicyMap expectedPrevious;
181 expectedPrevious.Set("pre",
182 POLICY_LEVEL_MANDATORY,
183 POLICY_SCOPE_USER,
184 new base::FundamentalValue(13),
185 NULL);
187 PolicyMap expectedCurrent;
188 expectedCurrent.CopyFrom(expectedPrevious);
189 expectedCurrent.Set("aaa",
190 POLICY_LEVEL_MANDATORY,
191 POLICY_SCOPE_USER,
192 new base::FundamentalValue(123),
193 NULL);
194 policy0_.Set("aaa",
195 POLICY_LEVEL_MANDATORY,
196 POLICY_SCOPE_USER,
197 new base::FundamentalValue(123),
198 NULL);
199 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
200 std::string()),
201 PolicyEquals(&expectedPrevious),
202 PolicyEquals(&expectedCurrent)));
203 provider0_.UpdateChromePolicy(policy0_);
204 Mock::VerifyAndClearExpectations(&observer);
206 // No changes.
207 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _)).Times(0);
208 provider0_.UpdateChromePolicy(policy0_);
209 Mock::VerifyAndClearExpectations(&observer);
210 EXPECT_TRUE(VerifyPolicies(
211 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expectedCurrent));
213 // New policy.
214 expectedPrevious.CopyFrom(expectedCurrent);
215 expectedCurrent.Set("bbb",
216 POLICY_LEVEL_MANDATORY,
217 POLICY_SCOPE_USER,
218 new base::FundamentalValue(456),
219 NULL);
220 policy0_.Set("bbb",
221 POLICY_LEVEL_MANDATORY,
222 POLICY_SCOPE_USER,
223 new base::FundamentalValue(456),
224 NULL);
225 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
226 std::string()),
227 PolicyEquals(&expectedPrevious),
228 PolicyEquals(&expectedCurrent)));
229 provider0_.UpdateChromePolicy(policy0_);
230 Mock::VerifyAndClearExpectations(&observer);
232 // Removed policy.
233 expectedPrevious.CopyFrom(expectedCurrent);
234 expectedCurrent.Erase("bbb");
235 policy0_.Erase("bbb");
236 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
237 std::string()),
238 PolicyEquals(&expectedPrevious),
239 PolicyEquals(&expectedCurrent)));
240 provider0_.UpdateChromePolicy(policy0_);
241 Mock::VerifyAndClearExpectations(&observer);
243 // Changed policy.
244 expectedPrevious.CopyFrom(expectedCurrent);
245 expectedCurrent.Set("aaa",
246 POLICY_LEVEL_MANDATORY,
247 POLICY_SCOPE_USER,
248 new base::FundamentalValue(789),
249 NULL);
250 policy0_.Set("aaa",
251 POLICY_LEVEL_MANDATORY,
252 POLICY_SCOPE_USER,
253 new base::FundamentalValue(789),
254 NULL);
256 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
257 std::string()),
258 PolicyEquals(&expectedPrevious),
259 PolicyEquals(&expectedCurrent)));
260 provider0_.UpdateChromePolicy(policy0_);
261 Mock::VerifyAndClearExpectations(&observer);
263 // No changes again.
264 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _)).Times(0);
265 provider0_.UpdateChromePolicy(policy0_);
266 Mock::VerifyAndClearExpectations(&observer);
267 EXPECT_TRUE(VerifyPolicies(
268 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expectedCurrent));
270 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
273 TEST_F(PolicyServiceTest, NotifyObserversInMultipleNamespaces) {
274 const std::string kExtension0("extension-0");
275 const std::string kExtension1("extension-1");
276 const std::string kExtension2("extension-2");
277 MockPolicyServiceObserver chrome_observer;
278 MockPolicyServiceObserver extension_observer;
279 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &chrome_observer);
280 policy_service_->AddObserver(POLICY_DOMAIN_EXTENSIONS, &extension_observer);
282 PolicyMap previous_policy_map;
283 previous_policy_map.Set("pre",
284 POLICY_LEVEL_MANDATORY,
285 POLICY_SCOPE_USER,
286 new base::FundamentalValue(13),
287 NULL);
288 PolicyMap policy_map;
289 policy_map.CopyFrom(previous_policy_map);
290 policy_map.Set("policy",
291 POLICY_LEVEL_MANDATORY,
292 POLICY_SCOPE_USER,
293 new base::StringValue("value"),
294 NULL);
296 scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
297 // The initial setup includes a policy for chrome that is now changing.
298 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
299 .CopyFrom(policy_map);
300 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension0))
301 .CopyFrom(policy_map);
302 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1))
303 .CopyFrom(policy_map);
305 const PolicyMap kEmptyPolicyMap;
306 EXPECT_CALL(
307 chrome_observer,
308 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()),
309 PolicyEquals(&previous_policy_map),
310 PolicyEquals(&policy_map)));
311 EXPECT_CALL(
312 extension_observer,
313 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension0),
314 PolicyEquals(&kEmptyPolicyMap),
315 PolicyEquals(&policy_map)));
316 EXPECT_CALL(
317 extension_observer,
318 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1),
319 PolicyEquals(&kEmptyPolicyMap),
320 PolicyEquals(&policy_map)));
321 provider0_.UpdatePolicy(bundle.Pass());
322 RunUntilIdle();
323 Mock::VerifyAndClearExpectations(&chrome_observer);
324 Mock::VerifyAndClearExpectations(&extension_observer);
326 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes,
327 // and kExtension2 is new.
328 previous_policy_map.CopyFrom(policy_map);
329 bundle.reset(new PolicyBundle());
330 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
331 .CopyFrom(policy_map);
332 policy_map.Set("policy",
333 POLICY_LEVEL_MANDATORY,
334 POLICY_SCOPE_USER,
335 new base::StringValue("another value"),
336 NULL);
337 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1))
338 .CopyFrom(policy_map);
339 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2))
340 .CopyFrom(policy_map);
342 EXPECT_CALL(chrome_observer, OnPolicyUpdated(_, _, _)).Times(0);
343 EXPECT_CALL(
344 extension_observer,
345 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension0),
346 PolicyEquals(&previous_policy_map),
347 PolicyEquals(&kEmptyPolicyMap)));
348 EXPECT_CALL(
349 extension_observer,
350 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1),
351 PolicyEquals(&previous_policy_map),
352 PolicyEquals(&policy_map)));
353 EXPECT_CALL(
354 extension_observer,
355 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2),
356 PolicyEquals(&kEmptyPolicyMap),
357 PolicyEquals(&policy_map)));
358 provider0_.UpdatePolicy(bundle.Pass());
359 RunUntilIdle();
360 Mock::VerifyAndClearExpectations(&chrome_observer);
361 Mock::VerifyAndClearExpectations(&extension_observer);
363 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &chrome_observer);
364 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS,
365 &extension_observer);
368 TEST_F(PolicyServiceTest, ObserverChangesPolicy) {
369 ChangePolicyObserver observer(&provider0_);
370 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
371 policy0_.Set("aaa",
372 POLICY_LEVEL_MANDATORY,
373 POLICY_SCOPE_USER,
374 new base::FundamentalValue(123),
375 NULL);
376 policy0_.Set("bbb",
377 POLICY_LEVEL_MANDATORY,
378 POLICY_SCOPE_USER,
379 new base::FundamentalValue(1234),
380 NULL);
381 // Should not crash.
382 provider0_.UpdateChromePolicy(policy0_);
383 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
384 EXPECT_TRUE(observer.observer_invoked());
387 TEST_F(PolicyServiceTest, Priorities) {
388 PolicyMap expected;
389 expected.Set("pre",
390 POLICY_LEVEL_MANDATORY,
391 POLICY_SCOPE_USER,
392 new base::FundamentalValue(13),
393 NULL);
394 expected.Set("aaa",
395 POLICY_LEVEL_MANDATORY,
396 POLICY_SCOPE_USER,
397 new base::FundamentalValue(0),
398 NULL);
399 policy0_.Set("aaa",
400 POLICY_LEVEL_MANDATORY,
401 POLICY_SCOPE_USER,
402 new base::FundamentalValue(0),
403 NULL);
404 policy1_.Set("aaa",
405 POLICY_LEVEL_MANDATORY,
406 POLICY_SCOPE_USER,
407 new base::FundamentalValue(1),
408 NULL);
409 policy2_.Set("aaa",
410 POLICY_LEVEL_MANDATORY,
411 POLICY_SCOPE_USER,
412 new base::FundamentalValue(2),
413 NULL);
414 provider0_.UpdateChromePolicy(policy0_);
415 provider1_.UpdateChromePolicy(policy1_);
416 provider2_.UpdateChromePolicy(policy2_);
417 EXPECT_TRUE(VerifyPolicies(
418 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
420 expected.Set("aaa",
421 POLICY_LEVEL_MANDATORY,
422 POLICY_SCOPE_USER,
423 new base::FundamentalValue(1),
424 NULL);
425 policy0_.Erase("aaa");
426 provider0_.UpdateChromePolicy(policy0_);
427 EXPECT_TRUE(VerifyPolicies(
428 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
430 expected.Set("aaa",
431 POLICY_LEVEL_MANDATORY,
432 POLICY_SCOPE_USER,
433 new base::FundamentalValue(2),
434 NULL);
435 policy1_.Set("aaa",
436 POLICY_LEVEL_RECOMMENDED,
437 POLICY_SCOPE_USER,
438 new base::FundamentalValue(1),
439 NULL);
440 provider1_.UpdateChromePolicy(policy1_);
441 EXPECT_TRUE(VerifyPolicies(
442 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
445 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) {
446 scoped_ptr<PolicyChangeRegistrar> registrar(new PolicyChangeRegistrar(
447 policy_service_.get(),
448 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())));
450 // Starting to observe existing policies doesn't trigger a notification.
451 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
452 registrar->Observe("pre", base::Bind(
453 &PolicyServiceTest::OnPolicyValueUpdated,
454 base::Unretained(this)));
455 registrar->Observe("aaa", base::Bind(
456 &PolicyServiceTest::OnPolicyValueUpdated,
457 base::Unretained(this)));
458 RunUntilIdle();
459 Mock::VerifyAndClearExpectations(this);
461 // Changing it now triggers a notification.
462 base::FundamentalValue kValue0(0);
463 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0)));
464 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
465 kValue0.DeepCopy(), NULL);
466 provider0_.UpdateChromePolicy(policy0_);
467 Mock::VerifyAndClearExpectations(this);
469 // Changing other values doesn't trigger a notification.
470 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
471 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
472 kValue0.DeepCopy(), NULL);
473 provider0_.UpdateChromePolicy(policy0_);
474 Mock::VerifyAndClearExpectations(this);
476 // Modifying the value triggers a notification.
477 base::FundamentalValue kValue1(1);
478 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0),
479 ValueEquals(&kValue1)));
480 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
481 kValue1.DeepCopy(), NULL);
482 provider0_.UpdateChromePolicy(policy0_);
483 Mock::VerifyAndClearExpectations(this);
485 // Removing the value triggers a notification.
486 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL));
487 policy0_.Erase("aaa");
488 provider0_.UpdateChromePolicy(policy0_);
489 Mock::VerifyAndClearExpectations(this);
491 // No more notifications after destroying the registrar.
492 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
493 registrar.reset();
494 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
495 kValue1.DeepCopy(), NULL);
496 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
497 kValue1.DeepCopy(), NULL);
498 provider0_.UpdateChromePolicy(policy0_);
499 Mock::VerifyAndClearExpectations(this);
502 TEST_F(PolicyServiceTest, RefreshPolicies) {
503 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber());
504 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber());
505 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber());
507 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
508 policy_service_->RefreshPolicies(base::Bind(
509 &PolicyServiceTest::OnPolicyRefresh,
510 base::Unretained(this)));
511 // Let any queued observer tasks run.
512 RunUntilIdle();
513 Mock::VerifyAndClearExpectations(this);
515 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
516 base::FundamentalValue kValue0(0);
517 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
518 kValue0.DeepCopy(), NULL);
519 provider0_.UpdateChromePolicy(policy0_);
520 Mock::VerifyAndClearExpectations(this);
522 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
523 base::FundamentalValue kValue1(1);
524 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
525 kValue1.DeepCopy(), NULL);
526 provider1_.UpdateChromePolicy(policy1_);
527 Mock::VerifyAndClearExpectations(this);
529 // A provider can refresh more than once after a RefreshPolicies call, but
530 // OnPolicyRefresh should be triggered only after all providers are
531 // refreshed.
532 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
533 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
534 kValue1.DeepCopy(), NULL);
535 provider1_.UpdateChromePolicy(policy1_);
536 Mock::VerifyAndClearExpectations(this);
538 // If another RefreshPolicies() call happens while waiting for a previous
539 // one to complete, then all providers must refresh again.
540 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
541 policy_service_->RefreshPolicies(base::Bind(
542 &PolicyServiceTest::OnPolicyRefresh,
543 base::Unretained(this)));
544 RunUntilIdle();
545 Mock::VerifyAndClearExpectations(this);
547 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
548 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
549 kValue0.DeepCopy(), NULL);
550 provider2_.UpdateChromePolicy(policy2_);
551 Mock::VerifyAndClearExpectations(this);
553 // Providers 0 and 1 must reload again.
554 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2);
555 base::FundamentalValue kValue2(2);
556 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
557 kValue2.DeepCopy(), NULL);
558 provider0_.UpdateChromePolicy(policy0_);
559 provider1_.UpdateChromePolicy(policy1_);
560 Mock::VerifyAndClearExpectations(this);
562 const PolicyMap& policies = policy_service_->GetPolicies(
563 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
564 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa")));
565 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb")));
568 TEST_F(PolicyServiceTest, NamespaceMerge) {
569 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle());
570 scoped_ptr<PolicyBundle> bundle1(new PolicyBundle());
571 scoped_ptr<PolicyBundle> bundle2(new PolicyBundle());
573 AddTestPolicies(bundle0.get(), "bundle0",
574 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
575 AddTestPolicies(bundle1.get(), "bundle1",
576 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
577 AddTestPolicies(bundle2.get(), "bundle2",
578 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE);
580 provider0_.UpdatePolicy(bundle0.Pass());
581 provider1_.UpdatePolicy(bundle1.Pass());
582 provider2_.UpdatePolicy(bundle2.Pass());
583 RunUntilIdle();
585 PolicyMap expected;
586 // For policies of the same level and scope, the first provider takes
587 // precedence, on every namespace.
588 expected.Set(kSameLevelPolicy,
589 POLICY_LEVEL_MANDATORY,
590 POLICY_SCOPE_USER,
591 new base::StringValue("bundle0"),
592 NULL);
593 // For policies with different levels and scopes, the highest priority
594 // level/scope combination takes precedence, on every namespace.
595 expected.Set(kDiffLevelPolicy,
596 POLICY_LEVEL_MANDATORY,
597 POLICY_SCOPE_MACHINE,
598 new base::StringValue("bundle2"),
599 NULL);
600 EXPECT_TRUE(policy_service_->GetPolicies(
601 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())).Equals(expected));
602 EXPECT_TRUE(policy_service_->GetPolicies(
603 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension)).Equals(expected));
606 TEST_F(PolicyServiceTest, IsInitializationComplete) {
607 // |provider0| has all domains initialized.
608 Mock::VerifyAndClearExpectations(&provider1_);
609 Mock::VerifyAndClearExpectations(&provider2_);
610 EXPECT_CALL(provider1_, IsInitializationComplete(_))
611 .WillRepeatedly(Return(false));
612 EXPECT_CALL(provider2_, IsInitializationComplete(_))
613 .WillRepeatedly(Return(false));
614 PolicyServiceImpl::Providers providers;
615 providers.push_back(&provider0_);
616 providers.push_back(&provider1_);
617 providers.push_back(&provider2_);
618 policy_service_.reset(new PolicyServiceImpl(providers));
619 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
620 EXPECT_FALSE(
621 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
623 // |provider2_| still doesn't have POLICY_DOMAIN_CHROME initialized, so
624 // the initialization status of that domain won't change.
625 MockPolicyServiceObserver observer;
626 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
627 policy_service_->AddObserver(POLICY_DOMAIN_EXTENSIONS, &observer);
628 EXPECT_CALL(observer, OnPolicyServiceInitialized(_)).Times(0);
629 Mock::VerifyAndClearExpectations(&provider1_);
630 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
631 .WillRepeatedly(Return(true));
632 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
633 .WillRepeatedly(Return(false));
634 const PolicyMap kPolicyMap;
635 provider1_.UpdateChromePolicy(kPolicyMap);
636 Mock::VerifyAndClearExpectations(&observer);
637 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
638 EXPECT_FALSE(
639 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
641 // Same if |provider1_| doesn't have POLICY_DOMAIN_EXTENSIONS initialized.
642 EXPECT_CALL(observer, OnPolicyServiceInitialized(_)).Times(0);
643 Mock::VerifyAndClearExpectations(&provider2_);
644 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
645 .WillRepeatedly(Return(false));
646 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
647 .WillRepeatedly(Return(true));
648 provider2_.UpdateChromePolicy(kPolicyMap);
649 Mock::VerifyAndClearExpectations(&observer);
650 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
651 EXPECT_FALSE(
652 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
654 // Now initialize POLICY_DOMAIN_CHROME on all the providers.
655 EXPECT_CALL(observer, OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME));
656 Mock::VerifyAndClearExpectations(&provider2_);
657 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
658 .WillRepeatedly(Return(true));
659 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
660 .WillRepeatedly(Return(true));
661 provider2_.UpdateChromePolicy(kPolicyMap);
662 Mock::VerifyAndClearExpectations(&observer);
663 EXPECT_TRUE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
664 // Other domains are still not initialized.
665 EXPECT_FALSE(
666 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
668 // Initialize the remaining domain.
669 EXPECT_CALL(observer, OnPolicyServiceInitialized(POLICY_DOMAIN_EXTENSIONS));
670 Mock::VerifyAndClearExpectations(&provider1_);
671 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
672 .WillRepeatedly(Return(true));
673 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
674 .WillRepeatedly(Return(true));
675 provider1_.UpdateChromePolicy(kPolicyMap);
676 Mock::VerifyAndClearExpectations(&observer);
677 EXPECT_TRUE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
678 EXPECT_TRUE(
679 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
681 // Cleanup.
682 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
683 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS, &observer);
686 TEST_F(PolicyServiceTest, FixDeprecatedPolicies) {
687 const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string());
688 const PolicyNamespace extension_namespace(POLICY_DOMAIN_EXTENSIONS, "xyz");
690 scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle());
691 PolicyMap& policy_map = policy_bundle->Get(chrome_namespace);
692 // Individual proxy policy values in the Chrome namespace should be collected
693 // into a dictionary.
694 policy_map.Set(key::kProxyServerMode,
695 POLICY_LEVEL_MANDATORY,
696 POLICY_SCOPE_USER,
697 new base::FundamentalValue(3),
698 NULL);
700 // Both these policies should be ignored, since there's a higher priority
701 // policy available.
702 policy_map.Set(key::kProxyMode,
703 POLICY_LEVEL_RECOMMENDED,
704 POLICY_SCOPE_USER,
705 new base::StringValue("pac_script"),
706 NULL);
707 policy_map.Set(key::kProxyPacUrl,
708 POLICY_LEVEL_RECOMMENDED,
709 POLICY_SCOPE_USER,
710 new base::StringValue("http://example.com/wpad.dat"),
711 NULL);
713 // Add a value to a non-Chrome namespace.
714 policy_bundle->Get(extension_namespace).Set(key::kProxyServerMode,
715 POLICY_LEVEL_MANDATORY,
716 POLICY_SCOPE_USER,
717 new base::FundamentalValue(3),
718 NULL);
720 // The resulting Chrome namespace map should have the collected policy.
721 PolicyMap expected_chrome;
722 scoped_ptr<base::DictionaryValue> expected_value(new base::DictionaryValue);
723 expected_value->SetInteger(key::kProxyServerMode, 3);
724 expected_chrome.Set(key::kProxySettings, POLICY_LEVEL_MANDATORY,
725 POLICY_SCOPE_USER, expected_value.release(), NULL);
727 // The resulting Extensions namespace map shouldn't have been modified.
728 PolicyMap expected_extension;
729 expected_extension.Set(key::kProxyServerMode,
730 POLICY_LEVEL_MANDATORY,
731 POLICY_SCOPE_USER,
732 new base::FundamentalValue(3),
733 NULL);
735 provider0_.UpdatePolicy(policy_bundle.Pass());
736 RunUntilIdle();
738 EXPECT_TRUE(VerifyPolicies(chrome_namespace, expected_chrome));
739 EXPECT_TRUE(VerifyPolicies(extension_namespace, expected_extension));
742 } // namespace policy