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

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@s comments 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_video_capture_stream_client.h"
10 #include "media/capture/service/video_capture_handler_impl.h"
11 #include "media/capture/service/video_capture_stream_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 VideoCaptureStreamImpl and ping its OnError()
147 // callback.
148 ASSERT_EQ(1u, video_capture_handler_impl_->device_and_streams_.size());
149 video_capture_handler_impl_->device_and_streams_.begin()
150 ->second.second->OnError("Terrible error");
151 run_loop.Run();
152 }
153
154 void FakeOnErrorFromDevice() {}
155
156 protected:
157 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl.
158 const base::MessageLoop loop_;
159
160 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to.
161 base::SystemMonitor system_monitor_;
162
163 // The component's entry point. Needs to be a scoped_ptr to be initialized
164 // after adding command line flag for the appropriate Device Factory.
165 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_;
166
167 // The mock of the (remote) video capture client.
168 MockVideoCaptureStreamClient stream_client_impl_;
169
170 // Local pointer to the generated Stream spitting VideoFrames.
171 mojom::VideoCaptureStreamPtr stream_;
172
173 // Variable to hold on to the returned list of enumerated devices.
174 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> video_devices_info_;
175
176 private:
177 DISALLOW_COPY_AND_ASSIGN(VideoCaptureTest);
178 };
179
180 TEST_F(VideoCaptureTest, CreateAndDestroy) {}
181
182 // Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply ->
183 // RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() ->
184 // get frames. This test uses a FakeVideoCaptureDeviceFactory and succeeds all
185 // right.
186 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCapture) {
187 InSequence s;
188 UseFakeFactory();
189 InitializeVideoCaptureHandlerImpl();
190 EnumerateDevices();
191 RequestVideoCaptureStream();
192
193 StartAndExpectFrames();
194 stream_->Stop();
195 }
196
197 // Tests a full cycle EnumerateDevices() -> EnumerateDevicesReply ->
198 // RequestVideoCaptureStream()-> RequestVideoCaptureStreamReply -> Start() ->
199 // Error !!!.
200 TEST_F(VideoCaptureTest, RequestStreamAndStartAndError) {
201 InSequence s;
202 UseFakeFactory();
203 InitializeVideoCaptureHandlerImpl();
204 EnumerateDevices();
205 RequestVideoCaptureStream();
206
207 StartAndExpectFramesAndError();
208 stream_->Stop();
209 }
210
211 // Same as RequestStreamAndStartAndCapture, with a real capture device.
212 TEST_F(VideoCaptureTest, RequestStreamAndStartAndCaptureWithRealDevice) {
213 InSequence s;
214
215 UseRealFactory();
216 InitializeVideoCaptureHandlerImpl();
217 EnumerateDevices();
218 if (video_devices_info_.size() == 0u) // No real devices.
219 return;
220 RequestVideoCaptureStream();
221
222 StartAndExpectFrames();
223 stream_->Stop();
224 }
225
226 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/service/video_capture_stream_impl_unittest.cc ('k') | media/mojo/common/mojo_shared_buffer_video_frame.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698