Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / audio / audio_input_volume_unittest.cc
blob19c712a99ef49bc1ad4d692bb148148a709d96a0
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() : audio_manager_(AudioManager::CreateForTesting()) {}
43 bool HasCoreAudioAndInputDevices() {
44 #if defined(OS_WIN)
45 // TODO(henrika): add support for volume control on Windows XP as well.
46 if (!CoreAudioUtil::IsSupported())
47 return false;
48 #endif
49 return audio_manager_->HasAudioInputDevices();
52 // Helper method which checks if the stream has volume support.
53 bool HasDeviceVolumeControl(AudioInputStream* stream) {
54 if (!stream)
55 return false;
57 return (stream->GetMaxVolume() != 0.0);
60 AudioInputStream* CreateAndOpenStream(const std::string& device_id) {
61 const AudioParameters& params =
62 audio_manager_->GetInputStreamParameters(device_id);
63 AudioInputStream* ais = audio_manager_->MakeAudioInputStream(
64 params, device_id);
65 EXPECT_TRUE(NULL != ais);
67 #if defined(OS_MACOSX)
68 EXPECT_TRUE(ais->Open());
69 #else
70 // Some linux devices do not support our settings and some Windows devices
71 // may be "currently unavailable", we may fail to open those devices.
72 if (!ais->Open()) {
73 // Default device should always be able to be opened.
74 EXPECT_TRUE(AudioManagerBase::kDefaultDeviceId != device_id);
75 ais->Close();
76 ais = NULL;
78 #endif
80 return ais;
83 #if defined(OS_WIN)
84 base::win::ScopedCOMInitializer com_init_;
85 #endif
87 scoped_ptr<AudioManager> audio_manager_;
90 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
91 // Currently failing on linux ARM bot: http://crbug/238490
92 // Also flaky on x86_64: http://crbug/236936
93 #define MAYBE_InputVolumeTest DISABLED_InputVolumeTest
94 #else
95 #define MAYBE_InputVolumeTest InputVolumeTest
96 #endif
98 TEST_F(AudioInputVolumeTest, MAYBE_InputVolumeTest) {
99 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices());
101 // Retrieve a list of all available input devices.
102 AudioDeviceNames device_names;
103 audio_manager_->GetAudioInputDeviceNames(&device_names);
104 if (device_names.empty()) {
105 LOG(WARNING) << "Could not find any available input device";
106 return;
109 // Scan all available input devices and repeat the same test for all of them.
110 for (AudioDeviceNames::const_iterator it = device_names.begin();
111 it != device_names.end();
112 ++it) {
113 AudioInputStream* ais = CreateAndOpenStream(it->unique_id);
114 if (!ais) {
115 DLOG(WARNING) << "Failed to open stream for device " << it->unique_id;
116 continue;
119 if (!HasDeviceVolumeControl(ais)) {
120 DLOG(WARNING) << "Device: " << it->unique_id
121 << ", does not have volume control.";
122 ais->Close();
123 continue;
126 double max_volume = ais->GetMaxVolume();
127 EXPECT_GT(max_volume, 0.0);
129 // Store the current input-device volume level.
130 double original_volume = ais->GetVolume();
131 EXPECT_GE(original_volume, 0.0);
132 #if defined(OS_WIN) || defined(OS_MACOSX)
133 // Note that |original_volume| can be higher than |max_volume| on Linux.
134 EXPECT_LE(original_volume, max_volume);
135 #endif
137 // Set the volume to the maxiumum level..
138 ais->SetVolume(max_volume);
139 double current_volume = ais->GetVolume();
140 EXPECT_EQ(max_volume, current_volume);
142 // Set the volume to the minimum level (=0).
143 double new_volume = 0.0;
144 ais->SetVolume(new_volume);
145 #if defined(OS_LINUX)
146 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
147 #else
148 current_volume = ais->GetVolume();
149 #endif
150 EXPECT_EQ(new_volume, current_volume);
152 // Set the volume to the mid level (50% of max).
153 // Verify that the absolute error is small enough.
154 new_volume = max_volume / 2;
155 ais->SetVolume(new_volume);
156 #if defined(OS_LINUX)
157 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
158 #else
159 current_volume = ais->GetVolume();
160 #endif
161 EXPECT_LT(current_volume, max_volume);
162 EXPECT_GT(current_volume, 0);
163 EXPECT_NEAR(current_volume, new_volume, 0.25 * max_volume);
165 // Restores the volume to the original value.
166 ais->SetVolume(original_volume);
167 current_volume = ais->GetVolume();
168 EXPECT_EQ(original_volume, current_volume);
170 ais->Close();
174 } // namespace media