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

Side by Side Diff: content/browser/renderer_host/media/media_devices_dispatcher_host_unittest.cc

Issue 2380793002: Migrate MediaDevices.enumerateDevices to Mojo (Closed)
Patch Set: rebase Created 4 years, 2 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 "content/browser/renderer_host/media/media_devices_dispatcher_host.h"
6
7 #include <stddef.h>
8
9 #include <memory>
10 #include <queue>
11 #include <utility>
12 #include <vector>
13
14 #include "base/bind.h"
15 #include "base/command_line.h"
16 #include "base/run_loop.h"
17 #include "base/threading/thread_task_runner_handle.h"
18 #include "content/browser/renderer_host/media/media_stream_manager.h"
19 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
20 #include "content/browser/renderer_host/media/video_capture_manager.h"
21 #include "content/public/browser/media_device_id.h"
22 #include "content/public/test/mock_resource_context.h"
23 #include "content/public/test/test_browser_context.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "media/audio/audio_device_description.h"
26 #include "media/audio/mock_audio_manager.h"
27 #include "media/base/media_switches.h"
28 #include "media/capture/video/fake_video_capture_device_factory.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "url/origin.h"
32
33 namespace content {
34
35 namespace {
36
37 const int kProcessId = 5;
38 const int kRenderId = 6;
39
40 void PhysicalDevicesEnumerated(base::Closure quit_closure,
41 MediaDeviceEnumeration* out,
42 const MediaDeviceEnumeration& enumeration) {
43 *out = enumeration;
44 quit_closure.Run();
45 }
46
47 } // namespace
48
49 class MediaDevicesDispatcherHostTest : public testing::Test {
50 public:
51 MediaDevicesDispatcherHostTest()
52 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
53 origin_(GURL("https://test.com")) {
54 // Make sure we use fake devices to avoid long delays.
55 base::CommandLine::ForCurrentProcess()->AppendSwitch(
56 switches::kUseFakeDeviceForMediaStream);
57 audio_manager_.reset(
58 new media::MockAudioManager(base::ThreadTaskRunnerHandle::Get()));
59 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
60
61 MockResourceContext* mock_resource_context =
62 static_cast<MockResourceContext*>(
63 browser_context_.GetResourceContext());
64
65 host_ = base::MakeUnique<MediaDevicesDispatcherHost>(
66 kProcessId, kRenderId, mock_resource_context->GetMediaDeviceIDSalt(),
67 media_stream_manager_.get(), true /* use_fake_ui */);
68 }
69
70 void SetUp() override {
71 base::RunLoop run_loop;
72 MediaDevicesManager::BoolDeviceTypes devices_to_enumerate;
73 devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_INPUT] = true;
74 devices_to_enumerate[MEDIA_DEVICE_TYPE_VIDEO_INPUT] = true;
75 devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true;
76 media_stream_manager_->media_devices_manager()->EnumerateDevices(
77 devices_to_enumerate,
78 base::Bind(&PhysicalDevicesEnumerated, run_loop.QuitClosure(),
79 &physical_devices_));
80 run_loop.Run();
81
82 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size(), 0u);
83 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size(), 0u);
84 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size(), 0u);
85 }
86
87 protected:
88 void DevicesEnumerated(
89 const base::Closure& closure,
90 const std::vector<std::vector<MediaDeviceInfo>>& devices) {
91 enumerated_devices_ = devices;
92 closure.Run();
93 }
94
95 void EnumerateDevicesAndWaitForResult(
96 bool enumerate_audio_input,
97 bool enumerate_video_input,
98 bool enumerate_audio_output,
99 std::unique_ptr<MediaStreamUIProxy> ui_proxy) {
100 if (ui_proxy)
101 host_->SetFakeUIProxyForTesting(std::move(ui_proxy));
102 base::RunLoop run_loop;
103 host_->EnumerateDevices(
104 enumerate_audio_input, enumerate_video_input, enumerate_audio_output,
105 origin_, base::Bind(&MediaDevicesDispatcherHostTest::DevicesEnumerated,
106 base::Unretained(this), run_loop.QuitClosure()));
107 run_loop.Run();
108
109 ASSERT_FALSE(enumerated_devices_.empty());
110 if (enumerate_audio_input)
111 EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_AUDIO_INPUT].empty());
112 if (enumerate_video_input)
113 EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_VIDEO_INPUT].empty());
114 if (enumerate_audio_output)
115 EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].empty());
116
117 EXPECT_FALSE(DoesContainRawIds(enumerated_devices_));
118 EXPECT_TRUE(DoesEveryDeviceMapToRawId(enumerated_devices_, origin_));
119 }
120
121 bool DoesContainRawIds(
122 const std::vector<std::vector<MediaDeviceInfo>>& enumeration) {
123 for (size_t i = 0; i < NUM_MEDIA_DEVICE_TYPES; ++i) {
124 for (const auto& device_info : enumeration[i]) {
125 for (const auto& raw_device_info : physical_devices_[i]) {
126 // Skip default and communications audio devices, whose IDs are not
127 // translated.
128 if (device_info.device_id ==
129 media::AudioDeviceDescription::kDefaultDeviceId ||
130 device_info.device_id ==
131 media::AudioDeviceDescription::kCommunicationsDeviceId) {
132 continue;
133 }
134 if (device_info.device_id == raw_device_info.device_id)
135 return true;
136 }
137 }
138 }
139 return false;
140 }
141
142 bool DoesEveryDeviceMapToRawId(
143 const std::vector<std::vector<MediaDeviceInfo>>& enumeration,
144 const url::Origin& origin) {
145 for (size_t i = 0; i < NUM_MEDIA_DEVICE_TYPES; ++i) {
146 for (const auto& device_info : enumeration[i]) {
147 bool found_match = false;
148 for (const auto& raw_device_info : physical_devices_[i]) {
149 if (DoesMediaDeviceIDMatchHMAC(
150 browser_context_.GetResourceContext()->GetMediaDeviceIDSalt(),
151 origin, device_info.device_id, raw_device_info.device_id)) {
152 EXPECT_FALSE(found_match);
153 found_match = true;
154 }
155 }
156 if (!found_match)
157 return false;
158 }
159 }
160 return true;
161 }
162
163 // Returns true if all devices have labels, false otherwise.
164 bool DoesContainLabels(
165 const std::vector<std::vector<MediaDeviceInfo>>& enumeration) {
166 for (const auto& device_info_array : enumeration) {
167 for (const auto& device_info : device_info_array) {
168 if (device_info.label.empty())
169 return false;
170 }
171 }
172 return true;
173 }
174
175 // Returns true if no devices have labels, false otherwise.
176 bool DoesNotContainLabels(
177 const std::vector<std::vector<MediaDeviceInfo>>& enumeration) {
178 for (const auto& device_info_array : enumeration) {
179 for (const auto& device_info : device_info_array) {
180 if (!device_info.label.empty())
181 return false;
182 }
183 }
184 return true;
185 }
186
187 std::unique_ptr<MediaDevicesDispatcherHost> host_;
188 std::unique_ptr<MediaStreamManager> media_stream_manager_;
189 content::TestBrowserThreadBundle thread_bundle_;
190 std::unique_ptr<media::AudioManager, media::AudioManagerDeleter>
191 audio_manager_;
192 content::TestBrowserContext browser_context_;
193 MediaDeviceEnumeration physical_devices_;
194 url::Origin origin_;
195
196 std::vector<std::vector<MediaDeviceInfo>> enumerated_devices_;
197 };
198
199 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioInputDevices) {
200 EnumerateDevicesAndWaitForResult(true, false, false, nullptr);
201 EXPECT_TRUE(DoesContainLabels(enumerated_devices_));
202 }
203
204 TEST_F(MediaDevicesDispatcherHostTest, EnumerateVideoInputDevices) {
205 EnumerateDevicesAndWaitForResult(false, true, false, nullptr);
206 EXPECT_TRUE(DoesContainLabels(enumerated_devices_));
207 }
208
209 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioOutputDevices) {
210 EnumerateDevicesAndWaitForResult(false, false, true, nullptr);
211 EXPECT_TRUE(DoesContainLabels(enumerated_devices_));
212 }
213
214 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAllDevices) {
215 EnumerateDevicesAndWaitForResult(true, true, true, nullptr);
216 EXPECT_TRUE(DoesContainLabels(enumerated_devices_));
217 }
218
219 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioInputDevicesNoAccess) {
220 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui_proxy =
221 base::MakeUnique<FakeMediaStreamUIProxy>();
222 fake_ui_proxy->SetMicAccess(false);
223 EnumerateDevicesAndWaitForResult(true, false, false,
224 std::move(fake_ui_proxy));
225 EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_));
226 }
227
228 TEST_F(MediaDevicesDispatcherHostTest, EnumerateVideoInputDevicesNoAccess) {
229 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui_proxy =
230 base::MakeUnique<FakeMediaStreamUIProxy>();
231 fake_ui_proxy->SetCameraAccess(false);
232 EnumerateDevicesAndWaitForResult(false, true, false,
233 std::move(fake_ui_proxy));
234 EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_));
235 }
236
237 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioOutputDevicesNoAccess) {
238 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui_proxy =
239 base::MakeUnique<FakeMediaStreamUIProxy>();
240 fake_ui_proxy->SetMicAccess(false);
241 EnumerateDevicesAndWaitForResult(false, false, true,
242 std::move(fake_ui_proxy));
243 EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_));
244 }
245
246 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAllDevicesNoAccess) {
247 std::unique_ptr<FakeMediaStreamUIProxy> fake_ui_proxy =
248 base::MakeUnique<FakeMediaStreamUIProxy>();
249 fake_ui_proxy->SetMicAccess(false);
250 fake_ui_proxy->SetCameraAccess(false);
251 EnumerateDevicesAndWaitForResult(true, true, true, std::move(fake_ui_proxy));
252 EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_));
253 }
254
255 }; // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/media/media_devices_dispatcher_host.cc ('k') | content/common/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698