Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: media/capture/service/video_capture_unittest.cc

Issue 1699553002: Mojo Video Capture service in media/capture (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: perkj@ comments - mostly simplifications of the mojom. and renaming Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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::StreamOptionsPtr options = mojom::StreamOptions::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(
115 std::move(options), "https://localhost", 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_, DoFrameAvailable(_, _)).Times(3);
126 EXPECT_CALL(stream_client_impl_, DoFrameAvailable(_, _))
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_, DoFrameAvailable(_, _)).Times(AnyNumber());
139 stream_->Start(stream_client_impl_.CreateProxy());
140
141 EXPECT_CALL(stream_client_impl_, Error(_))
142 .Times(1)
143 .WillOnce(RunClosure(quit_closure));
144
145 // Access the first and only StreamImpl and ping its OnError() callback.
146 ASSERT_EQ(1u, video_capture_handler_impl_->device_and_streams_.size());
147 video_capture_handler_impl_->device_and_streams_.begin()
148 ->second.second->OnError("Terrible error");
149 run_loop.Run();
150 }
151
152 void FakeOnErrorFromDevice() {}
153
154 protected:
155 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl.
156 const base::MessageLoop loop_;
157
158 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to.
159 base::SystemMonitor system_monitor_;
160
161 // The component's entry point. Needs to be a scoped_ptr to be initialized
162 // after adding command line flag for the appropriate Device Factory.
163 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_;
164
165 // The mock of the (remote) video capture client.
166 MockStreamClient stream_client_impl_;
167
168 // Local pointer to the generated Stream spitting VideoFrames.
169 mojom::VideoCaptureStreamPtr stream_;
170
171 // Variable to hold on to the returned list of enumerated devices.
172 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> video_devices_info_;
173
174 private:
175 DISALLOW_COPY_AND_ASSIGN(VideoCaptureTest);
176 };
177
178 TEST_F(VideoCaptureTest, CreateAndDestroy) {}
179
180 // Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply ->
181 // RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() ->
182 // get frames. This test uses a FakeVideoCaptureDeviceFactory and succeeds all
183 // right.
184 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCapture) {
185 InSequence s;
186 UseFakeFactory();
187 InitializeVideoCaptureHandlerImpl();
188 EnumerateDevices();
189 RequestVideoCaptureStream();
190
191 StartAndExpectFrames();
192 stream_->Stop();
193 }
194
195 // Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply ->
196 // RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() ->
197 // Error !!!.
198 TEST_F(VideoCaptureTest, RequestStreamAndStartAndError) {
199 InSequence s;
200 UseFakeFactory();
201 InitializeVideoCaptureHandlerImpl();
202 EnumerateDevices();
203 RequestVideoCaptureStream();
204
205 StartAndExpectFramesAndError();
206 stream_->Stop();
207 }
208
209 // Same as RequestStreamAndStartAndCapture, with a real capture device.
210 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCaptureWithRealDevice) {
211 InSequence s;
212
213 UseRealFactory();
214 InitializeVideoCaptureHandlerImpl();
215 EnumerateDevices();
216 if (video_devices_info_.size() == 0u) // No real devices.
217 return;
218 RequestVideoCaptureStream();
219
220 StartAndExpectFrames();
221 stream_->Stop();
222 }
223
224 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698