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

Side by Side Diff: components/video_capture/video_capture_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698