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.h"
6 #include "content/common/child_process.h"
7 #include "content/common/media/video_capture_messages.h"
8 #include "content/renderer/media/video_capture_impl.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
13 using ::testing::AtLeast
;
14 using ::testing::Return
;
16 #define CAPABILITY_SMALL {176, 144, 30, media::VideoCaptureCapability::kI420, \
18 #define CAPABILITY_LARGE {320, 240, 30, media::VideoCaptureCapability::kI420, \
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 MockVideoCaptureClient
: public media::VideoCapture::EventHandler
{
39 MockVideoCaptureClient() {}
40 virtual ~MockVideoCaptureClient() {}
42 // EventHandler implementation.
43 MOCK_METHOD1(OnStarted
, void(media::VideoCapture
* capture
));
44 MOCK_METHOD1(OnStopped
, void(media::VideoCapture
* capture
));
45 MOCK_METHOD1(OnPaused
, void(media::VideoCapture
* capture
));
46 MOCK_METHOD2(OnError
, void(media::VideoCapture
* capture
, int error_code
));
47 MOCK_METHOD1(OnRemoved
, void(media::VideoCapture
* capture
));
48 MOCK_METHOD2(OnBufferReady
,
49 void(media::VideoCapture
* capture
,
50 scoped_refptr
<media::VideoCapture::VideoFrameBuffer
> buf
));
51 MOCK_METHOD2(OnDeviceInfoReceived
,
52 void(media::VideoCapture
* capture
,
53 const media::VideoCaptureParams
& device_info
));
56 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient
);
59 class VideoCaptureImplTest
: public ::testing::Test
{
61 class MockVideoCaptureImpl
: public VideoCaptureImpl
{
63 MockVideoCaptureImpl(const media::VideoCaptureSessionId id
,
64 scoped_refptr
<base::MessageLoopProxy
> ml_proxy
,
65 VideoCaptureMessageFilter
* filter
)
66 : VideoCaptureImpl(id
, ml_proxy
, filter
) {
68 virtual ~MockVideoCaptureImpl() {}
70 // Override Send() to mimic device to send events.
71 virtual void Send(IPC::Message
* message
) OVERRIDE
{
74 // In this method, messages are sent to the according handlers as if
77 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl
, *message
)
78 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start
, DeviceStartCapture
)
79 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause
, DevicePauseCapture
)
80 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop
, DeviceStopCapture
)
81 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady
,
82 DeviceReceiveEmptyBuffer
)
83 IPC_MESSAGE_UNHANDLED(handled
= false)
89 void DeviceStartCapture(int device_id
,
90 const media::VideoCaptureParams
& params
) {
91 media::VideoCaptureParams device_info
= params
;
92 OnDeviceInfoReceived(device_info
);
93 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED
);
96 void DevicePauseCapture(int device_id
) {}
98 void DeviceStopCapture(int device_id
) {
99 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED
);
102 void DeviceReceiveEmptyBuffer(int device_id
, int buffer_id
) {}
105 VideoCaptureImplTest() {
106 message_loop_
.reset(new MessageLoop(MessageLoop::TYPE_IO
));
107 message_loop_proxy_
=
108 base::MessageLoopProxy::current().get();
109 child_process_
.reset(new ChildProcess());
111 message_filter_
= new MockVideoCaptureMessageFilter
;
114 video_capture_impl_
= new MockVideoCaptureImpl(session_id_
,
118 video_capture_impl_
->device_id_
= 2;
121 virtual ~VideoCaptureImplTest() {
122 delete video_capture_impl_
;
126 scoped_ptr
<MessageLoop
> message_loop_
;
127 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy_
;
128 scoped_ptr
<ChildProcess
> child_process_
;
129 scoped_refptr
<MockVideoCaptureMessageFilter
> message_filter_
;
130 media::VideoCaptureSessionId session_id_
;
131 MockVideoCaptureImpl
* video_capture_impl_
;
134 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest
);
137 TEST_F(VideoCaptureImplTest
, Simple
) {
138 // Execute SetCapture() and StopCapture() for one client.
139 scoped_ptr
<MockVideoCaptureClient
> client(new MockVideoCaptureClient
);
140 media::VideoCaptureCapability capability
= CAPABILITY_SMALL
;
142 EXPECT_CALL(*client
, OnStarted(_
))
144 EXPECT_CALL(*client
, OnDeviceInfoReceived(_
,_
))
147 video_capture_impl_
->StartCapture(client
.get(), capability
);
148 message_loop_
->RunUntilIdle();
150 EXPECT_CALL(*client
, OnStopped(_
))
152 EXPECT_CALL(*client
, OnRemoved(_
))
155 video_capture_impl_
->StopCapture(client
.get());
156 message_loop_
->RunUntilIdle();
159 TEST_F(VideoCaptureImplTest
, TwoClientsInSequence
) {
160 // Execute SetCapture() and StopCapture() for 2 clients in sequence.
161 scoped_ptr
<MockVideoCaptureClient
> client(new MockVideoCaptureClient
);
162 media::VideoCaptureCapability capability
= CAPABILITY_SMALL
;
164 EXPECT_CALL(*client
, OnStarted(_
))
166 EXPECT_CALL(*client
, OnDeviceInfoReceived(_
,_
))
169 video_capture_impl_
->StartCapture(client
.get(), capability
);
170 message_loop_
->RunUntilIdle();
172 EXPECT_CALL(*client
, OnStopped(_
))
174 EXPECT_CALL(*client
, OnRemoved(_
))
177 video_capture_impl_
->StopCapture(client
.get());
178 message_loop_
->RunUntilIdle();
180 EXPECT_CALL(*client
, OnStarted(_
))
182 EXPECT_CALL(*client
, OnDeviceInfoReceived(_
,_
))
185 video_capture_impl_
->StartCapture(client
.get(), capability
);
186 message_loop_
->RunUntilIdle();
188 EXPECT_CALL(*client
, OnStopped(_
))
190 EXPECT_CALL(*client
, OnRemoved(_
))
193 video_capture_impl_
->StopCapture(client
.get());
194 message_loop_
->RunUntilIdle();
197 TEST_F(VideoCaptureImplTest
, LargeAndSmall
) {
198 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
199 // The large client starts first and stops first.
200 scoped_ptr
<MockVideoCaptureClient
> client_small(new MockVideoCaptureClient
);
201 scoped_ptr
<MockVideoCaptureClient
> client_large(new MockVideoCaptureClient
);
202 media::VideoCaptureCapability capability_small
= CAPABILITY_SMALL
;
203 media::VideoCaptureCapability capability_large
= CAPABILITY_LARGE
;
205 EXPECT_CALL(*client_large
, OnStarted(_
))
207 EXPECT_CALL(*client_large
, OnDeviceInfoReceived(_
,_
))
209 EXPECT_CALL(*client_small
, OnStarted(_
))
211 EXPECT_CALL(*client_small
, OnDeviceInfoReceived(_
,_
))
214 video_capture_impl_
->StartCapture(client_large
.get(), capability_large
);
215 video_capture_impl_
->StartCapture(client_small
.get(), capability_small
);
216 message_loop_
->RunUntilIdle();
218 EXPECT_CALL(*client_large
, OnStopped(_
))
220 EXPECT_CALL(*client_large
, OnRemoved(_
))
222 EXPECT_CALL(*client_small
, OnStopped(_
))
224 EXPECT_CALL(*client_small
, OnRemoved(_
))
227 video_capture_impl_
->StopCapture(client_large
.get());
228 video_capture_impl_
->StopCapture(client_small
.get());
229 message_loop_
->RunUntilIdle();
232 TEST_F(VideoCaptureImplTest
, SmallAndLarge
) {
233 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
234 // The small client starts first and stops first.
235 scoped_ptr
<MockVideoCaptureClient
> client_small(new MockVideoCaptureClient
);
236 scoped_ptr
<MockVideoCaptureClient
> client_large(new MockVideoCaptureClient
);
237 media::VideoCaptureCapability capability_small
= CAPABILITY_SMALL
;
238 media::VideoCaptureCapability capability_large
= CAPABILITY_LARGE
;
240 EXPECT_CALL(*client_large
, OnStarted(_
))
242 EXPECT_CALL(*client_large
, OnDeviceInfoReceived(_
,_
))
244 EXPECT_CALL(*client_small
, OnStarted(_
))
246 EXPECT_CALL(*client_small
, OnDeviceInfoReceived(_
,_
))
248 .WillRepeatedly(Return());
250 video_capture_impl_
->StartCapture(client_small
.get(), capability_small
);
251 video_capture_impl_
->StartCapture(client_large
.get(), capability_large
);
252 message_loop_
->RunUntilIdle();
254 EXPECT_CALL(*client_large
, OnStopped(_
))
256 EXPECT_CALL(*client_large
, OnRemoved(_
))
258 EXPECT_CALL(*client_small
, OnStopped(_
))
260 EXPECT_CALL(*client_small
, OnRemoved(_
))
263 video_capture_impl_
->StopCapture(client_small
.get());
264 video_capture_impl_
->StopCapture(client_large
.get());
265 message_loop_
->RunUntilIdle();
268 TEST_F(VideoCaptureImplTest
, TwoClientsWithSameSize
) {
269 // Execute SetCapture() and StopCapture() for 2 clients simultaneously.
270 // The client1 starts first and stops first.
271 scoped_ptr
<MockVideoCaptureClient
> client1(new MockVideoCaptureClient
);
272 scoped_ptr
<MockVideoCaptureClient
> client2(new MockVideoCaptureClient
);
273 media::VideoCaptureCapability capability
= CAPABILITY_SMALL
;
275 EXPECT_CALL(*client1
, OnStarted(_
))
277 EXPECT_CALL(*client1
, OnDeviceInfoReceived(_
,_
))
279 EXPECT_CALL(*client2
, OnStarted(_
))
281 EXPECT_CALL(*client2
, OnDeviceInfoReceived(_
,_
))
284 video_capture_impl_
->StartCapture(client1
.get(), capability
);
285 video_capture_impl_
->StartCapture(client2
.get(), capability
);
286 message_loop_
->RunUntilIdle();
288 EXPECT_CALL(*client1
, OnStopped(_
))
290 EXPECT_CALL(*client1
, OnRemoved(_
))
292 EXPECT_CALL(*client2
, OnStopped(_
))
294 EXPECT_CALL(*client2
, OnRemoved(_
))
297 video_capture_impl_
->StopCapture(client1
.get());
298 video_capture_impl_
->StopCapture(client2
.get());
299 message_loop_
->RunUntilIdle();
302 } // namespace content