Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / audio / audio_input_volume_unittest.cc
blobd6ee313bc250a6ede92bc0b95f90598aa3386061
1 // Copyright (c) 2012 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.
5 #include <cmath>
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "media/audio/audio_io.h"
10 #include "media/audio/audio_manager_base.h"
11 #include "media/audio/audio_unittest_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if defined(OS_WIN)
15 #include "base/win/scoped_com_initializer.h"
16 #include "media/audio/win/core_audio_util_win.h"
17 #endif
19 namespace media {
21 double GetVolumeAfterSetVolumeOnLinux(AudioInputStream* ais,
22 double target_volume) {
23 // SetVolume() is asynchronous on Linux, we need to keep trying until
24 // the SetVolume() operation is done.
25 static const int kTimesToRun = 10;
26 double volume = 0.0;
27 for (int i = 0; i < kTimesToRun; ++i) {
28 volume = ais->GetVolume();
29 if (volume == target_volume)
30 break;
32 // Sleep 100ms to wait for the operation.
33 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
36 return volume;
39 class AudioInputVolumeTest : public ::testing::Test {
40 protected:
41 AudioInputVolumeTest()
42 : audio_manager_(AudioManager::CreateForTesting())
43 #if defined(OS_WIN)
44 , com_init_(base::win::ScopedCOMInitializer::kMTA)
45 #endif
49 bool HasCoreAudioAndInputDevices() {
50 #if defined(OS_WIN)
51 // TODO(henrika): add support for volume control on Windows XP as well.
52 if (!CoreAudioUtil::IsSupported())
53 return false;
54 #endif
55 return audio_manager_->HasAudioInputDevices();
58 // Helper method which checks if the stream has volume support.
59 bool HasDeviceVolumeControl(AudioInputStream* stream) {
60 if (!stream)
61 return false;
63 return (stream->GetMaxVolume() != 0.0);
66 AudioInputStream* CreateAndOpenStream(const std::string& device_id) {
67 const AudioParameters& params =
68 audio_manager_->GetInputStreamParameters(device_id);
69 AudioInputStream* ais = audio_manager_->MakeAudioInputStream(
70 params, device_id);
71 EXPECT_TRUE(NULL != ais);
73 #if defined(OS_LINUX) || defined(OS_OPENBSD)
74 // Some linux devices do not support our settings, we may fail to open
75 // those devices.
76 if (!ais->Open()) {
77 // Default device should always be able to be opened.
78 EXPECT_TRUE(AudioManagerBase::kDefaultDeviceId != device_id);
79 ais->Close();
80 ais = NULL;
82 #elif defined(OS_WIN) || defined(OS_MACOSX)
83 EXPECT_TRUE(ais->Open());
84 #endif
86 return ais;
89 scoped_ptr<AudioManager> audio_manager_;
91 #if defined(OS_WIN)
92 base::win::ScopedCOMInitializer com_init_;
93 #endif
96 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
97 // Currently failing on linux ARM bot: http://crbug/238490
98 // Also flaky on x86_64: http://crbug/236936
99 #define MAYBE_InputVolumeTest DISABLED_InputVolumeTest
100 #else
101 #define MAYBE_InputVolumeTest InputVolumeTest
102 #endif
104 TEST_F(AudioInputVolumeTest, MAYBE_InputVolumeTest) {
105 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices());
107 // Retrieve a list of all available input devices.
108 AudioDeviceNames device_names;
109 audio_manager_->GetAudioInputDeviceNames(&device_names);
110 if (device_names.empty()) {
111 LOG(WARNING) << "Could not find any available input device";
112 return;
115 // Scan all available input devices and repeat the same test for all of them.
116 for (AudioDeviceNames::const_iterator it = device_names.begin();
117 it != device_names.end();
118 ++it) {
119 AudioInputStream* ais = CreateAndOpenStream(it->unique_id);
120 if (!ais) {
121 DLOG(WARNING) << "Failed to open stream for device " << it->unique_id;
122 continue;
125 if (!HasDeviceVolumeControl(ais)) {
126 DLOG(WARNING) << "Device: " << it->unique_id
127 << ", does not have volume control.";
128 ais->Close();
129 continue;
132 double max_volume = ais->GetMaxVolume();
133 EXPECT_GT(max_volume, 0.0);
135 // Store the current input-device volume level.
136 double original_volume = ais->GetVolume();
137 EXPECT_GE(original_volume, 0.0);
138 #if defined(OS_WIN) || defined(OS_MACOSX)
139 // Note that |original_volume| can be higher than |max_volume| on Linux.
140 EXPECT_LE(original_volume, max_volume);
141 #endif
143 // Set the volume to the maxiumum level..
144 ais->SetVolume(max_volume);
145 double current_volume = ais->GetVolume();
146 EXPECT_EQ(max_volume, current_volume);
148 // Set the volume to the minimum level (=0).
149 double new_volume = 0.0;
150 ais->SetVolume(new_volume);
151 #if defined(OS_LINUX)
152 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
153 #else
154 current_volume = ais->GetVolume();
155 #endif
156 EXPECT_EQ(new_volume, current_volume);
158 // Set the volume to the mid level (50% of max).
159 // Verify that the absolute error is small enough.
160 new_volume = max_volume / 2;
161 ais->SetVolume(new_volume);
162 #if defined(OS_LINUX)
163 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
164 #else
165 current_volume = ais->GetVolume();
166 #endif
167 EXPECT_LT(current_volume, max_volume);
168 EXPECT_GT(current_volume, 0);
169 EXPECT_NEAR(current_volume, new_volume, 0.25 * max_volume);
171 // Restores the volume to the original value.
172 ais->SetVolume(original_volume);
173 current_volume = ais->GetVolume();
174 EXPECT_EQ(original_volume, current_volume);
176 ais->Close();
180 } // namespace media