| 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_METHOD0(OnCacheUpdate, void()); |
| 41 void OnProviderGoingAway() {} | 41 void OnCacheGoingAway() {} |
| 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 |
| 107 registrar.Init(cache->GetManagedPolicyProvider(), &observer); | 107 cache->AddObserver(&observer); |
| 108 |
| 108 if (expect_changed_policy) | 109 if (expect_changed_policy) |
| 109 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | 110 EXPECT_CALL(observer, OnCacheUpdate()).Times(1); |
| 110 else | 111 else |
| 111 EXPECT_CALL(observer, OnUpdatePolicy()).Times(0); | 112 EXPECT_CALL(observer, OnCacheUpdate()).Times(0); |
| 112 cache->SetPolicy(*policy); | 113 cache->SetPolicy(*policy); |
| 113 testing::Mock::VerifyAndClearExpectations(&observer); | 114 testing::Mock::VerifyAndClearExpectations(&observer); |
| 115 |
| 116 cache->RemoveObserver(&observer); |
| 114 } | 117 } |
| 115 | 118 |
| 116 FilePath test_file() { | 119 FilePath test_file() { |
| 117 return temp_dir_.path().AppendASCII("UserPolicyCacheTest"); | 120 return temp_dir_.path().AppendASCII("UserPolicyCacheTest"); |
| 118 } | 121 } |
| 119 | 122 |
| 120 const PolicyMap& mandatory_policy(const UserPolicyCache& cache) { | 123 const PolicyMap& mandatory_policy(const UserPolicyCache& cache) { |
| 121 return cache.mandatory_policy_; | 124 return cache.mandatory_policy_; |
| 122 } | 125 } |
| 123 | 126 |
| 124 const PolicyMap& recommended_policy(const UserPolicyCache& cache) { | 127 const PolicyMap& recommended_policy(const UserPolicyCache& cache) { |
| 125 return cache.recommended_policy_; | 128 return cache.recommended_policy_; |
| 126 } | 129 } |
| 127 | 130 |
| 128 MessageLoop loop_; | 131 MessageLoop loop_; |
| 129 MockConfigurationPolicyProviderObserver observer; | 132 MockCloudPolicyCacheBaseObserver observer; |
| 130 | 133 |
| 131 private: | 134 private: |
| 132 ScopedTempDir temp_dir_; | 135 ScopedTempDir temp_dir_; |
| 133 BrowserThread ui_thread_; | 136 BrowserThread ui_thread_; |
| 134 BrowserThread file_thread_; | 137 BrowserThread file_thread_; |
| 135 }; | 138 }; |
| 136 | 139 |
| 137 TEST_F(UserPolicyCacheTest, DecodePolicy) { | 140 TEST_F(UserPolicyCacheTest, DecodePolicy) { |
| 138 em::CloudPolicySettings settings; | 141 em::CloudPolicySettings settings; |
| 139 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org"); | 142 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. | 376 // If new-style policy comes in, it should override old-style policy. |
| 374 policy = CreateHomepagePolicy("http://www.example.com", | 377 policy = CreateHomepagePolicy("http://www.example.com", |
| 375 base::Time::NowFromSystemTime(), | 378 base::Time::NowFromSystemTime(), |
| 376 em::PolicyOptions::RECOMMENDED); | 379 em::PolicyOptions::RECOMMENDED); |
| 377 SetPolicy(&cache, policy, true); | 380 SetPolicy(&cache, policy, true); |
| 378 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); | 381 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); |
| 379 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); | 382 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 380 } | 383 } |
| 381 | 384 |
| 382 } // namespace policy | 385 } // namespace policy |
| OLD | NEW |