Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / media / video_capture_impl_unittest.cc
blob6859402a4285af1b115de1e6b2715e142e94009f
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.
5 #include "base/message_loop/message_loop.h"
6 #include "content/child/child_process.h"
7 #include "content/common/media/video_capture_messages.h"
8 #include "content/renderer/media/video_capture_impl.h"
9 #include "media/base/bind_to_current_loop.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using ::testing::_;
14 using ::testing::AtLeast;
15 using ::testing::InvokeWithoutArgs;
16 using ::testing::Return;
17 using ::testing::SaveArg;
19 namespace content {
21 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
22 public:
23 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
25 // Filter implementation.
26 MOCK_METHOD1(Send, bool(IPC::Message* message));
28 protected:
29 virtual ~MockVideoCaptureMessageFilter() {}
31 private:
32 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
35 class VideoCaptureImplTest : public ::testing::Test {
36 public:
37 class MockVideoCaptureImpl : public VideoCaptureImpl {
38 public:
39 MockVideoCaptureImpl(const media::VideoCaptureSessionId id,
40 VideoCaptureMessageFilter* filter)
41 : VideoCaptureImpl(id, filter) {
43 ~MockVideoCaptureImpl() override {}
45 // Override Send() to mimic device to send events.
46 void Send(IPC::Message* message) override {
47 CHECK(message);
49 // In this method, messages are sent to the according handlers as if
50 // we are the device.
51 bool handled = true;
52 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message)
53 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, DeviceStartCapture)
54 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, DevicePauseCapture)
55 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, DeviceStopCapture)
56 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady,
57 DeviceReceiveEmptyBuffer)
58 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceSupportedFormats,
59 DeviceGetSupportedFormats)
60 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceFormatsInUse,
61 DeviceGetFormatsInUse)
62 IPC_MESSAGE_UNHANDLED(handled = false)
63 IPC_END_MESSAGE_MAP()
64 EXPECT_TRUE(handled);
65 delete message;
68 void DeviceStartCapture(int device_id,
69 media::VideoCaptureSessionId session_id,
70 const media::VideoCaptureParams& params) {
71 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED);
72 capture_params_ = params;
75 void DevicePauseCapture(int device_id) {}
77 void DeviceStopCapture(int device_id) {
78 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED);
81 void DeviceReceiveEmptyBuffer(int device_id,
82 int buffer_id,
83 uint32 sync_point,
84 double consumer_resource_utilization) {}
86 void DeviceGetSupportedFormats(int device_id,
87 media::VideoCaptureSessionId session_id) {
88 // When the mock message filter receives a request for the device
89 // supported formats, replies immediately with an empty format list.
90 OnDeviceSupportedFormatsEnumerated(
91 media::VideoCaptureFormats());
94 void DeviceGetFormatsInUse(int device_id,
95 media::VideoCaptureSessionId session_id) {
96 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
99 void ReceiveStateChangeMessage(VideoCaptureState state) {
100 OnStateChanged(state);
103 const media::VideoCaptureParams& capture_params() const {
104 return capture_params_;
107 private:
108 media::VideoCaptureParams capture_params_;
111 VideoCaptureImplTest() {
112 params_small_.requested_format = media::VideoCaptureFormat(
113 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420);
115 params_large_.requested_format = media::VideoCaptureFormat(
116 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420);
118 child_process_.reset(new ChildProcess());
120 message_filter_ = new MockVideoCaptureMessageFilter;
121 session_id_ = 1;
123 video_capture_impl_.reset(new MockVideoCaptureImpl(
124 session_id_, message_filter_.get()));
126 video_capture_impl_->device_id_ = 2;
129 virtual ~VideoCaptureImplTest() {
132 protected:
133 MOCK_METHOD2(OnFrameReady,
134 void(const scoped_refptr<media::VideoFrame>&,
135 const base::TimeTicks&));
136 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState));
137 MOCK_METHOD1(OnDeviceFormatsInUse,
138 void(const media::VideoCaptureFormats&));
139 MOCK_METHOD1(OnDeviceSupportedFormats,
140 void(const media::VideoCaptureFormats&));
142 void Init() {
143 video_capture_impl_->Init();
146 void StartCapture(int client_id,
147 const media::VideoCaptureParams& params) {
148 video_capture_impl_->StartCapture(
149 client_id, params,
150 base::Bind(&VideoCaptureImplTest::OnStateUpdate,
151 base::Unretained(this)),
152 base::Bind(&VideoCaptureImplTest::OnFrameReady,
153 base::Unretained(this)));
156 void StopCapture(int client_id) {
157 video_capture_impl_->StopCapture(client_id);
160 void DeInit() {
161 video_capture_impl_->DeInit();
164 void GetDeviceSupportedFormats() {
165 const base::Callback<void(const media::VideoCaptureFormats&)>
166 callback = base::Bind(
167 &VideoCaptureImplTest::OnDeviceSupportedFormats,
168 base::Unretained(this));
169 video_capture_impl_->GetDeviceSupportedFormats(callback);
172 void GetDeviceFormatsInUse() {
173 const base::Callback<void(const media::VideoCaptureFormats&)>
174 callback = base::Bind(
175 &VideoCaptureImplTest::OnDeviceFormatsInUse,
176 base::Unretained(this));
177 video_capture_impl_->GetDeviceFormatsInUse(callback);
180 base::MessageLoop message_loop_;
181 scoped_ptr<ChildProcess> child_process_;
182 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
183 media::VideoCaptureSessionId session_id_;
184 scoped_ptr<MockVideoCaptureImpl> video_capture_impl_;
185 media::VideoCaptureParams params_small_;
186 media::VideoCaptureParams params_large_;
188 private:
189 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
192 TEST_F(VideoCaptureImplTest, Simple) {
193 // Execute SetCapture() and StopCapture() for one client.
194 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
195 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
197 Init();
198 StartCapture(0, params_small_);
199 StopCapture(0);
200 DeInit();
203 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
204 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
205 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
207 Init();
208 StartCapture(0, params_small_);
209 StopCapture(0);
210 StartCapture(1, params_small_);
211 StopCapture(1);
212 DeInit();
215 TEST_F(VideoCaptureImplTest, LargeAndSmall) {
216 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
217 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
219 Init();
220 StartCapture(0, params_large_);
221 StopCapture(0);
222 StartCapture(1, params_small_);
223 StopCapture(1);
224 DeInit();
227 TEST_F(VideoCaptureImplTest, SmallAndLarge) {
228 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
229 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
231 Init();
232 StartCapture(0, params_small_);
233 StopCapture(0);
234 StartCapture(1, params_large_);
235 StopCapture(1);
236 DeInit();
239 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the
240 // provided callback.
241 TEST_F(VideoCaptureImplTest, GetDeviceFormats) {
242 EXPECT_CALL(*this, OnDeviceSupportedFormats(_));
244 Init();
245 GetDeviceSupportedFormats();
246 DeInit();
249 // Check that two requests to GetDeviceSupportedFormats() end up eventually
250 // calling the provided callbacks.
251 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) {
252 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)).Times(2);
254 Init();
255 GetDeviceSupportedFormats();
256 GetDeviceSupportedFormats();
257 DeInit();
260 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
261 // provided callback.
262 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) {
263 EXPECT_CALL(*this, OnDeviceFormatsInUse(_));
265 Init();
266 GetDeviceFormatsInUse();
267 DeInit();
270 TEST_F(VideoCaptureImplTest, AlreadyStarted) {
271 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2);
272 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2);
274 Init();
275 StartCapture(0, params_small_);
276 StartCapture(1, params_large_);
277 StopCapture(0);
278 StopCapture(1);
279 DeInit();
280 DCHECK(video_capture_impl_->capture_params().requested_format
281 .frame_size ==
282 params_small_.requested_format.frame_size);
285 TEST_F(VideoCaptureImplTest, EndedBeforeStop) {
286 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
287 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
289 Init();
290 StartCapture(0, params_small_);
292 // Receive state change message from browser.
293 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED);
295 StopCapture(0);
296 DeInit();
299 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) {
300 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
301 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR));
303 Init();
304 StartCapture(0, params_small_);
306 // Receive state change message from browser.
307 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR);
309 StopCapture(0);
310 DeInit();
313 } // namespace content