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