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

Side by Side Diff: media/audio/audio_manager_unittest.cc

Issue 1901583005: Revert of Pass task runners to AudioManager constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/environment.h" 6 #include "base/environment.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
11 #include "base/test/test_message_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "build/build_config.h" 10 #include "build/build_config.h"
14 #include "media/audio/audio_manager.h" 11 #include "media/audio/audio_manager.h"
15 #include "media/audio/audio_manager_base.h" 12 #include "media/audio/audio_manager_base.h"
16 #include "media/audio/audio_output_proxy.h" 13 #include "media/audio/audio_output_proxy.h"
17 #include "media/audio/audio_unittest_util.h" 14 #include "media/audio/audio_unittest_util.h"
18 #include "media/audio/fake_audio_log_factory.h" 15 #include "media/audio/fake_audio_log_factory.h"
19 #include "media/audio/fake_audio_manager.h" 16 #include "media/audio/fake_audio_manager.h"
20 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
21 18
22 #if defined(USE_ALSA) 19 #if defined(USE_ALSA)
23 #include "media/audio/alsa/audio_manager_alsa.h" 20 #include "media/audio/alsa/audio_manager_alsa.h"
24 #endif // defined(USE_ALSA) 21 #endif // defined(USE_ALSA)
25 22
26 #if defined(OS_WIN) 23 #if defined(OS_WIN)
27 #include "base/win/scoped_com_initializer.h" 24 #include "base/win/scoped_com_initializer.h"
28 #include "media/audio/win/audio_manager_win.h" 25 #include "media/audio/win/audio_manager_win.h"
29 #include "media/audio/win/wavein_input_win.h" 26 #include "media/audio/win/wavein_input_win.h"
30 #endif 27 #endif
31 28
32 #if defined(USE_PULSEAUDIO) 29 #if defined(USE_PULSEAUDIO)
33 #include "media/audio/pulse/audio_manager_pulse.h" 30 #include "media/audio/pulse/audio_manager_pulse.h"
34 #endif // defined(USE_PULSEAUDIO) 31 #endif // defined(USE_PULSEAUDIO)
35 32
36 namespace media { 33 namespace media {
37 34
38 namespace {
39 template <typename T>
40 struct TestAudioManagerFactory {
41 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) {
42 return ScopedAudioManagerPtr(new T(base::ThreadTaskRunnerHandle::Get(),
43 base::ThreadTaskRunnerHandle::Get(),
44 audio_log_factory));
45 }
46 };
47
48 #if defined(USE_PULSEAUDIO)
49 template <>
50 struct TestAudioManagerFactory<AudioManagerPulse> {
51 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) {
52 std::unique_ptr<AudioManagerPulse, AudioManagerDeleter> manager(
53 new AudioManagerPulse(base::ThreadTaskRunnerHandle::Get(),
54 base::ThreadTaskRunnerHandle::Get(),
55 audio_log_factory));
56 if (!manager->Init())
57 manager.reset();
58 return std::move(manager);
59 }
60 };
61 #endif // defined(USE_PULSEAUDIO)
62
63 template <>
64 struct TestAudioManagerFactory<std::nullptr_t> {
65 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) {
66 return AudioManager::Create(base::ThreadTaskRunnerHandle::Get(),
67 base::ThreadTaskRunnerHandle::Get(), nullptr,
68 audio_log_factory);
69 }
70 };
71 } // namespace
72
73 // Test fixture which allows us to override the default enumeration API on 35 // Test fixture which allows us to override the default enumeration API on
74 // Windows. 36 // Windows.
75 class AudioManagerTest : public ::testing::Test { 37 class AudioManagerTest : public ::testing::Test {
76 public: 38 public:
77 void HandleDefaultDeviceIDsTest() { 39 void HandleDefaultDeviceIDsTest() {
78 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, 40 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
79 CHANNEL_LAYOUT_STEREO, 48000, 16, 2048); 41 CHANNEL_LAYOUT_STEREO, 48000, 16, 2048);
80 42
81 // Create a stream with the default device id "". 43 // Create a stream with the default device id "".
82 AudioOutputStream* stream = 44 AudioOutputStream* stream =
(...skipping 25 matching lines...) Expand all
108 *params = audio_manager_->GetDefaultOutputStreamParameters(); 70 *params = audio_manager_->GetDefaultOutputStreamParameters();
109 } 71 }
110 72
111 void GetAssociatedOutputDeviceID(const std::string& input_device_id, 73 void GetAssociatedOutputDeviceID(const std::string& input_device_id,
112 std::string* output_device_id) { 74 std::string* output_device_id) {
113 *output_device_id = 75 *output_device_id =
114 audio_manager_->GetAssociatedOutputDeviceID(input_device_id); 76 audio_manager_->GetAssociatedOutputDeviceID(input_device_id);
115 } 77 }
116 78
117 protected: 79 protected:
118 AudioManagerTest() { CreateAudioManagerForTesting(); } 80 AudioManagerTest() : audio_manager_(AudioManager::CreateForTesting()) {
119 ~AudioManagerTest() override {} 81 // Wait for audio thread initialization to complete. Otherwise the
82 // enumeration type may not have been set yet.
83 base::WaitableEvent event(false, false);
84 audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind(
85 &base::WaitableEvent::Signal, base::Unretained(&event)));
86 event.Wait();
87 }
120 88
121 #if defined(OS_WIN) 89 #if defined(OS_WIN)
122 bool SetMMDeviceEnumeration() { 90 bool SetMMDeviceEnumeration() {
123 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); 91 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get());
124 // Windows Wave is used as default if Windows XP was detected => 92 // Windows Wave is used as default if Windows XP was detected =>
125 // return false since MMDevice is not supported on XP. 93 // return false since MMDevice is not supported on XP.
126 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) 94 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration)
127 return false; 95 return false;
128 96
129 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); 97 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 ++it; 142 ++it;
175 } 143 }
176 } else { 144 } else {
177 // Log a warning so we can see the status on the build bots. No need to 145 // Log a warning so we can see the status on the build bots. No need to
178 // break the test though since this does successfully test the code and 146 // break the test though since this does successfully test the code and
179 // some failure cases. 147 // some failure cases.
180 LOG(WARNING) << "No input devices detected"; 148 LOG(WARNING) << "No input devices detected";
181 } 149 }
182 } 150 }
183 151
184 bool InputDevicesAvailable() { 152 void HasInputDevicesAvailable(bool* has_devices) {
185 return audio_manager_->HasAudioInputDevices(); 153 *has_devices = audio_manager_->HasAudioInputDevices();
186 }
187 bool OutputDevicesAvailable() {
188 return audio_manager_->HasAudioOutputDevices();
189 } 154 }
190 155
191 template <typename T = std::nullptr_t> 156 void HasOutputDevicesAvailable(bool* has_devices) {
157 *has_devices = audio_manager_->HasAudioOutputDevices();
158 }
159
160 bool InputDevicesAvailable() {
161 bool has_devices = false;
162 RunOnAudioThread(base::Bind(&AudioManagerTest::HasInputDevicesAvailable,
163 base::Unretained(this), &has_devices));
164 return has_devices;
165 }
166
167 bool OutputDevicesAvailable() {
168 bool has_devices = false;
169 RunOnAudioThread(base::Bind(&AudioManagerTest::HasOutputDevicesAvailable,
170 base::Unretained(this), &has_devices));
171 return has_devices;
172 }
173
174 #if defined(USE_ALSA) || defined(USE_PULSEAUDIO)
175 template <class T>
192 void CreateAudioManagerForTesting() { 176 void CreateAudioManagerForTesting() {
193 // Only one AudioManager may exist at a time, so destroy the one we're 177 // Only one AudioManager may exist at a time, so destroy the one we're
194 // currently holding before creating a new one. 178 // currently holding before creating a new one.
195 // Flush the message loop to run any shutdown tasks posted by AudioManager.
196 audio_manager_.reset(); 179 audio_manager_.reset();
197 base::RunLoop().RunUntilIdle(); 180 audio_manager_.reset(T::Create(&fake_audio_log_factory_));
181 }
182 #endif
198 183
199 audio_manager_ = 184 // Synchronously runs the provided callback/closure on the audio thread.
200 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_); 185 void RunOnAudioThread(const base::Closure& closure) {
201 // A few AudioManager implementations post initialization tasks to 186 if (!audio_manager_->GetTaskRunner()->BelongsToCurrentThread()) {
202 // audio thread. Flush the thread to ensure that |audio_manager_| is 187 base::WaitableEvent event(false, false);
203 // initialized and ready to use before returning from this function. 188 audio_manager_->GetTaskRunner()->PostTask(
204 // TODO(alokp): We should perhaps do this in AudioManager::Create(). 189 FROM_HERE,
205 base::RunLoop().RunUntilIdle(); 190 base::Bind(&AudioManagerTest::RunOnAudioThreadImpl,
191 base::Unretained(this),
192 closure,
193 &event));
194 event.Wait();
195 } else {
196 closure.Run();
197 }
206 } 198 }
207 199
208 base::TestMessageLoop message_loop_; 200 void RunOnAudioThreadImpl(const base::Closure& closure,
201 base::WaitableEvent* event) {
202 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
203 closure.Run();
204 event->Signal();
205 }
206
209 FakeAudioLogFactory fake_audio_log_factory_; 207 FakeAudioLogFactory fake_audio_log_factory_;
210 ScopedAudioManagerPtr audio_manager_; 208 scoped_ptr<AudioManager> audio_manager_;
211 }; 209 };
212 210
213 TEST_F(AudioManagerTest, HandleDefaultDeviceIDs) { 211 TEST_F(AudioManagerTest, HandleDefaultDeviceIDs) {
214 // Use a fake manager so we can makeup device ids, this will still use the 212 // Use a fake manager so we can makeup device ids, this will still use the
215 // AudioManagerBase code. 213 // AudioManagerBase code.
216 CreateAudioManagerForTesting<FakeAudioManager>(); 214 audio_manager_.reset(new FakeAudioManager(&fake_audio_log_factory_));
217 HandleDefaultDeviceIDsTest(); 215 RunOnAudioThread(base::Bind(&AudioManagerTest::HandleDefaultDeviceIDsTest,
218 base::RunLoop().RunUntilIdle(); 216 base::Unretained(this)));
219 } 217 }
220 218
221 // Test that devices can be enumerated. 219 // Test that devices can be enumerated.
222 TEST_F(AudioManagerTest, EnumerateInputDevices) { 220 TEST_F(AudioManagerTest, EnumerateInputDevices) {
223 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); 221 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
224 222
225 AudioDeviceNames device_names; 223 AudioDeviceNames device_names;
226 audio_manager_->GetAudioInputDeviceNames(&device_names); 224 RunOnAudioThread(
225 base::Bind(&AudioManager::GetAudioInputDeviceNames,
226 base::Unretained(audio_manager_.get()),
227 &device_names));
227 CheckDeviceNames(device_names); 228 CheckDeviceNames(device_names);
228 } 229 }
229 230
230 // Test that devices can be enumerated. 231 // Test that devices can be enumerated.
231 TEST_F(AudioManagerTest, EnumerateOutputDevices) { 232 TEST_F(AudioManagerTest, EnumerateOutputDevices) {
232 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); 233 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable());
233 234
234 AudioDeviceNames device_names; 235 AudioDeviceNames device_names;
235 audio_manager_->GetAudioOutputDeviceNames(&device_names); 236 RunOnAudioThread(
237 base::Bind(&AudioManager::GetAudioOutputDeviceNames,
238 base::Unretained(audio_manager_.get()),
239 &device_names));
236 CheckDeviceNames(device_names); 240 CheckDeviceNames(device_names);
237 } 241 }
238 242
239 // Run additional tests for Windows since enumeration can be done using 243 // Run additional tests for Windows since enumeration can be done using
240 // two different APIs. MMDevice is default for Vista and higher and Wave 244 // two different APIs. MMDevice is default for Vista and higher and Wave
241 // is default for XP and lower. 245 // is default for XP and lower.
242 #if defined(OS_WIN) 246 #if defined(OS_WIN)
243 247
244 // Override default enumeration API and force usage of Windows MMDevice. 248 // Override default enumeration API and force usage of Windows MMDevice.
245 // This test will only run on Windows Vista and higher. 249 // This test will only run on Windows Vista and higher.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 audio_manager_->GetAudioOutputDeviceNames(&device_names); 396 audio_manager_->GetAudioOutputDeviceNames(&device_names);
393 CheckDeviceNames(device_names); 397 CheckDeviceNames(device_names);
394 } 398 }
395 #endif // defined(USE_ALSA) 399 #endif // defined(USE_ALSA)
396 400
397 TEST_F(AudioManagerTest, GetDefaultOutputStreamParameters) { 401 TEST_F(AudioManagerTest, GetDefaultOutputStreamParameters) {
398 #if defined(OS_WIN) || defined(OS_MACOSX) 402 #if defined(OS_WIN) || defined(OS_MACOSX)
399 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); 403 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
400 404
401 AudioParameters params; 405 AudioParameters params;
402 GetDefaultOutputStreamParameters(&params); 406 RunOnAudioThread(
407 base::Bind(&AudioManagerTest::GetDefaultOutputStreamParameters,
408 base::Unretained(this), &params));
403 EXPECT_TRUE(params.IsValid()); 409 EXPECT_TRUE(params.IsValid());
404 #endif // defined(OS_WIN) || defined(OS_MACOSX) 410 #endif // defined(OS_WIN) || defined(OS_MACOSX)
405 } 411 }
406 412
407 TEST_F(AudioManagerTest, GetAssociatedOutputDeviceID) { 413 TEST_F(AudioManagerTest, GetAssociatedOutputDeviceID) {
408 #if defined(OS_WIN) || defined(OS_MACOSX) 414 #if defined(OS_WIN) || defined(OS_MACOSX)
409 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable() && OutputDevicesAvailable()); 415 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable() && OutputDevicesAvailable());
410 416
411 AudioDeviceNames device_names; 417 AudioDeviceNames device_names;
412 audio_manager_->GetAudioInputDeviceNames(&device_names); 418 RunOnAudioThread(base::Bind(&AudioManager::GetAudioInputDeviceNames,
419 base::Unretained(audio_manager_.get()),
420 &device_names));
413 bool found_an_associated_device = false; 421 bool found_an_associated_device = false;
414 for (AudioDeviceNames::iterator it = device_names.begin(); 422 for (AudioDeviceNames::iterator it = device_names.begin();
415 it != device_names.end(); 423 it != device_names.end();
416 ++it) { 424 ++it) {
417 EXPECT_FALSE(it->unique_id.empty()); 425 EXPECT_FALSE(it->unique_id.empty());
418 EXPECT_FALSE(it->device_name.empty()); 426 EXPECT_FALSE(it->device_name.empty());
419 std::string output_device_id; 427 std::string output_device_id;
420 GetAssociatedOutputDeviceID(it->unique_id, &output_device_id); 428 RunOnAudioThread(base::Bind(&AudioManagerTest::GetAssociatedOutputDeviceID,
429 base::Unretained(this), it->unique_id,
430 &output_device_id));
421 if (!output_device_id.empty()) { 431 if (!output_device_id.empty()) {
422 DVLOG(2) << it->unique_id << " matches with " << output_device_id; 432 DVLOG(2) << it->unique_id << " matches with " << output_device_id;
423 found_an_associated_device = true; 433 found_an_associated_device = true;
424 } 434 }
425 } 435 }
426 436
427 EXPECT_TRUE(found_an_associated_device); 437 EXPECT_TRUE(found_an_associated_device);
428 #endif // defined(OS_WIN) || defined(OS_MACOSX) 438 #endif // defined(OS_WIN) || defined(OS_MACOSX)
429 } 439 }
430 440
431 } // namespace media 441 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/audio_manager_factory_unittest.cc ('k') | media/audio/audio_output_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698