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

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

Issue 1637793004: [WIP] Mojo Video Capture thingy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rockot@ comments 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/video_capture_handler_impl.h"
8 #include "media/base/media_switches.h"
9 #include "media/capture/video/fake_video_capture_device_factory.h"
10 #include "mojo/converters/geometry/geometry_type_converters.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using ::testing::_;
15 using ::testing::InSequence;
16 using ::testing::Field;
17
18 namespace video_capture {
19
20 namespace {
21
22 ACTION_P(RunClosure, closure) {
23 closure.Run();
24 }
25
26 } // anonymous namespace
27
28 class VideoCaptureHandlerImplTest : public testing::Test {
29 public:
30 VideoCaptureHandlerImplTest() {}
31
32 void SetUp() override {
33 base::CommandLine::ForCurrentProcess()->AppendSwitch(
34 switches::kUseFakeDeviceForMediaStream);
35 video_capture_handler_impl_.reset(
36 new VideoCaptureHandlerImpl(nullptr /* app */));
37 }
38
39 MOCK_METHOD1(DoOnRequestMediaDevicesReply,
40 void(const mojom::MediaDevicesInfoReply&));
41 void OnRequestMediaDevicesReply(mojom::MediaDevicesInfoReplyPtr reply) {
42 DVLOG(1) << __FUNCTION__;
43 DoOnRequestMediaDevicesReply(*reply);
44 media_devices_info_ = std::move(reply);
45 }
46 MOCK_METHOD1(DoOnRequestStreamReply,
47 void(const mojom::Stream&));
48 void OnRequestStreamReply(mojom::StreamPtr stream) {
49 DVLOG(1) << __FUNCTION__;
50 DoOnRequestStreamReply(*stream);
51 }
52
53 void RequestMediaDevices(
54 mojom::MediaDevicesInfoRequestPtr request,
55 const VideoCaptureHandlerImpl::RequestMediaDevicesCallback& callback) {
56 video_capture_handler_impl_->RequestMediaDevices(std::move(request),
57 callback);
58 }
59
60 void SetNumFakeDevices(int num) {
61 static_cast<media::FakeVideoCaptureDeviceFactory*>(
62 video_capture_handler_impl_->video_capture_device_factory_.get())
63 ->set_number_of_devices(num);
64 }
65
66 void RequestStream(
67 mojom::StreamOptionsPtr options,
68 const mojo::String& security_origin,
69 const VideoCaptureHandlerImpl::RequestStreamCallback& callback) {
70 video_capture_handler_impl_->RequestStream(std::move(options),
71 security_origin, callback);
72 }
73
74 protected:
75 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl.
76 const base::MessageLoop loop_;
77
78 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to.
79 base::SystemMonitor system_monitor_;
80
81 // The class under test. Needs to be a scoped_ptr to be initialized after
82 // adding |kUseFakeDeviceForMediaStream| command line flag.
83 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_;
84
85 // Variable to hold on to the returned list of enumerated devices.
86 mojom::MediaDevicesInfoReplyPtr media_devices_info_;
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandlerImplTest);
90 };
91
92 TEST_F(VideoCaptureHandlerImplTest, CreateAndDestroy) {
93 }
94
95 // Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback().
96 TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevices) {
97 InSequence s;
98 const int kRequestId = 123;
99
100 mojom::MediaDevicesInfoRequestPtr request =
101 mojom::MediaDevicesInfoRequest::New();
102 request->request_id = kRequestId;
103
104 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
105 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
106 base::Unretained(this));
107
108 base::RunLoop run_loop;
109 base::Closure quit_closure = run_loop.QuitClosure();
110 EXPECT_CALL(*this,
111 DoOnRequestMediaDevicesReply(
112 Field(&mojom::MediaDevicesInfoReply::request_id, kRequestId)))
113 .Times(1)
114 .WillOnce(RunClosure(quit_closure));
115 RequestMediaDevices(std::move(request), std::move(callback));
116
117 run_loop.Run();
118 ASSERT_EQ(1u, media_devices_info_->sources.size());
119 EXPECT_EQ(mojom::SourceKind::Video, media_devices_info_->sources[0]->kind);
120 }
121
122 // Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback() before
123 // and after a change in system devices has happened.
124 TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevicesAndReenumerate) {
125 InSequence s;
126 const int kRequestId = 123;
127
128 { // 1 Enumeration RequestMediaDevices()-> RequestMediaDevicesCallback().
129 mojom::MediaDevicesInfoRequestPtr request =
130 mojom::MediaDevicesInfoRequest::New();
131 request->request_id = kRequestId;
132
133 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
134 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
135 base::Unretained(this));
136
137 base::RunLoop run_loop;
138 base::Closure quit_closure = run_loop.QuitClosure();
139 EXPECT_CALL(*this,
140 DoOnRequestMediaDevicesReply(Field(
141 &mojom::MediaDevicesInfoReply::request_id, kRequestId)))
142 .Times(1)
143 .WillOnce(RunClosure(quit_closure));
144 RequestMediaDevices(std::move(request), std::move(callback));
145 run_loop.Run();
146
147 ASSERT_GE(media_devices_info_->sources.size(), 1u);
148 }
149
150 SetNumFakeDevices(2);
151 system_monitor_.ProcessDevicesChanged(
152 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
153 base::RunLoop().RunUntilIdle();
154
155 {
156 mojom::MediaDevicesInfoRequestPtr request =
157 mojom::MediaDevicesInfoRequest::New();
158 request->request_id = kRequestId;
159
160 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
161 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
162 base::Unretained(this));
163
164 base::RunLoop run_loop;
165 base::Closure quit_closure = run_loop.QuitClosure();
166 EXPECT_CALL(*this,
167 DoOnRequestMediaDevicesReply(Field(
168 &mojom::MediaDevicesInfoReply::request_id, kRequestId)))
169 .Times(1)
170 .WillOnce(RunClosure(quit_closure));
171 RequestMediaDevices(std::move(request), std::move(callback));
172 run_loop.Run();
173
174 ASSERT_GE(media_devices_info_->sources.size(), 2u);
175 }
176 }
177
178 // Tests a cycle RequestStream() -> RequestStreamCallback().
179 TEST_F(VideoCaptureHandlerImplTest, RequestStream) {
180 InSequence s;
181
182 { // Enumerate devices to get a valid |id| for the first one.
183 const int kRequestId = 123;
184 mojom::MediaDevicesInfoRequestPtr request =
185 mojom::MediaDevicesInfoRequest::New();
186 request->request_id = kRequestId;
187
188 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
189 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
190 base::Unretained(this));
191
192 base::RunLoop run_loop;
193 base::Closure quit_closure = run_loop.QuitClosure();
194 EXPECT_CALL(*this,
195 DoOnRequestMediaDevicesReply(Field(
196 &mojom::MediaDevicesInfoReply::request_id, kRequestId)))
197 .Times(1)
198 .WillOnce(RunClosure(quit_closure));
199 RequestMediaDevices(std::move(request), std::move(callback));
200
201 run_loop.Run();
202 }
203 {
204 const int kRequestId = 456;
205 mojom::StreamOptionsPtr options = mojom::StreamOptions::New();
206 options->request_id = kRequestId;
207 options->device_id = media_devices_info_->sources[0]->device_id;
208 options->capture_size = mojo::Size::From(gfx::Size(640, 480));
209 options->frame_rate = 30.0;
210
211 VideoCaptureHandlerImpl::RequestStreamCallback callback =
212 base::Bind(&VideoCaptureHandlerImplTest::OnRequestStreamReply,
213 base::Unretained(this));
214
215 base::RunLoop run_loop;
216 base::Closure quit_closure = run_loop.QuitClosure();
217
218 // TODO(mcasas): Inspect the replied sources[0] contents.
219 EXPECT_CALL(*this, DoOnRequestStreamReply(_))
220 .Times(1)
221 .WillOnce(RunClosure(quit_closure));
222 RequestStream(std::move(options), "https://localhost", std::move(callback));
223
224 run_loop.Run();
225 }
226 }
227
228 } // namespace video_capture
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698