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"
14 using ::testing::AtLeast
;
15 using ::testing::InvokeWithoutArgs
;
16 using ::testing::Return
;
17 using ::testing::SaveArg
;
21 class MockVideoCaptureMessageFilter
: public VideoCaptureMessageFilter
{
23 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
25 // Filter implementation.
26 MOCK_METHOD1(Send
, bool(IPC::Message
* message
));
29 virtual ~MockVideoCaptureMessageFilter() {}
32 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter
);
35 class VideoCaptureImplTest
: public ::testing::Test
{
37 class MockVideoCaptureImpl
: public VideoCaptureImpl
{
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
{
49 // In this method, messages are sent to the according handlers as if
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)
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
,
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_
;
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
;
123 video_capture_impl_
.reset(new MockVideoCaptureImpl(
124 session_id_
, message_filter_
.get()));
126 video_capture_impl_
->device_id_
= 2;
129 virtual ~VideoCaptureImplTest() {
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
&));
143 video_capture_impl_
->Init();
146 void StartCapture(int client_id
,
147 const media::VideoCaptureParams
& params
) {
148 video_capture_impl_
->StartCapture(
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
);
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_
;
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
));
198 StartCapture(0, params_small_
);
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);
208 StartCapture(0, params_small_
);
210 StartCapture(1, params_small_
);
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);
220 StartCapture(0, params_large_
);
222 StartCapture(1, params_small_
);
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);
232 StartCapture(0, params_small_
);
234 StartCapture(1, params_large_
);
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(_
));
245 GetDeviceSupportedFormats();
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);
255 GetDeviceSupportedFormats();
256 GetDeviceSupportedFormats();
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(_
));
266 GetDeviceFormatsInUse();
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);
275 StartCapture(0, params_small_
);
276 StartCapture(1, params_large_
);
280 DCHECK(video_capture_impl_
->capture_params().requested_format
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
));
290 StartCapture(0, params_small_
);
292 // Receive state change message from browser.
293 video_capture_impl_
->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED
);
299 TEST_F(VideoCaptureImplTest
, ErrorBeforeStop
) {
300 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
));
301 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR
));
304 StartCapture(0, params_small_
);
306 // Receive state change message from browser.
307 video_capture_impl_
->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR
);
313 } // namespace content