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

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: emircan@ 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(DoOnRequestMediaDevicesReply, void(void));
39 void OnRequestMediaDevicesReply(mojo::Array<mojom::WebSourceInfoPtr> reply) {
40 DoOnRequestMediaDevicesReply();
41 media_devices_info_ = std::move(reply);
42 }
43 MOCK_METHOD1(DoOnRequestStreamReply, void(const mojom::Stream&));
44 void OnRequestStreamReply(mojom::StreamPtr stream) {
45 DVLOG(1) << __FUNCTION__;
46 DoOnRequestStreamReply(*stream);
47 }
48
49 void RequestMediaDevices(
50 const VideoCaptureHandlerImpl::RequestMediaDevicesCallback& callback) {
51 video_capture_handler_impl_->RequestMediaDevices(callback);
52 }
53
54 void SetNumFakeDevices(int num) {
55 static_cast<media::FakeVideoCaptureDeviceFactory*>(
56 video_capture_handler_impl_->video_capture_device_factory_.get())
57 ->set_number_of_devices(num);
58 }
59
60 void RequestStream(
61 mojom::StreamOptionsPtr options,
62 const mojo::String& security_origin,
63 const VideoCaptureHandlerImpl::RequestStreamCallback& callback) {
64 video_capture_handler_impl_->RequestStream(std::move(options),
65 security_origin, callback);
66 }
67
68 protected:
69 // Needed for the mojo::ApplicationImpl inside VideoCaptureHandlerImpl.
70 const base::MessageLoop loop_;
71
72 // Need a SystemMonitor for VideoCaptureHandlerImpl to register to.
73 base::SystemMonitor system_monitor_;
74
75 // The class under test. Needs to be a scoped_ptr to be initialized after
76 // adding |kUseFakeDeviceForMediaStream| command line flag.
77 scoped_ptr<VideoCaptureHandlerImpl> video_capture_handler_impl_;
78
79 // Variable to hold on to the returned list of enumerated devices.
80 mojo::Array<mojom::WebSourceInfoPtr> media_devices_info_;
81
82 private:
83 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandlerImplTest);
84 };
85
86 TEST_F(VideoCaptureHandlerImplTest, CreateAndDestroy) {}
87
88 // Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback().
89 TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevices) {
90 InSequence s;
91
92 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
93 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
94 base::Unretained(this));
95
96 base::RunLoop run_loop;
97 base::Closure quit_closure = run_loop.QuitClosure();
98 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply())
99 .Times(1)
100 .WillOnce(RunClosure(quit_closure));
101 RequestMediaDevices(std::move(callback));
102
103 run_loop.Run();
104 ASSERT_EQ(1u, media_devices_info_.size());
105 EXPECT_EQ(mojom::SourceKind::Video, media_devices_info_[0]->kind);
106 }
107
108 // Tests a cycle RequestMediaDevices()-> RequestMediaDevicesCallback() before
109 // and after a change in system devices has happened.
110 TEST_F(VideoCaptureHandlerImplTest, RequestMediaDevicesAndReenumerate) {
111 InSequence s;
112 { // 1 Enumeration RequestMediaDevices()-> RequestMediaDevicesCallback().
113 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
114 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
115 base::Unretained(this));
116
117 base::RunLoop run_loop;
118 base::Closure quit_closure = run_loop.QuitClosure();
119 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply())
120 .Times(1)
121 .WillOnce(RunClosure(quit_closure));
122 RequestMediaDevices(std::move(callback));
123 run_loop.Run();
124
125 ASSERT_GE(media_devices_info_.size(), 1u);
126 }
127
128 SetNumFakeDevices(2);
129 system_monitor_.ProcessDevicesChanged(
130 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
131 base::RunLoop().RunUntilIdle();
132
133 {
134 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
135 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
136 base::Unretained(this));
137
138 base::RunLoop run_loop;
139 base::Closure quit_closure = run_loop.QuitClosure();
140 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply())
141 .Times(1)
142 .WillOnce(RunClosure(quit_closure));
143 RequestMediaDevices(std::move(callback));
144 run_loop.Run();
145
146 ASSERT_GE(media_devices_info_.size(), 2u);
147 }
148 }
149
150 // Tests a cycle RequestStream() -> RequestStreamCallback().
151 TEST_F(VideoCaptureHandlerImplTest, RequestStream) {
152 InSequence s;
153
154 { // Enumerate devices to get a valid |id| for the first one.
155 VideoCaptureHandlerImpl::RequestMediaDevicesCallback callback =
156 base::Bind(&VideoCaptureHandlerImplTest::OnRequestMediaDevicesReply,
157 base::Unretained(this));
158
159 base::RunLoop run_loop;
160 base::Closure quit_closure = run_loop.QuitClosure();
161 EXPECT_CALL(*this, DoOnRequestMediaDevicesReply())
162 .Times(1)
163 .WillOnce(RunClosure(quit_closure));
164 RequestMediaDevices(std::move(callback));
165
166 run_loop.Run();
167 }
168 {
169 mojom::StreamOptionsPtr options = mojom::StreamOptions::New();
170 options->device_id = media_devices_info_[0]->device_id;
171 options->capture_size = mojo::Size::From(gfx::Size(640, 480));
172 options->frame_rate = 30.0;
173
174 VideoCaptureHandlerImpl::RequestStreamCallback callback =
175 base::Bind(&VideoCaptureHandlerImplTest::OnRequestStreamReply,
176 base::Unretained(this));
177
178 base::RunLoop run_loop;
179 base::Closure quit_closure = run_loop.QuitClosure();
180
181 // TODO(mcasas): Inspect the replied sources[0] contents.
182 EXPECT_CALL(*this, DoOnRequestStreamReply(_))
183 .Times(1)
184 .WillOnce(RunClosure(quit_closure));
185 RequestStream(std::move(options), "https://localhost", std::move(callback));
186
187 run_loop.Run();
188 }
189 }
190
191 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698