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