Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / video_capture_impl_unittest.cc
blob282cc3e3b637114e22798f092ce81a1a6ffb9914
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"
13 using ::testing::_;
14 using ::testing::AtLeast;
15 using ::testing::InvokeWithoutArgs;
16 using ::testing::Return;
17 using ::testing::SaveArg;
19 namespace content {
21 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
22 public:
23 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
25 // Filter implementation.
26 MOCK_METHOD1(Send, bool(IPC::Message* message));
28 protected:
29 virtual ~MockVideoCaptureMessageFilter() {}
31 private:
32 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
35 class VideoCaptureImplTest : public ::testing::Test {
36 public:
37 class MockVideoCaptureImpl : public VideoCaptureImpl {
38 public:
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 {
47 CHECK(message);
49 // In this method, messages are sent to the according handlers as if
50 // we are the device.
51 bool handled = true;
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)
63 IPC_END_MESSAGE_MAP()
64 EXPECT_TRUE(handled);
65 delete message;
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,
82 int buffer_id,
83 uint32 sync_point) {}
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_;
106 private:
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;
120 session_id_ = 1;
122 video_capture_impl_.reset(new MockVideoCaptureImpl(
123 session_id_, message_filter_.get()));
125 video_capture_impl_->device_id_ = 2;
128 virtual ~VideoCaptureImplTest() {
131 protected:
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&));
141 void Init() {
142 video_capture_impl_->Init();
145 void StartCapture(int client_id,
146 const media::VideoCaptureParams& params) {
147 video_capture_impl_->StartCapture(
148 client_id, params,
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);
159 void DeInit() {
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_;
187 private:
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));
196 Init();
197 StartCapture(0, params_small_);
198 StopCapture(0);
199 DeInit();
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);
206 Init();
207 StartCapture(0, params_small_);
208 StopCapture(0);
209 StartCapture(1, params_small_);
210 StopCapture(1);
211 DeInit();
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);
218 Init();
219 StartCapture(0, params_large_);
220 StopCapture(0);
221 StartCapture(1, params_small_);
222 StopCapture(1);
223 DeInit();
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);
230 Init();
231 StartCapture(0, params_small_);
232 StopCapture(0);
233 StartCapture(1, params_large_);
234 StopCapture(1);
235 DeInit();
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(_));
243 Init();
244 GetDeviceSupportedFormats();
245 DeInit();
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);
253 Init();
254 GetDeviceSupportedFormats();
255 GetDeviceSupportedFormats();
256 DeInit();
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(_));
264 Init();
265 GetDeviceFormatsInUse();
266 DeInit();
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);
273 Init();
274 StartCapture(0, params_small_);
275 StartCapture(1, params_large_);
276 StopCapture(0);
277 StopCapture(1);
278 DeInit();
279 DCHECK(video_capture_impl_->capture_params().requested_format
280 .frame_size ==
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));
288 Init();
289 StartCapture(0, params_small_);
291 // Receive state change message from browser.
292 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED);
294 StopCapture(0);
295 DeInit();
298 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) {
299 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
300 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR));
302 Init();
303 StartCapture(0, params_small_);
305 // Receive state change message from browser.
306 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR);
308 StopCapture(0);
309 DeInit();
312 } // namespace content