OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/command_line.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "media/base/media_switches.h" |
| 8 #include "media/capture/service/video_capture_handler_impl.h" |
| 9 #include "media/capture/video/fake_video_capture_device_factory.h" |
| 10 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using ::testing::_; |
| 15 using ::testing::InSequence; |
| 16 using ::testing::Field; |
| 17 |
| 18 namespace media { |
| 19 |
| 20 namespace { |
| 21 |
| 22 ACTION_P(RunClosure, closure) { |
| 23 closure.Run(); |
| 24 } |
| 25 |
| 26 } // anonymous namespace |
| 27 |
| 28 class VideoCaptureHandlerImplTest : public testing::Test { |
| 29 public: |
| 30 VideoCaptureHandlerImplTest() {} |
| 31 |
| 32 void SetUp() override { |
| 33 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 34 switches::kUseFakeDeviceForMediaStream); |
| 35 video_capture_handler_impl_.reset( |
| 36 new VideoCaptureHandlerImpl(nullptr /* app */)); |
| 37 } |
| 38 |
| 39 MOCK_METHOD1(DoOnRequestMediaDevicesReply, |
| 40 void(const video_capture::MediaDevicesInfoReply&)); |
| 41 void OnRequestMediaDevicesReply( |
| 42 video_capture::MediaDevicesInfoReplyPtr reply) { |
| 43 DVLOG(1) << __FUNCTION__; |
| 44 DoOnRequestMediaDevicesReply(*reply); |
| 45 media_devices_info_ = std::move(reply); |
| 46 } |
| 47 MOCK_METHOD1(DoOnRequestStreamReply, void(const video_capture::Stream&)); |
| 48 void OnRequestStreamReply(video_capture::StreamPtr stream) { |
| 49 DVLOG(1) << __FUNCTION__; |
| 50 DoOnRequestStreamReply(*stream); |
| 51 } |
| 52 |
| 53 void RequestMediaDevices( |
| 54 video_capture::MediaDevicesInfoRequestPtr request, |
| 55 const VideoCaptureHandlerImpl::RequestMediaDevicesCallback& callback) { |
| 56 video_capture_handler_impl_->RequestMediaDevices(std::move(request), |
| 57 callback); |
| 58 } |
| 59 |
| 60 void SetNumFakeDevices(int num) { |
| 61 static_cast<media::FakeVideoCaptureDeviceFactory*>( |
| 62 video_capture_handler_impl_->video_capture_device_factory_.get()) |
| 63 ->set_number_of_devices(num); |
| 64 } |
| 65 |
| 66 void RequestStream( |
| 67 video_capture::StreamOptionsPtr options, |
| 68 const mojo::String& security_origin, |
| 69 const VideoCaptureHandlerImpl::RequestStreamCallback& callback) { |
| 70 video_capture_handler_impl_->RequestStream(std::move(options), |
| 71 security_origin, callback); |
| 72 } |
| 73 |
| 74 protected: |
| 75 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl. |
| 76 const base::MessageLoop loop_; |
| 77 |
| 78 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to. |
| 79 base::SystemMonitor system_monitor_; |
| 80 |
| 81 // The class under test. Needs to be a scoped_ptr to be initialized after |
| 82 // adding |kUseFakeDeviceForMediaStream| command line flag. |
| 83 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_; |
| 84 |
| 85 // Variable to hold on to the returned list of enumerated devices. |
| 86 video_capture::MediaDevicesInfoReplyPtr media_devices_info_; |
| 87 |
| 88 private: |
| 89 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandlerImplTest); |
| 90 }; |
| 91 |
| 92 TEST_F(VideoCaptureHandlerImplTest, CreateAndDestroy) {} |
| 93 |
| 94 // Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback(). |
| 95 TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevices) { |
| 96 InSequence s; |
| 97 const int kRequestId = 123; |
| 98 |
| 99 video_capture::MediaDevicesInfoRequestPtr request = |
| 100 video_capture::MediaDevicesInfoRequest::New(); |
| 101 request->request_id = kRequestId; |
| 102 |
| 103 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback = |
| 104 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply, |
| 105 base::Unretained(this)); |
| 106 |
| 107 base::RunLoop run_loop; |
| 108 base::Closure quit_closure = run_loop.QuitClosure(); |
| 109 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply(Field( |
| 110 &video_capture::MediaDevicesInfoReply::request_id, |
| 111 kRequestId))) |
| 112 .Times(1) |
| 113 .WillOnce(RunClosure(quit_closure)); |
| 114 RequestMediaDevices(std::move(request), std::move(callback)); |
| 115 |
| 116 run_loop.Run(); |
| 117 ASSERT_EQ(1u, media_devices_info_->sources.size()); |
| 118 EXPECT_EQ(video_capture::SourceKind::Video, |
| 119 media_devices_info_->sources[0]->kind); |
| 120 } |
| 121 |
| 122 // Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback() before |
| 123 // and after a change in system devices has happened. |
| 124 TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevicesAndReenumerate) { |
| 125 InSequence s; |
| 126 const int kRequestId = 123; |
| 127 |
| 128 { // 1 Enumeration RequestMediaDevices()-> RequestMediaDevicesCallback(). |
| 129 video_capture::MediaDevicesInfoRequestPtr request = |
| 130 video_capture::MediaDevicesInfoRequest::New(); |
| 131 request->request_id = kRequestId; |
| 132 |
| 133 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback = |
| 134 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply, |
| 135 base::Unretained(this)); |
| 136 |
| 137 base::RunLoop run_loop; |
| 138 base::Closure quit_closure = run_loop.QuitClosure(); |
| 139 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply(Field( |
| 140 &video_capture::MediaDevicesInfoReply::request_id, |
| 141 kRequestId))) |
| 142 .Times(1) |
| 143 .WillOnce(RunClosure(quit_closure)); |
| 144 RequestMediaDevices(std::move(request), std::move(callback)); |
| 145 run_loop.Run(); |
| 146 |
| 147 ASSERT_GE(media_devices_info_->sources.size(), 1u); |
| 148 } |
| 149 |
| 150 SetNumFakeDevices(2); |
| 151 system_monitor_.ProcessDevicesChanged( |
| 152 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); |
| 153 base::RunLoop().RunUntilIdle(); |
| 154 |
| 155 { |
| 156 video_capture::MediaDevicesInfoRequestPtr request = |
| 157 video_capture::MediaDevicesInfoRequest::New(); |
| 158 request->request_id = kRequestId; |
| 159 |
| 160 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback = |
| 161 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply, |
| 162 base::Unretained(this)); |
| 163 |
| 164 base::RunLoop run_loop; |
| 165 base::Closure quit_closure = run_loop.QuitClosure(); |
| 166 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply(Field( |
| 167 &video_capture::MediaDevicesInfoReply::request_id, |
| 168 kRequestId))) |
| 169 .Times(1) |
| 170 .WillOnce(RunClosure(quit_closure)); |
| 171 RequestMediaDevices(std::move(request), std::move(callback)); |
| 172 run_loop.Run(); |
| 173 |
| 174 ASSERT_GE(media_devices_info_->sources.size(), 2u); |
| 175 } |
| 176 } |
| 177 |
| 178 // Tests a cycle RequestStream() -> RequestStreamCallback(). |
| 179 TEST_F(VideoCaptureHandlerImplTest, RequestStream) { |
| 180 InSequence s; |
| 181 |
| 182 { // Enumerate devices to get a valid |id| for the first one. |
| 183 const int kRequestId = 123; |
| 184 video_capture::MediaDevicesInfoRequestPtr request = |
| 185 video_capture::MediaDevicesInfoRequest::New(); |
| 186 request->request_id = kRequestId; |
| 187 |
| 188 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback = |
| 189 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply, |
| 190 base::Unretained(this)); |
| 191 |
| 192 base::RunLoop run_loop; |
| 193 base::Closure quit_closure = run_loop.QuitClosure(); |
| 194 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply(Field( |
| 195 &video_capture::MediaDevicesInfoReply::request_id, |
| 196 kRequestId))) |
| 197 .Times(1) |
| 198 .WillOnce(RunClosure(quit_closure)); |
| 199 RequestMediaDevices(std::move(request), std::move(callback)); |
| 200 |
| 201 run_loop.Run(); |
| 202 } |
| 203 { |
| 204 const int kRequestId = 456; |
| 205 video_capture::StreamOptionsPtr options = |
| 206 video_capture::StreamOptions::New(); |
| 207 options->request_id = kRequestId; |
| 208 options->device_id = media_devices_info_->sources[0]->device_id; |
| 209 options->capture_size = mojo::Size::From(gfx::Size(640, 480)); |
| 210 options->frame_rate = 30.0; |
| 211 |
| 212 VideoCaptureHandlerImpl::RequestStreamCallback callback = |
| 213 base::Bind(&VideoCaptureHandlerImplTest::OnRequestStreamReply, |
| 214 base::Unretained(this)); |
| 215 |
| 216 base::RunLoop run_loop; |
| 217 base::Closure quit_closure = run_loop.QuitClosure(); |
| 218 |
| 219 // TODO(mcasas): Inspect the replied sources[0] contents. |
| 220 EXPECT_CALL(*this, DoOnRequestStreamReply(_)) |
| 221 .Times(1) |
| 222 .WillOnce(RunClosure(quit_closure)); |
| 223 RequestStream(std::move(options), "https://localhost", std::move(callback)); |
| 224 |
| 225 run_loop.Run(); |
| 226 } |
| 227 } |
| 228 |
| 229 } // namespace media |
OLD | NEW |