Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 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" | |
| 10 | |
| 11 namespace policy { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 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())) | |
| 20 .Set(name, | |
| 21 POLICY_LEVEL_MANDATORY, | |
| 22 POLICY_SCOPE_USER, | |
| 23 base::Value::CreateStringValue(value), | |
| 24 NULL); | |
| 25 } | |
| 26 | |
| 27 class MockPolicyProviderAndroidDelegate : public PolicyProviderAndroidDelegate { | |
| 28 public: | |
| 29 MockPolicyProviderAndroidDelegate() {} | |
| 30 ~MockPolicyProviderAndroidDelegate() {} | |
|
Joao da Silva
2014/02/06 15:38:27
virtual
Bernhard Bauer
2014/02/06 16:34:58
Done.
| |
| 31 | |
| 32 MOCK_METHOD0(RefreshPolicies, void()); | |
| 33 MOCK_METHOD0(PolicyProviderShutdown, void()); | |
| 34 | |
| 35 private: | |
| 36 DISALLOW_COPY_AND_ASSIGN(MockPolicyProviderAndroidDelegate); | |
| 37 }; | |
| 38 | |
| 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 { | |
| 43 public: | |
| 44 PolicyProviderAndroidTestFixture() {} | |
| 45 ~PolicyProviderAndroidTestFixture() { | |
| 46 provider_.Shutdown(); | |
| 47 } | |
| 48 | |
| 49 PolicyProviderAndroid* Get() { | |
| 50 return &provider_; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 PolicyProviderAndroid provider_; | |
| 55 DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTestFixture); | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 class PolicyProviderAndroidTest : public ::testing::Test { | |
| 61 protected: | |
| 62 PolicyProviderAndroidTest(); | |
| 63 virtual ~PolicyProviderAndroidTest(); | |
| 64 | |
| 65 virtual void SetUp() OVERRIDE; | |
| 66 virtual void TearDown() OVERRIDE; | |
| 67 | |
| 68 private: | |
| 69 MockPolicyProviderAndroidDelegate delegate_; | |
|
Joao da Silva
2014/02/06 15:38:27
not used
Bernhard Bauer
2014/02/06 16:34:58
Removed.
| |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTest); | |
| 72 }; | |
| 73 | |
| 74 PolicyProviderAndroidTest::PolicyProviderAndroidTest() {} | |
| 75 PolicyProviderAndroidTest::~PolicyProviderAndroidTest() {} | |
| 76 | |
| 77 void PolicyProviderAndroidTest::SetUp() {} | |
| 78 | |
| 79 void PolicyProviderAndroidTest::TearDown() { | |
| 80 PolicyProviderAndroid::SetShouldWaitForPolicy(false); | |
| 81 } | |
| 82 | |
| 83 TEST_F(PolicyProviderAndroidTest, InitializationCompleted) { | |
| 84 PolicyProviderAndroidTestFixture provider; | |
| 85 EXPECT_TRUE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 86 | |
| 87 const PolicyBundle kEmptyBundle; | |
| 88 EXPECT_TRUE(provider.Get()->policies().Equals(kEmptyBundle)); | |
| 89 } | |
| 90 | |
| 91 TEST_F(PolicyProviderAndroidTest, WaitForInitialization) { | |
| 92 PolicyProviderAndroid::SetShouldWaitForPolicy(true); | |
| 93 PolicyProviderAndroidTestFixture provider; | |
| 94 EXPECT_FALSE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 95 | |
| 96 scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle); | |
| 97 SetPolicy(policy_bundle.get(), "key", "value"); | |
| 98 PolicyBundle expected_policy_bundle; | |
| 99 expected_policy_bundle.CopyFrom(*policy_bundle); | |
| 100 provider.Get()->SetPolicies(policy_bundle.Pass()); | |
| 101 EXPECT_TRUE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
| 102 EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle)); | |
| 103 } | |
| 104 | |
| 105 TEST_F(PolicyProviderAndroidTest, RefreshPolicies) { | |
| 106 MockPolicyProviderAndroidDelegate delegate; | |
| 107 PolicyProviderAndroidTestFixture provider; | |
| 108 | |
| 109 provider.Get()->SetDelegate(&delegate); | |
| 110 | |
| 111 scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle); | |
| 112 SetPolicy(policy_bundle.get(), "key", "old_value"); | |
| 113 PolicyBundle expected_policy_bundle; | |
| 114 expected_policy_bundle.CopyFrom(*policy_bundle); | |
| 115 provider.Get()->SetPolicies(policy_bundle.Pass()); | |
| 116 EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle)); | |
| 117 | |
| 118 EXPECT_CALL(delegate, RefreshPolicies()).Times(1); | |
| 119 provider.Get()->RefreshPolicies(); | |
| 120 | |
| 121 policy_bundle.reset(new PolicyBundle); | |
| 122 SetPolicy(policy_bundle.get(), "key", "new_value"); | |
| 123 expected_policy_bundle.CopyFrom(*policy_bundle); | |
| 124 provider.Get()->SetPolicies(policy_bundle.Pass()); | |
| 125 EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle)); | |
| 126 | |
| 127 EXPECT_CALL(delegate, PolicyProviderShutdown()).Times(1); | |
| 128 } | |
| 129 | |
| 130 } // namespace policy | |
| OLD | NEW |