OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/policy/core/common/async_policy_provider.h" | 5 #include "components/policy/core/common/async_policy_provider.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 const std::string& name, | 29 const std::string& name, |
30 const std::string& value) { | 30 const std::string& value) { |
31 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 31 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
32 .Set(name, | 32 .Set(name, |
33 POLICY_LEVEL_MANDATORY, | 33 POLICY_LEVEL_MANDATORY, |
34 POLICY_SCOPE_USER, | 34 POLICY_SCOPE_USER, |
35 new base::StringValue(value), | 35 new base::StringValue(value), |
36 NULL); | 36 NULL); |
37 } | 37 } |
38 | 38 |
39 void SetPlatformPolicy(PolicyBundle* bundle, | |
Thiemo Nagel
2015/09/04 20:15:53
Is it really necessary to have a separate function
fhorschig
2015/09/07 14:09:34
No. Removed.
| |
40 const std::string& name, | |
41 const std::string& value) { | |
42 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | |
43 .SetWithSource(name, | |
44 POLICY_LEVEL_MANDATORY, | |
45 POLICY_SCOPE_USER, | |
46 new base::StringValue(value), | |
47 NULL, | |
48 POLICY_SOURCE_PLATFORM); | |
49 } | |
50 | |
39 class MockPolicyLoader : public AsyncPolicyLoader { | 51 class MockPolicyLoader : public AsyncPolicyLoader { |
40 public: | 52 public: |
41 explicit MockPolicyLoader( | 53 explicit MockPolicyLoader( |
42 scoped_refptr<base::SequencedTaskRunner> task_runner); | 54 scoped_refptr<base::SequencedTaskRunner> task_runner); |
43 ~MockPolicyLoader() override; | 55 ~MockPolicyLoader() override; |
44 | 56 |
45 // Load() returns a scoped_ptr<PolicyBundle> but it can't be mocked because | 57 // Load() returns a scoped_ptr<PolicyBundle> but it can't be mocked because |
46 // scoped_ptr is moveable but not copyable. This override forwards the | 58 // scoped_ptr is moveable but not copyable. This override forwards the |
47 // call to MockLoad() which returns a PolicyBundle*, and returns a copy | 59 // call to MockLoad() which returns a PolicyBundle*, and returns a copy |
48 // wrapped in a passed scoped_ptr. | 60 // wrapped in a passed scoped_ptr. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 void AsyncPolicyProviderTest::TearDown() { | 132 void AsyncPolicyProviderTest::TearDown() { |
121 if (provider_) { | 133 if (provider_) { |
122 provider_->Shutdown(); | 134 provider_->Shutdown(); |
123 provider_.reset(); | 135 provider_.reset(); |
124 } | 136 } |
125 loop_.RunUntilIdle(); | 137 loop_.RunUntilIdle(); |
126 } | 138 } |
127 | 139 |
128 TEST_F(AsyncPolicyProviderTest, RefreshPolicies) { | 140 TEST_F(AsyncPolicyProviderTest, RefreshPolicies) { |
129 PolicyBundle refreshed_bundle; | 141 PolicyBundle refreshed_bundle; |
130 SetPolicy(&refreshed_bundle, "policy", "refreshed"); | 142 SetPlatformPolicy(&refreshed_bundle, "policy", "refreshed"); |
131 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle)); | 143 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle)); |
132 | 144 |
133 MockConfigurationPolicyObserver observer; | 145 MockConfigurationPolicyObserver observer; |
134 provider_->AddObserver(&observer); | 146 provider_->AddObserver(&observer); |
135 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 147 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
136 provider_->RefreshPolicies(); | 148 provider_->RefreshPolicies(); |
137 loop_.RunUntilIdle(); | 149 loop_.RunUntilIdle(); |
138 // The refreshed policies are now provided. | 150 // The refreshed policies are now provided. |
139 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 151 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
140 provider_->RemoveObserver(&observer); | 152 provider_->RemoveObserver(&observer); |
141 } | 153 } |
142 | 154 |
143 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) { | 155 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) { |
144 PolicyBundle refreshed_bundle; | 156 PolicyBundle refreshed_bundle; |
145 SetPolicy(&refreshed_bundle, "policy", "refreshed"); | 157 SetPlatformPolicy(&refreshed_bundle, "policy", "refreshed"); |
146 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle)); | 158 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle)); |
147 | 159 |
148 MockConfigurationPolicyObserver observer; | 160 MockConfigurationPolicyObserver observer; |
149 provider_->AddObserver(&observer); | 161 provider_->AddObserver(&observer); |
150 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 162 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
151 provider_->RefreshPolicies(); | 163 provider_->RefreshPolicies(); |
152 // Doesn't refresh before going through the background thread. | 164 // Doesn't refresh before going through the background thread. |
153 Mock::VerifyAndClearExpectations(&observer); | 165 Mock::VerifyAndClearExpectations(&observer); |
154 | 166 |
155 // Doesn't refresh if another RefreshPolicies request is made. | 167 // Doesn't refresh if another RefreshPolicies request is made. |
156 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 168 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
157 provider_->RefreshPolicies(); | 169 provider_->RefreshPolicies(); |
158 Mock::VerifyAndClearExpectations(&observer); | 170 Mock::VerifyAndClearExpectations(&observer); |
159 | 171 |
160 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); | 172 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1); |
161 loop_.RunUntilIdle(); | 173 loop_.RunUntilIdle(); |
162 // The refreshed policies are now provided. | 174 // The refreshed policies are now provided. |
163 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); | 175 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle)); |
164 Mock::VerifyAndClearExpectations(&observer); | 176 Mock::VerifyAndClearExpectations(&observer); |
165 provider_->RemoveObserver(&observer); | 177 provider_->RemoveObserver(&observer); |
166 } | 178 } |
167 | 179 |
168 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) { | 180 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) { |
169 PolicyBundle reloaded_bundle; | 181 PolicyBundle reloaded_bundle; |
170 SetPolicy(&reloaded_bundle, "policy", "reloaded"); | 182 SetPlatformPolicy(&reloaded_bundle, "policy", "reloaded"); |
171 PolicyBundle refreshed_bundle; | 183 PolicyBundle refreshed_bundle; |
172 SetPolicy(&refreshed_bundle, "policy", "refreshed"); | 184 SetPlatformPolicy(&refreshed_bundle, "policy", "refreshed"); |
173 | 185 |
174 Sequence load_sequence; | 186 Sequence load_sequence; |
175 // Reload. | 187 // Reload. |
176 EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence) | 188 EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence) |
177 .WillOnce(Return(&reloaded_bundle)); | 189 .WillOnce(Return(&reloaded_bundle)); |
178 // RefreshPolicies. | 190 // RefreshPolicies. |
179 EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence) | 191 EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence) |
180 .WillOnce(Return(&refreshed_bundle)); | 192 .WillOnce(Return(&refreshed_bundle)); |
181 | 193 |
182 MockConfigurationPolicyObserver observer; | 194 MockConfigurationPolicyObserver observer; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 229 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
218 provider_->Shutdown(); | 230 provider_->Shutdown(); |
219 loop_.RunUntilIdle(); | 231 loop_.RunUntilIdle(); |
220 Mock::VerifyAndClearExpectations(&observer); | 232 Mock::VerifyAndClearExpectations(&observer); |
221 | 233 |
222 provider_->RemoveObserver(&observer); | 234 provider_->RemoveObserver(&observer); |
223 provider_.reset(); | 235 provider_.reset(); |
224 } | 236 } |
225 | 237 |
226 } // namespace policy | 238 } // namespace policy |
OLD | NEW |