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/interfaces/video_capture.mojom.h" |
| 9 #include "media/capture/service/mock_stream_client.h" |
| 10 #include "media/capture/service/stream_impl.h" |
| 11 #include "media/capture/service/video_capture_handler_impl.h" |
| 12 #include "media/capture/video/fake_video_capture_device_factory.h" |
| 13 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 #if defined(OS_MACOSX) |
| 18 #include "media/base/mac/avfoundation_glue.h" |
| 19 #endif |
| 20 |
| 21 using ::testing::_; |
| 22 using ::testing::AnyNumber; |
| 23 using ::testing::InSequence; |
| 24 using ::testing::Field; |
| 25 |
| 26 namespace media { |
| 27 |
| 28 namespace { |
| 29 |
| 30 ACTION_P(RunClosure, closure) { |
| 31 closure.Run(); |
| 32 } |
| 33 |
| 34 void UseFakeFactory() { |
| 35 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 36 switches::kUseFakeDeviceForMediaStream); |
| 37 } |
| 38 |
| 39 void UseRealFactory() { |
| 40 #if defined(OS_MACOSX) |
| 41 AVFoundationGlue::InitializeAVFoundation(); |
| 42 #endif |
| 43 } |
| 44 |
| 45 } // anonymous namespace |
| 46 |
| 47 // This test coalesces all the classes and objects that conform a video capture |
| 48 // component from the outside. It then exercises the .mojom interfaces. |
| 49 // TODO(mcasas): This test is an integration test of sorts, but we use |
| 50 // a VideoCaptureHandlerImpl class directly and not via a Mojo pipe. Remove this |
| 51 // test when an VideoCapture Mojo App (i.e. a truly black box) test is landed. |
| 52 class VideoCaptureTest : public ::testing::Test { |
| 53 public: |
| 54 VideoCaptureTest() {} |
| 55 |
| 56 void InitializeVideoCaptureHandlerImpl() { |
| 57 video_capture_handler_impl_.reset( |
| 58 new VideoCaptureHandlerImpl(nullptr /* app */)); |
| 59 base::RunLoop().RunUntilIdle(); |
| 60 } |
| 61 |
| 62 void RequestMediaDevices( |
| 63 video_capture::MediaDevicesInfoRequestPtr request, |
| 64 const VideoCaptureHandlerImpl::RequestMediaDevicesCallback& callback) { |
| 65 video_capture_handler_impl_->RequestMediaDevices(std::move(request), |
| 66 callback); |
| 67 } |
| 68 |
| 69 MOCK_METHOD1(DoOnRequestMediaDevicesReply, |
| 70 void(const video_capture::MediaDevicesInfoReply&)); |
| 71 void OnRequestMediaDevicesReply( |
| 72 video_capture::MediaDevicesInfoReplyPtr reply) { |
| 73 DVLOG(1) << __FUNCTION__; |
| 74 DoOnRequestMediaDevicesReply(*reply); |
| 75 media_devices_info_ = std::move(reply); |
| 76 } |
| 77 |
| 78 MOCK_METHOD1(DoOnRequestStreamReply, void(const video_capture::Stream&)); |
| 79 void OnRequestStreamReply(video_capture::StreamPtr stream) { |
| 80 EXPECT_TRUE(stream.is_bound()); |
| 81 DoOnRequestStreamReply(*stream); |
| 82 stream_ = std::move(stream); |
| 83 } |
| 84 |
| 85 // Enumerate devices to get a valid |id| for the first one. |
| 86 void EnumerateDevices() { |
| 87 const int kRequestId = 123; |
| 88 video_capture::MediaDevicesInfoRequestPtr request = |
| 89 video_capture::MediaDevicesInfoRequest::New(); |
| 90 request->request_id = kRequestId; |
| 91 |
| 92 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback = base::Bind( |
| 93 &VideoCaptureTest::OnRequestMediaDevicesReply, base::Unretained(this)); |
| 94 |
| 95 base::RunLoop run_loop; |
| 96 base::Closure quit_closure = run_loop.QuitClosure(); |
| 97 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply(Field( |
| 98 &video_capture::MediaDevicesInfoReply::request_id, |
| 99 kRequestId))) |
| 100 .Times(1) |
| 101 .WillOnce(RunClosure(quit_closure)); |
| 102 RequestMediaDevices(std::move(request), std::move(callback)); |
| 103 |
| 104 run_loop.Run(); |
| 105 } |
| 106 |
| 107 // This block runs a RequestStream() --> get a Stream(). |
| 108 void RequestStream() { |
| 109 const int kRequestId = 456; |
| 110 |
| 111 video_capture::StreamOptionsPtr options = |
| 112 video_capture::StreamOptions::New(); |
| 113 options->request_id = kRequestId; |
| 114 options->device_id = media_devices_info_->sources[0]->device_id; |
| 115 options->capture_size = mojo::Size::From(gfx::Size(320, 240)); |
| 116 options->frame_rate = 30.0; |
| 117 |
| 118 VideoCaptureHandlerImpl::RequestStreamCallback callback = base::Bind( |
| 119 &VideoCaptureTest::OnRequestStreamReply, base::Unretained(this)); |
| 120 base::RunLoop run_loop; |
| 121 base::Closure quit_closure = run_loop.QuitClosure(); |
| 122 |
| 123 EXPECT_CALL(*this, DoOnRequestStreamReply(_)) |
| 124 .Times(1) |
| 125 .WillOnce(RunClosure(quit_closure)); |
| 126 |
| 127 video_capture_handler_impl_->RequestStream( |
| 128 std::move(options), "https://localhost", std::move(callback)); |
| 129 run_loop.Run(); |
| 130 ASSERT_TRUE(!!stream_.get()); |
| 131 } |
| 132 |
| 133 // Call Stream::Start() and expect some frames. |
| 134 void StartAndExpectFrames() { |
| 135 base::RunLoop run_loop; |
| 136 base::Closure quit_closure = run_loop.QuitClosure(); |
| 137 |
| 138 EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _)).Times(3); |
| 139 EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _)) |
| 140 .Times(1) |
| 141 .WillOnce(RunClosure(quit_closure)); |
| 142 stream_->Start(stream_client_impl_.CreateProxy()); |
| 143 |
| 144 run_loop.Run(); |
| 145 } |
| 146 |
| 147 void StartAndExpectFramesAndError() { |
| 148 base::RunLoop run_loop; |
| 149 base::Closure quit_closure = run_loop.QuitClosure(); |
| 150 |
| 151 EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _)).Times(AnyNumber()); |
| 152 stream_->Start(stream_client_impl_.CreateProxy()); |
| 153 |
| 154 EXPECT_CALL(stream_client_impl_, Error(_)) |
| 155 .Times(1) |
| 156 .WillOnce(RunClosure(quit_closure)); |
| 157 |
| 158 // Access the first and only StreamImpl and ping its OnError() callback. |
| 159 ASSERT_EQ(1u, video_capture_handler_impl_->device_and_streams_.size()); |
| 160 video_capture_handler_impl_->device_and_streams_.begin() |
| 161 ->second.second->OnError("Terrible error"); |
| 162 run_loop.Run(); |
| 163 } |
| 164 |
| 165 void FakeOnErrorFromDevice() {} |
| 166 |
| 167 protected: |
| 168 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl. |
| 169 const base::MessageLoop loop_; |
| 170 |
| 171 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to. |
| 172 base::SystemMonitor system_monitor_; |
| 173 |
| 174 // The component's entry point. Needs to be a scoped_ptr to be initialized |
| 175 // after adding command line flag for the appropriate Device Factory. |
| 176 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_; |
| 177 |
| 178 // The mock of the (remote) video capture client. |
| 179 MockStreamClient stream_client_impl_; |
| 180 |
| 181 // Local pointer to the generated Stream spitting VideoFrames. |
| 182 video_capture::StreamPtr stream_; |
| 183 |
| 184 // Variable to hold on to the returned list of enumerated devices. |
| 185 video_capture::MediaDevicesInfoReplyPtr media_devices_info_; |
| 186 |
| 187 private: |
| 188 DISALLOW_COPY_AND_ASSIGN(VideoCaptureTest); |
| 189 }; |
| 190 |
| 191 TEST_F(VideoCaptureTest, CreateAndDestroy) {} |
| 192 |
| 193 // Tests a full cycle RequestMediaDevices() -> RequestMediaDevicesReply -> |
| 194 // RequestStream()-> RequestStreamReply -> Start() -> get frames. |
| 195 // This test uses a FakeVideoCaptureDeviceFactory and succeeds all right. |
| 196 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCapture) { |
| 197 InSequence s; |
| 198 UseFakeFactory(); |
| 199 InitializeVideoCaptureHandlerImpl(); |
| 200 EnumerateDevices(); |
| 201 RequestStream(); |
| 202 |
| 203 StartAndExpectFrames(); |
| 204 stream_->Stop(); |
| 205 } |
| 206 |
| 207 // Tests a full cycle RequestMediaDevices() -> RequestMediaDevicesReply -> |
| 208 // RequestStream()-> RequestStreamReply -> Start() -> Error !!!. |
| 209 TEST_F(VideoCaptureTest, RequestStreamAndStartAndError) { |
| 210 InSequence s; |
| 211 UseFakeFactory(); |
| 212 InitializeVideoCaptureHandlerImpl(); |
| 213 EnumerateDevices(); |
| 214 RequestStream(); |
| 215 |
| 216 StartAndExpectFramesAndError(); |
| 217 stream_->Stop(); |
| 218 } |
| 219 |
| 220 // Same as RequestStreamAndStartAndCapture, with a real capture device. |
| 221 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCaptureWithRealDevice) { |
| 222 InSequence s; |
| 223 |
| 224 UseRealFactory(); |
| 225 InitializeVideoCaptureHandlerImpl(); |
| 226 EnumerateDevices(); |
| 227 if (media_devices_info_->sources.size() == 0u) // No real devices. |
| 228 return; |
| 229 RequestStream(); |
| 230 |
| 231 StartAndExpectFrames(); |
| 232 stream_->Stop(); |
| 233 } |
| 234 |
| 235 } // namespace media |
OLD | NEW |