| 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/media/video_capture_messages.h" | 11 #include "content/common/media/video_capture_messages.h" |
| 12 #include "content/common/video_capture.mojom.h" |
| 12 #include "content/renderer/media/video_capture_impl.h" | 13 #include "content/renderer/media/video_capture_impl.h" |
| 13 #include "media/base/bind_to_current_loop.h" | 14 #include "media/base/bind_to_current_loop.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using ::testing::_; | 18 using ::testing::_; |
| 18 using ::testing::AtLeast; | 19 using ::testing::AtLeast; |
| 19 using ::testing::InvokeWithoutArgs; | 20 using ::testing::InvokeWithoutArgs; |
| 20 using ::testing::Return; | 21 using ::testing::Return; |
| 21 using ::testing::SaveArg; | 22 using ::testing::SaveArg; |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 | 25 |
| 26 // Mock implementation of the Mojo service implementation. |
| 27 // TODO(mcasas): In time MockVideoCaptureService will completely replace |
| 28 // MockVideoCaptureMessageFilter |
| 29 class MockVideoCaptureService : public mojom::VideoCaptureService { |
| 30 public: |
| 31 MockVideoCaptureService() = default; |
| 32 |
| 33 MOCK_METHOD1(Stop, void(int32_t)); |
| 34 MOCK_METHOD1(Pause, void(int32_t)); |
| 35 MOCK_METHOD1(RequestRefreshFrame, void(int32_t)); |
| 36 |
| 37 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureService); |
| 39 }; |
| 40 |
| 25 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { | 41 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { |
| 26 public: | 42 public: |
| 27 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} | 43 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} |
| 28 | 44 |
| 29 // Filter implementation. | 45 // Filter implementation. |
| 30 MOCK_METHOD1(Send, bool(IPC::Message* message)); | 46 MOCK_METHOD1(Send, bool(IPC::Message* message)); |
| 31 | 47 |
| 32 protected: | 48 protected: |
| 33 virtual ~MockVideoCaptureMessageFilter() {} | 49 virtual ~MockVideoCaptureMessageFilter() {} |
| 34 | 50 |
| 35 private: | 51 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); | 52 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); |
| 37 }; | 53 }; |
| 38 | 54 |
| 39 class VideoCaptureImplTest : public ::testing::Test { | 55 class VideoCaptureImplTest : public ::testing::Test { |
| 40 public: | 56 public: |
| 41 class MockVideoCaptureImpl : public VideoCaptureImpl { | 57 class MockVideoCaptureImpl : public VideoCaptureImpl { |
| 42 public: | 58 public: |
| 43 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, | 59 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, |
| 44 VideoCaptureMessageFilter* filter) | 60 VideoCaptureMessageFilter* filter) |
| 45 : VideoCaptureImpl(id, filter, base::ThreadTaskRunnerHandle::Get()), | 61 : VideoCaptureImpl(id, filter, base::ThreadTaskRunnerHandle::Get()), |
| 46 received_buffer_count_(0) {} | 62 received_buffer_count_(0) {} |
| 47 ~MockVideoCaptureImpl() override {} | 63 ~MockVideoCaptureImpl() override {} |
| 48 | 64 |
| 49 // Override Send() to mimic device to send events. | 65 // Override Send() to mimic device to send events. |
| 50 void Send(IPC::Message* message) override { | 66 void Send(IPC::Message* message) override { |
| 51 CHECK(message); | |
| 52 | |
| 53 // In this method, messages are sent to the according handlers as if | 67 // In this method, messages are sent to the according handlers as if |
| 54 // we are the device. | 68 // we are the device. |
| 55 bool handled = true; | 69 bool handled = true; |
| 56 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) | 70 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) |
| 57 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, DeviceStartCapture) | 71 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, DeviceStartCapture) |
| 58 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, DevicePauseCapture) | |
| 59 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, DeviceStopCapture) | |
| 60 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, | 72 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, |
| 61 DeviceReceiveEmptyBuffer) | 73 DeviceReceiveEmptyBuffer) |
| 62 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceSupportedFormats, | 74 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceSupportedFormats, |
| 63 DeviceGetSupportedFormats) | 75 DeviceGetSupportedFormats) |
| 64 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceFormatsInUse, | 76 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceFormatsInUse, |
| 65 DeviceGetFormatsInUse) | 77 DeviceGetFormatsInUse) |
| 66 IPC_MESSAGE_UNHANDLED(handled = false) | 78 IPC_MESSAGE_UNHANDLED(handled = false) |
| 67 IPC_END_MESSAGE_MAP() | 79 IPC_END_MESSAGE_MAP() |
| 68 EXPECT_TRUE(handled); | 80 EXPECT_TRUE(handled); |
| 69 delete message; | 81 delete message; |
| 70 } | 82 } |
| 71 | 83 |
| 72 void DeviceStartCapture(int device_id, | 84 void DeviceStartCapture(int device_id, |
| 73 media::VideoCaptureSessionId session_id, | 85 media::VideoCaptureSessionId session_id, |
| 74 const media::VideoCaptureParams& params) { | 86 const media::VideoCaptureParams& params) { |
| 75 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED); | 87 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED); |
| 76 capture_params_ = params; | 88 capture_params_ = params; |
| 77 } | 89 } |
| 78 | 90 |
| 79 void DevicePauseCapture(int device_id) {} | |
| 80 | |
| 81 void DeviceStopCapture(int device_id) { | |
| 82 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); | |
| 83 } | |
| 84 | |
| 85 void DeviceReceiveEmptyBuffer(int device_id, | 91 void DeviceReceiveEmptyBuffer(int device_id, |
| 86 int buffer_id, | 92 int buffer_id, |
| 87 const gpu::SyncToken& release_sync_token, | 93 const gpu::SyncToken& release_sync_token, |
| 88 double consumer_resource_utilization) { | 94 double consumer_resource_utilization) { |
| 89 received_buffer_count_++; | 95 received_buffer_count_++; |
| 90 } | 96 } |
| 91 | 97 |
| 92 void DeviceGetSupportedFormats(int device_id, | 98 void DeviceGetSupportedFormats(int device_id, |
| 93 media::VideoCaptureSessionId session_id) { | 99 media::VideoCaptureSessionId session_id) { |
| 94 // When the mock message filter receives a request for the device | 100 // When the mock message filter receives a request for the device |
| (...skipping 25 matching lines...) Expand all Loading... |
| 120 : child_process_(new ChildProcess()), | 126 : child_process_(new ChildProcess()), |
| 121 message_filter_(new MockVideoCaptureMessageFilter), | 127 message_filter_(new MockVideoCaptureMessageFilter), |
| 122 session_id_(1), | 128 session_id_(1), |
| 123 video_capture_impl_( | 129 video_capture_impl_( |
| 124 new MockVideoCaptureImpl(session_id_, message_filter_.get())) { | 130 new MockVideoCaptureImpl(session_id_, message_filter_.get())) { |
| 125 params_small_.requested_format = media::VideoCaptureFormat( | 131 params_small_.requested_format = media::VideoCaptureFormat( |
| 126 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); | 132 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); |
| 127 params_large_.requested_format = media::VideoCaptureFormat( | 133 params_large_.requested_format = media::VideoCaptureFormat( |
| 128 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); | 134 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); |
| 129 | 135 |
| 136 video_capture_impl_->SetVideoCaptureServiceForTesting( |
| 137 &mock_video_capture_service_); |
| 130 video_capture_impl_->device_id_ = 2; | 138 video_capture_impl_->device_id_ = 2; |
| 131 } | 139 } |
| 132 | |
| 133 virtual ~VideoCaptureImplTest() {} | 140 virtual ~VideoCaptureImplTest() {} |
| 134 | 141 |
| 135 protected: | 142 protected: |
| 136 MOCK_METHOD2(OnFrameReady, | 143 MOCK_METHOD2(OnFrameReady, |
| 137 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); | 144 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); |
| 138 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); | 145 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); |
| 139 MOCK_METHOD1(OnDeviceFormatsInUse, | 146 MOCK_METHOD1(OnDeviceFormatsInUse, |
| 140 void(const media::VideoCaptureFormats&)); | 147 void(const media::VideoCaptureFormats&)); |
| 141 MOCK_METHOD1(OnDeviceSupportedFormats, | 148 MOCK_METHOD1(OnDeviceSupportedFormats, |
| 142 void(const media::VideoCaptureFormats&)); | 149 void(const media::VideoCaptureFormats&)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 &VideoCaptureImplTest::OnDeviceFormatsInUse, | 199 &VideoCaptureImplTest::OnDeviceFormatsInUse, |
| 193 base::Unretained(this)); | 200 base::Unretained(this)); |
| 194 video_capture_impl_->GetDeviceFormatsInUse(callback); | 201 video_capture_impl_->GetDeviceFormatsInUse(callback); |
| 195 } | 202 } |
| 196 | 203 |
| 197 const base::MessageLoop message_loop_; | 204 const base::MessageLoop message_loop_; |
| 198 const std::unique_ptr<ChildProcess> child_process_; | 205 const std::unique_ptr<ChildProcess> child_process_; |
| 199 const scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; | 206 const scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; |
| 200 const media::VideoCaptureSessionId session_id_; | 207 const media::VideoCaptureSessionId session_id_; |
| 201 std::unique_ptr<MockVideoCaptureImpl> video_capture_impl_; | 208 std::unique_ptr<MockVideoCaptureImpl> video_capture_impl_; |
| 209 MockVideoCaptureService mock_video_capture_service_; |
| 202 media::VideoCaptureParams params_small_; | 210 media::VideoCaptureParams params_small_; |
| 203 media::VideoCaptureParams params_large_; | 211 media::VideoCaptureParams params_large_; |
| 204 | 212 |
| 205 private: | 213 private: |
| 206 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); | 214 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); |
| 207 }; | 215 }; |
| 208 | 216 |
| 209 TEST_F(VideoCaptureImplTest, Simple) { | 217 TEST_F(VideoCaptureImplTest, Simple) { |
| 210 // Execute SetCapture() and StopCapture() for one client. | 218 // Execute SetCapture() and StopCapture() for one client. |
| 211 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 219 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 | 352 |
| 345 StartCapture(0, params_small_); | 353 StartCapture(0, params_small_); |
| 346 | 354 |
| 347 // Receive state change message from browser. | 355 // Receive state change message from browser. |
| 348 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); | 356 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); |
| 349 | 357 |
| 350 StopCapture(0); | 358 StopCapture(0); |
| 351 } | 359 } |
| 352 | 360 |
| 353 } // namespace content | 361 } // namespace content |
| OLD | NEW |