1 // Copyright 2014 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 "base/memory/scoped_ptr.h"
6 #include "components/policy/core/common/policy_provider_android.h"
7 #include "components/policy/core/common/policy_provider_android_delegate.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 // Helper to write a policy in |bundle| with less code.
16 void SetPolicy(PolicyBundle
* bundle
,
17 const std::string
& name
,
18 const std::string
& value
) {
19 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
21 POLICY_LEVEL_MANDATORY
,
23 base::Value::CreateStringValue(value
),
27 class MockPolicyProviderAndroidDelegate
: public PolicyProviderAndroidDelegate
{
29 MockPolicyProviderAndroidDelegate() {}
30 virtual ~MockPolicyProviderAndroidDelegate() {}
32 MOCK_METHOD0(RefreshPolicies
, void());
33 MOCK_METHOD0(PolicyProviderShutdown
, void());
36 DISALLOW_COPY_AND_ASSIGN(MockPolicyProviderAndroidDelegate
);
39 // Test fixture that makes sure that we always call Shutdown() before destroying
40 // the policy provider. Allocate this just like a PolicyProviderAndroid and use
41 // Get() to get the policy provider.
42 class PolicyProviderAndroidTestFixture
{
44 PolicyProviderAndroidTestFixture() {}
45 ~PolicyProviderAndroidTestFixture() {
49 PolicyProviderAndroid
* Get() {
54 PolicyProviderAndroid provider_
;
55 DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTestFixture
);
60 class PolicyProviderAndroidTest
: public ::testing::Test
{
62 PolicyProviderAndroidTest();
63 virtual ~PolicyProviderAndroidTest();
65 virtual void SetUp() OVERRIDE
;
66 virtual void TearDown() OVERRIDE
;
69 DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTest
);
72 PolicyProviderAndroidTest::PolicyProviderAndroidTest() {}
73 PolicyProviderAndroidTest::~PolicyProviderAndroidTest() {}
75 void PolicyProviderAndroidTest::SetUp() {}
77 void PolicyProviderAndroidTest::TearDown() {
78 PolicyProviderAndroid::SetShouldWaitForPolicy(false);
81 TEST_F(PolicyProviderAndroidTest
, InitializationCompleted
) {
82 PolicyProviderAndroidTestFixture provider
;
83 EXPECT_TRUE(provider
.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
85 const PolicyBundle kEmptyBundle
;
86 EXPECT_TRUE(provider
.Get()->policies().Equals(kEmptyBundle
));
89 TEST_F(PolicyProviderAndroidTest
, WaitForInitialization
) {
90 PolicyProviderAndroid::SetShouldWaitForPolicy(true);
91 PolicyProviderAndroidTestFixture provider
;
92 EXPECT_FALSE(provider
.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
94 scoped_ptr
<PolicyBundle
> policy_bundle(new PolicyBundle
);
95 SetPolicy(policy_bundle
.get(), "key", "value");
96 PolicyBundle expected_policy_bundle
;
97 expected_policy_bundle
.CopyFrom(*policy_bundle
);
98 provider
.Get()->SetPolicies(policy_bundle
.Pass());
99 EXPECT_TRUE(provider
.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
100 EXPECT_TRUE(provider
.Get()->policies().Equals(expected_policy_bundle
));
103 TEST_F(PolicyProviderAndroidTest
, RefreshPolicies
) {
104 MockPolicyProviderAndroidDelegate delegate
;
105 PolicyProviderAndroidTestFixture provider
;
107 provider
.Get()->SetDelegate(&delegate
);
109 scoped_ptr
<PolicyBundle
> policy_bundle(new PolicyBundle
);
110 SetPolicy(policy_bundle
.get(), "key", "old_value");
111 PolicyBundle expected_policy_bundle
;
112 expected_policy_bundle
.CopyFrom(*policy_bundle
);
113 provider
.Get()->SetPolicies(policy_bundle
.Pass());
114 EXPECT_TRUE(provider
.Get()->policies().Equals(expected_policy_bundle
));
116 EXPECT_CALL(delegate
, RefreshPolicies()).Times(1);
117 provider
.Get()->RefreshPolicies();
119 policy_bundle
.reset(new PolicyBundle
);
120 SetPolicy(policy_bundle
.get(), "key", "new_value");
121 expected_policy_bundle
.CopyFrom(*policy_bundle
);
122 provider
.Get()->SetPolicies(policy_bundle
.Pass());
123 EXPECT_TRUE(provider
.Get()->policies().Equals(expected_policy_bundle
));
125 EXPECT_CALL(delegate
, PolicyProviderShutdown()).Times(1);
128 } // namespace policy