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/cloud/cloud_policy_manager.h" | 5 #include "chrome/browser/policy/cloud/cloud_policy_manager.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" |
8 #include "base/callback.h" | 9 #include "base/callback.h" |
9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/test/test_simple_task_runner.h" |
| 14 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
12 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 15 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
| 16 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
13 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
14 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 18 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
15 #include "chrome/browser/policy/cloud/policy_builder.h" | 19 #include "chrome/browser/policy/cloud/policy_builder.h" |
16 #include "chrome/browser/policy/configuration_policy_provider_test.h" | 20 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
17 #include "chrome/browser/policy/external_data_fetcher.h" | 21 #include "chrome/browser/policy/external_data_fetcher.h" |
18 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 22 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 23 #include "sync/notifier/invalidation_util.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
21 | 26 |
22 using testing::Mock; | 27 using testing::Mock; |
23 using testing::_; | 28 using testing::_; |
24 | 29 |
25 namespace em = enterprise_management; | 30 namespace em = enterprise_management; |
26 | 31 |
27 namespace policy { | 32 namespace policy { |
28 namespace { | 33 namespace { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 dm_protocol::kChromeUserPolicyType, | 141 dm_protocol::kChromeUserPolicyType, |
137 std::string()), | 142 std::string()), |
138 store) {} | 143 store) {} |
139 virtual ~TestCloudPolicyManager() {} | 144 virtual ~TestCloudPolicyManager() {} |
140 | 145 |
141 // Publish the protected members for testing. | 146 // Publish the protected members for testing. |
142 using CloudPolicyManager::client; | 147 using CloudPolicyManager::client; |
143 using CloudPolicyManager::store; | 148 using CloudPolicyManager::store; |
144 using CloudPolicyManager::service; | 149 using CloudPolicyManager::service; |
145 using CloudPolicyManager::CheckAndPublishPolicy; | 150 using CloudPolicyManager::CheckAndPublishPolicy; |
| 151 using CloudPolicyManager::StartRefreshScheduler; |
146 | 152 |
147 private: | 153 private: |
148 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); | 154 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); |
149 }; | 155 }; |
150 | 156 |
151 MATCHER_P(ProtoMatches, proto, "") { | 157 MATCHER_P(ProtoMatches, proto, "") { |
152 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | 158 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); |
153 } | 159 } |
154 | 160 |
155 class CloudPolicyManagerTest : public testing::Test { | 161 class CloudPolicyManagerTest : public testing::Test { |
(...skipping 16 matching lines...) Expand all Loading... |
172 manager_->Init(); | 178 manager_->Init(); |
173 Mock::VerifyAndClearExpectations(&store_); | 179 Mock::VerifyAndClearExpectations(&store_); |
174 manager_->AddObserver(&observer_); | 180 manager_->AddObserver(&observer_); |
175 } | 181 } |
176 | 182 |
177 virtual void TearDown() OVERRIDE { | 183 virtual void TearDown() OVERRIDE { |
178 manager_->RemoveObserver(&observer_); | 184 manager_->RemoveObserver(&observer_); |
179 manager_->Shutdown(); | 185 manager_->Shutdown(); |
180 } | 186 } |
181 | 187 |
| 188 // Sets up for an invalidations test. |
| 189 void CreateInvalidator() { |
| 190 // Add the invalidation registration info to the policy data. |
| 191 em::PolicyData* policy_data = new em::PolicyData(policy_.policy_data()); |
| 192 policy_data->set_invalidation_source(12345); |
| 193 policy_data->set_invalidation_name("12345"); |
| 194 store_.policy_.reset(policy_data); |
| 195 |
| 196 // Connect the core. |
| 197 MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| 198 EXPECT_CALL(*client, SetupRegistration(_, _)); |
| 199 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); |
| 200 |
| 201 // Create invalidation objects. |
| 202 task_runner_ = new base::TestSimpleTaskRunner(); |
| 203 invalidation_service_.reset(new invalidation::FakeInvalidationService()); |
| 204 invalidator_.reset(new CloudPolicyInvalidator( |
| 205 manager_.get(), |
| 206 manager_->core()->store(), |
| 207 task_runner_)); |
| 208 } |
| 209 |
| 210 void ShutdownInvalidator() { |
| 211 invalidator_->Shutdown(); |
| 212 } |
| 213 |
| 214 // Call EnableInvalidations on the manager. |
| 215 void EnableInvalidations() { |
| 216 manager_->EnableInvalidations( |
| 217 base::Bind( |
| 218 &CloudPolicyInvalidator::InitializeWithService, |
| 219 base::Unretained(invalidator_.get()), |
| 220 base::Unretained(invalidation_service_.get()))); |
| 221 } |
| 222 |
| 223 // Determine if the invalidator has registered with the invalidation service. |
| 224 bool IsInvalidatorRegistered() { |
| 225 syncer::ObjectIdSet object_ids = |
| 226 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); |
| 227 return object_ids.size() == 1 && |
| 228 object_ids.begin()->source() == 12345 && |
| 229 object_ids.begin()->name() == "12345"; |
| 230 } |
| 231 |
| 232 // Determine if the invalidator is unregistered with the invalidation service. |
| 233 bool IsInvalidatorUnregistered() { |
| 234 syncer::ObjectIdSet object_ids = |
| 235 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); |
| 236 return object_ids.empty(); |
| 237 } |
| 238 |
182 // Required by the refresh scheduler that's created by the manager. | 239 // Required by the refresh scheduler that's created by the manager. |
183 base::MessageLoop loop_; | 240 base::MessageLoop loop_; |
184 | 241 |
185 // Testing policy. | 242 // Testing policy. |
186 const PolicyNamespaceKey policy_ns_key_; | 243 const PolicyNamespaceKey policy_ns_key_; |
187 UserPolicyBuilder policy_; | 244 UserPolicyBuilder policy_; |
188 PolicyMap policy_map_; | 245 PolicyMap policy_map_; |
189 PolicyBundle expected_bundle_; | 246 PolicyBundle expected_bundle_; |
190 | 247 |
191 // Policy infrastructure. | 248 // Policy infrastructure. |
192 MockConfigurationPolicyObserver observer_; | 249 MockConfigurationPolicyObserver observer_; |
193 MockCloudPolicyStore store_; | 250 MockCloudPolicyStore store_; |
194 scoped_ptr<TestCloudPolicyManager> manager_; | 251 scoped_ptr<TestCloudPolicyManager> manager_; |
195 | 252 |
| 253 // Invalidation objects. |
| 254 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 255 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service_; |
| 256 scoped_ptr<CloudPolicyInvalidator> invalidator_; |
| 257 |
196 private: | 258 private: |
197 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); | 259 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); |
198 }; | 260 }; |
199 | 261 |
200 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { | 262 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { |
201 PolicyBundle empty_bundle; | 263 PolicyBundle empty_bundle; |
202 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); | 264 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
203 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 265 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
204 | 266 |
205 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | 267 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 TEST_F(CloudPolicyManagerTest, SignalOnError) { | 392 TEST_F(CloudPolicyManagerTest, SignalOnError) { |
331 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). | 393 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). |
332 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); | 394 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); |
333 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 395 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
334 store_.NotifyStoreError(); | 396 store_.NotifyStoreError(); |
335 Mock::VerifyAndClearExpectations(&observer_); | 397 Mock::VerifyAndClearExpectations(&observer_); |
336 | 398 |
337 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 399 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
338 } | 400 } |
339 | 401 |
| 402 TEST_F(CloudPolicyManagerTest, EnableInvalidationsBeforeRefreshScheduler) { |
| 403 CreateInvalidator(); |
| 404 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 405 EnableInvalidations(); |
| 406 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 407 manager_->StartRefreshScheduler(); |
| 408 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 409 ShutdownInvalidator(); |
| 410 } |
| 411 |
| 412 TEST_F(CloudPolicyManagerTest, EnableInvalidationsAfterRefreshScheduler) { |
| 413 CreateInvalidator(); |
| 414 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 415 manager_->StartRefreshScheduler(); |
| 416 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 417 EnableInvalidations(); |
| 418 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 419 ShutdownInvalidator(); |
| 420 } |
| 421 |
340 } // namespace | 422 } // namespace |
341 } // namespace policy | 423 } // namespace policy |
OLD | NEW |