Fix build break
[chromium-blink-merge.git] / content / renderer / media / video_capture_impl_unittest.cc
blob4b181d8e9ff4c9cd9efc0aca0c8a45f18da557d5
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"
12 using ::testing::_;
13 using ::testing::AtLeast;
14 using ::testing::Return;
16 #define CAPABILITY_SMALL {176, 144, 30, media::VideoCaptureCapability::kI420, \
17 0, false }
18 #define CAPABILITY_LARGE {320, 240, 30, media::VideoCaptureCapability::kI420, \
19 0, false }
21 namespace content {
23 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
24 public:
25 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
27 // Filter implementation.
28 MOCK_METHOD1(Send, bool(IPC::Message* message));
30 protected:
31 virtual ~MockVideoCaptureMessageFilter() {}
33 private:
34 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
37 class MockVideoCaptureClient : public media::VideoCapture::EventHandler {
38 public:
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));
55 private:
56 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient);
59 class VideoCaptureImplTest : public ::testing::Test {
60 public:
61 class MockVideoCaptureImpl : public VideoCaptureImpl {
62 public:
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 {
72 CHECK(message);
74 // In this method, messages are sent to the according handlers as if
75 // we are the device.
76 bool handled = true;
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)
84 IPC_END_MESSAGE_MAP()
85 EXPECT_TRUE(handled);
86 delete message;
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;
112 session_id_ = 1;
114 video_capture_impl_ = new MockVideoCaptureImpl(session_id_,
115 message_loop_proxy_,
116 message_filter_);
118 video_capture_impl_->device_id_ = 2;
121 virtual ~VideoCaptureImplTest() {
122 delete video_capture_impl_;
125 protected:
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_;
133 private:
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(_))
143 .WillOnce(Return());
144 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_))
145 .WillOnce(Return());
147 video_capture_impl_->StartCapture(client.get(), capability);
148 message_loop_->RunUntilIdle();
150 EXPECT_CALL(*client, OnStopped(_))
151 .WillOnce(Return());
152 EXPECT_CALL(*client, OnRemoved(_))
153 .WillOnce(Return());
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(_))
165 .WillOnce(Return());
166 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_))
167 .WillOnce(Return());
169 video_capture_impl_->StartCapture(client.get(), capability);
170 message_loop_->RunUntilIdle();
172 EXPECT_CALL(*client, OnStopped(_))
173 .WillOnce(Return());
174 EXPECT_CALL(*client, OnRemoved(_))
175 .WillOnce(Return());
177 video_capture_impl_->StopCapture(client.get());
178 message_loop_->RunUntilIdle();
180 EXPECT_CALL(*client, OnStarted(_))
181 .WillOnce(Return());
182 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_))
183 .WillOnce(Return());
185 video_capture_impl_->StartCapture(client.get(), capability);
186 message_loop_->RunUntilIdle();
188 EXPECT_CALL(*client, OnStopped(_))
189 .WillOnce(Return());
190 EXPECT_CALL(*client, OnRemoved(_))
191 .WillOnce(Return());
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(_))
206 .WillOnce(Return());
207 EXPECT_CALL(*client_large, OnDeviceInfoReceived(_,_))
208 .WillOnce(Return());
209 EXPECT_CALL(*client_small, OnStarted(_))
210 .WillOnce(Return());
211 EXPECT_CALL(*client_small, OnDeviceInfoReceived(_,_))
212 .WillOnce(Return());
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(_))
219 .WillOnce(Return());
220 EXPECT_CALL(*client_large, OnRemoved(_))
221 .WillOnce(Return());
222 EXPECT_CALL(*client_small, OnStopped(_))
223 .WillOnce(Return());
224 EXPECT_CALL(*client_small, OnRemoved(_))
225 .WillOnce(Return());
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(_))
241 .WillOnce(Return());
242 EXPECT_CALL(*client_large, OnDeviceInfoReceived(_,_))
243 .WillOnce(Return());
244 EXPECT_CALL(*client_small, OnStarted(_))
245 .WillOnce(Return());
246 EXPECT_CALL(*client_small, OnDeviceInfoReceived(_,_))
247 .Times(AtLeast(1))
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(_))
255 .WillOnce(Return());
256 EXPECT_CALL(*client_large, OnRemoved(_))
257 .WillOnce(Return());
258 EXPECT_CALL(*client_small, OnStopped(_))
259 .WillOnce(Return());
260 EXPECT_CALL(*client_small, OnRemoved(_))
261 .WillOnce(Return());
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(_))
276 .WillOnce(Return());
277 EXPECT_CALL(*client1, OnDeviceInfoReceived(_,_))
278 .WillOnce(Return());
279 EXPECT_CALL(*client2, OnStarted(_))
280 .WillOnce(Return());
281 EXPECT_CALL(*client2, OnDeviceInfoReceived(_,_))
282 .WillOnce(Return());
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(_))
289 .WillOnce(Return());
290 EXPECT_CALL(*client1, OnRemoved(_))
291 .WillOnce(Return());
292 EXPECT_CALL(*client2, OnStopped(_))
293 .WillOnce(Return());
294 EXPECT_CALL(*client2, OnRemoved(_))
295 .WillOnce(Return());
297 video_capture_impl_->StopCapture(client1.get());
298 video_capture_impl_->StopCapture(client2.get());
299 message_loop_->RunUntilIdle();
302 } // namespace content