Don't preload rarely seen large images
[chromium-blink-merge.git] / components / test_runner / mock_web_user_media_client.cc
blobf30fac9924beb9f87cb5df90cdd323c0e7a15871
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> {
39 public:
40 UserMediaRequestTask(MockWebUserMediaClient* object,
41 const WebUserMediaRequest& request,
42 const WebMediaStream result)
43 : WebMethodTask<MockWebUserMediaClient>(object),
44 request_(request),
45 result_(result) {
46 DCHECK(!result_.isNull());
49 void RunIfValid() override { request_.requestSucceeded(result_); }
51 private:
52 WebUserMediaRequest request_;
53 WebMediaStream result_;
55 DISALLOW_COPY_AND_ASSIGN(UserMediaRequestTask);
58 class UserMediaRequestConstraintFailedTask
59 : public WebMethodTask<MockWebUserMediaClient> {
60 public:
61 UserMediaRequestConstraintFailedTask(MockWebUserMediaClient* object,
62 const WebUserMediaRequest& request,
63 const WebString& constraint)
64 : WebMethodTask<MockWebUserMediaClient>(object),
65 request_(request),
66 constraint_(constraint) {}
68 void RunIfValid() override { request_.requestFailedConstraint(constraint_); }
70 private:
71 WebUserMediaRequest request_;
72 WebString constraint_;
74 DISALLOW_COPY_AND_ASSIGN(UserMediaRequestConstraintFailedTask);
77 class UserMediaRequestPermissionDeniedTask
78 : public WebMethodTask<MockWebUserMediaClient> {
79 public:
80 UserMediaRequestPermissionDeniedTask(MockWebUserMediaClient* object,
81 const WebUserMediaRequest& request)
82 : WebMethodTask<MockWebUserMediaClient>(object),
83 request_(request) {}
85 void RunIfValid() override { request_.requestFailed(); }
87 private:
88 WebUserMediaRequest request_;
90 DISALLOW_COPY_AND_ASSIGN(UserMediaRequestPermissionDeniedTask);
93 class MediaDevicesRequestTask : public WebMethodTask<MockWebUserMediaClient> {
94 public:
95 MediaDevicesRequestTask(MockWebUserMediaClient* object,
96 const WebMediaDevicesRequest& request,
97 const WebVector<WebMediaDeviceInfo>& result)
98 : WebMethodTask<MockWebUserMediaClient>(object),
99 request_(request),
100 result_(result) {}
102 void RunIfValid() override { request_.requestSucceeded(result_); }
104 private:
105 WebMediaDevicesRequest request_;
106 WebVector<WebMediaDeviceInfo> result_;
108 DISALLOW_COPY_AND_ASSIGN(MediaDevicesRequestTask);
111 class SourcesRequestTask : public WebMethodTask<MockWebUserMediaClient> {
112 public:
113 SourcesRequestTask(MockWebUserMediaClient* object,
114 const WebMediaStreamTrackSourcesRequest& request,
115 const WebVector<WebSourceInfo>& result)
116 : WebMethodTask<MockWebUserMediaClient>(object),
117 request_(request),
118 result_(result) {}
120 void RunIfValid() override { request_.requestSucceeded(result_); }
122 private:
123 WebMediaStreamTrackSourcesRequest request_;
124 WebVector<WebSourceInfo> result_;
126 DISALLOW_COPY_AND_ASSIGN(SourcesRequestTask);
129 class MockExtraData : public WebMediaStream::ExtraData {
130 public:
131 int foo;
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()) {
143 delegate_->PostTask(
144 new UserMediaRequestPermissionDeniedTask(this, request));
145 return;
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));
154 return;
156 constraints = request.videoConstraints();
157 if (!constraints.isNull() &&
158 !MockConstraints::VerifyConstraints(constraints, &failed_constraint)) {
159 delegate_->PostTask(new UserMediaRequestConstraintFailedTask(
160 this, request, failed_constraint));
161 return;
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,
173 "Mock audio device",
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,
182 "Mock video device",
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) {
201 struct {
202 const char* device_id;
203 WebMediaDeviceInfo::MediaDeviceKind kind;
204 const char* label;
205 const char* group_id;
206 } test_devices[] = {
208 "device1",
209 WebMediaDeviceInfo::MediaDeviceKindAudioInput,
210 "Built-in microphone",
211 "group1",
214 "device2",
215 WebMediaDeviceInfo::MediaDeviceKindAudioOutput,
216 "Built-in speakers",
217 "group1",
220 "device3",
221 WebMediaDeviceInfo::MediaDeviceKindVideoInput,
222 "Build-in webcam",
223 "group2",
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) {
244 struct {
245 const char* id;
246 WebSourceInfo::SourceKind kind;
247 const char* label;
248 WebSourceInfo::VideoFacingMode facing;
249 } test_sources[] = {
251 "device1",
252 WebSourceInfo::SourceKindAudio,
253 "Built-in microphone",
254 WebSourceInfo::VideoFacingModeNone,
257 "device2",
258 WebSourceInfo::SourceKindVideo,
259 "Build-in webcam",
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