Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: content/renderer/media/video_capture_impl_unittest.cc

Issue 1314483003: Improve VideoCaptureImpl unittest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/shared_memory.h"
5 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
6 #include "content/child/child_process.h" 7 #include "content/child/child_process.h"
7 #include "content/common/media/video_capture_messages.h" 8 #include "content/common/media/video_capture_messages.h"
8 #include "content/renderer/media/video_capture_impl.h" 9 #include "content/renderer/media/video_capture_impl.h"
9 #include "media/base/bind_to_current_loop.h" 10 #include "media/base/bind_to_current_loop.h"
10 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 using ::testing::_; 14 using ::testing::_;
14 using ::testing::AtLeast; 15 using ::testing::AtLeast;
(...skipping 12 matching lines...) Expand all
27 // Filter implementation. 28 // Filter implementation.
28 MOCK_METHOD1(Send, bool(IPC::Message* message)); 29 MOCK_METHOD1(Send, bool(IPC::Message* message));
29 30
30 protected: 31 protected:
31 virtual ~MockVideoCaptureMessageFilter() {} 32 virtual ~MockVideoCaptureMessageFilter() {}
32 33
33 private: 34 private:
34 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); 35 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
35 }; 36 };
36 37
37 class VideoCaptureImplTest : public ::testing::Test { 38 struct BufferReceivedTestArg {
39 BufferReceivedTestArg(int buffer_id,
40 media::VideoPixelFormat pixel_format,
41 media::VideoCapturePixelFormat capture_pixel_format,
42 const gfx::Size& coded_size,
43 const gpu::MailboxHolder& mailbox_holder)
44 : buffer_id(buffer_id), pixel_format(pixel_format),
45 capture_pixel_format(capture_pixel_format), coded_size(coded_size),
46 mailbox_holder(mailbox_holder) {}
47
48 int buffer_id;
49 media::VideoPixelFormat pixel_format;
50 media::VideoCapturePixelFormat capture_pixel_format;
51 const gfx::Size coded_size;
52 const gpu::MailboxHolder mailbox_holder;
53 };
54
55 class VideoCaptureImplTest :
56 public ::testing::TestWithParam<BufferReceivedTestArg> {
38 public: 57 public:
39 class MockVideoCaptureImpl : public VideoCaptureImpl { 58 class MockVideoCaptureImpl : public VideoCaptureImpl {
40 public: 59 public:
41 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, 60 MockVideoCaptureImpl(const media::VideoCaptureSessionId id,
42 VideoCaptureMessageFilter* filter) 61 VideoCaptureMessageFilter* filter)
43 : VideoCaptureImpl(id, filter) { 62 : VideoCaptureImpl(id, filter), received_buffer_count_(0) {
44 } 63 }
45 ~MockVideoCaptureImpl() override {} 64 ~MockVideoCaptureImpl() override {}
46 65
47 // Override Send() to mimic device to send events. 66 // Override Send() to mimic device to send events.
48 void Send(IPC::Message* message) override { 67 void Send(IPC::Message* message) override {
49 CHECK(message); 68 CHECK(message);
50 69
51 // In this method, messages are sent to the according handlers as if 70 // In this method, messages are sent to the according handlers as if
52 // we are the device. 71 // we are the device.
53 bool handled = true; 72 bool handled = true;
(...skipping 11 matching lines...) Expand all
65 IPC_END_MESSAGE_MAP() 84 IPC_END_MESSAGE_MAP()
66 EXPECT_TRUE(handled); 85 EXPECT_TRUE(handled);
67 delete message; 86 delete message;
68 } 87 }
69 88
70 void DeviceStartCapture(int device_id, 89 void DeviceStartCapture(int device_id,
71 media::VideoCaptureSessionId session_id, 90 media::VideoCaptureSessionId session_id,
72 const media::VideoCaptureParams& params) { 91 const media::VideoCaptureParams& params) {
73 // Do not call OnStateChanged(VIDEO_CAPTURE_STATE_STARTED) here, as this 92 // Do not call OnStateChanged(VIDEO_CAPTURE_STATE_STARTED) here, as this
74 // does not accurately reflect the behavior of the VideoCaptureHost. 93 // does not accurately reflect the behavior of the VideoCaptureHost.
75 capture_params_ = params;
76 } 94 }
77 95
78 void DevicePauseCapture(int device_id) {} 96 void DevicePauseCapture(int device_id) {}
mcasas 2015/09/01 18:09:53 Suggest mocking these two fellas since they do not
msu.koo 2015/09/02 08:38:10 It's better approach but I think changing these fu
mcasas 2015/09/02 16:51:26 SGTM, you can do that in a CL afterwards but usua
79 97
80 void DeviceStopCapture(int device_id) { 98 void DeviceStopCapture(int device_id) {
81 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); 99 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED);
82 } 100 }
83 101
84 void DeviceReceiveEmptyBuffer(int device_id, 102 void DeviceReceiveEmptyBuffer(int device_id,
85 int buffer_id, 103 int buffer_id,
86 uint32 sync_point, 104 uint32 sync_point,
87 double consumer_resource_utilization) {} 105 double consumer_resource_utilization) {
106 received_buffer_count_++;
107 }
88 108
89 void DeviceGetSupportedFormats(int device_id, 109 void DeviceGetSupportedFormats(int device_id,
90 media::VideoCaptureSessionId session_id) { 110 media::VideoCaptureSessionId session_id) {
91 // When the mock message filter receives a request for the device 111 // When the mock message filter receives a request for the device
92 // supported formats, replies immediately with an empty format list. 112 // supported formats, replies immediately with an empty format list.
93 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats()); 113 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats());
94 } 114 }
95 115
96 void DeviceGetFormatsInUse(int device_id, 116 void DeviceGetFormatsInUse(int device_id,
97 media::VideoCaptureSessionId session_id) { 117 media::VideoCaptureSessionId session_id) {
98 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); 118 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
99 } 119 }
100 120
101 void ReceiveStateChangeMessage(VideoCaptureState state) { 121 void ReceiveStateChangeMessage(VideoCaptureState state) {
102 OnStateChanged(state); 122 OnStateChanged(state);
103 } 123 }
104 124
105 const media::VideoCaptureParams& capture_params() const { 125 int received_buffer_count() const { return received_buffer_count_; }
106 return capture_params_;
107 }
108 126
109 private: 127 private:
110 media::VideoCaptureParams capture_params_; 128 int received_buffer_count_;
111 }; 129 };
112 130
113 VideoCaptureImplTest() { 131 VideoCaptureImplTest() {
114 params_small_.requested_format = media::VideoCaptureFormat( 132 params_small_.requested_format = media::VideoCaptureFormat(
115 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); 133 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420);
116 134
117 params_large_.requested_format = media::VideoCaptureFormat( 135 params_large_.requested_format = media::VideoCaptureFormat(
118 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); 136 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420);
119 137
120 child_process_.reset(new ChildProcess()); 138 child_process_.reset(new ChildProcess());
(...skipping 26 matching lines...) Expand all
147 void StartCapture(const media::VideoCaptureParams& params) { 165 void StartCapture(const media::VideoCaptureParams& params) {
148 video_capture_impl_->StartCapture( 166 video_capture_impl_->StartCapture(
149 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, 167 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate,
150 base::Unretained(this)), 168 base::Unretained(this)),
151 base::Bind(&VideoCaptureImplTest::OnFrameReady, 169 base::Bind(&VideoCaptureImplTest::OnFrameReady,
152 base::Unretained(this))); 170 base::Unretained(this)));
153 } 171 }
154 172
155 void StopCapture() { video_capture_impl_->StopCapture(); } 173 void StopCapture() { video_capture_impl_->StopCapture(); }
156 174
175 void NewBuffer(int buffer_id, const base::SharedMemory& shm) {
176 video_capture_impl_->OnBufferCreated(
177 base::SharedMemory::DuplicateHandle(shm.handle()),
178 shm.mapped_size(), buffer_id);
179 }
180
181 void BufferReceived(int buffer_id, const gfx::Size& size,
182 media::VideoPixelFormat pixel_format,
183 const gpu::MailboxHolder& mailbox_holder) {
184 video_capture_impl_->OnBufferReceived(
185 buffer_id, base::TimeTicks::Now(), base::DictionaryValue(),
186 pixel_format, media::VideoFrame::STORAGE_SHMEM, size,
187 gfx::Rect(size), mailbox_holder);
188 }
189
190 void BufferDestroyed(int buffer_id) {
191 video_capture_impl_->OnBufferDestroyed(buffer_id);
192 }
193
157 void DeInit() { 194 void DeInit() {
158 video_capture_impl_->DeInit(); 195 video_capture_impl_->DeInit();
159 } 196 }
160 197
161 void GetDeviceSupportedFormats() { 198 void GetDeviceSupportedFormats() {
162 const base::Callback<void(const media::VideoCaptureFormats&)> 199 const base::Callback<void(const media::VideoCaptureFormats&)>
163 callback = base::Bind( 200 callback = base::Bind(
164 &VideoCaptureImplTest::OnDeviceSupportedFormats, 201 &VideoCaptureImplTest::OnDeviceSupportedFormats,
165 base::Unretained(this)); 202 base::Unretained(this));
166 video_capture_impl_->GetDeviceSupportedFormats(callback); 203 video_capture_impl_->GetDeviceSupportedFormats(callback);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the 258 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
222 // provided callback. 259 // provided callback.
223 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { 260 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) {
224 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); 261 EXPECT_CALL(*this, OnDeviceFormatsInUse(_));
225 262
226 Init(); 263 Init();
227 GetDeviceFormatsInUse(); 264 GetDeviceFormatsInUse();
228 DeInit(); 265 DeInit();
229 } 266 }
230 267
268 TEST_P(VideoCaptureImplTest, BufferReceivedWithFormat) {
269 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1);
270 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1);
271 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1);
272
273 const BufferReceivedTestArg& buffer_arg = GetParam();
274
275 // Create a fake shared memory for buffer.
276 base::SharedMemory shm;
277 const size_t frame_size = media::VideoFrame::AllocationSize(
278 buffer_arg.pixel_format, buffer_arg.coded_size);
279 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size));
280
281 media::VideoCaptureParams params;
282 params.requested_format = media::VideoCaptureFormat(
283 buffer_arg.coded_size, 30, buffer_arg.capture_pixel_format);
284
285 Init();
286 StartCapture(params);
287 NewBuffer(buffer_arg.buffer_id, shm);
288 BufferReceived(buffer_arg.buffer_id, buffer_arg.coded_size,
289 buffer_arg.pixel_format, buffer_arg.mailbox_holder);
290 StopCapture();
291 BufferDestroyed(buffer_arg.buffer_id);
292 DeInit();
293 }
294
295 const BufferReceivedTestArg kBufferFormats[] = {
296 BufferReceivedTestArg(0, media::PIXEL_FORMAT_I420,
297 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420,
298 gfx::Size(1280, 720),
299 gpu::MailboxHolder()),
300 BufferReceivedTestArg(0, media::PIXEL_FORMAT_ARGB,
301 media::VIDEO_CAPTURE_PIXEL_FORMAT_ARGB,
302 gfx::Size(1280, 720),
303 gpu::MailboxHolder(gpu::Mailbox::Generate(), 0, 0))};
304
305 INSTANTIATE_TEST_CASE_P(I420AndARGB,
306 VideoCaptureImplTest,
307 testing::ValuesIn(kBufferFormats));
mcasas 2015/09/01 18:09:53 I meant defining it inside the INSTANTIATE_... so
msu.koo 2015/09/02 08:38:10 Done.
308
309 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) {
310 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1);
311 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1);
312 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0);
313
314 // Create a fake shared memory for buffer.
315 base::SharedMemory shm;
316 const size_t i420_frame_size = media::VideoFrame::AllocationSize(
317 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size);
318 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size));
319
320 Init();
321 StartCapture(params_large_);
322 NewBuffer(0, shm);
323 StopCapture();
324 BufferReceived(0, params_large_.requested_format.frame_size,
325 media::PIXEL_FORMAT_I420, gpu::MailboxHolder());
326 BufferDestroyed(0);
327 DeInit();
328
329 EXPECT_EQ(this->video_capture_impl_->received_buffer_count(), 1);
330 }
331
231 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { 332 TEST_F(VideoCaptureImplTest, EndedBeforeStop) {
232 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 333 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
233 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); 334 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
234 335
235 Init(); 336 Init();
236 StartCapture(params_small_); 337 StartCapture(params_small_);
237 338
238 // Receive state change message from browser. 339 // Receive state change message from browser.
239 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); 340 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED);
240 341
241 StopCapture(); 342 StopCapture();
242 DeInit(); 343 DeInit();
243 } 344 }
244 345
245 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { 346 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) {
246 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 347 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
247 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); 348 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR));
248 349
249 Init(); 350 Init();
250 StartCapture(params_small_); 351 StartCapture(params_small_);
251 352
252 // Receive state change message from browser. 353 // Receive state change message from browser.
253 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); 354 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR);
254 355
255 StopCapture(); 356 StopCapture();
256 DeInit(); 357 DeInit();
257 } 358 }
258 359
259 } // namespace content 360 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698