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

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 16 matching lines...) Expand all
31 private: 32 private:
32 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); 33 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
33 }; 34 };
34 35
35 class VideoCaptureImplTest : public ::testing::Test { 36 class VideoCaptureImplTest : public ::testing::Test {
36 public: 37 public:
37 class MockVideoCaptureImpl : public VideoCaptureImpl { 38 class MockVideoCaptureImpl : public VideoCaptureImpl {
38 public: 39 public:
39 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, 40 MockVideoCaptureImpl(const media::VideoCaptureSessionId id,
40 VideoCaptureMessageFilter* filter) 41 VideoCaptureMessageFilter* filter)
41 : VideoCaptureImpl(id, filter) { 42 : VideoCaptureImpl(id, filter), buffers_received_(0) {
42 } 43 }
43 ~MockVideoCaptureImpl() override {} 44 ~MockVideoCaptureImpl() override {}
44 45
45 // Override Send() to mimic device to send events. 46 // Override Send() to mimic device to send events.
46 void Send(IPC::Message* message) override { 47 void Send(IPC::Message* message) override {
47 CHECK(message); 48 CHECK(message);
48 49
49 // In this method, messages are sent to the according handlers as if 50 // In this method, messages are sent to the according handlers as if
50 // we are the device. 51 // we are the device.
51 bool handled = true; 52 bool handled = true;
(...skipping 22 matching lines...) Expand all
74 75
75 void DevicePauseCapture(int device_id) {} 76 void DevicePauseCapture(int device_id) {}
76 77
77 void DeviceStopCapture(int device_id) { 78 void DeviceStopCapture(int device_id) {
78 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); 79 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED);
79 } 80 }
80 81
81 void DeviceReceiveEmptyBuffer(int device_id, 82 void DeviceReceiveEmptyBuffer(int device_id,
82 int buffer_id, 83 int buffer_id,
83 uint32 sync_point, 84 uint32 sync_point,
84 double consumer_resource_utilization) {} 85 double consumer_resource_utilization) {
86 buffers_received_++;
87 }
85 88
86 void DeviceGetSupportedFormats(int device_id, 89 void DeviceGetSupportedFormats(int device_id,
87 media::VideoCaptureSessionId session_id) { 90 media::VideoCaptureSessionId session_id) {
88 // When the mock message filter receives a request for the device 91 // When the mock message filter receives a request for the device
89 // supported formats, replies immediately with an empty format list. 92 // supported formats, replies immediately with an empty format list.
90 OnDeviceSupportedFormatsEnumerated( 93 OnDeviceSupportedFormatsEnumerated(
91 media::VideoCaptureFormats()); 94 media::VideoCaptureFormats());
92 } 95 }
93 96
94 void DeviceGetFormatsInUse(int device_id, 97 void DeviceGetFormatsInUse(int device_id,
95 media::VideoCaptureSessionId session_id) { 98 media::VideoCaptureSessionId session_id) {
96 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); 99 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
97 } 100 }
98 101
99 void ReceiveStateChangeMessage(VideoCaptureState state) { 102 void ReceiveStateChangeMessage(VideoCaptureState state) {
100 OnStateChanged(state); 103 OnStateChanged(state);
101 } 104 }
102 105
103 const media::VideoCaptureParams& capture_params() const { 106 const media::VideoCaptureParams& capture_params() const {
104 return capture_params_; 107 return capture_params_;
105 } 108 }
106 109
110 const int GetReceivedBufferCounts() {
mcasas 2015/08/25 16:43:45 Fits in one line, and it's the method that should
msu.koo 2015/08/26 06:52:48 Done.
111 return buffers_received_;
112 }
113
107 private: 114 private:
108 media::VideoCaptureParams capture_params_; 115 media::VideoCaptureParams capture_params_;
116 int buffers_received_;
mcasas 2015/08/25 16:43:45 s/int/size_t/ ?
msu.koo 2015/08/26 06:52:48 Done.
109 }; 117 };
110 118
111 VideoCaptureImplTest() { 119 VideoCaptureImplTest() {
112 params_small_.requested_format = media::VideoCaptureFormat( 120 params_small_.requested_format = media::VideoCaptureFormat(
113 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); 121 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420);
114 122
115 params_large_.requested_format = media::VideoCaptureFormat( 123 params_large_.requested_format = media::VideoCaptureFormat(
116 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); 124 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420);
117 125
118 child_process_.reset(new ChildProcess()); 126 child_process_.reset(new ChildProcess());
(...skipping 27 matching lines...) Expand all
146 void StartCapture(int client_id, 154 void StartCapture(int client_id,
147 const media::VideoCaptureParams& params) { 155 const media::VideoCaptureParams& params) {
148 video_capture_impl_->StartCapture( 156 video_capture_impl_->StartCapture(
149 client_id, params, 157 client_id, params,
150 base::Bind(&VideoCaptureImplTest::OnStateUpdate, 158 base::Bind(&VideoCaptureImplTest::OnStateUpdate,
151 base::Unretained(this)), 159 base::Unretained(this)),
152 base::Bind(&VideoCaptureImplTest::OnFrameReady, 160 base::Bind(&VideoCaptureImplTest::OnFrameReady,
153 base::Unretained(this))); 161 base::Unretained(this)));
154 } 162 }
155 163
164 void NewBuffer(int buffer_id, const base::SharedMemory& shm) {
165 video_capture_impl_->OnBufferCreated(
166 base::SharedMemory::DuplicateHandle(shm.handle()),
167 shm.mapped_size(), buffer_id);
168 }
169
170 void BufferReceived(int buffer_id, const gfx::Size& size,
171 media::VideoPixelFormat pixel_format,
172 const gpu::MailboxHolder& mailbox_holder) {
173 video_capture_impl_->OnBufferReceived(
174 buffer_id, base::TimeTicks::Now(), base::DictionaryValue(),
175 pixel_format, media::VideoFrame::STORAGE_SHMEM, size,
176 gfx::Rect(size.width(), size.height()), mailbox_holder);
mcasas 2015/08/25 16:43:46 gfx::Rect(size) [1] [1] https://code.google.com/p
msu.koo 2015/08/26 06:52:48 Done.
177 }
178
179 void BufferDestroyed(int buffer_id) {
180 video_capture_impl_->OnBufferDestroyed(buffer_id);
181 }
182
156 void StopCapture(int client_id) { 183 void StopCapture(int client_id) {
157 video_capture_impl_->StopCapture(client_id); 184 video_capture_impl_->StopCapture(client_id);
158 } 185 }
159 186
160 void DeInit() { 187 void DeInit() {
161 video_capture_impl_->DeInit(); 188 video_capture_impl_->DeInit();
162 } 189 }
163 190
164 void GetDeviceSupportedFormats() { 191 void GetDeviceSupportedFormats() {
165 const base::Callback<void(const media::VideoCaptureFormats&)> 192 const base::Callback<void(const media::VideoCaptureFormats&)>
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 StartCapture(0, params_small_); 302 StartCapture(0, params_small_);
276 StartCapture(1, params_large_); 303 StartCapture(1, params_large_);
277 StopCapture(0); 304 StopCapture(0);
278 StopCapture(1); 305 StopCapture(1);
279 DeInit(); 306 DeInit();
280 DCHECK(video_capture_impl_->capture_params().requested_format 307 DCHECK(video_capture_impl_->capture_params().requested_format
281 .frame_size == 308 .frame_size ==
282 params_small_.requested_format.frame_size); 309 params_small_.requested_format.frame_size);
283 } 310 }
284 311
312 TEST_F(VideoCaptureImplTest, I420BufferReceived) {
313 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1);
314 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1);
315 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1);
316
317 // Create a fake shared memory for buffer.
318 base::SharedMemory shm;
319 size_t i420_frame_size = media::VideoFrame::AllocationSize(
320 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size);
321 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size));
322
323 Init();
324 StartCapture(0, params_large_);
325 NewBuffer(0, shm);
326 BufferReceived(0, params_large_.requested_format.frame_size,
327 media::PIXEL_FORMAT_I420, gpu::MailboxHolder());
328 StopCapture(0);
329 BufferDestroyed(0);
330 DeInit();
331 }
332
333 TEST_F(VideoCaptureImplTest, ARGBBufferReceived) {
mcasas 2015/08/25 16:43:45 Consider making this test and the previous one par
msu.koo 2015/08/26 06:52:48 Done.
334 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1);
335 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1);
336 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1);
337
338 // Create a fake shared memory for buffer.
339 base::SharedMemory shm;
340 size_t i420_frame_size = media::VideoFrame::AllocationSize(
mcasas 2015/08/25 16:43:45 s/i420_frame_size/argb_frame_size/ nit: make it co
msu.koo 2015/08/26 06:52:48 Done.
341 media::PIXEL_FORMAT_ARGB, params_large_.requested_format.frame_size);
342 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size));
343
344 Init();
345 StartCapture(0, params_large_);
346 NewBuffer(0, shm);
347 BufferReceived(0, params_large_.requested_format.frame_size,
348 media::PIXEL_FORMAT_ARGB,
349 gpu::MailboxHolder(gpu::Mailbox::Generate(), 0, 0));
350 StopCapture(0);
351 BufferDestroyed(0);
352 DeInit();
353 }
354
355 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) {
356 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1);
357 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1);
358 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0);
359
360 // Create a fake shared memory for buffer.
361 base::SharedMemory shm;
362 size_t i420_frame_size = media::VideoFrame::AllocationSize(
363 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size);
364 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size));
365
366 Init();
367 StartCapture(0, params_large_);
368 NewBuffer(0, shm);
369 StopCapture(0);
370 BufferReceived(0, params_large_.requested_format.frame_size,
371 media::PIXEL_FORMAT_I420, gpu::MailboxHolder());
372 BufferDestroyed(0);
373 DeInit();
374
375 EXPECT_EQ(this->video_capture_impl_->GetReceivedBufferCounts(), 1);
376 }
377
285 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { 378 TEST_F(VideoCaptureImplTest, EndedBeforeStop) {
286 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 379 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
287 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); 380 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
288 381
289 Init(); 382 Init();
290 StartCapture(0, params_small_); 383 StartCapture(0, params_small_);
291 384
292 // Receive state change message from browser. 385 // Receive state change message from browser.
293 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); 386 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED);
294 387
295 StopCapture(0); 388 StopCapture(0);
296 DeInit(); 389 DeInit();
297 } 390 }
298 391
299 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { 392 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) {
300 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 393 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
301 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); 394 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR));
302 395
303 Init(); 396 Init();
304 StartCapture(0, params_small_); 397 StartCapture(0, params_small_);
305 398
306 // Receive state change message from browser. 399 // Receive state change message from browser.
307 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); 400 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR);
308 401
309 StopCapture(0); 402 StopCapture(0);
310 DeInit(); 403 DeInit();
311 } 404 }
312 405
313 } // namespace content 406 } // 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