Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/browser/policy/cloud_policy_provider_unittest.cc

Issue 8586030: Added ConfigurationPolicyProvider::RefreshPolicies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed LoginUtilsTest Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/cloud_policy_provider.h" 5 #include "chrome/browser/policy/cloud_policy_provider.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/browser_process.h"
Mattias Nissler (ping if slow) 2011/11/21 16:09:05 needed?
Joao da Silva 2011/11/21 16:49:21 Not after fixing the g_browser_process below.
10 #include "chrome/browser/policy/browser_policy_connector.h"
9 #include "chrome/browser/policy/cloud_policy_cache_base.h" 11 #include "chrome/browser/policy/cloud_policy_cache_base.h"
10 #include "chrome/browser/policy/cloud_policy_provider_impl.h" 12 #include "chrome/browser/policy/cloud_policy_provider_impl.h"
11 #include "chrome/browser/policy/configuration_policy_pref_store.h" 13 #include "chrome/browser/policy/configuration_policy_pref_store.h"
14 #include "chrome/browser/policy/configuration_policy_provider.h"
15 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
12 #include "policy/policy_constants.h" 16 #include "policy/policy_constants.h"
13 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
14 18
15 using testing::AnyNumber; 19 using testing::AnyNumber;
20 using testing::Mock;
21 using testing::StrictMock;
16 using testing::_; 22 using testing::_;
17 23
18 namespace policy { 24 namespace policy {
19 25
20 class MockCloudPolicyCache : public CloudPolicyCacheBase { 26 class MockCloudPolicyCache : public CloudPolicyCacheBase {
21 public: 27 public:
22 MockCloudPolicyCache() {} 28 MockCloudPolicyCache() {}
23 virtual ~MockCloudPolicyCache() {} 29 virtual ~MockCloudPolicyCache() {}
24 30
25 // CloudPolicyCacheBase implementation. 31 // CloudPolicyCacheBase implementation.
26 void Load() {} 32 void Load() OVERRIDE {}
27 void SetPolicy(const em::PolicyFetchResponse& policy) {} 33 void SetPolicy(const em::PolicyFetchResponse& policy) OVERRIDE {}
28 bool DecodePolicyData(const em::PolicyData& policy_data, 34 bool DecodePolicyData(const em::PolicyData& policy_data,
29 PolicyMap* mandatory, 35 PolicyMap* mandatory,
30 PolicyMap* recommended) { 36 PolicyMap* recommended) OVERRIDE {
31 return true;
32 }
33 bool IsReady() {
34 return true; 37 return true;
35 } 38 }
36 39
40 void SetUnmanaged() OVERRIDE {
41 is_unmanaged_ = true;
42 }
43
37 // Non-const accessors for underlying PolicyMaps. 44 // Non-const accessors for underlying PolicyMaps.
38 PolicyMap* raw_mandatory_policy() { 45 PolicyMap* raw_mandatory_policy() {
39 return &mandatory_policy_; 46 return &mandatory_policy_;
40 } 47 }
41 48
42 PolicyMap* raw_recommended_policy() { 49 PolicyMap* raw_recommended_policy() {
43 return &recommended_policy_; 50 return &recommended_policy_;
44 } 51 }
45 52
46 void SetUnmanaged() {
47 is_unmanaged_ = true;
48 }
49
50 void SetFetchingDone() {
51 // Implement pure virtual method.
52 }
53
54 void set_initialized(bool initialized) { 53 void set_initialized(bool initialized) {
55 initialization_complete_ = initialized; 54 initialization_complete_ = initialized;
56 } 55 }
57 56
58 private: 57 private:
59 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache); 58 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache);
60 }; 59 };
61 60
62 class CloudPolicyProviderTest : public testing::Test { 61 class CloudPolicyProviderTest : public testing::Test {
63 protected: 62 protected:
64 void CreateCloudPolicyProvider(CloudPolicyCacheBase::PolicyLevel level) { 63 void CreateCloudPolicyProvider(CloudPolicyCacheBase::PolicyLevel level) {
65 cloud_policy_provider_.reset(new CloudPolicyProviderImpl( 64 cloud_policy_provider_.reset(
66 GetChromePolicyDefinitionList(), level)); 65 new CloudPolicyProviderImpl(
66 &browser_policy_connector_,
67 GetChromePolicyDefinitionList(),
68 level));
67 } 69 }
68 70
69 // Appends the caches to a provider and then provides the policies to 71 // Appends the caches to a provider and then provides the policies to
70 // |policy_map_|. 72 // |policy_map_|.
71 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n, 73 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n,
72 CloudPolicyCacheBase::PolicyLevel level) { 74 CloudPolicyCacheBase::PolicyLevel level) {
73 CloudPolicyProviderImpl provider( 75 CloudPolicyProviderImpl provider(
76 g_browser_process->browser_policy_connector(),
Mattias Nissler (ping if slow) 2011/11/21 16:09:05 Wait, shouldn't you use &browser_policy_connector_
Joao da Silva 2011/11/21 16:49:21 Yep, sloppy me. Fixed.
74 GetChromePolicyDefinitionList(), 77 GetChromePolicyDefinitionList(),
75 level); 78 level);
76 for (int i = 0; i < n; i++) { 79 for (int i = 0; i < n; i++) {
77 provider.AppendCache(&caches[i]); 80 provider.AppendCache(&caches[i]);
78 } 81 }
79 policy_map_.reset(new PolicyMap()); 82 policy_map_.reset(new PolicyMap());
80 provider.Provide(policy_map_.get()); 83 provider.Provide(policy_map_.get());
81 } 84 }
82 85
83 // Checks a string policy in |policy_map_|. 86 // Checks a string policy in |policy_map_|.
(...skipping 19 matching lines...) Expand all
103 EXPECT_TRUE(NULL == policy_map_->Get(type)); 106 EXPECT_TRUE(NULL == policy_map_->Get(type));
104 } 107 }
105 108
106 void CombineTwoPolicyMaps(const PolicyMap& base, 109 void CombineTwoPolicyMaps(const PolicyMap& base,
107 const PolicyMap& overlay, 110 const PolicyMap& overlay,
108 PolicyMap* out_map) { 111 PolicyMap* out_map) {
109 DCHECK(cloud_policy_provider_.get()); 112 DCHECK(cloud_policy_provider_.get());
110 cloud_policy_provider_->CombineTwoPolicyMaps(base, overlay, out_map); 113 cloud_policy_provider_->CombineTwoPolicyMaps(base, overlay, out_map);
111 } 114 }
112 115
116 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_;
117
113 private: 118 private:
114 // Some tests need a list of policies that doesn't contain any proxy 119 BrowserPolicyConnector browser_policy_connector_;
115 // policies. Note: these policies will be handled as if they had the
116 // type of Value::TYPE_INTEGER.
117 static const ConfigurationPolicyType simple_policies[];
118
119 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_;
120 scoped_ptr<PolicyMap> policy_map_; 120 scoped_ptr<PolicyMap> policy_map_;
121 }; 121 };
122 122
123 // Proxy setting distributed over multiple caches. 123 // Proxy setting distributed over multiple caches.
124 TEST_F(CloudPolicyProviderTest, 124 TEST_F(CloudPolicyProviderTest,
125 ProxySettingDistributedOverMultipleCaches) { 125 ProxySettingDistributedOverMultipleCaches) {
126 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by 126 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by
127 // one instance of CloudPolicyProvider. The first cache has some policies but 127 // one instance of CloudPolicyProvider. The first cache has some policies but
128 // no proxy-related ones. The following caches have each one proxy-policy set. 128 // no proxy-related ones. The following caches have each one proxy-policy set.
129 const int n = 6; 129 const int n = 6;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 B.Set(kPolicyProxyServer, Value::CreateIntegerValue(b_value)); 233 B.Set(kPolicyProxyServer, Value::CreateIntegerValue(b_value));
234 B.Set(kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value)); 234 B.Set(kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value));
235 B.Set(kPolicyProxyBypassList, Value::CreateIntegerValue(b_value)); 235 B.Set(kPolicyProxyBypassList, Value::CreateIntegerValue(b_value));
236 236
237 CombineTwoPolicyMaps(A, B, &C); 237 CombineTwoPolicyMaps(A, B, &C);
238 238
239 EXPECT_TRUE(A.Equals(C)); 239 EXPECT_TRUE(A.Equals(C));
240 EXPECT_FALSE(B.Equals(C)); 240 EXPECT_FALSE(B.Equals(C));
241 } 241 }
242 242
243 TEST_F(CloudPolicyProviderTest, RefreshPolicies) {
244 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY);
245 StrictMock<MockConfigurationPolicyObserver> observer;
Mattias Nissler (ping if slow) 2011/11/21 16:09:05 Using a StrictMock to ensure that no other events
Joao da Silva 2011/11/21 16:49:21 Removed. This was introduced when the test compla
246 ConfigurationPolicyObserverRegistrar registrar;
247 registrar.Init(cloud_policy_provider_.get(), &observer);
248
249 // OnUpdatePolicy is called when the provider doesn't have any caches.
250 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
251 cloud_policy_provider_->RefreshPolicies();
252 Mock::VerifyAndClearExpectations(&observer);
253
254 // OnUpdatePolicy is called when all the caches have updated.
255 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(2);
256 MockCloudPolicyCache cache0;
257 MockCloudPolicyCache cache1;
258 MockCloudPolicyCache cache2;
Mattias Nissler (ping if slow) 2011/11/21 16:09:05 It's a bit weird to have the cache2 declaration he
Joao da Silva 2011/11/21 16:49:21 Done.
259 cloud_policy_provider_->AppendCache(&cache0);
260 cloud_policy_provider_->AppendCache(&cache1);
261 Mock::VerifyAndClearExpectations(&observer);
262
263 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
264 cloud_policy_provider_->RefreshPolicies();
265 Mock::VerifyAndClearExpectations(&observer);
266
267 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
268 // Updating just one of the caches is not enough.
269 cloud_policy_provider_->OnCacheUpdate(&cache0);
270 Mock::VerifyAndClearExpectations(&observer);
271
272 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
273 // This cache wasn't available when RefreshPolicies was called, so it isn't
274 // required to fire the update.
Mattias Nissler (ping if slow) 2011/11/21 16:09:05 Is that intended? Either way, it probably doesn't
Joao da Silva 2011/11/21 16:49:21 It doesn't matter indeed, this comment is for clar
275 cloud_policy_provider_->AppendCache(&cache2);
276 Mock::VerifyAndClearExpectations(&observer);
277
278 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
279 cloud_policy_provider_->OnCacheUpdate(&cache1);
280 Mock::VerifyAndClearExpectations(&observer);
281
282 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
283 cloud_policy_provider_->RefreshPolicies();
284 Mock::VerifyAndClearExpectations(&observer);
285
286 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
287 cloud_policy_provider_->OnCacheUpdate(&cache0);
288 cloud_policy_provider_->OnCacheUpdate(&cache1);
289 Mock::VerifyAndClearExpectations(&observer);
290
291 // If a cache refreshes more than once, the provider should still waits for
Mattias Nissler (ping if slow) 2011/11/21 16:09:05 s/waits/wait/
Joao da Silva 2011/11/21 16:49:21 Done.
292 // the others before firing the update.
293 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
294 cloud_policy_provider_->OnCacheUpdate(&cache0);
295 Mock::VerifyAndClearExpectations(&observer);
296
297 // Fire updates if one of the required caches goes away while waiting.
298 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
299 cloud_policy_provider_->OnCacheGoingAway(&cache2);
300 Mock::VerifyAndClearExpectations(&observer);
301
302 // The other 2 caches are removed (and trigger updates) at shutdown.
303 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(2);
304 }
305
243 } // namespace policy 306 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698