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 |
| 17 namespace media { |
| 18 |
| 19 namespace { |
| 20 |
| 21 ACTION_P(RunClosure, closure) { |
| 22 closure.Run(); |
| 23 } |
| 24 |
| 25 } // anonymous namespace |
| 26 |
| 27 class VideoCaptureHandlerImplTest : public testing::Test { |
| 28 public: |
| 29 VideoCaptureHandlerImplTest() {} |
| 30 |
| 31 void SetUp() override { |
| 32 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 33 switches::kUseFakeDeviceForMediaStream); |
| 34 video_capture_handler_impl_.reset( |
| 35 new VideoCaptureHandlerImpl(nullptr /* app */)); |
| 36 } |
| 37 |
| 38 MOCK_METHOD0(DoOnEnumerateDevicesReply, void(void)); |
| 39 void OnEnumerateDevicesReply( |
| 40 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> reply) { |
| 41 DoOnEnumerateDevicesReply(); |
| 42 media_devices_info_ = std::move(reply); |
| 43 } |
| 44 MOCK_METHOD1(DoOnRequestVideoCaptureStreamReply, |
| 45 void(const mojom::VideoCaptureStream&)); |
| 46 void OnRequestVideoCaptureStreamReply(mojom::VideoCaptureStreamPtr stream) { |
| 47 DVLOG(1) << __FUNCTION__; |
| 48 DoOnRequestVideoCaptureStreamReply(*stream); |
| 49 } |
| 50 |
| 51 void EnumerateDevices( |
| 52 const VideoCaptureHandlerImpl::EnumerateDevicesCallback& callback) { |
| 53 video_capture_handler_impl_->EnumerateDevices(callback); |
| 54 } |
| 55 |
| 56 void SetNumFakeDevices(int num) { |
| 57 static_cast<media::FakeVideoCaptureDeviceFactory*>( |
| 58 video_capture_handler_impl_->video_capture_device_factory_.get()) |
| 59 ->set_number_of_devices(num); |
| 60 } |
| 61 |
| 62 void RequestVideoCaptureStream( |
| 63 mojom::StreamOptionsPtr options, |
| 64 const mojo::String& security_origin, |
| 65 const VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback& |
| 66 callback) { |
| 67 video_capture_handler_impl_->RequestVideoCaptureStream( |
| 68 std::move(options), security_origin, callback); |
| 69 } |
| 70 |
| 71 protected: |
| 72 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl. |
| 73 const base::MessageLoop loop_; |
| 74 |
| 75 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to. |
| 76 base::SystemMonitor system_monitor_; |
| 77 |
| 78 // The class under test. Needs to be a scoped_ptr to be initialized after |
| 79 // adding |kUseFakeDeviceForMediaStream| command line flag. |
| 80 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_; |
| 81 |
| 82 // Variable to hold on to the returned list of enumerated devices. |
| 83 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> media_devices_info_; |
| 84 |
| 85 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandlerImplTest); |
| 87 }; |
| 88 |
| 89 TEST_F(VideoCaptureHandlerImplTest, CreateAndDestroy) {} |
| 90 |
| 91 // Tests a cycle EnumerateDevices()-> EnumerateDevicesCallback(). |
| 92 TEST_F(VideoCaptureHandlerImplTest, EnumerateDevices) { |
| 93 InSequence s; |
| 94 |
| 95 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
| 96 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
| 97 base::Unretained(this)); |
| 98 |
| 99 base::RunLoop run_loop; |
| 100 base::Closure quit_closure = run_loop.QuitClosure(); |
| 101 EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
| 102 .Times(1) |
| 103 .WillOnce(RunClosure(quit_closure)); |
| 104 EnumerateDevices(std::move(callback)); |
| 105 |
| 106 run_loop.Run(); |
| 107 ASSERT_EQ(1u, media_devices_info_.size()); |
| 108 } |
| 109 |
| 110 // Tests a cycle EnumerateDevices()-> EnumerateDevicesCallback() before |
| 111 // and after a change in system devices has happened. |
| 112 TEST_F(VideoCaptureHandlerImplTest, EnumerateDevicesAndReenumerate) { |
| 113 InSequence s; |
| 114 { // 1 Enumeration EnumerateDevices()-> EnumerateDevicesCallback(). |
| 115 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
| 116 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
| 117 base::Unretained(this)); |
| 118 |
| 119 base::RunLoop run_loop; |
| 120 base::Closure quit_closure = run_loop.QuitClosure(); |
| 121 EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
| 122 .Times(1) |
| 123 .WillOnce(RunClosure(quit_closure)); |
| 124 EnumerateDevices(std::move(callback)); |
| 125 run_loop.Run(); |
| 126 |
| 127 ASSERT_GE(media_devices_info_.size(), 1u); |
| 128 } |
| 129 |
| 130 SetNumFakeDevices(2); |
| 131 system_monitor_.ProcessDevicesChanged( |
| 132 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); |
| 133 base::RunLoop().RunUntilIdle(); |
| 134 |
| 135 { |
| 136 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
| 137 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
| 138 base::Unretained(this)); |
| 139 |
| 140 base::RunLoop run_loop; |
| 141 base::Closure quit_closure = run_loop.QuitClosure(); |
| 142 EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
| 143 .Times(1) |
| 144 .WillOnce(RunClosure(quit_closure)); |
| 145 EnumerateDevices(std::move(callback)); |
| 146 run_loop.Run(); |
| 147 |
| 148 ASSERT_GE(media_devices_info_.size(), 2u); |
| 149 } |
| 150 } |
| 151 |
| 152 // Tests a cycle RequestVideoCaptureStream() -> |
| 153 // RequestVideoCaptureStreamCallback(). |
| 154 TEST_F(VideoCaptureHandlerImplTest, RequestVideoCaptureStream) { |
| 155 InSequence s; |
| 156 |
| 157 { // Enumerate devices to get a valid |id| for the first one. |
| 158 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = |
| 159 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply, |
| 160 base::Unretained(this)); |
| 161 |
| 162 base::RunLoop run_loop; |
| 163 base::Closure quit_closure = run_loop.QuitClosure(); |
| 164 EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
| 165 .Times(1) |
| 166 .WillOnce(RunClosure(quit_closure)); |
| 167 EnumerateDevices(std::move(callback)); |
| 168 |
| 169 run_loop.Run(); |
| 170 } |
| 171 { |
| 172 mojom::StreamOptionsPtr options = mojom::StreamOptions::New(); |
| 173 options->device_id = media_devices_info_[0]->device_id; |
| 174 options->capture_size = mojo::Size::From(gfx::Size(640, 480)); |
| 175 options->frame_rate = 30.0; |
| 176 |
| 177 VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback callback = |
| 178 base::Bind( |
| 179 &VideoCaptureHandlerImplTest::OnRequestVideoCaptureStreamReply, |
| 180 base::Unretained(this)); |
| 181 |
| 182 base::RunLoop run_loop; |
| 183 base::Closure quit_closure = run_loop.QuitClosure(); |
| 184 |
| 185 // TODO(mcasas): Inspect the replied sources[0] contents. |
| 186 EXPECT_CALL(*this, DoOnRequestVideoCaptureStreamReply(_)) |
| 187 .Times(1) |
| 188 .WillOnce(RunClosure(quit_closure)); |
| 189 RequestVideoCaptureStream(std::move(options), "https://localhost", |
| 190 std::move(callback)); |
| 191 |
| 192 run_loop.Run(); |
| 193 } |
| 194 } |
| 195 |
| 196 } // namespace media |
OLD | NEW |