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 "content/shell/renderer/test_runner/mock_web_user_media_client.h"
7 #include "base/logging.h"
8 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
9 #include "content/shell/renderer/test_runner/mock_constraints.h"
10 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
11 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h"
12 #include "third_party/WebKit/public/platform/WebMediaStream.h"
13 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
15 #include "third_party/WebKit/public/platform/WebVector.h"
16 #include "third_party/WebKit/public/web/WebDocument.h"
17 #include "third_party/WebKit/public/web/WebMediaDevicesRequest.h"
18 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h"
19 #include "third_party/WebKit/public/web/WebUserMediaRequest.h"
21 using blink::WebMediaConstraints
;
22 using blink::WebMediaDeviceInfo
;
23 using blink::WebMediaDevicesRequest
;
24 using blink::WebMediaStream
;
25 using blink::WebMediaStreamSource
;
26 using blink::WebMediaStreamTrack
;
27 using blink::WebString
;
28 using blink::WebUserMediaRequest
;
29 using blink::WebVector
;
33 class UserMediaRequestTask
: public WebMethodTask
<MockWebUserMediaClient
> {
35 UserMediaRequestTask(MockWebUserMediaClient
* object
,
36 const WebUserMediaRequest
& request
,
37 const WebMediaStream result
)
38 : WebMethodTask
<MockWebUserMediaClient
>(object
),
41 DCHECK(!result_
.isNull());
44 virtual void RunIfValid() OVERRIDE
{ request_
.requestSucceeded(result_
); }
47 WebUserMediaRequest request_
;
48 WebMediaStream result_
;
51 class UserMediaRequestConstraintFailedTask
52 : public WebMethodTask
<MockWebUserMediaClient
> {
54 UserMediaRequestConstraintFailedTask(MockWebUserMediaClient
* object
,
55 const WebUserMediaRequest
& request
,
56 const WebString
& constraint
)
57 : WebMethodTask
<MockWebUserMediaClient
>(object
),
59 constraint_(constraint
) {}
61 virtual void RunIfValid() OVERRIDE
{
62 request_
.requestFailedConstraint(constraint_
);
66 WebUserMediaRequest request_
;
67 WebString constraint_
;
70 class UserMediaRequestPermissionDeniedTask
71 : public WebMethodTask
<MockWebUserMediaClient
> {
73 UserMediaRequestPermissionDeniedTask(MockWebUserMediaClient
* object
,
74 const WebUserMediaRequest
& request
)
75 : WebMethodTask
<MockWebUserMediaClient
>(object
),
78 virtual void RunIfValid() OVERRIDE
{ request_
.requestFailed(); }
81 WebUserMediaRequest request_
;
84 class MediaDevicesRequestTask
: public WebMethodTask
<MockWebUserMediaClient
> {
86 MediaDevicesRequestTask(MockWebUserMediaClient
* object
,
87 const WebMediaDevicesRequest
& request
,
88 const WebVector
<WebMediaDeviceInfo
>& result
)
89 : WebMethodTask
<MockWebUserMediaClient
>(object
),
93 virtual void RunIfValid() OVERRIDE
{ request_
.requestSucceeded(result_
); }
96 WebMediaDevicesRequest request_
;
97 WebVector
<WebMediaDeviceInfo
> result_
;
100 class MockExtraData
: public WebMediaStream::ExtraData
{
105 MockWebUserMediaClient::MockWebUserMediaClient(WebTestDelegate
* delegate
)
106 : delegate_(delegate
) {}
108 void MockWebUserMediaClient::requestUserMedia(
109 const WebUserMediaRequest
& stream_request
) {
110 DCHECK(!stream_request
.isNull());
111 WebUserMediaRequest request
= stream_request
;
113 if (request
.ownerDocument().isNull() || !request
.ownerDocument().frame()) {
115 new UserMediaRequestPermissionDeniedTask(this, request
));
119 WebMediaConstraints constraints
= request
.audioConstraints();
120 WebString failed_constraint
;
121 if (!constraints
.isNull() &&
122 !MockConstraints::VerifyConstraints(constraints
, &failed_constraint
)) {
123 delegate_
->postTask(new UserMediaRequestConstraintFailedTask(
124 this, request
, failed_constraint
));
127 constraints
= request
.videoConstraints();
128 if (!constraints
.isNull() &&
129 !MockConstraints::VerifyConstraints(constraints
, &failed_constraint
)) {
130 delegate_
->postTask(new UserMediaRequestConstraintFailedTask(
131 this, request
, failed_constraint
));
135 const size_t zero
= 0;
136 const size_t one
= 1;
137 WebVector
<WebMediaStreamTrack
> audio_tracks(request
.audio() ? one
: zero
);
138 WebVector
<WebMediaStreamTrack
> video_tracks(request
.video() ? one
: zero
);
140 if (request
.audio()) {
141 WebMediaStreamSource source
;
142 source
.initialize("MockAudioDevice#1",
143 WebMediaStreamSource::TypeAudio
,
144 "Mock audio device");
145 audio_tracks
[0].initialize(source
);
148 if (request
.video()) {
149 WebMediaStreamSource source
;
150 source
.initialize("MockVideoDevice#1",
151 WebMediaStreamSource::TypeVideo
,
152 "Mock video device");
153 video_tracks
[0].initialize(source
);
156 WebMediaStream stream
;
157 stream
.initialize(audio_tracks
, video_tracks
);
159 stream
.setExtraData(new MockExtraData());
161 delegate_
->postTask(new UserMediaRequestTask(this, request
, stream
));
164 void MockWebUserMediaClient::cancelUserMediaRequest(
165 const WebUserMediaRequest
&) {
168 void MockWebUserMediaClient::requestMediaDevices(
169 const WebMediaDevicesRequest
& request
) {
170 const size_t three
= 3;
171 WebVector
<WebMediaDeviceInfo
> devices(three
);
173 devices
[0].initialize("device1",
174 WebMediaDeviceInfo::MediaDeviceKindAudioInput
,
175 "Built-in microphone",
177 devices
[1].initialize("device2",
178 WebMediaDeviceInfo::MediaDeviceKindAudioOutput
,
181 devices
[2].initialize("device3",
182 WebMediaDeviceInfo::MediaDeviceKindVideoInput
,
186 delegate_
->postTask(new MediaDevicesRequestTask(this, request
, devices
));
189 void MockWebUserMediaClient::cancelMediaDevicesRequest(
190 const WebMediaDevicesRequest
&) {
193 } // namespace content