1 // Copyright 2014 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.
5 #include "components/test_runner/mock_web_user_media_client.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "components/test_runner/mock_constraints.h"
10 #include "components/test_runner/web_test_delegate.h"
11 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
12 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h"
13 #include "third_party/WebKit/public/platform/WebMediaStream.h"
14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
16 #include "third_party/WebKit/public/platform/WebMediaStreamTrackSourcesRequest.h"
17 #include "third_party/WebKit/public/platform/WebSourceInfo.h"
18 #include "third_party/WebKit/public/platform/WebVector.h"
19 #include "third_party/WebKit/public/web/WebDocument.h"
20 #include "third_party/WebKit/public/web/WebMediaDevicesRequest.h"
21 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h"
22 #include "third_party/WebKit/public/web/WebUserMediaRequest.h"
24 using blink::WebMediaConstraints
;
25 using blink::WebMediaDeviceInfo
;
26 using blink::WebMediaDevicesRequest
;
27 using blink::WebMediaStream
;
28 using blink::WebMediaStreamSource
;
29 using blink::WebMediaStreamTrack
;
30 using blink::WebMediaStreamTrackSourcesRequest
;
31 using blink::WebSourceInfo
;
32 using blink::WebString
;
33 using blink::WebUserMediaRequest
;
34 using blink::WebVector
;
36 namespace test_runner
{
38 class UserMediaRequestTask
: public WebMethodTask
<MockWebUserMediaClient
> {
40 UserMediaRequestTask(MockWebUserMediaClient
* object
,
41 const WebUserMediaRequest
& request
,
42 const WebMediaStream result
)
43 : WebMethodTask
<MockWebUserMediaClient
>(object
),
46 DCHECK(!result_
.isNull());
49 void RunIfValid() override
{ request_
.requestSucceeded(result_
); }
52 WebUserMediaRequest request_
;
53 WebMediaStream result_
;
55 DISALLOW_COPY_AND_ASSIGN(UserMediaRequestTask
);
58 class UserMediaRequestConstraintFailedTask
59 : public WebMethodTask
<MockWebUserMediaClient
> {
61 UserMediaRequestConstraintFailedTask(MockWebUserMediaClient
* object
,
62 const WebUserMediaRequest
& request
,
63 const WebString
& constraint
)
64 : WebMethodTask
<MockWebUserMediaClient
>(object
),
66 constraint_(constraint
) {}
68 void RunIfValid() override
{ request_
.requestFailedConstraint(constraint_
); }
71 WebUserMediaRequest request_
;
72 WebString constraint_
;
74 DISALLOW_COPY_AND_ASSIGN(UserMediaRequestConstraintFailedTask
);
77 class UserMediaRequestPermissionDeniedTask
78 : public WebMethodTask
<MockWebUserMediaClient
> {
80 UserMediaRequestPermissionDeniedTask(MockWebUserMediaClient
* object
,
81 const WebUserMediaRequest
& request
)
82 : WebMethodTask
<MockWebUserMediaClient
>(object
),
85 void RunIfValid() override
{ request_
.requestFailed(); }
88 WebUserMediaRequest request_
;
90 DISALLOW_COPY_AND_ASSIGN(UserMediaRequestPermissionDeniedTask
);
93 class MediaDevicesRequestTask
: public WebMethodTask
<MockWebUserMediaClient
> {
95 MediaDevicesRequestTask(MockWebUserMediaClient
* object
,
96 const WebMediaDevicesRequest
& request
,
97 const WebVector
<WebMediaDeviceInfo
>& result
)
98 : WebMethodTask
<MockWebUserMediaClient
>(object
),
102 void RunIfValid() override
{ request_
.requestSucceeded(result_
); }
105 WebMediaDevicesRequest request_
;
106 WebVector
<WebMediaDeviceInfo
> result_
;
108 DISALLOW_COPY_AND_ASSIGN(MediaDevicesRequestTask
);
111 class SourcesRequestTask
: public WebMethodTask
<MockWebUserMediaClient
> {
113 SourcesRequestTask(MockWebUserMediaClient
* object
,
114 const WebMediaStreamTrackSourcesRequest
& request
,
115 const WebVector
<WebSourceInfo
>& result
)
116 : WebMethodTask
<MockWebUserMediaClient
>(object
),
120 void RunIfValid() override
{ request_
.requestSucceeded(result_
); }
123 WebMediaStreamTrackSourcesRequest request_
;
124 WebVector
<WebSourceInfo
> result_
;
126 DISALLOW_COPY_AND_ASSIGN(SourcesRequestTask
);
129 class MockExtraData
: public WebMediaStream::ExtraData
{
134 MockWebUserMediaClient::MockWebUserMediaClient(WebTestDelegate
* delegate
)
135 : delegate_(delegate
) {}
137 void MockWebUserMediaClient::requestUserMedia(
138 const WebUserMediaRequest
& stream_request
) {
139 DCHECK(!stream_request
.isNull());
140 WebUserMediaRequest request
= stream_request
;
142 if (request
.ownerDocument().isNull() || !request
.ownerDocument().frame()) {
144 new UserMediaRequestPermissionDeniedTask(this, request
));
148 WebMediaConstraints constraints
= request
.audioConstraints();
149 WebString failed_constraint
;
150 if (!constraints
.isNull() &&
151 !MockConstraints::VerifyConstraints(constraints
, &failed_constraint
)) {
152 delegate_
->PostTask(new UserMediaRequestConstraintFailedTask(
153 this, request
, failed_constraint
));
156 constraints
= request
.videoConstraints();
157 if (!constraints
.isNull() &&
158 !MockConstraints::VerifyConstraints(constraints
, &failed_constraint
)) {
159 delegate_
->PostTask(new UserMediaRequestConstraintFailedTask(
160 this, request
, failed_constraint
));
164 const size_t zero
= 0;
165 const size_t one
= 1;
166 WebVector
<WebMediaStreamTrack
> audio_tracks(request
.audio() ? one
: zero
);
167 WebVector
<WebMediaStreamTrack
> video_tracks(request
.video() ? one
: zero
);
169 if (request
.audio()) {
170 WebMediaStreamSource source
;
171 source
.initialize("MockAudioDevice#1",
172 WebMediaStreamSource::TypeAudio
,
174 false /* remote */, true /* readonly */);
175 audio_tracks
[0].initialize(source
);
178 if (request
.video()) {
179 WebMediaStreamSource source
;
180 source
.initialize("MockVideoDevice#1",
181 WebMediaStreamSource::TypeVideo
,
183 false /* remote */, true /* readonly */);
184 video_tracks
[0].initialize(source
);
187 WebMediaStream stream
;
188 stream
.initialize(audio_tracks
, video_tracks
);
190 stream
.setExtraData(new MockExtraData());
192 delegate_
->PostTask(new UserMediaRequestTask(this, request
, stream
));
195 void MockWebUserMediaClient::cancelUserMediaRequest(
196 const WebUserMediaRequest
&) {
199 void MockWebUserMediaClient::requestMediaDevices(
200 const WebMediaDevicesRequest
& request
) {
202 const char* device_id
;
203 WebMediaDeviceInfo::MediaDeviceKind kind
;
205 const char* group_id
;
209 WebMediaDeviceInfo::MediaDeviceKindAudioInput
,
210 "Built-in microphone",
215 WebMediaDeviceInfo::MediaDeviceKindAudioOutput
,
221 WebMediaDeviceInfo::MediaDeviceKindVideoInput
,
227 WebVector
<WebMediaDeviceInfo
> devices(arraysize(test_devices
));
228 for (size_t i
= 0; i
< arraysize(test_devices
); ++i
) {
229 devices
[i
].initialize(WebString::fromUTF8(test_devices
[i
].device_id
),
230 test_devices
[i
].kind
,
231 WebString::fromUTF8(test_devices
[i
].label
),
232 WebString::fromUTF8(test_devices
[i
].group_id
));
235 delegate_
->PostTask(new MediaDevicesRequestTask(this, request
, devices
));
238 void MockWebUserMediaClient::cancelMediaDevicesRequest(
239 const WebMediaDevicesRequest
&) {
242 void MockWebUserMediaClient::requestSources(
243 const blink::WebMediaStreamTrackSourcesRequest
& request
) {
246 WebSourceInfo::SourceKind kind
;
248 WebSourceInfo::VideoFacingMode facing
;
252 WebSourceInfo::SourceKindAudio
,
253 "Built-in microphone",
254 WebSourceInfo::VideoFacingModeNone
,
258 WebSourceInfo::SourceKindVideo
,
260 WebSourceInfo::VideoFacingModeEnvironment
,
264 WebVector
<WebSourceInfo
> sources(arraysize(test_sources
));
265 for (size_t i
= 0; i
< arraysize(test_sources
); ++i
) {
266 sources
[i
].initialize(WebString::fromUTF8(test_sources
[i
].id
),
267 test_sources
[i
].kind
,
268 WebString::fromUTF8(test_sources
[i
].label
),
269 test_sources
[i
].facing
);
272 delegate_
->PostTask(new SourcesRequestTask(this, request
, sources
));
275 } // namespace test_runner