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
,
85 void DeviceGetSupportedFormats(int device_id
,
86 media::VideoCaptureSessionId session_id
) {
87 // When the mock message filter receives a request for the device
88 // supported formats, replies immediately with an empty format list.
89 OnDeviceSupportedFormatsEnumerated(
90 media::VideoCaptureFormats());
93 void DeviceGetFormatsInUse(int device_id
,
94 media::VideoCaptureSessionId session_id
) {
95 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
98 void ReceiveStateChangeMessage(VideoCaptureState state
) {
99 OnStateChanged(state
);
102 const media::VideoCaptureParams
& capture_params() const {
103 return capture_params_
;
107 media::VideoCaptureParams capture_params_
;
110 VideoCaptureImplTest() {
111 params_small_
.requested_format
= media::VideoCaptureFormat(
112 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420
);
114 params_large_
.requested_format
= media::VideoCaptureFormat(
115 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420
);
117 child_process_
.reset(new ChildProcess());
119 message_filter_
= new MockVideoCaptureMessageFilter
;
122 video_capture_impl_
.reset(new MockVideoCaptureImpl(
123 session_id_
, message_filter_
.get()));
125 video_capture_impl_
->device_id_
= 2;
128 virtual ~VideoCaptureImplTest() {
132 MOCK_METHOD2(OnFrameReady
,
133 void(const scoped_refptr
<media::VideoFrame
>&,
134 const base::TimeTicks
&));
135 MOCK_METHOD1(OnStateUpdate
, void(VideoCaptureState
));
136 MOCK_METHOD1(OnDeviceFormatsInUse
,
137 void(const media::VideoCaptureFormats
&));
138 MOCK_METHOD1(OnDeviceSupportedFormats
,
139 void(const media::VideoCaptureFormats
&));
142 video_capture_impl_
->Init();
145 void StartCapture(int client_id
,
146 const media::VideoCaptureParams
& params
) {
147 video_capture_impl_
->StartCapture(
149 base::Bind(&VideoCaptureImplTest::OnStateUpdate
,
150 base::Unretained(this)),
151 base::Bind(&VideoCaptureImplTest::OnFrameReady
,
152 base::Unretained(this)));
155 void StopCapture(int client_id
) {
156 video_capture_impl_
->StopCapture(client_id
);
160 video_capture_impl_
->DeInit();
163 void GetDeviceSupportedFormats() {
164 const base::Callback
<void(const media::VideoCaptureFormats
&)>
165 callback
= base::Bind(
166 &VideoCaptureImplTest::OnDeviceSupportedFormats
,
167 base::Unretained(this));
168 video_capture_impl_
->GetDeviceSupportedFormats(callback
);
171 void GetDeviceFormatsInUse() {
172 const base::Callback
<void(const media::VideoCaptureFormats
&)>
173 callback
= base::Bind(
174 &VideoCaptureImplTest::OnDeviceFormatsInUse
,
175 base::Unretained(this));
176 video_capture_impl_
->GetDeviceFormatsInUse(callback
);
179 base::MessageLoop message_loop_
;
180 scoped_ptr
<ChildProcess
> child_process_
;
181 scoped_refptr
<MockVideoCaptureMessageFilter
> message_filter_
;
182 media::VideoCaptureSessionId session_id_
;
183 scoped_ptr
<MockVideoCaptureImpl
> video_capture_impl_
;
184 media::VideoCaptureParams params_small_
;
185 media::VideoCaptureParams params_large_
;
188 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest
);
191 TEST_F(VideoCaptureImplTest
, Simple
) {
192 // Execute SetCapture() and StopCapture() for one client.
193 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
));
194 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
));
197 StartCapture(0, params_small_
);
202 TEST_F(VideoCaptureImplTest
, TwoClientsInSequence
) {
203 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
)).Times(2);
204 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
)).Times(2);
207 StartCapture(0, params_small_
);
209 StartCapture(1, params_small_
);
214 TEST_F(VideoCaptureImplTest
, LargeAndSmall
) {
215 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
)).Times(2);
216 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
)).Times(2);
219 StartCapture(0, params_large_
);
221 StartCapture(1, params_small_
);
226 TEST_F(VideoCaptureImplTest
, SmallAndLarge
) {
227 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
)).Times(2);
228 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
)).Times(2);
231 StartCapture(0, params_small_
);
233 StartCapture(1, params_large_
);
238 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the
239 // provided callback.
240 TEST_F(VideoCaptureImplTest
, GetDeviceFormats
) {
241 EXPECT_CALL(*this, OnDeviceSupportedFormats(_
));
244 GetDeviceSupportedFormats();
248 // Check that two requests to GetDeviceSupportedFormats() end up eventually
249 // calling the provided callbacks.
250 TEST_F(VideoCaptureImplTest
, TwoClientsGetDeviceFormats
) {
251 EXPECT_CALL(*this, OnDeviceSupportedFormats(_
)).Times(2);
254 GetDeviceSupportedFormats();
255 GetDeviceSupportedFormats();
259 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
260 // provided callback.
261 TEST_F(VideoCaptureImplTest
, GetDeviceFormatsInUse
) {
262 EXPECT_CALL(*this, OnDeviceFormatsInUse(_
));
265 GetDeviceFormatsInUse();
269 TEST_F(VideoCaptureImplTest
, AlreadyStarted
) {
270 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
)).Times(2);
271 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
)).Times(2);
274 StartCapture(0, params_small_
);
275 StartCapture(1, params_large_
);
279 DCHECK(video_capture_impl_
->capture_params().requested_format
281 params_small_
.requested_format
.frame_size
);
284 TEST_F(VideoCaptureImplTest
, EndedBeforeStop
) {
285 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
));
286 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
));
289 StartCapture(0, params_small_
);
291 // Receive state change message from browser.
292 video_capture_impl_
->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED
);
298 TEST_F(VideoCaptureImplTest
, ErrorBeforeStop
) {
299 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
));
300 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR
));
303 StartCapture(0, params_small_
);
305 // Receive state change message from browser.
306 video_capture_impl_
->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR
);
312 } // namespace content