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

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(media::VideoPixelFormat pixel_format,
40 const std::vector<gpu::MailboxHolder>& mailbox_holders)
41 : pixel_format(pixel_format), mailbox_holders(mailbox_holders) {}
42
43 BufferReceivedTestArg(media::VideoPixelFormat pixel_format)
44 : pixel_format(pixel_format) {}
45
46 media::VideoPixelFormat pixel_format;
47 std::vector<gpu::MailboxHolder> mailbox_holders;
48 };
49
50 static const BufferReceivedTestArg kBufferFormats[] = {
51 BufferReceivedTestArg(media::PIXEL_FORMAT_I420),
52 BufferReceivedTestArg(
53 media::PIXEL_FORMAT_ARGB,
54 std::vector<gpu::MailboxHolder>(
55 1, gpu::MailboxHolder(gpu::Mailbox::Generate(), 0, 0)))};
56
57 class VideoCaptureImplTest :
58 public ::testing::TestWithParam<BufferReceivedTestArg> {
38 public: 59 public:
39 class MockVideoCaptureImpl : public VideoCaptureImpl { 60 class MockVideoCaptureImpl : public VideoCaptureImpl {
40 public: 61 public:
41 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, 62 MockVideoCaptureImpl(const media::VideoCaptureSessionId id,
42 VideoCaptureMessageFilter* filter) 63 VideoCaptureMessageFilter* filter)
43 : VideoCaptureImpl(id, filter) { 64 : VideoCaptureImpl(id, filter), received_buffer_count_(0) {
44 } 65 }
45 ~MockVideoCaptureImpl() override {} 66 ~MockVideoCaptureImpl() override {}
46 67
47 // Override Send() to mimic device to send events. 68 // Override Send() to mimic device to send events.
48 void Send(IPC::Message* message) override { 69 void Send(IPC::Message* message) override {
49 CHECK(message); 70 CHECK(message);
50 71
51 // In this method, messages are sent to the according handlers as if 72 // In this method, messages are sent to the according handlers as if
52 // we are the device. 73 // we are the device.
53 bool handled = true; 74 bool handled = true;
(...skipping 11 matching lines...) Expand all
65 IPC_END_MESSAGE_MAP() 86 IPC_END_MESSAGE_MAP()
66 EXPECT_TRUE(handled); 87 EXPECT_TRUE(handled);
67 delete message; 88 delete message;
68 } 89 }
69 90
70 void DeviceStartCapture(int device_id, 91 void DeviceStartCapture(int device_id,
71 media::VideoCaptureSessionId session_id, 92 media::VideoCaptureSessionId session_id,
72 const media::VideoCaptureParams& params) { 93 const media::VideoCaptureParams& params) {
73 // Do not call OnStateChanged(VIDEO_CAPTURE_STATE_STARTED) here, as this 94 // Do not call OnStateChanged(VIDEO_CAPTURE_STATE_STARTED) here, as this
74 // does not accurately reflect the behavior of the VideoCaptureHost. 95 // does not accurately reflect the behavior of the VideoCaptureHost.
75 capture_params_ = params;
76 } 96 }
77 97
78 void DevicePauseCapture(int device_id) {} 98 void DevicePauseCapture(int device_id) {}
79 99
80 void DeviceStopCapture(int device_id) { 100 void DeviceStopCapture(int device_id) {
81 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); 101 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED);
82 } 102 }
83 103
84 void DeviceReceiveEmptyBuffer(int device_id, 104 void DeviceReceiveEmptyBuffer(int device_id,
85 int buffer_id, 105 int buffer_id,
86 uint32 sync_point, 106 uint32 sync_point,
87 double consumer_resource_utilization) {} 107 double consumer_resource_utilization) {
108 received_buffer_count_++;
109 }
88 110
89 void DeviceGetSupportedFormats(int device_id, 111 void DeviceGetSupportedFormats(int device_id,
90 media::VideoCaptureSessionId session_id) { 112 media::VideoCaptureSessionId session_id) {
91 // When the mock message filter receives a request for the device 113 // When the mock message filter receives a request for the device
92 // supported formats, replies immediately with an empty format list. 114 // supported formats, replies immediately with an empty format list.
93 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats()); 115 OnDeviceSupportedFormatsEnumerated(media::VideoCaptureFormats());
94 } 116 }
95 117
96 void DeviceGetFormatsInUse(int device_id, 118 void DeviceGetFormatsInUse(int device_id,
97 media::VideoCaptureSessionId session_id) { 119 media::VideoCaptureSessionId session_id) {
98 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats()); 120 OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
99 } 121 }
100 122
101 void ReceiveStateChangeMessage(VideoCaptureState state) { 123 void ReceiveStateChangeMessage(VideoCaptureState state) {
102 OnStateChanged(state); 124 OnStateChanged(state);
103 } 125 }
104 126
105 const media::VideoCaptureParams& capture_params() const { 127 int received_buffer_count() const { return received_buffer_count_; }
106 return capture_params_;
107 }
108 128
109 private: 129 private:
110 media::VideoCaptureParams capture_params_; 130 int received_buffer_count_;
111 }; 131 };
112 132
113 VideoCaptureImplTest() { 133 VideoCaptureImplTest() {
114 params_small_.requested_format = media::VideoCaptureFormat( 134 params_small_.requested_format = media::VideoCaptureFormat(
115 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); 135 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
116 136
117 params_large_.requested_format = media::VideoCaptureFormat( 137 params_large_.requested_format = media::VideoCaptureFormat(
118 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); 138 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
119 139
120 child_process_.reset(new ChildProcess()); 140 child_process_.reset(new ChildProcess());
(...skipping 26 matching lines...) Expand all
147 void StartCapture(const media::VideoCaptureParams& params) { 167 void StartCapture(const media::VideoCaptureParams& params) {
148 video_capture_impl_->StartCapture( 168 video_capture_impl_->StartCapture(
149 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, 169 params, base::Bind(&VideoCaptureImplTest::OnStateUpdate,
150 base::Unretained(this)), 170 base::Unretained(this)),
151 base::Bind(&VideoCaptureImplTest::OnFrameReady, 171 base::Bind(&VideoCaptureImplTest::OnFrameReady,
152 base::Unretained(this))); 172 base::Unretained(this)));
153 } 173 }
154 174
155 void StopCapture() { video_capture_impl_->StopCapture(); } 175 void StopCapture() { video_capture_impl_->StopCapture(); }
156 176
177 void NewBuffer(int buffer_id, const base::SharedMemory& shm) {
178 video_capture_impl_->OnBufferCreated(
179 base::SharedMemory::DuplicateHandle(shm.handle()),
180 shm.mapped_size(), buffer_id);
181 }
182
183 void BufferReceived(int buffer_id, const gfx::Size& size,
184 media::VideoPixelFormat pixel_format,
185 const std::vector<gpu::MailboxHolder>& mailbox_holders) {
186 video_capture_impl_->OnBufferReceived(
187 buffer_id, base::TimeTicks::Now(), base::DictionaryValue(),
188 pixel_format, media::VideoFrame::STORAGE_SHMEM, size,
189 gfx::Rect(size), mailbox_holders);
190 }
191
192 void BufferDestroyed(int buffer_id) {
193 video_capture_impl_->OnBufferDestroyed(buffer_id);
194 }
195
157 void DeInit() { 196 void DeInit() {
158 video_capture_impl_->DeInit(); 197 video_capture_impl_->DeInit();
159 } 198 }
160 199
161 void GetDeviceSupportedFormats() { 200 void GetDeviceSupportedFormats() {
162 const base::Callback<void(const media::VideoCaptureFormats&)> 201 const base::Callback<void(const media::VideoCaptureFormats&)>
163 callback = base::Bind( 202 callback = base::Bind(
164 &VideoCaptureImplTest::OnDeviceSupportedFormats, 203 &VideoCaptureImplTest::OnDeviceSupportedFormats,
165 base::Unretained(this)); 204 base::Unretained(this));
166 video_capture_impl_->GetDeviceSupportedFormats(callback); 205 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 260 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the
222 // provided callback. 261 // provided callback.
223 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { 262 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) {
224 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); 263 EXPECT_CALL(*this, OnDeviceFormatsInUse(_));
225 264
226 Init(); 265 Init();
227 GetDeviceFormatsInUse(); 266 GetDeviceFormatsInUse();
228 DeInit(); 267 DeInit();
229 } 268 }
230 269
270 TEST_P(VideoCaptureImplTest, BufferReceivedWithFormat) {
271 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1);
272 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1);
273 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1);
274
275 const BufferReceivedTestArg& buffer_arg = GetParam();
276 const gfx::Size size(1280, 720);
277
278 // Create a fake shared memory for buffer.
279 base::SharedMemory shm;
280 const size_t frame_size = media::VideoFrame::AllocationSize(
281 buffer_arg.pixel_format, size);
282 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size));
283
284 media::VideoCaptureParams params;
285 params.requested_format = media::VideoCaptureFormat(
286 size, 30, buffer_arg.pixel_format);
287
288 Init();
289 StartCapture(params);
290 NewBuffer(0, shm);
291 BufferReceived(0, size, buffer_arg.pixel_format, buffer_arg.mailbox_holders);
292 StopCapture();
293 BufferDestroyed(0);
294 DeInit();
295 }
296
297 INSTANTIATE_TEST_CASE_P(I420AndARGB,
298 VideoCaptureImplTest,
299 testing::ValuesIn(kBufferFormats));
300
301 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) {
302 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1);
303 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1);
304 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0);
305
306 // Create a fake shared memory for buffer.
307 base::SharedMemory shm;
308 const size_t i420_frame_size = media::VideoFrame::AllocationSize(
309 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size);
310 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size));
311
312 Init();
313 StartCapture(params_large_);
314 NewBuffer(0, shm);
315 StopCapture();
316 BufferReceived(0, params_large_.requested_format.frame_size,
317 media::PIXEL_FORMAT_I420, std::vector<gpu::MailboxHolder>());
318 BufferDestroyed(0);
319 DeInit();
320
321 EXPECT_EQ(this->video_capture_impl_->received_buffer_count(), 1);
322 }
323
231 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { 324 TEST_F(VideoCaptureImplTest, EndedBeforeStop) {
232 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 325 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
233 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); 326 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED));
234 327
235 Init(); 328 Init();
236 StartCapture(params_small_); 329 StartCapture(params_small_);
237 330
238 // Receive state change message from browser. 331 // Receive state change message from browser.
239 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); 332 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED);
240 333
241 StopCapture(); 334 StopCapture();
242 DeInit(); 335 DeInit();
243 } 336 }
244 337
245 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { 338 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) {
246 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); 339 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED));
247 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); 340 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR));
248 341
249 Init(); 342 Init();
250 StartCapture(params_small_); 343 StartCapture(params_small_);
251 344
252 // Receive state change message from browser. 345 // Receive state change message from browser.
253 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); 346 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR);
254 347
255 StopCapture(); 348 StopCapture();
256 DeInit(); 349 DeInit();
257 } 350 }
258 351
259 } // namespace content 352 } // 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