OLD | NEW |
---|---|
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" | |
8 #include "base/bind_helpers.h" | |
7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
8 #include "base/values.h" | 10 #include "base/values.h" |
9 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
12 | 14 |
13 using ::testing::AnyNumber; | 15 using ::testing::AnyNumber; |
14 using ::testing::Mock; | 16 using ::testing::Mock; |
15 using ::testing::_; | 17 using ::testing::_; |
16 | 18 |
17 namespace policy { | 19 namespace policy { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 class MockPolicyServiceObserver : public PolicyService::Observer { | 23 class MockPolicyServiceObserver : public PolicyService::Observer { |
22 public: | 24 public: |
23 virtual ~MockPolicyServiceObserver() {} | 25 virtual ~MockPolicyServiceObserver() {} |
24 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, | 26 MOCK_METHOD4(OnPolicyUpdated, void(PolicyDomain, |
25 const std::string&, | 27 const std::string&, |
26 const PolicyMap& previous, | 28 const PolicyMap& previous, |
27 const PolicyMap& current)); | 29 const PolicyMap& current)); |
28 }; | 30 }; |
29 | 31 |
32 class MockPolicyValueObserver { | |
33 public: | |
34 ~MockPolicyValueObserver() {} | |
35 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*, | |
Mattias Nissler (ping if slow)
2012/04/23 17:14:18
You could put that mock method on the Test class f
Joao da Silva
2012/04/23 17:29:26
Done.
| |
36 const base::Value*)); | |
37 }; | |
38 | |
30 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with | 39 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with |
31 // their expected values. | 40 // their expected values. |
32 MATCHER_P(PolicyEquals, expected, "") { | 41 MATCHER_P(PolicyEquals, expected, "") { |
33 return arg.Equals(*expected); | 42 return arg.Equals(*expected); |
34 } | 43 } |
35 | 44 |
45 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated() | |
46 // with their expected values. | |
47 MATCHER_P(ValueEquals, expected, "") { | |
48 return base::Value::Equals(arg, expected); | |
49 } | |
50 | |
36 } // namespace | 51 } // namespace |
37 | 52 |
38 class PolicyServiceTest : public testing::Test { | 53 class PolicyServiceTest : public testing::Test { |
39 public: | 54 public: |
40 PolicyServiceTest() {} | 55 PolicyServiceTest() {} |
41 | 56 |
42 void SetUp() OVERRIDE { | 57 void SetUp() OVERRIDE { |
43 provider0_.AddMandatoryPolicy("pre", base::Value::CreateIntegerValue(13)); | 58 provider0_.AddMandatoryPolicy("pre", base::Value::CreateIntegerValue(13)); |
44 provider0_.SetInitializationComplete(true); | 59 provider0_.SetInitializationComplete(true); |
45 provider1_.SetInitializationComplete(true); | 60 provider1_.SetInitializationComplete(true); |
46 provider2_.SetInitializationComplete(true); | 61 provider2_.SetInitializationComplete(true); |
47 PolicyServiceImpl::Providers providers; | 62 PolicyServiceImpl::Providers providers; |
48 providers.push_back(&provider0_); | 63 providers.push_back(&provider0_); |
49 providers.push_back(&provider1_); | 64 providers.push_back(&provider1_); |
50 providers.push_back(&provider2_); | 65 providers.push_back(&provider2_); |
51 policy_service_.reset(new PolicyServiceImpl(providers)); | 66 policy_service_.reset(new PolicyServiceImpl(providers)); |
52 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, "", &observer_); | |
53 } | |
54 | |
55 void TearDown() OVERRIDE { | |
56 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer_); | |
57 } | 67 } |
58 | 68 |
59 // Returns true if the policies for |domain|, |component_id| match |expected|. | 69 // Returns true if the policies for |domain|, |component_id| match |expected|. |
60 bool VerifyPolicies(PolicyDomain domain, | 70 bool VerifyPolicies(PolicyDomain domain, |
61 const std::string& component_id, | 71 const std::string& component_id, |
62 const PolicyMap& expected) { | 72 const PolicyMap& expected) { |
63 const PolicyMap* policies = | 73 const PolicyMap* policies = |
64 policy_service_->GetPolicies(domain, component_id); | 74 policy_service_->GetPolicies(domain, component_id); |
65 return policies && policies->Equals(expected); | 75 return policies && policies->Equals(expected); |
66 } | 76 } |
67 | 77 |
68 protected: | 78 protected: |
69 MockConfigurationPolicyProvider provider0_; | 79 MockConfigurationPolicyProvider provider0_; |
70 MockConfigurationPolicyProvider provider1_; | 80 MockConfigurationPolicyProvider provider1_; |
71 MockConfigurationPolicyProvider provider2_; | 81 MockConfigurationPolicyProvider provider2_; |
72 scoped_ptr<PolicyServiceImpl> policy_service_; | 82 scoped_ptr<PolicyServiceImpl> policy_service_; |
73 MockPolicyServiceObserver observer_; | |
74 | 83 |
75 private: | 84 private: |
76 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest); | 85 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest); |
77 }; | 86 }; |
78 | 87 |
79 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) { | 88 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) { |
80 PolicyMap expected; | 89 PolicyMap expected; |
81 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 90 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
82 base::Value::CreateIntegerValue(13)); | 91 base::Value::CreateIntegerValue(13)); |
83 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 92 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
84 } | 93 } |
85 | 94 |
86 TEST_F(PolicyServiceTest, NotifyObservers) { | 95 TEST_F(PolicyServiceTest, NotifyObservers) { |
96 MockPolicyServiceObserver observer; | |
97 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, "", &observer); | |
98 | |
87 PolicyMap expectedPrevious; | 99 PolicyMap expectedPrevious; |
88 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 100 expectedPrevious.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
89 base::Value::CreateIntegerValue(13)); | 101 base::Value::CreateIntegerValue(13)); |
90 | 102 |
91 PolicyMap expectedCurrent; | 103 PolicyMap expectedCurrent; |
92 expectedCurrent.CopyFrom(expectedPrevious); | 104 expectedCurrent.CopyFrom(expectedPrevious); |
93 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 105 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
94 base::Value::CreateIntegerValue(123)); | 106 base::Value::CreateIntegerValue(123)); |
95 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(123)); | 107 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(123)); |
96 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 108 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
97 PolicyEquals(&expectedPrevious), | 109 PolicyEquals(&expectedPrevious), |
98 PolicyEquals(&expectedCurrent))); | 110 PolicyEquals(&expectedCurrent))); |
99 provider0_.RefreshPolicies(); | 111 provider0_.RefreshPolicies(); |
100 Mock::VerifyAndClearExpectations(&observer_); | 112 Mock::VerifyAndClearExpectations(&observer); |
101 | 113 |
102 // No changes. | 114 // No changes. |
103 EXPECT_CALL(observer_, OnPolicyUpdated(_, _, _, _)).Times(0); | 115 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
104 provider0_.RefreshPolicies(); | 116 provider0_.RefreshPolicies(); |
105 Mock::VerifyAndClearExpectations(&observer_); | 117 Mock::VerifyAndClearExpectations(&observer); |
106 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 118 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
107 | 119 |
108 // New policy. | 120 // New policy. |
109 expectedPrevious.CopyFrom(expectedCurrent); | 121 expectedPrevious.CopyFrom(expectedCurrent); |
110 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 122 expectedCurrent.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
111 base::Value::CreateIntegerValue(456)); | 123 base::Value::CreateIntegerValue(456)); |
112 provider0_.AddMandatoryPolicy("bbb", base::Value::CreateIntegerValue(456)); | 124 provider0_.AddMandatoryPolicy("bbb", base::Value::CreateIntegerValue(456)); |
113 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 125 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
114 PolicyEquals(&expectedPrevious), | 126 PolicyEquals(&expectedPrevious), |
115 PolicyEquals(&expectedCurrent))); | 127 PolicyEquals(&expectedCurrent))); |
116 provider0_.RefreshPolicies(); | 128 provider0_.RefreshPolicies(); |
117 Mock::VerifyAndClearExpectations(&observer_); | 129 Mock::VerifyAndClearExpectations(&observer); |
118 | 130 |
119 // Removed policy. | 131 // Removed policy. |
120 expectedPrevious.CopyFrom(expectedCurrent); | 132 expectedPrevious.CopyFrom(expectedCurrent); |
121 expectedCurrent.Erase("bbb"); | 133 expectedCurrent.Erase("bbb"); |
122 provider0_.RemovePolicy("bbb"); | 134 provider0_.RemovePolicy("bbb"); |
123 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 135 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
124 PolicyEquals(&expectedPrevious), | 136 PolicyEquals(&expectedPrevious), |
125 PolicyEquals(&expectedCurrent))); | 137 PolicyEquals(&expectedCurrent))); |
126 provider0_.RefreshPolicies(); | 138 provider0_.RefreshPolicies(); |
127 Mock::VerifyAndClearExpectations(&observer_); | 139 Mock::VerifyAndClearExpectations(&observer); |
128 | 140 |
129 // Changed policy. | 141 // Changed policy. |
130 expectedPrevious.CopyFrom(expectedCurrent); | 142 expectedPrevious.CopyFrom(expectedCurrent); |
131 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 143 expectedCurrent.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
132 base::Value::CreateIntegerValue(789)); | 144 base::Value::CreateIntegerValue(789)); |
133 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(789)); | 145 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(789)); |
134 EXPECT_CALL(observer_, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", | 146 EXPECT_CALL(observer, OnPolicyUpdated(POLICY_DOMAIN_CHROME, "", |
135 PolicyEquals(&expectedPrevious), | 147 PolicyEquals(&expectedPrevious), |
136 PolicyEquals(&expectedCurrent))); | 148 PolicyEquals(&expectedCurrent))); |
137 provider0_.RefreshPolicies(); | 149 provider0_.RefreshPolicies(); |
138 Mock::VerifyAndClearExpectations(&observer_); | 150 Mock::VerifyAndClearExpectations(&observer); |
139 | 151 |
140 // No changes again. | 152 // No changes again. |
141 EXPECT_CALL(observer_, OnPolicyUpdated(_, _, _, _)).Times(0); | 153 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _, _)).Times(0); |
142 provider0_.RefreshPolicies(); | 154 provider0_.RefreshPolicies(); |
143 Mock::VerifyAndClearExpectations(&observer_); | 155 Mock::VerifyAndClearExpectations(&observer); |
144 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); | 156 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expectedCurrent)); |
157 | |
158 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, "", &observer); | |
145 } | 159 } |
146 | 160 |
147 TEST_F(PolicyServiceTest, Priorities) { | 161 TEST_F(PolicyServiceTest, Priorities) { |
148 PolicyMap expected; | 162 PolicyMap expected; |
149 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 163 expected.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
150 base::Value::CreateIntegerValue(13)); | 164 base::Value::CreateIntegerValue(13)); |
151 EXPECT_CALL(observer_, OnPolicyUpdated(_, _, _, _)).Times(AnyNumber()); | |
152 | |
153 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 165 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
154 base::Value::CreateIntegerValue(0)); | 166 base::Value::CreateIntegerValue(0)); |
155 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(0)); | 167 provider0_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(0)); |
156 provider1_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(1)); | 168 provider1_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(1)); |
157 provider2_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(2)); | 169 provider2_.AddMandatoryPolicy("aaa", base::Value::CreateIntegerValue(2)); |
158 provider0_.RefreshPolicies(); | 170 provider0_.RefreshPolicies(); |
159 provider1_.RefreshPolicies(); | 171 provider1_.RefreshPolicies(); |
160 provider2_.RefreshPolicies(); | 172 provider2_.RefreshPolicies(); |
161 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 173 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
162 | 174 |
163 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 175 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
164 base::Value::CreateIntegerValue(1)); | 176 base::Value::CreateIntegerValue(1)); |
165 provider0_.RemovePolicy("aaa"); | 177 provider0_.RemovePolicy("aaa"); |
166 provider0_.RefreshPolicies(); | 178 provider0_.RefreshPolicies(); |
167 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 179 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
168 | 180 |
169 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 181 expected.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
170 base::Value::CreateIntegerValue(2)); | 182 base::Value::CreateIntegerValue(2)); |
171 provider1_.AddRecommendedPolicy("aaa", base::Value::CreateIntegerValue(1)); | 183 provider1_.AddRecommendedPolicy("aaa", base::Value::CreateIntegerValue(1)); |
172 provider1_.RefreshPolicies(); | 184 provider1_.RefreshPolicies(); |
173 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); | 185 EXPECT_TRUE(VerifyPolicies(POLICY_DOMAIN_CHROME, "", expected)); |
174 } | 186 } |
175 | 187 |
188 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) { | |
189 MockPolicyValueObserver observer; | |
190 scoped_ptr<PolicyChangeRegistrar> registrar( | |
191 new PolicyChangeRegistrar( | |
192 policy_service_.get(), POLICY_DOMAIN_CHROME, "")); | |
193 | |
194 // Starting to observe existing policies doesn't trigger a notification. | |
195 EXPECT_CALL(observer, OnPolicyValueUpdated(_, _)).Times(0); | |
196 registrar->Observe("pre", base::Bind( | |
197 &MockPolicyValueObserver::OnPolicyValueUpdated, | |
198 base::Unretained(&observer))); | |
199 registrar->Observe("aaa", base::Bind( | |
200 &MockPolicyValueObserver::OnPolicyValueUpdated, | |
201 base::Unretained(&observer))); | |
202 Mock::VerifyAndClearExpectations(&observer); | |
203 | |
204 // Changing it now triggers a notification. | |
205 base::FundamentalValue kValue0(0); | |
206 EXPECT_CALL(observer, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0))); | |
207 provider0_.AddMandatoryPolicy("aaa", kValue0.DeepCopy()); | |
208 provider0_.RefreshPolicies(); | |
209 Mock::VerifyAndClearExpectations(&observer); | |
210 | |
211 // Changing other values doesn't trigger a notification. | |
212 EXPECT_CALL(observer, OnPolicyValueUpdated(_, _)).Times(0); | |
213 provider0_.AddMandatoryPolicy("bbb", kValue0.DeepCopy()); | |
214 provider0_.RefreshPolicies(); | |
215 Mock::VerifyAndClearExpectations(&observer); | |
216 | |
217 // Modifying the value triggers a notification. | |
218 base::FundamentalValue kValue1(1); | |
219 EXPECT_CALL(observer, OnPolicyValueUpdated(ValueEquals(&kValue0), | |
220 ValueEquals(&kValue1))); | |
221 provider0_.AddMandatoryPolicy("aaa", kValue1.DeepCopy()); | |
222 provider0_.RefreshPolicies(); | |
223 Mock::VerifyAndClearExpectations(&observer); | |
224 | |
225 // Removing the value triggers a notification. | |
226 EXPECT_CALL(observer, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL)); | |
227 provider0_.RemovePolicy("aaa"); | |
228 provider0_.RefreshPolicies(); | |
229 Mock::VerifyAndClearExpectations(&observer); | |
230 | |
231 // No more notifications after destroying the registrar. | |
232 EXPECT_CALL(observer, OnPolicyValueUpdated(_, _)).Times(0); | |
233 registrar.reset(); | |
234 provider0_.AddMandatoryPolicy("aaa", kValue1.DeepCopy()); | |
235 provider0_.AddMandatoryPolicy("pre", kValue1.DeepCopy()); | |
236 provider0_.RefreshPolicies(); | |
237 Mock::VerifyAndClearExpectations(&observer); | |
238 } | |
239 | |
176 } // namespace policy | 240 } // namespace policy |
OLD | NEW |