Chromium Code Reviews| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
| 11 #include "content/common/video_capture.mojom.h" | 11 #include "content/common/video_capture.mojom.h" |
| 12 #include "content/renderer/media/video_capture_impl.h" | 12 #include "content/renderer/media/video_capture_impl.h" |
| 13 #include "mojo/public/cpp/system/platform_handle.h" | 13 #include "mojo/public/cpp/system/platform_handle.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using ::testing::_; | 17 using ::testing::_; |
| 18 using ::testing::InSequence; | |
| 18 using ::testing::Invoke; | 19 using ::testing::Invoke; |
| 19 using ::testing::InvokeWithoutArgs; | 20 using ::testing::InvokeWithoutArgs; |
| 20 using ::testing::SaveArg; | 21 using ::testing::SaveArg; |
| 21 using ::testing::WithArgs; | 22 using ::testing::WithArgs; |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 | 25 |
| 25 const int kSessionId = 11; | 26 const int kSessionId = 11; |
| 26 | 27 |
| 27 void RunEmptyFormatsCallback(const VideoCaptureDeviceFormatsCB& callback) { | 28 void RunEmptyFormatsCallback(const VideoCaptureDeviceFormatsCB& callback) { |
| 28 media::VideoCaptureFormats formats; | 29 media::VideoCaptureFormats formats; |
| 29 callback.Run(formats); | 30 callback.Run(formats); |
| 30 } | 31 } |
| 31 | 32 |
| 33 ACTION(DoNothing) {} | |
| 34 | |
| 32 // Mock implementation of the Mojo Host service. | 35 // Mock implementation of the Mojo Host service. |
| 33 class MockMojoVideoCaptureHost : public mojom::VideoCaptureHost { | 36 class MockMojoVideoCaptureHost : public mojom::VideoCaptureHost { |
| 34 public: | 37 public: |
| 35 MockMojoVideoCaptureHost() : released_buffer_count_(0) { | 38 MockMojoVideoCaptureHost() : released_buffer_count_(0) { |
| 36 ON_CALL(*this, GetDeviceSupportedFormats(_, _, _)) | 39 ON_CALL(*this, GetDeviceSupportedFormats(_, _, _)) |
| 37 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); | 40 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); |
| 38 ON_CALL(*this, GetDeviceFormatsInUse(_, _, _)) | 41 ON_CALL(*this, GetDeviceFormatsInUse(_, _, _)) |
| 39 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); | 42 .WillByDefault(WithArgs<2>(Invoke(RunEmptyFormatsCallback))); |
| 40 ON_CALL(*this, ReleaseBuffer(_, _, _)) | 43 ON_CALL(*this, ReleaseBuffer(_, _, _)) |
| 41 .WillByDefault(InvokeWithoutArgs( | 44 .WillByDefault(InvokeWithoutArgs( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 public: | 85 public: |
| 83 VideoCaptureImplTest() | 86 VideoCaptureImplTest() |
| 84 : video_capture_impl_(new VideoCaptureImpl(kSessionId)) { | 87 : video_capture_impl_(new VideoCaptureImpl(kSessionId)) { |
| 85 params_small_.requested_format = media::VideoCaptureFormat( | 88 params_small_.requested_format = media::VideoCaptureFormat( |
| 86 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); | 89 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); |
| 87 params_large_.requested_format = media::VideoCaptureFormat( | 90 params_large_.requested_format = media::VideoCaptureFormat( |
| 88 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); | 91 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); |
| 89 | 92 |
| 90 video_capture_impl_->SetVideoCaptureHostForTesting( | 93 video_capture_impl_->SetVideoCaptureHostForTesting( |
| 91 &mock_video_capture_host_); | 94 &mock_video_capture_host_); |
| 95 | |
| 96 ON_CALL(mock_video_capture_host_, DoStart(_, _, _)) | |
| 97 .WillByDefault(InvokeWithoutArgs([this]() { | |
| 98 video_capture_impl_->OnStateChanged( | |
| 99 mojom::VideoCaptureState::STARTED); | |
| 100 })); | |
| 92 } | 101 } |
| 93 | 102 |
| 94 protected: | 103 protected: |
| 95 // These four mocks are used to create callbacks for the different oeprations. | 104 // These four mocks are used to create callbacks for the different oeprations. |
| 96 MOCK_METHOD2(OnFrameReady, | 105 MOCK_METHOD2(OnFrameReady, |
| 97 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); | 106 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); |
| 98 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); | 107 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); |
| 99 MOCK_METHOD1(OnDeviceFormatsInUse, void(const media::VideoCaptureFormats&)); | 108 MOCK_METHOD1(OnDeviceFormatsInUse, void(const media::VideoCaptureFormats&)); |
| 100 MOCK_METHOD1(OnDeviceSupportedFormats, | 109 MOCK_METHOD1(OnDeviceSupportedFormats, |
| 101 void(const media::VideoCaptureFormats&)); | 110 void(const media::VideoCaptureFormats&)); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 186 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 178 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 187 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
| 179 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 188 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
| 180 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 189 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
| 181 | 190 |
| 182 StartCapture(0, params_small_); | 191 StartCapture(0, params_small_); |
| 183 StopCapture(0); | 192 StopCapture(0); |
| 184 } | 193 } |
| 185 | 194 |
| 186 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { | 195 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { |
| 187 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 196 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 188 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 197 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 189 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 198 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
| 190 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 199 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
| 191 | 200 |
| 192 StartCapture(0, params_small_); | 201 StartCapture(0, params_small_); |
| 193 StopCapture(0); | 202 StopCapture(0); |
| 194 StartCapture(1, params_small_); | 203 StartCapture(1, params_small_); |
| 195 StopCapture(1); | 204 StopCapture(1); |
| 196 } | 205 } |
| 197 | 206 |
| 198 TEST_F(VideoCaptureImplTest, LargeAndSmall) { | 207 TEST_F(VideoCaptureImplTest, LargeAndSmall) { |
| 199 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 208 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 200 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 209 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 201 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); | 210 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_large_)); |
| 202 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 211 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
| 203 | 212 |
| 204 StartCapture(0, params_large_); | 213 StartCapture(0, params_large_); |
| 205 StopCapture(0); | 214 StopCapture(0); |
| 206 StartCapture(1, params_small_); | 215 StartCapture(1, params_small_); |
| 207 StopCapture(1); | 216 StopCapture(1); |
| 208 } | 217 } |
| 209 | 218 |
| 210 TEST_F(VideoCaptureImplTest, SmallAndLarge) { | 219 TEST_F(VideoCaptureImplTest, SmallAndLarge) { |
| 211 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 220 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 212 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 221 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 213 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 222 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
| 214 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 223 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
| 215 | 224 |
| 216 StartCapture(0, params_small_); | 225 StartCapture(0, params_small_); |
| 217 StopCapture(0); | 226 StopCapture(0); |
| 218 StartCapture(1, params_large_); | 227 StartCapture(1, params_large_); |
| 219 StopCapture(1); | 228 StopCapture(1); |
| 220 } | 229 } |
| 221 | 230 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 StopCapture(0); | 305 StopCapture(0); |
| 297 // A buffer received after StopCapture() triggers an instant ReleaseBuffer(). | 306 // A buffer received after StopCapture() triggers an instant ReleaseBuffer(). |
| 298 SimulateBufferReceived(kBufferId, params_large_.requested_format.frame_size); | 307 SimulateBufferReceived(kBufferId, params_large_.requested_format.frame_size); |
| 299 SimulateBufferDestroyed(kBufferId); | 308 SimulateBufferDestroyed(kBufferId); |
| 300 | 309 |
| 301 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1); | 310 EXPECT_EQ(mock_video_capture_host_.released_buffer_count(), 1); |
| 302 } | 311 } |
| 303 | 312 |
| 304 TEST_F(VideoCaptureImplTest, AlreadyStarted) { | 313 TEST_F(VideoCaptureImplTest, AlreadyStarted) { |
| 305 media::VideoCaptureParams params = {}; | 314 media::VideoCaptureParams params = {}; |
| 306 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 315 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 307 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 316 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 308 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, _)) | 317 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)) |
| 309 .WillOnce(SaveArg<2>(¶ms)); | 318 .WillOnce(DoAll(InvokeWithoutArgs([this]() { |
| 319 video_capture_impl_->OnStateChanged( | |
| 320 mojom::VideoCaptureState::STARTED); | |
| 321 }), | |
| 322 SaveArg<2>(¶ms))); | |
| 310 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | 323 EXPECT_CALL(mock_video_capture_host_, Stop(_)); |
| 311 | 324 |
| 312 StartCapture(0, params_small_); | 325 StartCapture(0, params_small_); |
| 313 StartCapture(1, params_large_); | 326 StartCapture(1, params_large_); |
| 314 StopCapture(0); | 327 StopCapture(0); |
| 315 StopCapture(1); | 328 StopCapture(1); |
| 316 DCHECK(params.requested_format == params_small_.requested_format); | 329 DCHECK(params.requested_format == params_small_.requested_format); |
| 317 } | 330 } |
| 318 | 331 |
| 319 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { | 332 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 333 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); | 346 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); |
| 334 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); | 347 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)); |
| 335 | 348 |
| 336 StartCapture(0, params_small_); | 349 StartCapture(0, params_small_); |
| 337 | 350 |
| 338 OnStateChanged(mojom::VideoCaptureState::FAILED); | 351 OnStateChanged(mojom::VideoCaptureState::FAILED); |
| 339 | 352 |
| 340 StopCapture(0); | 353 StopCapture(0); |
| 341 } | 354 } |
| 342 | 355 |
| 356 TEST_F(VideoCaptureImplTest, BufferReceivedBeforeOnStarted) { | |
| 357 const int kBufferId = 16; | |
| 358 | |
| 359 base::SharedMemory shm; | |
| 360 const size_t frame_size = media::VideoFrame::AllocationSize( | |
| 361 media::PIXEL_FORMAT_I420, params_small_.requested_format.frame_size); | |
| 362 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); | |
| 363 | |
| 364 InSequence s; | |
| 365 EXPECT_CALL(mock_video_capture_host_, DoStart(_, kSessionId, params_small_)) | |
| 366 .WillOnce(DoNothing()); | |
|
emircan
2017/03/13 21:03:35
Do you need to call DoNothing() here? You can repl
braveyao
2017/03/14 00:05:16
Oh, ".Times(1)" doesn't work here simply.
And we w
| |
| 367 EXPECT_CALL(mock_video_capture_host_, ReleaseBuffer(_, kBufferId, _)); | |
| 368 StartCapture(0, params_small_); | |
| 369 SimulateOnBufferCreated(kBufferId, shm); | |
| 370 SimulateBufferReceived(kBufferId, params_small_.requested_format.frame_size); | |
| 371 | |
| 372 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | |
| 373 EXPECT_CALL(mock_video_capture_host_, RequestRefreshFrame(_)); | |
| 374 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED); | |
| 375 | |
| 376 // Additional STARTED will cause RequestRefreshFrame a second time. | |
| 377 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | |
| 378 EXPECT_CALL(mock_video_capture_host_, RequestRefreshFrame(_)); | |
| 379 video_capture_impl_->OnStateChanged(mojom::VideoCaptureState::STARTED); | |
| 380 | |
| 381 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | |
| 382 EXPECT_CALL(mock_video_capture_host_, Stop(_)); | |
| 383 StopCapture(0); | |
| 384 } | |
| 385 | |
| 343 } // namespace content | 386 } // namespace content |
| OLD | NEW |