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.
4
5 #include "content/shell/renderer/test_runner/mock_web_user_media_client.h"
6
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"
20
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;
30
31 namespace content {
32
33 class UserMediaRequestTask : public WebMethodTask<MockWebUserMediaClient> {
34 public:
UserMediaRequestTask(MockWebUserMediaClient * object,const WebUserMediaRequest & request,const WebMediaStream result)35 UserMediaRequestTask(MockWebUserMediaClient* object,
36 const WebUserMediaRequest& request,
37 const WebMediaStream result)
38 : WebMethodTask<MockWebUserMediaClient>(object),
39 request_(request),
40 result_(result) {
41 DCHECK(!result_.isNull());
42 }
43
runIfValid()44 virtual void runIfValid() OVERRIDE {
45 request_.requestSucceeded(result_);
46 }
47
48 private:
49 WebUserMediaRequest request_;
50 WebMediaStream result_;
51 };
52
53 class UserMediaRequestConstraintFailedTask
54 : public WebMethodTask<MockWebUserMediaClient> {
55 public:
UserMediaRequestConstraintFailedTask(MockWebUserMediaClient * object,const WebUserMediaRequest & request,const WebString & constraint)56 UserMediaRequestConstraintFailedTask(MockWebUserMediaClient* object,
57 const WebUserMediaRequest& request,
58 const WebString& constraint)
59 : WebMethodTask<MockWebUserMediaClient>(object),
60 request_(request),
61 constraint_(constraint) {}
62
runIfValid()63 virtual void runIfValid() OVERRIDE {
64 request_.requestFailedConstraint(constraint_);
65 }
66
67 private:
68 WebUserMediaRequest request_;
69 WebString constraint_;
70 };
71
72 class UserMediaRequestPermissionDeniedTask
73 : public WebMethodTask<MockWebUserMediaClient> {
74 public:
UserMediaRequestPermissionDeniedTask(MockWebUserMediaClient * object,const WebUserMediaRequest & request)75 UserMediaRequestPermissionDeniedTask(MockWebUserMediaClient* object,
76 const WebUserMediaRequest& request)
77 : WebMethodTask<MockWebUserMediaClient>(object),
78 request_(request) {}
79
runIfValid()80 virtual void runIfValid() OVERRIDE {
81 request_.requestFailed();
82 }
83
84 private:
85 WebUserMediaRequest request_;
86 };
87
88 class MediaDevicesRequestTask : public WebMethodTask<MockWebUserMediaClient> {
89 public:
MediaDevicesRequestTask(MockWebUserMediaClient * object,const WebMediaDevicesRequest & request,const WebVector<WebMediaDeviceInfo> & result)90 MediaDevicesRequestTask(MockWebUserMediaClient* object,
91 const WebMediaDevicesRequest& request,
92 const WebVector<WebMediaDeviceInfo>& result)
93 : WebMethodTask<MockWebUserMediaClient>(object),
94 request_(request),
95 result_(result) {}
96
runIfValid()97 virtual void runIfValid() OVERRIDE {
98 request_.requestSucceeded(result_);
99 }
100
101 private:
102 WebMediaDevicesRequest request_;
103 WebVector<WebMediaDeviceInfo> result_;
104 };
105
106 class MockExtraData : public WebMediaStream::ExtraData {
107 public:
108 int foo;
109 };
110
MockWebUserMediaClient(WebTestDelegate * delegate)111 MockWebUserMediaClient::MockWebUserMediaClient(WebTestDelegate* delegate)
112 : delegate_(delegate) {}
113
requestUserMedia(const WebUserMediaRequest & stream_request)114 void MockWebUserMediaClient::requestUserMedia(
115 const WebUserMediaRequest& stream_request) {
116 DCHECK(!stream_request.isNull());
117 WebUserMediaRequest request = stream_request;
118
119 if (request.ownerDocument().isNull() || !request.ownerDocument().frame()) {
120 delegate_->postTask(
121 new UserMediaRequestPermissionDeniedTask(this, request));
122 return;
123 }
124
125 WebMediaConstraints constraints = request.audioConstraints();
126 WebString failed_constraint;
127 if (!constraints.isNull() &&
128 !MockConstraints::VerifyConstraints(constraints, &failed_constraint)) {
129 delegate_->postTask(new UserMediaRequestConstraintFailedTask(
130 this, request, failed_constraint));
131 return;
132 }
133 constraints = request.videoConstraints();
134 if (!constraints.isNull() &&
135 !MockConstraints::VerifyConstraints(constraints, &failed_constraint)) {
136 delegate_->postTask(new UserMediaRequestConstraintFailedTask(
137 this, request, failed_constraint));
138 return;
139 }
140
141 const size_t zero = 0;
142 const size_t one = 1;
143 WebVector<WebMediaStreamTrack> audio_tracks(request.audio() ? one : zero);
144 WebVector<WebMediaStreamTrack> video_tracks(request.video() ? one : zero);
145
146 if (request.audio()) {
147 WebMediaStreamSource source;
148 source.initialize("MockAudioDevice#1",
149 WebMediaStreamSource::TypeAudio,
150 "Mock audio device");
151 audio_tracks[0].initialize(source);
152 }
153
154 if (request.video()) {
155 WebMediaStreamSource source;
156 source.initialize("MockVideoDevice#1",
157 WebMediaStreamSource::TypeVideo,
158 "Mock video device");
159 video_tracks[0].initialize(source);
160 }
161
162 WebMediaStream stream;
163 stream.initialize(audio_tracks, video_tracks);
164
165 stream.setExtraData(new MockExtraData());
166
167 delegate_->postTask(new UserMediaRequestTask(this, request, stream));
168 }
169
cancelUserMediaRequest(const WebUserMediaRequest &)170 void MockWebUserMediaClient::cancelUserMediaRequest(
171 const WebUserMediaRequest&) {
172 }
173
requestMediaDevices(const WebMediaDevicesRequest & request)174 void MockWebUserMediaClient::requestMediaDevices(
175 const WebMediaDevicesRequest& request) {
176 const size_t three = 3;
177 WebVector<WebMediaDeviceInfo> devices(three);
178
179 devices[0].initialize("device1",
180 WebMediaDeviceInfo::MediaDeviceKindAudioInput,
181 "Built-in microphone",
182 "group1");
183 devices[1].initialize("device2",
184 WebMediaDeviceInfo::MediaDeviceKindAudioOutput,
185 "Built-in speakers",
186 "group1");
187 devices[2].initialize("device3",
188 WebMediaDeviceInfo::MediaDeviceKindVideoInput,
189 "Build-in webcam",
190 "group2");
191
192 delegate_->postTask(new MediaDevicesRequestTask(this, request, devices));
193 }
194
cancelMediaDevicesRequest(const WebMediaDevicesRequest &)195 void MockWebUserMediaClient::cancelMediaDevicesRequest(
196 const WebMediaDevicesRequest&) {
197 }
198
199 } // namespace content
200