Add ICU message format support
[chromium-blink-merge.git] / media / audio / audio_input_volume_unittest.cc
blob3fa1f40b907dcbb4e9e0ec4ec8279e1cf1b99889
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_LINUX) || defined(OS_OPENBSD)
68 // Some linux devices do not support our settings, we may fail to open
69 // those devices.
70 if (!ais->Open()) {
71 // Default device should always be able to be opened.
72 EXPECT_TRUE(AudioManagerBase::kDefaultDeviceId != device_id);
73 ais->Close();
74 ais = NULL;
76 #elif defined(OS_WIN) || defined(OS_MACOSX)
77 EXPECT_TRUE(ais->Open());
78 #endif
80 return ais;
83 scoped_ptr<AudioManager> audio_manager_;
86 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
87 // Currently failing on linux ARM bot: http://crbug/238490
88 // Also flaky on x86_64: http://crbug/236936
89 #define MAYBE_InputVolumeTest DISABLED_InputVolumeTest
90 #else
91 #define MAYBE_InputVolumeTest InputVolumeTest
92 #endif
94 TEST_F(AudioInputVolumeTest, MAYBE_InputVolumeTest) {
95 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices());
97 // Retrieve a list of all available input devices.
98 AudioDeviceNames device_names;
99 audio_manager_->GetAudioInputDeviceNames(&device_names);
100 if (device_names.empty()) {
101 LOG(WARNING) << "Could not find any available input device";
102 return;
105 // Scan all available input devices and repeat the same test for all of them.
106 for (AudioDeviceNames::const_iterator it = device_names.begin();
107 it != device_names.end();
108 ++it) {
109 AudioInputStream* ais = CreateAndOpenStream(it->unique_id);
110 if (!ais) {
111 DLOG(WARNING) << "Failed to open stream for device " << it->unique_id;
112 continue;
115 if (!HasDeviceVolumeControl(ais)) {
116 DLOG(WARNING) << "Device: " << it->unique_id
117 << ", does not have volume control.";
118 ais->Close();
119 continue;
122 double max_volume = ais->GetMaxVolume();
123 EXPECT_GT(max_volume, 0.0);
125 // Store the current input-device volume level.
126 double original_volume = ais->GetVolume();
127 EXPECT_GE(original_volume, 0.0);
128 #if defined(OS_WIN) || defined(OS_MACOSX)
129 // Note that |original_volume| can be higher than |max_volume| on Linux.
130 EXPECT_LE(original_volume, max_volume);
131 #endif
133 // Set the volume to the maxiumum level..
134 ais->SetVolume(max_volume);
135 double current_volume = ais->GetVolume();
136 EXPECT_EQ(max_volume, current_volume);
138 // Set the volume to the minimum level (=0).
139 double new_volume = 0.0;
140 ais->SetVolume(new_volume);
141 #if defined(OS_LINUX)
142 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
143 #else
144 current_volume = ais->GetVolume();
145 #endif
146 EXPECT_EQ(new_volume, current_volume);
148 // Set the volume to the mid level (50% of max).
149 // Verify that the absolute error is small enough.
150 new_volume = max_volume / 2;
151 ais->SetVolume(new_volume);
152 #if defined(OS_LINUX)
153 current_volume = GetVolumeAfterSetVolumeOnLinux(ais, new_volume);
154 #else
155 current_volume = ais->GetVolume();
156 #endif
157 EXPECT_LT(current_volume, max_volume);
158 EXPECT_GT(current_volume, 0);
159 EXPECT_NEAR(current_volume, new_volume, 0.25 * max_volume);
161 // Restores the volume to the original value.
162 ais->SetVolume(original_volume);
163 current_volume = ais->GetVolume();
164 EXPECT_EQ(original_volume, current_volume);
166 ais->Close();
170 } // namespace media