OLD | NEW |
---|---|
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 Loading... | |
27 | 28 |
28 protected: | 29 protected: |
29 virtual ~MockVideoCaptureMessageFilter() {} | 30 virtual ~MockVideoCaptureMessageFilter() {} |
30 | 31 |
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 { |
mcasas
2015/08/25 00:14:58
This is odd: A test class that Mocks the object to
msu.koo
2015/08/25 00:45:23
Thank you for your comments. I agree with your opi
| |
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), buffer_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 Loading... | |
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 buffer_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 |
107 private: | 110 private: |
108 media::VideoCaptureParams capture_params_; | 111 media::VideoCaptureParams capture_params_; |
112 | |
113 public: | |
114 int buffer_received_; | |
ajose
2015/08/24 17:46:31
s/buffer_received/buffers_received/ ?
Also consid
msu.koo
2015/08/25 00:45:23
Done.
| |
109 }; | 115 }; |
110 | 116 |
111 VideoCaptureImplTest() { | 117 VideoCaptureImplTest() { |
112 params_small_.requested_format = media::VideoCaptureFormat( | 118 params_small_.requested_format = media::VideoCaptureFormat( |
113 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); | 119 gfx::Size(176, 144), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); |
114 | 120 |
115 params_large_.requested_format = media::VideoCaptureFormat( | 121 params_large_.requested_format = media::VideoCaptureFormat( |
116 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); | 122 gfx::Size(320, 240), 30, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420); |
117 | 123 |
118 child_process_.reset(new ChildProcess()); | 124 child_process_.reset(new ChildProcess()); |
(...skipping 27 matching lines...) Expand all Loading... | |
146 void StartCapture(int client_id, | 152 void StartCapture(int client_id, |
147 const media::VideoCaptureParams& params) { | 153 const media::VideoCaptureParams& params) { |
148 video_capture_impl_->StartCapture( | 154 video_capture_impl_->StartCapture( |
149 client_id, params, | 155 client_id, params, |
150 base::Bind(&VideoCaptureImplTest::OnStateUpdate, | 156 base::Bind(&VideoCaptureImplTest::OnStateUpdate, |
151 base::Unretained(this)), | 157 base::Unretained(this)), |
152 base::Bind(&VideoCaptureImplTest::OnFrameReady, | 158 base::Bind(&VideoCaptureImplTest::OnFrameReady, |
153 base::Unretained(this))); | 159 base::Unretained(this))); |
154 } | 160 } |
155 | 161 |
162 void NewBuffer(int buffer_id, const base::SharedMemory& shm) { | |
163 video_capture_impl_->OnBufferCreated( | |
164 base::SharedMemory::DuplicateHandle(shm.handle()), | |
165 shm.mapped_size(), buffer_id); | |
166 } | |
167 | |
168 void BufferReceived(int buffer_id, const gfx::Size& size) { | |
169 base::DictionaryValue metadata; | |
170 gpu::MailboxHolder mailbox; | |
171 video_capture_impl_->OnBufferReceived( | |
172 buffer_id, base::TimeTicks::Now(), base::DictionaryValue(), | |
173 media::PIXEL_FORMAT_I420, media::VideoFrame::STORAGE_SHMEM, size, | |
174 gfx::Rect(size.width(), size.height()), mailbox); | |
175 } | |
176 | |
177 void BufferDestroyed(int buffer_id) { | |
178 video_capture_impl_->OnBufferDestroyed(buffer_id); | |
179 } | |
180 | |
156 void StopCapture(int client_id) { | 181 void StopCapture(int client_id) { |
157 video_capture_impl_->StopCapture(client_id); | 182 video_capture_impl_->StopCapture(client_id); |
158 } | 183 } |
159 | 184 |
160 void DeInit() { | 185 void DeInit() { |
161 video_capture_impl_->DeInit(); | 186 video_capture_impl_->DeInit(); |
162 } | 187 } |
163 | 188 |
164 void GetDeviceSupportedFormats() { | 189 void GetDeviceSupportedFormats() { |
165 const base::Callback<void(const media::VideoCaptureFormats&)> | 190 const base::Callback<void(const media::VideoCaptureFormats&)> |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 StartCapture(0, params_small_); | 300 StartCapture(0, params_small_); |
276 StartCapture(1, params_large_); | 301 StartCapture(1, params_large_); |
277 StopCapture(0); | 302 StopCapture(0); |
278 StopCapture(1); | 303 StopCapture(1); |
279 DeInit(); | 304 DeInit(); |
280 DCHECK(video_capture_impl_->capture_params().requested_format | 305 DCHECK(video_capture_impl_->capture_params().requested_format |
281 .frame_size == | 306 .frame_size == |
282 params_small_.requested_format.frame_size); | 307 params_small_.requested_format.frame_size); |
283 } | 308 } |
284 | 309 |
310 TEST_F(VideoCaptureImplTest, BufferReceived) { | |
311 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | |
312 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | |
313 EXPECT_CALL(*this, OnFrameReady(_, _)); | |
314 | |
315 // Create a fake shared memory for buffer. | |
316 base::SharedMemory shm; | |
317 size_t i420_frame_size = media::VideoFrame::AllocationSize( | |
318 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); | |
319 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size)); | |
320 | |
321 Init(); | |
322 StartCapture(0, params_large_); | |
323 NewBuffer(0, shm); | |
324 BufferReceived(0, params_large_.requested_format.frame_size); | |
325 StopCapture(0); | |
326 BufferDestroyed(0); | |
327 DeInit(); | |
328 } | |
ajose
2015/08/24 17:46:31
EXPECT_EQ(this->video_capture_impl_->buffer_receiv
msu.koo
2015/08/25 00:45:23
I think this line is not required on this unittest
| |
329 | |
330 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { | |
331 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | |
332 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | |
333 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); | |
334 | |
335 // Create a fake shared memory for buffer. | |
336 base::SharedMemory shm; | |
337 size_t i420_frame_size = media::VideoFrame::AllocationSize( | |
338 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); | |
339 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size)); | |
340 | |
341 Init(); | |
342 StartCapture(0, params_large_); | |
343 NewBuffer(0, shm); | |
344 StopCapture(0); | |
345 BufferReceived(0, params_large_.requested_format.frame_size); | |
346 BufferDestroyed(0); | |
347 DeInit(); | |
348 | |
349 EXPECT_EQ(this->video_capture_impl_->buffer_received_, 1); | |
350 } | |
351 | |
285 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { | 352 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { |
286 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 353 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
287 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 354 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
288 | 355 |
289 Init(); | 356 Init(); |
290 StartCapture(0, params_small_); | 357 StartCapture(0, params_small_); |
291 | 358 |
292 // Receive state change message from browser. | 359 // Receive state change message from browser. |
293 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); | 360 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); |
294 | 361 |
295 StopCapture(0); | 362 StopCapture(0); |
296 DeInit(); | 363 DeInit(); |
297 } | 364 } |
298 | 365 |
299 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { | 366 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { |
300 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 367 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
301 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); | 368 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); |
302 | 369 |
303 Init(); | 370 Init(); |
304 StartCapture(0, params_small_); | 371 StartCapture(0, params_small_); |
305 | 372 |
306 // Receive state change message from browser. | 373 // Receive state change message from browser. |
307 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); | 374 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); |
308 | 375 |
309 StopCapture(0); | 376 StopCapture(0); |
310 DeInit(); | 377 DeInit(); |
311 } | 378 } |
312 | 379 |
313 } // namespace content | 380 } // namespace content |
OLD | NEW |