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

Side by Side Diff: media/capture/service/video_capture_handler_impl_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 and magjed@s comments 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/service/video_capture_handler_impl.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
17 namespace media {
18
19 namespace {
20
21 ACTION_P(RunClosure, closure) {
22 closure.Run();
23 }
24
25 } // anonymous namespace
26
27 class VideoCaptureHandlerImplTest : public testing::Test {
28 public:
29 VideoCaptureHandlerImplTest() {}
30
31 void SetUp() override {
32 base::CommandLine::ForCurrentProcess()->AppendSwitch(
33 switches::kUseFakeDeviceForMediaStream);
34 video_capture_handler_impl_.reset(
35 new VideoCaptureHandlerImpl(nullptr /* app */));
36 }
37
38 MOCK_METHOD0(DoOnEnumerateDevicesReply, void(void));
39 void OnEnumerateDevicesReply(
40 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> reply) {
41 DoOnEnumerateDevicesReply();
42 media_devices_info_ = std::move(reply);
43 }
44 MOCK_METHOD1(DoOnRequestVideoCaptureStreamReply,
45 void(const mojom::VideoCaptureStream&));
46 void OnRequestVideoCaptureStreamReply(mojom::VideoCaptureStreamPtr stream) {
47 DVLOG(1) << __FUNCTION__;
48 DoOnRequestVideoCaptureStreamReply(*stream);
49 }
50
51 void EnumerateDevices(
52 const VideoCaptureHandlerImpl::EnumerateDevicesCallback& callback) {
53 video_capture_handler_impl_->EnumerateDevices(callback);
54 }
55
56 void SetNumFakeDevices(int num) {
57 static_cast<media::FakeVideoCaptureDeviceFactory*>(
58 video_capture_handler_impl_->video_capture_device_factory_.get())
59 ->set_number_of_devices(num);
60 }
61
62 void RequestVideoCaptureStream(
63 mojom::VideoCaptureOptionsPtr options,
64 const VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback&
65 callback) {
66 video_capture_handler_impl_->RequestVideoCaptureStream(std::move(options),
67 callback);
68 }
69
70 protected:
71 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl.
72 const base::MessageLoop loop_;
73
74 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to.
75 base::SystemMonitor system_monitor_;
76
77 // The class under test. Needs to be a scoped_ptr to be initialized after
78 // adding |kUseFakeDeviceForMediaStream| command line flag.
79 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_;
80
81 // Variable to hold on to the returned list of enumerated devices.
82 mojo::Array<mojom::VideoCaptureDeviceInfoPtr> media_devices_info_;
83
84 private:
85 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandlerImplTest);
86 };
87
88 TEST_F(VideoCaptureHandlerImplTest, CreateAndDestroy) {}
89
90 // Tests a cycle EnumerateDevices()-> EnumerateDevicesCallback().
91 TEST_F(VideoCaptureHandlerImplTest, EnumerateDevices) {
92 InSequence s;
93
94 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback =
95 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply,
96 base::Unretained(this));
97
98 base::RunLoop run_loop;
99 base::Closure quit_closure = run_loop.QuitClosure();
100 EXPECT_CALL(*this, DoOnEnumerateDevicesReply())
101 .Times(1)
102 .WillOnce(RunClosure(quit_closure));
103 EnumerateDevices(std::move(callback));
104
105 run_loop.Run();
106 ASSERT_EQ(1u, media_devices_info_.size());
107 }
108
109 // Tests a cycle EnumerateDevices()-> EnumerateDevicesCallback() before
110 // and after a change in system devices has happened.
111 TEST_F(VideoCaptureHandlerImplTest, EnumerateDevicesAndReenumerate) {
112 InSequence s;
113 { // 1 Enumeration EnumerateDevices()-> EnumerateDevicesCallback().
114 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback =
115 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply,
116 base::Unretained(this));
117
118 base::RunLoop run_loop;
119 base::Closure quit_closure = run_loop.QuitClosure();
120 EXPECT_CALL(*this, DoOnEnumerateDevicesReply())
121 .Times(1)
122 .WillOnce(RunClosure(quit_closure));
123 EnumerateDevices(std::move(callback));
124 run_loop.Run();
125
126 ASSERT_GE(media_devices_info_.size(), 1u);
127 }
128
129 SetNumFakeDevices(2);
130 system_monitor_.ProcessDevicesChanged(
131 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
132 base::RunLoop().RunUntilIdle();
133
134 {
135 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback =
136 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply,
137 base::Unretained(this));
138
139 base::RunLoop run_loop;
140 base::Closure quit_closure = run_loop.QuitClosure();
141 EXPECT_CALL(*this, DoOnEnumerateDevicesReply())
142 .Times(1)
143 .WillOnce(RunClosure(quit_closure));
144 EnumerateDevices(std::move(callback));
145 run_loop.Run();
146
147 ASSERT_GE(media_devices_info_.size(), 2u);
148 }
149 }
150
151 // Tests a cycle RequestVideoCaptureStream() ->
152 // RequestVideoCaptureStreamCallback().
153 TEST_F(VideoCaptureHandlerImplTest, RequestVideoCaptureStream) {
154 InSequence s;
155
156 { // Enumerate devices to get a valid |id| for the first one.
157 VideoCaptureHandlerImpl::EnumerateDevicesCallback callback =
158 base::Bind(&VideoCaptureHandlerImplTest::OnEnumerateDevicesReply,
159 base::Unretained(this));
160
161 base::RunLoop run_loop;
162 base::Closure quit_closure = run_loop.QuitClosure();
163 EXPECT_CALL(*this, DoOnEnumerateDevicesReply())
164 .Times(1)
165 .WillOnce(RunClosure(quit_closure));
166 EnumerateDevices(std::move(callback));
167
168 run_loop.Run();
169 }
170 {
171 mojom::VideoCaptureOptionsPtr options = mojom::VideoCaptureOptions::New();
172 options->device_id = media_devices_info_[0]->device_id;
173 options->capture_size = mojo::Size::From(gfx::Size(640, 480));
174 options->frame_rate = 30.0;
175
176 VideoCaptureHandlerImpl::RequestVideoCaptureStreamCallback callback =
177 base::Bind(
178 &VideoCaptureHandlerImplTest::OnRequestVideoCaptureStreamReply,
179 base::Unretained(this));
180
181 base::RunLoop run_loop;
182 base::Closure quit_closure = run_loop.QuitClosure();
183
184 // TODO(mcasas): Inspect the replied sources[0] contents.
185 EXPECT_CALL(*this, DoOnRequestVideoCaptureStreamReply(_))
186 .Times(1)
187 .WillOnce(RunClosure(quit_closure));
188 RequestVideoCaptureStream(std::move(options), std::move(callback));
189
190 run_loop.Run();
191 }
192 }
193
194 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698