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