| 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/mock_device_management_service.h" | 5 #include "chrome/browser/policy/mock_device_management_service.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 | 8 |
| 9 using testing::Action; | 9 using testing::Action; |
| 10 | 10 |
| 11 namespace em = enterprise_management; | 11 namespace em = enterprise_management; |
| 12 | 12 |
| 13 namespace policy { | 13 namespace policy { |
| 14 namespace { |
| 14 | 15 |
| 15 class MockDeviceManagementRequestJob : public DeviceManagementRequestJob { | 16 // Common mock request job functionality. |
| 17 class MockRequestJobBase : public DeviceManagementRequestJob { |
| 16 public: | 18 public: |
| 17 MockDeviceManagementRequestJob( | 19 MockRequestJobBase(JobType type, |
| 18 JobType type, | 20 MockDeviceManagementService* service) |
| 19 MockDeviceManagementService* service, | |
| 20 DeviceManagementStatus status, | |
| 21 const enterprise_management::DeviceManagementResponse& response) | |
| 22 : DeviceManagementRequestJob(type), | 21 : DeviceManagementRequestJob(type), |
| 23 service_(service), | 22 service_(service) {} |
| 24 status_(status), | 23 virtual ~MockRequestJobBase() {} |
| 25 response_(response) {} | |
| 26 virtual ~MockDeviceManagementRequestJob() {} | |
| 27 | 24 |
| 28 protected: | 25 protected: |
| 29 virtual void Run() OVERRIDE { | 26 virtual void Run() OVERRIDE { |
| 30 service_->StartJob(ExtractParameter(dm_protocol::kParamRequest), | 27 service_->StartJob(ExtractParameter(dm_protocol::kParamRequest), |
| 31 gaia_token_, | 28 gaia_token_, |
| 32 ExtractParameter(dm_protocol::kParamOAuthToken), | 29 ExtractParameter(dm_protocol::kParamOAuthToken), |
| 33 dm_token_, | 30 dm_token_, |
| 34 ExtractParameter(dm_protocol::kParamUserAffiliation), | 31 ExtractParameter(dm_protocol::kParamUserAffiliation), |
| 35 ExtractParameter(dm_protocol::kParamDeviceID), | 32 ExtractParameter(dm_protocol::kParamDeviceID), |
| 36 request_); | 33 request_); |
| 37 callback_.Run(status_, response_); | |
| 38 } | 34 } |
| 39 | 35 |
| 40 private: | 36 private: |
| 41 // Searches for a query parameter and returns the associated value. | 37 // Searches for a query parameter and returns the associated value. |
| 42 const std::string& ExtractParameter(const std::string& name) const { | 38 const std::string& ExtractParameter(const std::string& name) const { |
| 43 for (ParameterMap::const_iterator entry(query_params_.begin()); | 39 for (ParameterMap::const_iterator entry(query_params_.begin()); |
| 44 entry != query_params_.end(); | 40 entry != query_params_.end(); |
| 45 ++entry) { | 41 ++entry) { |
| 46 if (name == entry->first) | 42 if (name == entry->first) |
| 47 return entry->second; | 43 return entry->second; |
| 48 } | 44 } |
| 49 | 45 |
| 50 return EmptyString(); | 46 return EmptyString(); |
| 51 } | 47 } |
| 52 | 48 |
| 53 MockDeviceManagementService* service_; | 49 MockDeviceManagementService* service_; |
| 54 DeviceManagementStatus status_; | |
| 55 enterprise_management::DeviceManagementResponse response_; | |
| 56 | 50 |
| 57 DISALLOW_COPY_AND_ASSIGN(MockDeviceManagementRequestJob); | 51 DISALLOW_COPY_AND_ASSIGN(MockRequestJobBase); |
| 58 }; | 52 }; |
| 59 | 53 |
| 60 ACTION_P3(CreateMockDeviceManagementRequestJob, service, status, response) { | 54 // Synchronous mock request job that immediately completes on calling Run(). |
| 61 return new MockDeviceManagementRequestJob(arg0, service, status, response); | 55 class SyncRequestJob : public MockRequestJobBase { |
| 56 public: |
| 57 SyncRequestJob(JobType type, |
| 58 MockDeviceManagementService* service, |
| 59 DeviceManagementStatus status, |
| 60 const em::DeviceManagementResponse& response) |
| 61 : MockRequestJobBase(type, service), |
| 62 status_(status), |
| 63 response_(response) {} |
| 64 virtual ~SyncRequestJob() {} |
| 65 |
| 66 protected: |
| 67 virtual void Run() OVERRIDE { |
| 68 MockRequestJobBase::Run(); |
| 69 callback_.Run(status_, response_); |
| 70 } |
| 71 |
| 72 private: |
| 73 DeviceManagementStatus status_; |
| 74 em::DeviceManagementResponse response_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(SyncRequestJob); |
| 77 }; |
| 78 |
| 79 // Asynchronous job that allows the test to delay job completion. |
| 80 class AsyncRequestJob : public MockRequestJobBase, |
| 81 public MockDeviceManagementJob { |
| 82 public: |
| 83 AsyncRequestJob(JobType type, MockDeviceManagementService* service) |
| 84 : MockRequestJobBase(type, service) {} |
| 85 virtual ~AsyncRequestJob() {} |
| 86 |
| 87 protected: |
| 88 virtual void SendResponse( |
| 89 DeviceManagementStatus status, |
| 90 const em::DeviceManagementResponse& response) OVERRIDE { |
| 91 callback_.Run(status, response); |
| 92 } |
| 93 |
| 94 private: |
| 95 DISALLOW_COPY_AND_ASSIGN(AsyncRequestJob); |
| 96 }; |
| 97 |
| 98 } // namespace |
| 99 |
| 100 ACTION_P3(CreateSyncMockDeviceManagementJob, service, status, response) { |
| 101 return new SyncRequestJob(arg0, service, status, response); |
| 62 } | 102 } |
| 63 | 103 |
| 104 ACTION_P2(CreateAsyncMockDeviceManagementJob, service, mock_job) { |
| 105 AsyncRequestJob* job = new AsyncRequestJob(arg0, service); |
| 106 *mock_job = job; |
| 107 return job; |
| 108 } |
| 109 |
| 110 MockDeviceManagementJob::~MockDeviceManagementJob() {} |
| 111 |
| 64 MockDeviceManagementService::MockDeviceManagementService() | 112 MockDeviceManagementService::MockDeviceManagementService() |
| 65 : DeviceManagementService("") {} | 113 : DeviceManagementService("") {} |
| 66 | 114 |
| 67 MockDeviceManagementService::~MockDeviceManagementService() {} | 115 MockDeviceManagementService::~MockDeviceManagementService() {} |
| 68 | 116 |
| 69 Action<MockDeviceManagementService::CreateJobFunction> | 117 Action<MockDeviceManagementService::CreateJobFunction> |
| 70 MockDeviceManagementService::SucceedJob( | 118 MockDeviceManagementService::SucceedJob( |
| 71 const enterprise_management::DeviceManagementResponse& response) { | 119 const em::DeviceManagementResponse& response) { |
| 72 return CreateMockDeviceManagementRequestJob(this, DM_STATUS_SUCCESS, | 120 return CreateSyncMockDeviceManagementJob(this, DM_STATUS_SUCCESS, response); |
| 73 response); | |
| 74 } | 121 } |
| 75 | 122 |
| 76 Action<MockDeviceManagementService::CreateJobFunction> | 123 Action<MockDeviceManagementService::CreateJobFunction> |
| 77 MockDeviceManagementService::FailJob(DeviceManagementStatus status) { | 124 MockDeviceManagementService::FailJob(DeviceManagementStatus status) { |
| 78 const enterprise_management::DeviceManagementResponse dummy_response; | 125 const em::DeviceManagementResponse dummy_response; |
| 79 return CreateMockDeviceManagementRequestJob(this, status, dummy_response); | 126 return CreateSyncMockDeviceManagementJob(this, status, dummy_response); |
| 127 } |
| 128 |
| 129 Action<MockDeviceManagementService::CreateJobFunction> |
| 130 MockDeviceManagementService::CreateAsyncJob(MockDeviceManagementJob** job) { |
| 131 return CreateAsyncMockDeviceManagementJob(this, job); |
| 80 } | 132 } |
| 81 | 133 |
| 82 } // namespace policy | 134 } // namespace policy |
| OLD | NEW |