Chromium Code Reviews| 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 void (CloudPolicyInvalidator::*initialize) | |
| 217 (invalidation::InvalidationService*) = | |
| 218 &CloudPolicyInvalidator::Initialize; | |
|
Joao da Silva
2013/07/29 19:44:32
Don't do this. Rename the Initialize() methods ins
Steve Condie
2013/07/29 20:44:51
Done.
| |
| 219 manager_->EnableInvalidations( | |
| 220 base::Bind( | |
| 221 initialize, | |
| 222 base::Unretained(invalidator_.get()), | |
| 223 base::Unretained(invalidation_service_.get()))); | |
| 224 } | |
| 225 | |
| 226 // Determine if the invalidator has registered with the invalidation service. | |
| 227 bool IsInvalidatorRegistered() { | |
| 228 syncer::ObjectIdSet object_ids = | |
| 229 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); | |
| 230 return object_ids.size() == 1 && | |
| 231 object_ids.begin()->source() == 12345 && | |
| 232 object_ids.begin()->name() == "12345"; | |
| 233 } | |
| 234 | |
| 235 // Determine if the invalidator is unregistered with the invalidation service. | |
| 236 bool IsInvalidatorUnregistered() { | |
| 237 syncer::ObjectIdSet object_ids = | |
| 238 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); | |
| 239 return object_ids.empty(); | |
| 240 } | |
| 241 | |
| 182 // Required by the refresh scheduler that's created by the manager. | 242 // Required by the refresh scheduler that's created by the manager. |
| 183 base::MessageLoop loop_; | 243 base::MessageLoop loop_; |
| 184 | 244 |
| 185 // Testing policy. | 245 // Testing policy. |
| 186 const PolicyNamespaceKey policy_ns_key_; | 246 const PolicyNamespaceKey policy_ns_key_; |
| 187 UserPolicyBuilder policy_; | 247 UserPolicyBuilder policy_; |
| 188 PolicyMap policy_map_; | 248 PolicyMap policy_map_; |
| 189 PolicyBundle expected_bundle_; | 249 PolicyBundle expected_bundle_; |
| 190 | 250 |
| 191 // Policy infrastructure. | 251 // Policy infrastructure. |
| 192 MockConfigurationPolicyObserver observer_; | 252 MockConfigurationPolicyObserver observer_; |
| 193 MockCloudPolicyStore store_; | 253 MockCloudPolicyStore store_; |
| 194 scoped_ptr<TestCloudPolicyManager> manager_; | 254 scoped_ptr<TestCloudPolicyManager> manager_; |
| 195 | 255 |
| 256 // Invalidation objects. | |
| 257 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 258 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service_; | |
| 259 scoped_ptr<CloudPolicyInvalidator> invalidator_; | |
| 260 | |
| 196 private: | 261 private: |
| 197 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); | 262 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); |
| 198 }; | 263 }; |
| 199 | 264 |
| 200 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { | 265 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { |
| 201 PolicyBundle empty_bundle; | 266 PolicyBundle empty_bundle; |
| 202 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); | 267 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| 203 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 268 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
| 204 | 269 |
| 205 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | 270 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 TEST_F(CloudPolicyManagerTest, SignalOnError) { | 395 TEST_F(CloudPolicyManagerTest, SignalOnError) { |
| 331 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). | 396 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). |
| 332 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); | 397 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); |
| 333 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 398 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 334 store_.NotifyStoreError(); | 399 store_.NotifyStoreError(); |
| 335 Mock::VerifyAndClearExpectations(&observer_); | 400 Mock::VerifyAndClearExpectations(&observer_); |
| 336 | 401 |
| 337 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 402 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
| 338 } | 403 } |
| 339 | 404 |
| 405 TEST_F(CloudPolicyManagerTest, EnableInvalidationsBeforeRefreshScheduler) { | |
| 406 CreateInvalidator(); | |
| 407 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 408 EnableInvalidations(); | |
| 409 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 410 manager_->StartRefreshScheduler(); | |
| 411 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 412 ShutdownInvalidator(); | |
| 413 } | |
| 414 | |
| 415 TEST_F(CloudPolicyManagerTest, EnableInvalidationsAfterRefreshScheduler) { | |
| 416 CreateInvalidator(); | |
| 417 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 418 manager_->StartRefreshScheduler(); | |
| 419 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 420 EnableInvalidations(); | |
| 421 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 422 ShutdownInvalidator(); | |
| 423 } | |
| 424 | |
| 425 | |
|
Joao da Silva
2013/07/29 19:44:32
nit: remove extra newline
Steve Condie
2013/07/29 20:44:51
Done.
| |
| 340 } // namespace | 426 } // namespace |
| 341 } // namespace policy | 427 } // namespace policy |
| OLD | NEW |