| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/mock_device_management_service.h" | |
| 6 | |
| 7 #include "base/string_util.h" | |
| 8 | |
| 9 using testing::Action; | |
| 10 | |
| 11 namespace em = enterprise_management; | |
| 12 | |
| 13 namespace policy { | |
| 14 namespace { | |
| 15 | |
| 16 // Common mock request job functionality. | |
| 17 class MockRequestJobBase : public DeviceManagementRequestJob { | |
| 18 public: | |
| 19 MockRequestJobBase(JobType type, | |
| 20 MockDeviceManagementService* service) | |
| 21 : DeviceManagementRequestJob(type), | |
| 22 service_(service) {} | |
| 23 virtual ~MockRequestJobBase() {} | |
| 24 | |
| 25 protected: | |
| 26 virtual void Run() OVERRIDE { | |
| 27 service_->StartJob(ExtractParameter(dm_protocol::kParamRequest), | |
| 28 gaia_token_, | |
| 29 ExtractParameter(dm_protocol::kParamOAuthToken), | |
| 30 dm_token_, | |
| 31 ExtractParameter(dm_protocol::kParamUserAffiliation), | |
| 32 ExtractParameter(dm_protocol::kParamDeviceID), | |
| 33 request_); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 // Searches for a query parameter and returns the associated value. | |
| 38 const std::string& ExtractParameter(const std::string& name) const { | |
| 39 for (ParameterMap::const_iterator entry(query_params_.begin()); | |
| 40 entry != query_params_.end(); | |
| 41 ++entry) { | |
| 42 if (name == entry->first) | |
| 43 return entry->second; | |
| 44 } | |
| 45 | |
| 46 return EmptyString(); | |
| 47 } | |
| 48 | |
| 49 MockDeviceManagementService* service_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(MockRequestJobBase); | |
| 52 }; | |
| 53 | |
| 54 // Synchronous mock request job that immediately completes on calling Run(). | |
| 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); | |
| 102 } | |
| 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 | |
| 112 MockDeviceManagementService::MockDeviceManagementService() | |
| 113 : DeviceManagementService("") {} | |
| 114 | |
| 115 MockDeviceManagementService::~MockDeviceManagementService() {} | |
| 116 | |
| 117 Action<MockDeviceManagementService::CreateJobFunction> | |
| 118 MockDeviceManagementService::SucceedJob( | |
| 119 const em::DeviceManagementResponse& response) { | |
| 120 return CreateSyncMockDeviceManagementJob(this, DM_STATUS_SUCCESS, response); | |
| 121 } | |
| 122 | |
| 123 Action<MockDeviceManagementService::CreateJobFunction> | |
| 124 MockDeviceManagementService::FailJob(DeviceManagementStatus status) { | |
| 125 const em::DeviceManagementResponse 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); | |
| 132 } | |
| 133 | |
| 134 } // namespace policy | |
| OLD | NEW |