| 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/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 14 #include "chrome/browser/invalidation/fake_invalidation_service.h" | 15 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
| 15 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
| 16 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | 17 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
| 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 18 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| 18 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 19 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
| 19 #include "chrome/browser/policy/cloud/policy_builder.h" | 20 #include "chrome/browser/policy/cloud/policy_builder.h" |
| 20 #include "chrome/browser/policy/configuration_policy_provider_test.h" | 21 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 21 #include "chrome/browser/policy/external_data_fetcher.h" | 22 #include "chrome/browser/policy/external_data_fetcher.h" |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 dm_protocol::kChromeUserPolicyType, | 144 dm_protocol::kChromeUserPolicyType, |
| 144 std::string()), | 145 std::string()), |
| 145 store) {} | 146 store) {} |
| 146 virtual ~TestCloudPolicyManager() {} | 147 virtual ~TestCloudPolicyManager() {} |
| 147 | 148 |
| 148 // Publish the protected members for testing. | 149 // Publish the protected members for testing. |
| 149 using CloudPolicyManager::client; | 150 using CloudPolicyManager::client; |
| 150 using CloudPolicyManager::store; | 151 using CloudPolicyManager::store; |
| 151 using CloudPolicyManager::service; | 152 using CloudPolicyManager::service; |
| 152 using CloudPolicyManager::CheckAndPublishPolicy; | 153 using CloudPolicyManager::CheckAndPublishPolicy; |
| 153 using CloudPolicyManager::StartRefreshScheduler; | 154 using CloudPolicyManager::StartInvalidator; |
| 155 using CloudPolicyManager::StopInvalidator; |
| 154 | 156 |
| 155 private: | 157 private: |
| 156 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); | 158 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); |
| 157 }; | 159 }; |
| 158 | 160 |
| 159 MATCHER_P(ProtoMatches, proto, "") { | 161 MATCHER_P(ProtoMatches, proto, "") { |
| 160 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | 162 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); |
| 161 } | 163 } |
| 162 | 164 |
| 163 class CloudPolicyManagerTest : public testing::Test { | 165 class CloudPolicyManagerTest : public testing::Test { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 180 manager_->Init(); | 182 manager_->Init(); |
| 181 Mock::VerifyAndClearExpectations(&store_); | 183 Mock::VerifyAndClearExpectations(&store_); |
| 182 manager_->AddObserver(&observer_); | 184 manager_->AddObserver(&observer_); |
| 183 } | 185 } |
| 184 | 186 |
| 185 virtual void TearDown() OVERRIDE { | 187 virtual void TearDown() OVERRIDE { |
| 186 manager_->RemoveObserver(&observer_); | 188 manager_->RemoveObserver(&observer_); |
| 187 manager_->Shutdown(); | 189 manager_->Shutdown(); |
| 188 } | 190 } |
| 189 | 191 |
| 190 // Sets up for an invalidations test. | 192 // Creates the invalidator object for an invalidations test. |
| 191 void CreateInvalidator() { | 193 void CreateInvalidator() { |
| 192 // Add the invalidation registration info to the policy data. | 194 // Add the invalidation registration info to the policy data. |
| 193 em::PolicyData* policy_data = new em::PolicyData(policy_.policy_data()); | 195 em::PolicyData* policy_data = new em::PolicyData(policy_.policy_data()); |
| 194 policy_data->set_invalidation_source(12345); | 196 policy_data->set_invalidation_source(12345); |
| 195 policy_data->set_invalidation_name("12345"); | 197 policy_data->set_invalidation_name("12345"); |
| 196 store_.policy_.reset(policy_data); | 198 store_.policy_.reset(policy_data); |
| 197 | 199 |
| 198 // Connect the core. | 200 // Connect the core. |
| 199 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | 201 MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| 200 EXPECT_CALL(*client, SetupRegistration(_, _)); | 202 EXPECT_CALL(*client, SetupRegistration(_, _)); |
| 201 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); | 203 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); |
| 204 manager_->core()->StartRefreshScheduler(); |
| 202 | 205 |
| 203 // Create invalidation objects. | 206 // Create invalidation objects. |
| 204 task_runner_ = new base::TestSimpleTaskRunner(); | 207 task_runner_ = new base::TestSimpleTaskRunner(); |
| 205 invalidation_service_.reset(new invalidation::FakeInvalidationService()); | 208 invalidation_service_.reset(new invalidation::FakeInvalidationService()); |
| 206 invalidator_.reset(new CloudPolicyInvalidator( | 209 invalidator_.reset(new CloudPolicyInvalidator( |
| 210 base::Bind( |
| 211 &CloudPolicyManagerTest::GetInvalidationService, |
| 212 base::Unretained(this)), |
| 207 manager_.get(), | 213 manager_.get(), |
| 208 manager_->core()->store(), | 214 manager_->core()->store(), |
| 209 task_runner_)); | 215 task_runner_)); |
| 210 } | 216 } |
| 211 | 217 |
| 212 void ShutdownInvalidator() { | 218 void ShutdownInvalidator() { |
| 213 invalidator_->Shutdown(); | 219 invalidator_->Shutdown(); |
| 214 } | 220 } |
| 215 | 221 |
| 216 // Call EnableInvalidations on the manager. | 222 // Get a pointer to the invalidation service. |
| 217 void EnableInvalidations() { | 223 invalidation::InvalidationService* GetInvalidationService() { |
| 218 manager_->EnableInvalidations( | 224 return invalidation_service_.get(); |
| 219 base::Bind( | |
| 220 &CloudPolicyInvalidator::InitializeWithService, | |
| 221 base::Unretained(invalidator_.get()), | |
| 222 base::Unretained(invalidation_service_.get()))); | |
| 223 } | 225 } |
| 224 | 226 |
| 225 // Determine if the invalidator has registered with the invalidation service. | 227 // Determine if the invalidator has registered with the invalidation service. |
| 226 bool IsInvalidatorRegistered() { | 228 bool IsInvalidatorRegistered() { |
| 227 syncer::ObjectIdSet object_ids = | 229 syncer::ObjectIdSet object_ids = |
| 228 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); | 230 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); |
| 229 return object_ids.size() == 1 && | 231 return object_ids.size() == 1 && |
| 230 object_ids.begin()->source() == 12345 && | 232 object_ids.begin()->source() == 12345 && |
| 231 object_ids.begin()->name() == "12345"; | 233 object_ids.begin()->name() == "12345"; |
| 232 } | 234 } |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 TEST_F(CloudPolicyManagerTest, SignalOnError) { | 396 TEST_F(CloudPolicyManagerTest, SignalOnError) { |
| 395 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). | 397 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). |
| 396 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); | 398 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); |
| 397 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 399 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 398 store_.NotifyStoreError(); | 400 store_.NotifyStoreError(); |
| 399 Mock::VerifyAndClearExpectations(&observer_); | 401 Mock::VerifyAndClearExpectations(&observer_); |
| 400 | 402 |
| 401 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 403 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
| 402 } | 404 } |
| 403 | 405 |
| 404 TEST_F(CloudPolicyManagerTest, EnableInvalidationsBeforeRefreshScheduler) { | 406 TEST_F(CloudPolicyManagerTest, StartInvalidatorAfterSetInvalidator) { |
| 405 CreateInvalidator(); | 407 CreateInvalidator(); |
| 408 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory(invalidator_.get()); |
| 406 EXPECT_TRUE(IsInvalidatorUnregistered()); | 409 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 407 EnableInvalidations(); | 410 manager_->SetInvalidator(weak_factory.GetWeakPtr()); |
| 408 EXPECT_TRUE(IsInvalidatorUnregistered()); | 411 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 409 manager_->StartRefreshScheduler(); | 412 manager_->StartInvalidator(); |
| 410 EXPECT_TRUE(IsInvalidatorRegistered()); | 413 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 411 ShutdownInvalidator(); | 414 ShutdownInvalidator(); |
| 412 } | 415 } |
| 413 | 416 |
| 414 TEST_F(CloudPolicyManagerTest, EnableInvalidationsAfterRefreshScheduler) { | 417 TEST_F(CloudPolicyManagerTest, StartInvalidatorBeforeSetInvalidator) { |
| 415 CreateInvalidator(); | 418 CreateInvalidator(); |
| 419 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory(invalidator_.get()); |
| 416 EXPECT_TRUE(IsInvalidatorUnregistered()); | 420 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 417 manager_->StartRefreshScheduler(); | 421 manager_->StartInvalidator(); |
| 418 EXPECT_TRUE(IsInvalidatorUnregistered()); | 422 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 419 EnableInvalidations(); | 423 manager_->SetInvalidator(weak_factory.GetWeakPtr()); |
| 420 EXPECT_TRUE(IsInvalidatorRegistered()); | 424 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 421 ShutdownInvalidator(); | 425 ShutdownInvalidator(); |
| 422 } | 426 } |
| 423 | 427 |
| 428 TEST_F(CloudPolicyManagerTest, StartAndStopInvalidatorBeforeSetInvalidator) { |
| 429 CreateInvalidator(); |
| 430 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory(invalidator_.get()); |
| 431 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 432 manager_->StartInvalidator(); |
| 433 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 434 manager_->StopInvalidator(); |
| 435 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 436 manager_->SetInvalidator(weak_factory.GetWeakPtr()); |
| 437 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 438 ShutdownInvalidator(); |
| 439 } |
| 440 |
| 441 TEST_F(CloudPolicyManagerTest, StopInvalidator) { |
| 442 CreateInvalidator(); |
| 443 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory(invalidator_.get()); |
| 444 manager_->SetInvalidator(weak_factory.GetWeakPtr()); |
| 445 manager_->StartInvalidator(); |
| 446 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 447 manager_->StopInvalidator(); |
| 448 EXPECT_TRUE(IsInvalidatorUnregistered()); |
| 449 manager_->StartInvalidator(); |
| 450 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 451 ShutdownInvalidator(); |
| 452 } |
| 453 |
| 454 TEST_F(CloudPolicyManagerTest, InvalidateInvalidatorPointer) { |
| 455 CreateInvalidator(); |
| 456 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory(invalidator_.get()); |
| 457 manager_->SetInvalidator(weak_factory.GetWeakPtr()); |
| 458 manager_->StartInvalidator(); |
| 459 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 460 ShutdownInvalidator(); |
| 461 weak_factory.InvalidateWeakPtrs(); |
| 462 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 463 manager_->StopInvalidator(); |
| 464 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 465 manager_->StartInvalidator(); |
| 466 EXPECT_TRUE(IsInvalidatorRegistered()); |
| 467 } |
| 468 |
| 424 } // namespace | 469 } // namespace |
| 425 } // namespace policy | 470 } // namespace policy |
| OLD | NEW |