OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "media/audio/audio_manager_base.h" | 5 #include "base/environment.h" |
6 #include "base/memory/scoped_ptr.h" | |
7 #include "base/win/scoped_com_initializer.h" | |
8 #include "media/audio/audio_manager.h" | |
9 #if defined(OS_WIN) | |
10 #include "media/audio/win/audio_manager_win.h" | |
11 #endif | |
6 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
7 | 13 |
8 namespace media { | 14 using base::win::ScopedCOMInitializer; |
15 using media::AudioDeviceNames; | |
9 | 16 |
10 // Test that devices can be enumerated. | 17 // Test fixture which allows us to override the default enumeration API on |
11 TEST(AudioInputDeviceTest, EnumerateDevices) { | 18 // Windows. |
12 AudioDeviceNames device_names; | 19 class AudioInputDeviceTest |
13 ASSERT_TRUE(AudioManager::GetAudioManager() != NULL); | 20 : public ::testing::Test { |
14 AudioManager::GetAudioManager()->GetAudioInputDeviceNames( | 21 protected: |
15 &device_names); | 22 #if defined(OS_WIN) |
23 // Store current device-enumeration type to ensure that it can be restored | |
24 // after last test in this test suite. | |
25 static void SetUpTestCase() { | |
26 enumeration_type_ = static_cast<AudioManagerWin*>( | |
27 AudioManager::GetAudioManager())->enumeration_type(); | |
28 } | |
29 | |
30 // Restore pre-test state of device-enumeration type. | |
31 static void TearDownTestCase() { | |
32 static_cast<AudioManagerWin*>( | |
33 AudioManager::GetAudioManager())->SetEnumerationType(enumeration_type_); | |
34 } | |
35 | |
36 bool SetMMDeviceEnumeration(AudioManager* audio_manager) { | |
37 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager); | |
38 // Windows Wave is used as default if Windows XP was detected => | |
39 // return false since MMDevice is not supported on XP. | |
40 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) | |
41 return false; | |
42 | |
43 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); | |
44 return true; | |
45 } | |
46 | |
47 void SetWaveEnumeration(AudioManager* audio_manager) { | |
48 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager); | |
49 amw->SetEnumerationType(AudioManagerWin::kWaveEnumeration); | |
50 } | |
51 | |
52 // Stores the pre-test state representing the device-enumeration type. | |
53 static AudioManagerWin::EnumerationType enumeration_type_; | |
54 #endif | |
55 }; | |
56 | |
57 AudioManagerWin::EnumerationType AudioInputDeviceTest::enumeration_type_ = | |
58 AudioManagerWin::kUninitializedEnumeration; | |
59 | |
60 // Convenience method which ensures that we are not running on the build | |
61 // bots which lacks audio device support. | |
62 static bool CanRunAudioTests() { | |
63 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
64 if (env->HasVar("CHROME_HEADLESS")) | |
65 return false; | |
66 return true; | |
67 } | |
68 | |
69 // Helper method which verifies that the device list starts with a valid | |
70 // default record followed by non-default device names. | |
71 static void CheckDeviceNames(const AudioDeviceNames& device_names) { | |
16 if (!device_names.empty()) { | 72 if (!device_names.empty()) { |
17 AudioDeviceNames::const_iterator it = device_names.begin(); | 73 AudioDeviceNames::const_iterator it = device_names.begin(); |
18 // The first device in the list is the prepended default device. | 74 |
75 // The first device in the list should always be the default device. | |
19 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName), | 76 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName), |
20 it->device_name); | 77 it->device_name); |
tommi (sloooow) - chröme
2011/11/23 09:47:13
indent
| |
21 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id); | 78 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id); |
22 ++it; | 79 ++it; |
23 | 80 |
24 // Other devices should have non-empty name and id. | 81 // Other devices should have non-empty name and id and should not contain |
82 // default name or id. | |
25 while (it != device_names.end()) { | 83 while (it != device_names.end()) { |
26 EXPECT_NE("", it->device_name); | 84 EXPECT_FALSE(it->device_name.empty()); |
27 EXPECT_NE("", it->unique_id); | 85 EXPECT_FALSE(it->unique_id.empty()); |
86 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName), | |
87 it->device_name); | |
88 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId), | |
89 it->unique_id); | |
28 ++it; | 90 ++it; |
29 } | 91 } |
30 } | 92 } |
31 } | 93 } |
32 | 94 |
33 } // namespace media | 95 // Test that devices can be enumerated. |
96 TEST_F(AudioInputDeviceTest, EnumerateDevices) { | |
97 if (!CanRunAudioTests()) | |
98 return; | |
99 // The MMDevice API requires a correct COM environment. | |
100 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); | |
101 AudioDeviceNames device_names; | |
102 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
103 EXPECT_TRUE(audio_man != NULL); | |
104 audio_man->GetAudioInputDeviceNames(&device_names); | |
105 CheckDeviceNames(device_names); | |
106 } | |
107 | |
108 // Run additional tests for Windows since enumeration can be done using | |
109 // two different APIs. MMDevice is default for Vista and higher and Wave | |
110 // is default for XP and lower. | |
111 #if defined(OS_WIN) | |
112 | |
113 // Override default enumeration API and force usage of Windows MMDevice. | |
114 // This test will only run on Windows Vista and higher. | |
115 TEST_F(AudioInputDeviceTest, EnumerateDevicesWinMMDevice) { | |
116 if (!CanRunAudioTests()) | |
117 return; | |
118 // The MMDevice API requires a correct COM environment. | |
119 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); | |
120 AudioDeviceNames device_names; | |
121 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
122 if (!SetMMDeviceEnumeration(audio_man)) { | |
123 // Usage of MMDevice will fail on XP and lower. | |
124 return; | |
125 } | |
126 audio_man->GetAudioInputDeviceNames(&device_names); | |
127 CheckDeviceNames(device_names); | |
128 } | |
129 | |
130 // Override default enumeration API and force usage of Windows Wave. | |
131 // This test will run on Windows XP, Windows Vista and Windows 7. | |
132 TEST_F(AudioInputDeviceTest, EnumerateDevicesWinWave) { | |
133 if (!CanRunAudioTests()) | |
134 return; | |
135 AudioDeviceNames device_names; | |
136 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
137 EXPECT_TRUE(audio_man != NULL); | |
138 SetWaveEnumeration(audio_man); | |
139 audio_man->GetAudioInputDeviceNames(&device_names); | |
140 CheckDeviceNames(device_names); | |
141 } | |
142 | |
143 #endif | |
OLD | NEW |