Chromium Code Reviews| Index: chrome/browser/policy/cloud_policy_manager_unittest.cc |
| diff --git a/chrome/browser/policy/user_cloud_policy_manager_unittest.cc b/chrome/browser/policy/cloud_policy_manager_unittest.cc |
| similarity index 59% |
| copy from chrome/browser/policy/user_cloud_policy_manager_unittest.cc |
| copy to chrome/browser/policy/cloud_policy_manager_unittest.cc |
| index 4bed8977cbfed3a484b7c15dc3115d82620c2530..2ac7c4719a9c9f29dd1de27c6a9a36dbc5814a66 100644 |
| --- a/chrome/browser/policy/user_cloud_policy_manager_unittest.cc |
| +++ b/chrome/browser/policy/cloud_policy_manager_unittest.cc |
| @@ -2,29 +2,25 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "chrome/browser/policy/cloud_policy_manager.h" |
| + |
| #include "base/basictypes.h" |
| +#include "base/compiler_specific.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/message_loop.h" |
| -#include "chrome/browser/policy/cloud_policy_service.h" |
| #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| +#include "chrome/browser/policy/mock_cloud_policy_client.h" |
| #include "chrome/browser/policy/mock_cloud_policy_store.h" |
| #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| -#include "chrome/browser/policy/mock_device_management_service.h" |
| -#include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| -#include "chrome/browser/policy/user_cloud_policy_manager.h" |
| -#include "chrome/browser/prefs/browser_prefs.h" |
| -#include "chrome/test/base/testing_pref_service.h" |
| -#include "policy/policy_constants.h" |
| +#include "chrome/browser/policy/policy_builder.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| -namespace em = enterprise_management; |
| - |
| -using testing::AnyNumber; |
| -using testing::AtLeast; |
| using testing::Mock; |
| using testing::_; |
| +namespace em = enterprise_management; |
| + |
| namespace policy { |
| namespace { |
| @@ -75,8 +71,8 @@ ConfigurationPolicyProvider* TestHarness::CreateProvider( |
| store_ = new MockCloudPolicyStore(); |
| store_->NotifyStoreLoaded(); |
| EXPECT_CALL(*store_, Load()); |
| - return new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_).Pass(), |
| - false); |
| + return new CloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_)); |
| + Mock::VerifyAndClearExpectations(store_); |
|
Lei Zhang
2012/11/05 20:11:00
Umm, this does not look right...
Mattias Nissler (ping if slow)
2012/11/06 09:13:28
Indeed, thanks for pointing out.
CL with the fix
|
| } |
| void TestHarness::InstallEmptyPolicy() {} |
| @@ -129,103 +125,128 @@ INSTANTIATE_TEST_CASE_P( |
| testing::Values(TestHarness::CreateMandatory, |
| TestHarness::CreateRecommended)); |
| -class UserCloudPolicyManagerTest : public testing::Test { |
| +class TestCloudPolicyManager : public CloudPolicyManager { |
| + public: |
| + explicit TestCloudPolicyManager(scoped_ptr<CloudPolicyStore> store) |
| + : CloudPolicyManager(store.Pass()) {} |
| + virtual ~TestCloudPolicyManager() {} |
| + |
| + // Publish the protected members for testing. |
| + using CloudPolicyManager::InitializeService; |
| + using CloudPolicyManager::ShutdownService; |
| + using CloudPolicyManager::StartRefreshScheduler; |
| + using CloudPolicyManager::CheckAndPublishPolicy; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); |
| +}; |
| + |
| +MATCHER_P(ProtoMatches, proto, "") { |
| + return arg.SerializePartialAsString() == proto.SerializePartialAsString(); |
| +} |
| + |
| +class CloudPolicyManagerTest : public testing::Test { |
| protected: |
| - UserCloudPolicyManagerTest() |
| - : store_(NULL) {} |
| + CloudPolicyManagerTest() {} |
| virtual void SetUp() OVERRIDE { |
| - chrome::RegisterLocalState(&prefs_); |
| - |
| // Set up a policy map for testing. |
| policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| base::Value::CreateStringValue("value")); |
| expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( |
| policy_map_); |
| - // Create a fake policy blob to deliver to the client. |
| - em::PolicyData policy_data; |
| - em::PolicyFetchResponse* policy_response = |
| - policy_blob_.mutable_policy_response()->add_response(); |
| - ASSERT_TRUE(policy_data.SerializeToString( |
| - policy_response->mutable_policy_data())); |
| + policy_.payload().mutable_homepagelocation()->set_homepagelocation( |
| + "http://www.example.com"); |
| + policy_.Build(); |
| - EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) |
| - .Times(AnyNumber()); |
| - } |
| - |
| - void CreateManager(bool wait_for_policy_fetch) { |
| store_ = new MockCloudPolicyStore(); |
| EXPECT_CALL(*store_, Load()); |
| manager_.reset( |
| - new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_).Pass(), |
| - wait_for_policy_fetch)); |
| + new TestCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_))); |
| + Mock::VerifyAndClearExpectations(store_); |
| registrar_.Init(manager_.get(), &observer_); |
| } |
| - void CreateManagerWithPendingFetch() { |
| - CreateManager(true); |
| - manager_->Initialize(&prefs_, &device_management_service_, |
| - USER_AFFILIATION_NONE); |
| - EXPECT_FALSE(manager_->IsInitializationComplete()); |
| - |
| - // Finishing the Load() operation shouldn't set the initialized flag. |
| - EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| - store_->NotifyStoreLoaded(); |
| - EXPECT_FALSE(manager_->IsInitializationComplete()); |
| - Mock::VerifyAndClearExpectations(&observer_); |
| - } |
| - |
| // Required by the refresh scheduler that's created by the manager. |
| MessageLoop loop_; |
| - // Convenience policy objects. |
| - em::DeviceManagementResponse policy_blob_; |
| + // Testing policy. |
| + UserPolicyBuilder policy_; |
| PolicyMap policy_map_; |
| PolicyBundle expected_bundle_; |
| // Policy infrastructure. |
| - TestingPrefService prefs_; |
| MockConfigurationPolicyObserver observer_; |
| - MockDeviceManagementService device_management_service_; |
| MockCloudPolicyStore* store_; |
| - scoped_ptr<UserCloudPolicyManager> manager_; |
| + scoped_ptr<TestCloudPolicyManager> manager_; |
| ConfigurationPolicyObserverRegistrar registrar_; |
| private: |
| - DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); |
| + DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); |
| }; |
| -TEST_F(UserCloudPolicyManagerTest, Init) { |
| - CreateManager(false); |
| +TEST_F(CloudPolicyManagerTest, InitAndShutdown) { |
| PolicyBundle empty_bundle; |
| EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| EXPECT_FALSE(manager_->IsInitializationComplete()); |
| + EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| + manager_->CheckAndPublishPolicy(); |
| + Mock::VerifyAndClearExpectations(&observer_); |
| + |
| store_->policy_map_.CopyFrom(policy_map_); |
| + store_->policy_.reset(new em::PolicyData(policy_.policy_data())); |
| EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| store_->NotifyStoreLoaded(); |
| + Mock::VerifyAndClearExpectations(&observer_); |
| EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| EXPECT_TRUE(manager_->IsInitializationComplete()); |
| + |
| + MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| + EXPECT_CALL(*client, SetupRegistration(_, _)); |
| + manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); |
| + Mock::VerifyAndClearExpectations(client); |
| + EXPECT_TRUE(manager_->cloud_policy_client()); |
| + EXPECT_TRUE(manager_->cloud_policy_service()); |
| + |
| + EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| + manager_->CheckAndPublishPolicy(); |
| + Mock::VerifyAndClearExpectations(&observer_); |
| + |
| + manager_->ShutdownService(); |
| + EXPECT_FALSE(manager_->cloud_policy_client()); |
| + EXPECT_FALSE(manager_->cloud_policy_service()); |
| } |
| -TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) { |
| - // Load policy, make sure it goes away when ShutdownAndRemove() is called. |
| - CreateManager(false); |
| +TEST_F(CloudPolicyManagerTest, RegistrationAndFetch) { |
| + EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| + store_->NotifyStoreLoaded(); |
| + Mock::VerifyAndClearExpectations(&observer_); |
| + EXPECT_TRUE(manager_->IsInitializationComplete()); |
| + |
| + MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| + manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); |
| + |
| + client->SetDMToken(policy_.policy_data().request_token()); |
| + client->NotifyRegistrationStateChanged(); |
| + |
| + client->SetPolicy(policy_.policy()); |
| + EXPECT_CALL(*store_, Store(ProtoMatches(policy_.policy()))); |
| + client->NotifyPolicyFetched(); |
| + Mock::VerifyAndClearExpectations(store_); |
| + |
| store_->policy_map_.CopyFrom(policy_map_); |
| EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| store_->NotifyStoreLoaded(); |
| + Mock::VerifyAndClearExpectations(&observer_); |
| EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| - EXPECT_TRUE(manager_->IsInitializationComplete()); |
| - EXPECT_CALL(*store_, Clear()); |
| - manager_->ShutdownAndRemovePolicy(); |
| - EXPECT_FALSE(manager_->cloud_policy_service()); |
| } |
| -TEST_F(UserCloudPolicyManagerTest, Update) { |
| - CreateManager(false); |
| +TEST_F(CloudPolicyManagerTest, Update) { |
| EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| store_->NotifyStoreLoaded(); |
| + Mock::VerifyAndClearExpectations(&observer_); |
| EXPECT_TRUE(manager_->IsInitializationComplete()); |
| PolicyBundle empty_bundle; |
| EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| @@ -233,14 +254,14 @@ TEST_F(UserCloudPolicyManagerTest, Update) { |
| store_->policy_map_.CopyFrom(policy_map_); |
| EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| store_->NotifyStoreLoaded(); |
| + Mock::VerifyAndClearExpectations(&observer_); |
| EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| EXPECT_TRUE(manager_->IsInitializationComplete()); |
| } |
| -TEST_F(UserCloudPolicyManagerTest, RefreshNotRegistered) { |
| - CreateManager(false); |
| - manager_->Initialize(&prefs_, &device_management_service_, |
| - USER_AFFILIATION_NONE); |
| +TEST_F(CloudPolicyManagerTest, RefreshNotRegistered) { |
| + MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| + manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); |
| EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| store_->NotifyStoreLoaded(); |
| @@ -252,27 +273,28 @@ TEST_F(UserCloudPolicyManagerTest, RefreshNotRegistered) { |
| Mock::VerifyAndClearExpectations(&observer_); |
| } |
| -TEST_F(UserCloudPolicyManagerTest, RefreshSuccessful) { |
| - CreateManager(false); |
| - manager_->Initialize(&prefs_, &device_management_service_, |
| - USER_AFFILIATION_NONE); |
| - manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", |
| - "client_id"); |
| +TEST_F(CloudPolicyManagerTest, RefreshSuccessful) { |
| + MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| + manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); |
| + // Simulate a store load. |
| + store_->policy_.reset(new em::PolicyData(policy_.policy_data())); |
| EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| + EXPECT_CALL(*client, SetupRegistration(_, _)); |
| store_->NotifyStoreLoaded(); |
| + Mock::VerifyAndClearExpectations(client); |
| Mock::VerifyAndClearExpectations(&observer_); |
| + // Acknowledge registration. |
| + client->SetDMToken(policy_.policy_data().request_token()); |
| + |
| // Start a refresh. |
| EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| - MockDeviceManagementJob* request_job; |
| - EXPECT_CALL(device_management_service_, |
| - CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| - .WillOnce(device_management_service_.CreateAsyncJob(&request_job)); |
| + EXPECT_CALL(*client, FetchPolicy()); |
| manager_->RefreshPolicies(); |
| - ASSERT_TRUE(request_job); |
| - store_->policy_map_.CopyFrom(policy_map_); |
| + Mock::VerifyAndClearExpectations(client); |
| Mock::VerifyAndClearExpectations(&observer_); |
| + store_->policy_map_.CopyFrom(policy_map_); |
| // A stray reload should be suppressed until the refresh completes. |
| EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| @@ -282,8 +304,10 @@ TEST_F(UserCloudPolicyManagerTest, RefreshSuccessful) { |
| // Respond to the policy fetch, which should trigger a write to |store_|. |
| EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| EXPECT_CALL(*store_, Store(_)); |
| - request_job->SendResponse(DM_STATUS_SUCCESS, policy_blob_); |
| + client->SetPolicy(policy_.policy()); |
| + client->NotifyPolicyFetched(); |
| Mock::VerifyAndClearExpectations(&observer_); |
| + Mock::VerifyAndClearExpectations(store_); |
| // The load notification from |store_| should trigger the policy update. |
| EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| @@ -292,67 +316,5 @@ TEST_F(UserCloudPolicyManagerTest, RefreshSuccessful) { |
| Mock::VerifyAndClearExpectations(&observer_); |
| } |
| -TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetch) { |
| - CreateManagerWithPendingFetch(); |
| - |
| - // Setting the token should trigger the policy fetch. |
| - EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| - MockDeviceManagementJob* fetch_request = NULL; |
| - EXPECT_CALL(device_management_service_, |
| - CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| - .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); |
| - manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", |
| - "client_id"); |
| - ASSERT_TRUE(fetch_request); |
| - EXPECT_FALSE(manager_->IsInitializationComplete()); |
| - Mock::VerifyAndClearExpectations(&observer_); |
| - |
| - // Respond to the policy fetch, which should trigger a write to |store_|. |
| - EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| - EXPECT_CALL(*store_, Store(_)); |
| - fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); |
| - EXPECT_FALSE(manager_->IsInitializationComplete()); |
| - Mock::VerifyAndClearExpectations(&observer_); |
| - |
| - // The load notification from |store_| should trigger the policy update and |
| - // flip the initialized bit. |
| - EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| - store_->NotifyStoreLoaded(); |
| - EXPECT_TRUE(manager_->IsInitializationComplete()); |
| - Mock::VerifyAndClearExpectations(&observer_); |
| -} |
| - |
| -TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) { |
| - CreateManagerWithPendingFetch(); |
| - |
| - // Setting the token should trigger the policy fetch. |
| - EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| - MockDeviceManagementJob* fetch_request = NULL; |
| - EXPECT_CALL(device_management_service_, |
| - CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| - .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); |
| - manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", |
| - "client_id"); |
| - ASSERT_TRUE(fetch_request); |
| - EXPECT_FALSE(manager_->IsInitializationComplete()); |
| - Mock::VerifyAndClearExpectations(&observer_); |
| - |
| - // Make the policy fetch fail, at which point the manager should bail out. |
| - EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); |
| - fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_); |
| - EXPECT_TRUE(manager_->IsInitializationComplete()); |
| - Mock::VerifyAndClearExpectations(&observer_); |
| -} |
| - |
| -TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) { |
| - CreateManagerWithPendingFetch(); |
| - |
| - // Cancelling the initial fetch should flip the flag. |
| - EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| - manager_->CancelWaitForPolicyFetch(); |
| - EXPECT_TRUE(manager_->IsInitializationComplete()); |
| - Mock::VerifyAndClearExpectations(&observer_); |
| -} |
| - |
| } // namespace |
| } // namespace policy |