| 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 "components/video_capture/mock_stream_client.h" |
| 8 #include "components/video_capture/public/interfaces/video_capture.mojom.h" |
| 9 #include "components/video_capture/stream_impl.h" |
| 10 #include "components/video_capture/video_capture_handler_impl.h" |
| 11 #include "media/base/media_switches.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 video_capture { |
| 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 mojom::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 mojom::MediaDevicesInfoReply&)); |
| 71 void OnRequestMediaDevicesReply(mojom::MediaDevicesInfoReplyPtr reply) { |
| 72 DVLOG(1) << __FUNCTION__; |
| 73 DoOnRequestMediaDevicesReply(*reply); |
| 74 media_devices_info_ = std::move(reply); |
| 75 } |
| 76 |
| 77 MOCK_METHOD1(DoOnRequestStreamReply, void(const mojom::Stream&)); |
| 78 void OnRequestStreamReply(mojom::StreamPtr stream) { |
| 79 EXPECT_TRUE(stream.is_bound()); |
| 80 DoOnRequestStreamReply(*stream); |
| 81 stream_ = std::move(stream); |
| 82 } |
| 83 |
| 84 // Enumerate devices to get a valid |id| for the first one. |
| 85 void EnumerateDevices() { |
| 86 const int kRequestId = 123; |
| 87 mojom::MediaDevicesInfoRequestPtr request = |
| 88 mojom::MediaDevicesInfoRequest::New(); |
| 89 request->request_id = kRequestId; |
| 90 |
| 91 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback = base::Bind( |
| 92 &VideoCaptureTest::OnRequestMediaDevicesReply, base::Unretained(this)); |
| 93 |
| 94 base::RunLoop run_loop; |
| 95 base::Closure quit_closure = run_loop.QuitClosure(); |
| 96 EXPECT_CALL(*this, |
| 97 DoOnRequestMediaDevicesReply(Field( |
| 98 &mojom::MediaDevicesInfoReply::request_id, kRequestId))) |
| 99 .Times(1) |
| 100 .WillOnce(RunClosure(quit_closure)); |
| 101 RequestMediaDevices(std::move(request), std::move(callback)); |
| 102 |
| 103 run_loop.Run(); |
| 104 } |
| 105 |
| 106 // This block runs a RequestStream() --> get a Stream(). |
| 107 void RequestStream() { |
| 108 const int kRequestId = 456; |
| 109 |
| 110 mojom::StreamOptionsPtr options = mojom::StreamOptions::New(); |
| 111 options->request_id = kRequestId; |
| 112 options->device_id = media_devices_info_->sources[0]->device_id; |
| 113 options->capture_size = mojo::Size::From(gfx::Size(320, 240)); |
| 114 options->frame_rate = 30.0; |
| 115 |
| 116 VideoCaptureHandlerImpl::RequestStreamCallback callback = base::Bind( |
| 117 &VideoCaptureTest::OnRequestStreamReply, base::Unretained(this)); |
| 118 base::RunLoop run_loop; |
| 119 base::Closure quit_closure = run_loop.QuitClosure(); |
| 120 |
| 121 EXPECT_CALL(*this, DoOnRequestStreamReply(_)) |
| 122 .Times(1) |
| 123 .WillOnce(RunClosure(quit_closure)); |
| 124 |
| 125 video_capture_handler_impl_->RequestStream( |
| 126 std::move(options), "https://localhost", std::move(callback)); |
| 127 run_loop.Run(); |
| 128 ASSERT_TRUE(!!stream_.get()); |
| 129 } |
| 130 |
| 131 // Call Stream::Start() and expect some frames. |
| 132 void StartAndExpectFrames() { |
| 133 base::RunLoop run_loop; |
| 134 base::Closure quit_closure = run_loop.QuitClosure(); |
| 135 |
| 136 EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _)).Times(3); |
| 137 EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _)) |
| 138 .Times(1) |
| 139 .WillOnce(RunClosure(quit_closure)); |
| 140 stream_->Start(stream_client_impl_.CreateProxy()); |
| 141 |
| 142 run_loop.Run(); |
| 143 } |
| 144 |
| 145 void StartAndExpectFramesAndError() { |
| 146 base::RunLoop run_loop; |
| 147 base::Closure quit_closure = run_loop.QuitClosure(); |
| 148 |
| 149 EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _)).Times(AnyNumber()); |
| 150 stream_->Start(stream_client_impl_.CreateProxy()); |
| 151 |
| 152 EXPECT_CALL(stream_client_impl_, Error(_)) |
| 153 .Times(1) |
| 154 .WillOnce(RunClosure(quit_closure)); |
| 155 |
| 156 // Access the first and only StreamImpl and ping its OnError() callback. |
| 157 ASSERT_EQ(1u, video_capture_handler_impl_->device_and_streams_.size()); |
| 158 video_capture_handler_impl_->device_and_streams_.begin() |
| 159 ->second.second->OnError("Terrible error"); |
| 160 run_loop.Run(); |
| 161 } |
| 162 |
| 163 void FakeOnErrorFromDevice() { |
| 164 } |
| 165 |
| 166 protected: |
| 167 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl. |
| 168 const base::MessageLoop loop_; |
| 169 |
| 170 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to. |
| 171 base::SystemMonitor system_monitor_; |
| 172 |
| 173 // The component's entry point. Needs to be a scoped_ptr to be initialized |
| 174 // after adding command line flag for the appropriate Device Factory. |
| 175 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_; |
| 176 |
| 177 // The mock of the (remote) video capture client. |
| 178 MockStreamClient stream_client_impl_; |
| 179 |
| 180 // Local pointer to the generated Stream spitting VideoFrames. |
| 181 mojom::StreamPtr stream_; |
| 182 |
| 183 // Variable to hold on to the returned list of enumerated devices. |
| 184 mojom::MediaDevicesInfoReplyPtr media_devices_info_; |
| 185 |
| 186 private: |
| 187 DISALLOW_COPY_AND_ASSIGN(VideoCaptureTest); |
| 188 }; |
| 189 |
| 190 TEST_F(VideoCaptureTest, CreateAndDestroy) { |
| 191 } |
| 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 RequestStream(); |
| 228 |
| 229 StartAndExpectFrames(); |
| 230 stream_->Stop(); |
| 231 } |
| 232 |
| 233 } // namespace video_capture |
| OLD | NEW |