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 RetryJob() OVERRIDE { | |
89 if (!retry_callback_.is_null()) | |
90 retry_callback_.Run(this); | |
91 Run(); | |
92 } | |
93 | |
94 virtual void SendResponse( | |
95 DeviceManagementStatus status, | |
96 const em::DeviceManagementResponse& response) OVERRIDE { | |
97 callback_.Run(status, response); | |
98 } | |
99 | |
100 private: | |
101 DISALLOW_COPY_AND_ASSIGN(AsyncRequestJob); | |
102 }; | |
103 | |
104 } // namespace | |
105 | |
106 ACTION_P3(CreateSyncMockDeviceManagementJob, service, status, response) { | |
107 return new SyncRequestJob(arg0, service, status, response); | |
108 } | |
109 | |
110 ACTION_P2(CreateAsyncMockDeviceManagementJob, service, mock_job) { | |
111 AsyncRequestJob* job = new AsyncRequestJob(arg0, service); | |
112 *mock_job = job; | |
113 return job; | |
114 } | |
115 | |
116 MockDeviceManagementJob::~MockDeviceManagementJob() {} | |
117 | |
118 MockDeviceManagementService::MockDeviceManagementService() | |
119 : DeviceManagementService("") {} | |
120 | |
121 MockDeviceManagementService::~MockDeviceManagementService() {} | |
122 | |
123 Action<MockDeviceManagementService::CreateJobFunction> | |
124 MockDeviceManagementService::SucceedJob( | |
125 const em::DeviceManagementResponse& response) { | |
126 return CreateSyncMockDeviceManagementJob(this, DM_STATUS_SUCCESS, response); | |
127 } | |
128 | |
129 Action<MockDeviceManagementService::CreateJobFunction> | |
130 MockDeviceManagementService::FailJob(DeviceManagementStatus status) { | |
131 const em::DeviceManagementResponse dummy_response; | |
132 return CreateSyncMockDeviceManagementJob(this, status, dummy_response); | |
133 } | |
134 | |
135 Action<MockDeviceManagementService::CreateJobFunction> | |
136 MockDeviceManagementService::CreateAsyncJob(MockDeviceManagementJob** job) { | |
137 return CreateAsyncMockDeviceManagementJob(this, job); | |
138 } | |
139 | |
140 } // namespace policy | |
OLD | NEW |