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 EnumerateDevices( |
| 63 const VideoCaptureHandlerImpl::EnumerateDevicesCallback& callback) { |
| 64 video_capture_handler_impl_->EnumerateDevices(callback); |
| 65 } |
| 66 |
| 67 MOCK_METHOD0(DoOnEnumerateDevicesReply, void(void)); |
| 68 void OnEnumerateDevicesReply( |
| 69 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> reply) { |
| 70 DoOnEnumerateDevicesReply(); |
| 71 video_devices_info_ = std::move(reply); |
| 72 } |
| 73 |
| 74 MOCK_METHOD1(DoOnRequestVideoCaptureStreamReply, |
| 75 void(const mojom::VideoCaptureStream&)); |
| 76 void OnRequestVideoCaptureStreamReply(mojom::VideoCaptureStreamPtr stream) { |
| 77 EXPECT_TRUE(stream.is_bound()); |
| 78 DoOnRequestVideoCaptureStreamReply(*stream); |
| 79 stream_ = std::move(stream); |
| 80 } |
| 81 |
| 82 // Enumerate devices to get a valid |id| for the first one. |
| 83 void EnumerateDevices() { |
| 84 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback = base::Bind( |
| 85 &VideoCaptureTest::OnEnumerateDevicesReply, base::Unretained(this)); |
| 86 |
| 87 base::RunLoop run_loop; |
| 88 base::Closure quit_closure = run_loop.QuitClosure(); |
| 89 EXPECT_CALL(*this, DoOnEnumerateDevicesReply()) |
| 90 .Times(1) |
| 91 .WillOnce(RunClosure(quit_closure)); |
| 92 EnumerateDevices(std::move(callback)); |
| 93 |
| 94 run_loop.Run(); |
| 95 } |
| 96 |
| 97 // This block runs a RequestVideoCaptureStream() --> get a Stream(). |
| 98 void RequestVideoCaptureStream() { |
| 99 mojom::VideoCaptureOptionsPtr options = mojom::VideoCaptureOptions::New(); |
| 100 options->device_id = video_devices_info_[0]->device_id; |
| 101 options->capture_size = mojo::Size::From(gfx::Size(320, 240)); |
| 102 options->frame_rate = 30.0; |
| 103 |
| 104 VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback callback = |
| 105 base::Bind(&VideoCaptureTest::OnRequestVideoCaptureStreamReply, |
| 106 base::Unretained(this)); |
| 107 base::RunLoop run_loop; |
| 108 base::Closure quit_closure = run_loop.QuitClosure(); |
| 109 |
| 110 EXPECT_CALL(*this, DoOnRequestVideoCaptureStreamReply(_)) |
| 111 .Times(1) |
| 112 .WillOnce(RunClosure(quit_closure)); |
| 113 |
| 114 video_capture_handler_impl_->RequestVideoCaptureStream(std::move(options), |
| 115 std::move(callback)); |
| 116 run_loop.Run(); |
| 117 ASSERT_TRUE(!!stream_.get()); |
| 118 } |
| 119 |
| 120 // Call Stream::Start() and expect some frames. |
| 121 void StartAndExpectFrames() { |
| 122 base::RunLoop run_loop; |
| 123 base::Closure quit_closure = run_loop.QuitClosure(); |
| 124 |
| 125 EXPECT_CALL(stream_client_impl_, DoOnFrameAvailable(_, _)).Times(3); |
| 126 EXPECT_CALL(stream_client_impl_, DoOnFrameAvailable(_, _)) |
| 127 .Times(1) |
| 128 .WillOnce(RunClosure(quit_closure)); |
| 129 stream_->Start(stream_client_impl_.CreateProxy()); |
| 130 |
| 131 run_loop.Run(); |
| 132 } |
| 133 |
| 134 void StartAndExpectFramesAndError() { |
| 135 base::RunLoop run_loop; |
| 136 base::Closure quit_closure = run_loop.QuitClosure(); |
| 137 |
| 138 EXPECT_CALL(stream_client_impl_, DoOnFrameAvailable(_, _)) |
| 139 .Times(AnyNumber()); |
| 140 stream_->Start(stream_client_impl_.CreateProxy()); |
| 141 |
| 142 EXPECT_CALL(stream_client_impl_, OnError(_)) |
| 143 .Times(1) |
| 144 .WillOnce(RunClosure(quit_closure)); |
| 145 |
| 146 // Access the first and only StreamImpl and ping its OnError() callback. |
| 147 ASSERT_EQ(1u, video_capture_handler_impl_->device_and_streams_.size()); |
| 148 video_capture_handler_impl_->device_and_streams_.begin() |
| 149 ->second.second->OnError("Terrible error"); |
| 150 run_loop.Run(); |
| 151 } |
| 152 |
| 153 void FakeOnErrorFromDevice() {} |
| 154 |
| 155 protected: |
| 156 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl. |
| 157 const base::MessageLoop loop_; |
| 158 |
| 159 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to. |
| 160 base::SystemMonitor system_monitor_; |
| 161 |
| 162 // The component's entry point. Needs to be a scoped_ptr to be initialized |
| 163 // after adding command line flag for the appropriate Device Factory. |
| 164 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_; |
| 165 |
| 166 // The mock of the (remote) video capture client. |
| 167 MockStreamClient stream_client_impl_; |
| 168 |
| 169 // Local pointer to the generated Stream spitting VideoFrames. |
| 170 mojom::VideoCaptureStreamPtr stream_; |
| 171 |
| 172 // Variable to hold on to the returned list of enumerated devices. |
| 173 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> video_devices_info_; |
| 174 |
| 175 private: |
| 176 DISALLOW_COPY_AND_ASSIGN(VideoCaptureTest); |
| 177 }; |
| 178 |
| 179 TEST_F(VideoCaptureTest, CreateAndDestroy) {} |
| 180 |
| 181 // Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply -> |
| 182 // RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() -> |
| 183 // get frames. This test uses a FakeVideoCaptureDeviceFactory and succeeds all |
| 184 // right. |
| 185 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCapture) { |
| 186 InSequence s; |
| 187 UseFakeFactory(); |
| 188 InitializeVideoCaptureHandlerImpl(); |
| 189 EnumerateDevices(); |
| 190 RequestVideoCaptureStream(); |
| 191 |
| 192 StartAndExpectFrames(); |
| 193 stream_->Stop(); |
| 194 } |
| 195 |
| 196 // Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply -> |
| 197 // RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() -> |
| 198 // Error !!!. |
| 199 TEST_F(VideoCaptureTest, RequestStreamAndStartAndError) { |
| 200 InSequence s; |
| 201 UseFakeFactory(); |
| 202 InitializeVideoCaptureHandlerImpl(); |
| 203 EnumerateDevices(); |
| 204 RequestVideoCaptureStream(); |
| 205 |
| 206 StartAndExpectFramesAndError(); |
| 207 stream_->Stop(); |
| 208 } |
| 209 |
| 210 // Same as RequestStreamAndStartAndCapture, with a real capture device. |
| 211 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCaptureWithRealDevice) { |
| 212 InSequence s; |
| 213 |
| 214 UseRealFactory(); |
| 215 InitializeVideoCaptureHandlerImpl(); |
| 216 EnumerateDevices(); |
| 217 if (video_devices_info_.size() == 0u) // No real devices. |
| 218 return; |
| 219 RequestVideoCaptureStream(); |
| 220 |
| 221 StartAndExpectFrames(); |
| 222 stream_->Stop(); |
| 223 } |
| 224 |
| 225 } // namespace media |
OLD | NEW |