OLD | NEW |
---|---|
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/user_policy_cache.h" | 5 #include "chrome/browser/policy/user_policy_cache.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 // Decodes a CloudPolicySettings object into two maps with mandatory and | 25 // Decodes a CloudPolicySettings object into two maps with mandatory and |
26 // recommended settings, respectively. The implementation is generated code | 26 // recommended settings, respectively. The implementation is generated code |
27 // in policy/cloud_policy_generated.cc. | 27 // in policy/cloud_policy_generated.cc. |
28 void DecodePolicy(const em::CloudPolicySettings& policy, | 28 void DecodePolicy(const em::CloudPolicySettings& policy, |
29 PolicyMap* mandatory, PolicyMap* recommended); | 29 PolicyMap* mandatory, PolicyMap* recommended); |
30 | 30 |
31 // The implementations of these methods are in cloud_policy_generated.cc. | 31 // The implementations of these methods are in cloud_policy_generated.cc. |
32 Value* DecodeIntegerValue(google::protobuf::int64 value); | 32 Value* DecodeIntegerValue(google::protobuf::int64 value); |
33 ListValue* DecodeStringList(const em::StringList& string_list); | 33 ListValue* DecodeStringList(const em::StringList& string_list); |
34 | 34 |
35 class MockConfigurationPolicyProviderObserver | 35 class MockCloudPolicyCacheBaseObserver |
36 : public ConfigurationPolicyProvider::Observer { | 36 : public CloudPolicyCacheBase::Observer { |
37 public: | 37 public: |
38 MockConfigurationPolicyProviderObserver() {} | 38 MockCloudPolicyCacheBaseObserver() {} |
39 virtual ~MockConfigurationPolicyProviderObserver() {} | 39 virtual ~MockCloudPolicyCacheBaseObserver() {} |
40 MOCK_METHOD0(OnUpdatePolicy, void()); | 40 MOCK_METHOD1(OnCacheUpdate, void(CloudPolicyCacheBase*)); |
41 void OnProviderGoingAway() {} | 41 void OnCacheGoingAway(CloudPolicyCacheBase*) {} |
42 }; | 42 }; |
43 | 43 |
44 // Tests the device management policy cache. | 44 // Tests the device management policy cache. |
45 class UserPolicyCacheTest : public testing::Test { | 45 class UserPolicyCacheTest : public testing::Test { |
46 protected: | 46 protected: |
47 UserPolicyCacheTest() | 47 UserPolicyCacheTest() |
48 : loop_(MessageLoop::TYPE_UI), | 48 : loop_(MessageLoop::TYPE_UI), |
49 ui_thread_(BrowserThread::UI, &loop_), | 49 ui_thread_(BrowserThread::UI, &loop_), |
50 file_thread_(BrowserThread::FILE, &loop_) {} | 50 file_thread_(BrowserThread::FILE, &loop_) {} |
51 | 51 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 EXPECT_TRUE(cached_policy.SerializeToString(&data)); | 96 EXPECT_TRUE(cached_policy.SerializeToString(&data)); |
97 int size = static_cast<int>(data.size()); | 97 int size = static_cast<int>(data.size()); |
98 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size)); | 98 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size)); |
99 } | 99 } |
100 | 100 |
101 // Takes ownership of |policy_response|. | 101 // Takes ownership of |policy_response|. |
102 void SetPolicy(UserPolicyCache* cache, | 102 void SetPolicy(UserPolicyCache* cache, |
103 em::PolicyFetchResponse* policy_response, | 103 em::PolicyFetchResponse* policy_response, |
104 bool expect_changed_policy) { | 104 bool expect_changed_policy) { |
105 scoped_ptr<em::PolicyFetchResponse> policy(policy_response); | 105 scoped_ptr<em::PolicyFetchResponse> policy(policy_response); |
106 ConfigurationPolicyObserverRegistrar registrar; | 106 cache->AddObserver(&observer); |
107 registrar.Init(cache->GetManagedPolicyProvider(), &observer); | 107 |
108 if (expect_changed_policy) | 108 if (expect_changed_policy) |
109 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | 109 EXPECT_CALL(observer, OnCacheUpdate(testing::_)).Times(1); |
Mattias Nissler (ping if slow)
2011/06/09 14:36:26
using testing::_ at the top of the file.
sfeuz
2011/06/13 06:53:53
Done.
| |
110 else | 110 else |
111 EXPECT_CALL(observer, OnUpdatePolicy()).Times(0); | 111 EXPECT_CALL(observer, OnCacheUpdate(testing::_)).Times(0); |
112 cache->SetPolicy(*policy); | 112 cache->SetPolicy(*policy); |
113 testing::Mock::VerifyAndClearExpectations(&observer); | 113 testing::Mock::VerifyAndClearExpectations(&observer); |
114 | |
115 cache->RemoveObserver(&observer); | |
114 } | 116 } |
115 | 117 |
116 FilePath test_file() { | 118 FilePath test_file() { |
117 return temp_dir_.path().AppendASCII("UserPolicyCacheTest"); | 119 return temp_dir_.path().AppendASCII("UserPolicyCacheTest"); |
118 } | 120 } |
119 | 121 |
120 const PolicyMap& mandatory_policy(const UserPolicyCache& cache) { | 122 const PolicyMap& mandatory_policy(const UserPolicyCache& cache) { |
121 return cache.mandatory_policy_; | 123 return cache.mandatory_policy_; |
122 } | 124 } |
123 | 125 |
124 const PolicyMap& recommended_policy(const UserPolicyCache& cache) { | 126 const PolicyMap& recommended_policy(const UserPolicyCache& cache) { |
125 return cache.recommended_policy_; | 127 return cache.recommended_policy_; |
126 } | 128 } |
127 | 129 |
128 MessageLoop loop_; | 130 MessageLoop loop_; |
129 MockConfigurationPolicyProviderObserver observer; | 131 MockCloudPolicyCacheBaseObserver observer; |
130 | 132 |
131 private: | 133 private: |
132 ScopedTempDir temp_dir_; | 134 ScopedTempDir temp_dir_; |
133 BrowserThread ui_thread_; | 135 BrowserThread ui_thread_; |
134 BrowserThread file_thread_; | 136 BrowserThread file_thread_; |
135 }; | 137 }; |
136 | 138 |
137 TEST_F(UserPolicyCacheTest, DecodePolicy) { | 139 TEST_F(UserPolicyCacheTest, DecodePolicy) { |
138 em::CloudPolicySettings settings; | 140 em::CloudPolicySettings settings; |
139 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org"); | 141 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org"); |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 // If new-style policy comes in, it should override old-style policy. | 375 // If new-style policy comes in, it should override old-style policy. |
374 policy = CreateHomepagePolicy("http://www.example.com", | 376 policy = CreateHomepagePolicy("http://www.example.com", |
375 base::Time::NowFromSystemTime(), | 377 base::Time::NowFromSystemTime(), |
376 em::PolicyOptions::RECOMMENDED); | 378 em::PolicyOptions::RECOMMENDED); |
377 SetPolicy(&cache, policy, true); | 379 SetPolicy(&cache, policy, true); |
378 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); | 380 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); |
379 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); | 381 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
380 } | 382 } |
381 | 383 |
382 } // namespace policy | 384 } // namespace policy |
OLD | NEW |