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 // TODO(ajose): http://crbug.com/522145 Improve and expand these tests.
22 // In particular, exercise VideoCaptureHostMsg_BufferReady.
23 class MockVideoCaptureMessageFilter
: public VideoCaptureMessageFilter
{
25 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
27 // Filter implementation.
28 MOCK_METHOD1(Send
, bool(IPC::Message
* message
));
31 virtual ~MockVideoCaptureMessageFilter() {}
34 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter
);
37 class VideoCaptureImplTest
: public ::testing::Test
{
39 class MockVideoCaptureImpl
: public VideoCaptureImpl
{
41 MockVideoCaptureImpl(const media::VideoCaptureSessionId id
,
42 VideoCaptureMessageFilter
* filter
)
43 : VideoCaptureImpl(id
, filter
) {
45 ~MockVideoCaptureImpl() override
{}
47 // Override Send() to mimic device to send events.
48 void Send(IPC::Message
* message
) override
{
51 // In this method, messages are sent to the according handlers as if
54 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl
, *message
)
55 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start
, DeviceStartCapture
)
56 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause
, DevicePauseCapture
)
57 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop
, DeviceStopCapture
)
58 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady
,
59 DeviceReceiveEmptyBuffer
)
60 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceSupportedFormats
,
61 DeviceGetSupportedFormats
)
62 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceFormatsInUse
,
63 DeviceGetFormatsInUse
)
64 IPC_MESSAGE_UNHANDLED(handled
= false)
70 void DeviceStartCapture(int device_id
,
71 media::VideoCaptureSessionId session_id
,
72 const media::VideoCaptureParams
& params
) {
73 // Do not call OnStateChanged(VIDEO_CAPTURE_STATE_STARTED) here, as this
74 // does not accurately reflect the behavior of the VideoCaptureHost.
75 capture_params_
= params
;
78 void DevicePauseCapture(int device_id
) {}
80 void DeviceStopCapture(int device_id
) {
81 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED
);
84 void DeviceReceiveEmptyBuffer(int device_id
,
87 double consumer_resource_utilization
) {}
89 void DeviceGetSupportedFormats(int device_id
,
90 media::VideoCaptureSessionId session_id
) {
91 // When the mock message filter receives a request for the device
92 // supported formats, replies immediately with an empty format list.
93 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats());
96 void DeviceGetFormatsInUse(int device_id
,
97 media::VideoCaptureSessionId session_id
) {
98 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
101 void ReceiveStateChangeMessage(VideoCaptureState state
) {
102 OnStateChanged(state
);
105 const media::VideoCaptureParams
& capture_params() const {
106 return capture_params_
;
110 media::VideoCaptureParams capture_params_
;
113 VideoCaptureImplTest() {
114 params_small_
.requested_format
= media::VideoCaptureFormat(
115 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420
);
117 params_large_
.requested_format
= media::VideoCaptureFormat(
118 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420
);
120 child_process_
.reset(new ChildProcess());
122 message_filter_
= new MockVideoCaptureMessageFilter
;
125 video_capture_impl_
.reset(new MockVideoCaptureImpl(
126 session_id_
, message_filter_
.get()));
128 video_capture_impl_
->device_id_
= 2;
131 virtual ~VideoCaptureImplTest() {
135 MOCK_METHOD2(OnFrameReady
,
136 void(const scoped_refptr
<media::VideoFrame
>&,
137 const base::TimeTicks
&));
138 MOCK_METHOD1(OnStateUpdate
, void(VideoCaptureState
));
139 MOCK_METHOD1(OnDeviceFormatsInUse
,
140 void(const media::VideoCaptureFormats
&));
141 MOCK_METHOD1(OnDeviceSupportedFormats
,
142 void(const media::VideoCaptureFormats
&));
145 video_capture_impl_
->Init();
148 void StartCapture(const media::VideoCaptureParams
& params
) {
149 video_capture_impl_
->StartCapture(
150 params
, base::Bind(&VideoCaptureImplTest::OnStateUpdate
,
151 base::Unretained(this)),
152 base::Bind(&VideoCaptureImplTest::OnFrameReady
,
153 base::Unretained(this)));
156 void StopCapture() { video_capture_impl_
->StopCapture(); }
159 video_capture_impl_
->DeInit();
162 void GetDeviceSupportedFormats() {
163 const base::Callback
<void(const media::VideoCaptureFormats
&)>
164 callback
= base::Bind(
165 &VideoCaptureImplTest::OnDeviceSupportedFormats
,
166 base::Unretained(this));
167 video_capture_impl_
->GetDeviceSupportedFormats(callback
);
170 void GetDeviceFormatsInUse() {
171 const base::Callback
<void(const media::VideoCaptureFormats
&)>
172 callback
= base::Bind(
173 &VideoCaptureImplTest::OnDeviceFormatsInUse
,
174 base::Unretained(this));
175 video_capture_impl_
->GetDeviceFormatsInUse(callback
);
178 base::MessageLoop message_loop_
;
179 scoped_ptr
<ChildProcess
> child_process_
;
180 scoped_refptr
<MockVideoCaptureMessageFilter
> message_filter_
;
181 media::VideoCaptureSessionId session_id_
;
182 scoped_ptr
<MockVideoCaptureImpl
> video_capture_impl_
;
183 media::VideoCaptureParams params_small_
;
184 media::VideoCaptureParams params_large_
;
187 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest
);
190 // Execute SetCapture() and StopCapture() for one client.
191 TEST_F(VideoCaptureImplTest
, Simple
) {
192 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
));
193 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
));
196 StartCapture(params_small_
);
201 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the
202 // provided callback.
203 TEST_F(VideoCaptureImplTest
, GetDeviceFormats
) {
204 EXPECT_CALL(*this, OnDeviceSupportedFormats(_
));
207 GetDeviceSupportedFormats();
211 // Check that two requests to GetDeviceSupportedFormats() end up eventually
212 // calling the provided callbacks.
213 TEST_F(VideoCaptureImplTest
, TwoClientsGetDeviceFormats
) {
214 EXPECT_CALL(*this, OnDeviceSupportedFormats(_
)).Times(2);
217 GetDeviceSupportedFormats();
218 GetDeviceSupportedFormats();
222 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
223 // provided callback.
224 TEST_F(VideoCaptureImplTest
, GetDeviceFormatsInUse
) {
225 EXPECT_CALL(*this, OnDeviceFormatsInUse(_
));
228 GetDeviceFormatsInUse();
232 TEST_F(VideoCaptureImplTest
, EndedBeforeStop
) {
233 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
));
234 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED
));
237 StartCapture(params_small_
);
239 // Receive state change message from browser.
240 video_capture_impl_
->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED
);
246 TEST_F(VideoCaptureImplTest
, ErrorBeforeStop
) {
247 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED
));
248 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR
));
251 StartCapture(params_small_
);
253 // Receive state change message from browser.
254 video_capture_impl_
->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR
);
260 } // namespace content