Fix VPN dialog password field focus
[chromium-blink-merge.git] / media / audio / audio_power_monitor_unittest.cc
blob2b30578d142f3dc369c2f9e88931b621ac7e0d65
1 // Copyright 2013 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 "media/audio/audio_power_monitor.h"
7 #include <limits>
9 #include "base/time/time.h"
10 #include "media/base/audio_bus.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace media {
15 static const int kSampleRate = 48000;
16 static const int kFramesPerBuffer = 128;
18 static const int kTimeConstantMillis = 5;
20 namespace {
22 // Container for each parameterized test's data (input and expected results).
23 class TestScenario {
24 public:
25 TestScenario(const float* data, int num_channels, int num_frames,
26 float expected_power, bool expected_clipped)
27 : expected_power_(expected_power), expected_clipped_(expected_clipped) {
28 CreatePopulatedBuffer(data, num_channels, num_frames);
31 // Copy constructor and assignment operator for ::testing::Values(...).
32 TestScenario(const TestScenario& other) { *this = other; }
33 TestScenario& operator=(const TestScenario& other) {
34 this->expected_power_ = other.expected_power_;
35 this->expected_clipped_ = other.expected_clipped_;
36 this->bus_ = AudioBus::Create(other.bus_->channels(), other.bus_->frames());
37 other.bus_->CopyTo(this->bus_.get());
38 return *this;
41 // Returns this TestScenario, but with a bad sample value placed in the middle
42 // of channel 0.
43 TestScenario WithABadSample(float bad_value) const {
44 TestScenario result(*this);
45 result.bus_->channel(0)[result.bus_->frames() / 2] = bad_value;
46 return result;
49 const AudioBus& data() const {
50 return *bus_;
53 float expected_power() const {
54 return expected_power_;
57 bool expected_clipped() const {
58 return expected_clipped_;
61 private:
62 // Creates an AudioBus, sized and populated with kFramesPerBuffer frames of
63 // data. The given test |data| is repeated to fill the buffer.
64 void CreatePopulatedBuffer(
65 const float* data, int num_channels, int num_frames) {
66 bus_ = AudioBus::Create(num_channels, kFramesPerBuffer);
67 for (int ch = 0; ch < num_channels; ++ch) {
68 for (int frames = 0; frames < kFramesPerBuffer; frames += num_frames) {
69 const int num_to_copy = std::min(num_frames, kFramesPerBuffer - frames);
70 memcpy(bus_->channel(ch) + frames, data + num_frames * ch,
71 sizeof(float) * num_to_copy);
76 float expected_power_;
77 bool expected_clipped_;
78 scoped_ptr<AudioBus> bus_;
81 // Value printer for TestScenario. Required to prevent Valgrind "access to
82 // uninitialized memory" errors (http://crbug.com/263315).
83 ::std::ostream& operator<<(::std::ostream& os, const TestScenario& ts) {
84 return os << "{" << ts.data().channels() << "-channel signal} --> {"
85 << ts.expected_power() << " dBFS, "
86 << (ts.expected_clipped() ? "clipped" : "not clipped")
87 << "}";
90 // An observer that receives power measurements. Each power measurement should
91 // should make progress towards the goal value.
92 class MeasurementObserver {
93 public:
94 explicit MeasurementObserver(float goal_power_measurement)
95 : goal_power_measurement_(goal_power_measurement),
96 measurement_count_(0),
97 last_power_measurement_(AudioPowerMonitor::zero_power()),
98 last_clipped_(false) {}
100 int measurement_count() const {
101 return measurement_count_;
104 float last_power_measurement() const {
105 return last_power_measurement_;
108 bool last_clipped() const {
109 return last_clipped_;
112 void OnPowerMeasured(float cur_power_measurement, bool clipped) {
113 if (measurement_count_ == 0) {
114 measurements_should_increase_ =
115 (cur_power_measurement < goal_power_measurement_);
116 } else {
117 SCOPED_TRACE(::testing::Message()
118 << "Power: goal=" << goal_power_measurement_
119 << "; last=" << last_power_measurement_
120 << "; cur=" << cur_power_measurement);
122 if (last_power_measurement_ != goal_power_measurement_) {
123 if (measurements_should_increase_) {
124 EXPECT_LE(last_power_measurement_, cur_power_measurement)
125 << "Measurements should be monotonically increasing.";
126 } else {
127 EXPECT_GE(last_power_measurement_, cur_power_measurement)
128 << "Measurements should be monotonically decreasing.";
130 } else {
131 EXPECT_EQ(last_power_measurement_, cur_power_measurement)
132 << "Measurements are numerically unstable at goal value.";
136 last_power_measurement_ = cur_power_measurement;
137 last_clipped_ = clipped;
138 ++measurement_count_;
141 private:
142 const float goal_power_measurement_;
143 int measurement_count_;
144 bool measurements_should_increase_;
145 float last_power_measurement_;
146 bool last_clipped_;
148 DISALLOW_COPY_AND_ASSIGN(MeasurementObserver);
151 } // namespace
153 class AudioPowerMonitorTest : public ::testing::TestWithParam<TestScenario> {
154 public:
155 AudioPowerMonitorTest()
156 : power_monitor_(kSampleRate,
157 base::TimeDelta::FromMilliseconds(kTimeConstantMillis)) {
160 void FeedAndCheckExpectedPowerIsMeasured(
161 const AudioBus& bus, float power, bool clipped) {
162 // Feed the AudioPowerMonitor, read measurements from it, and record them in
163 // MeasurementObserver.
164 static const int kNumFeedIters = 100;
165 MeasurementObserver observer(power);
166 for (int i = 0; i < kNumFeedIters; ++i) {
167 power_monitor_.Scan(bus, bus.frames());
168 const std::pair<float, bool>& reading =
169 power_monitor_.ReadCurrentPowerAndClip();
170 observer.OnPowerMeasured(reading.first, reading.second);
173 // Check that the results recorded by the observer are the same whole-number
174 // dBFS.
175 EXPECT_EQ(static_cast<int>(power),
176 static_cast<int>(observer.last_power_measurement()));
177 EXPECT_EQ(clipped, observer.last_clipped());
180 private:
181 AudioPowerMonitor power_monitor_;
183 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitorTest);
186 TEST_P(AudioPowerMonitorTest, MeasuresPowerOfSignal) {
187 const TestScenario& scenario = GetParam();
189 scoped_ptr<AudioBus> zeroed_bus =
190 AudioBus::Create(scenario.data().channels(), scenario.data().frames());
191 zeroed_bus->Zero();
193 // Send a "zero power" audio signal, then this scenario's audio signal, then
194 // the "zero power" audio signal again; testing that the power monitor
195 // measurements match expected values.
196 FeedAndCheckExpectedPowerIsMeasured(
197 *zeroed_bus, AudioPowerMonitor::zero_power(), false);
198 FeedAndCheckExpectedPowerIsMeasured(
199 scenario.data(), scenario.expected_power(), scenario.expected_clipped());
200 FeedAndCheckExpectedPowerIsMeasured(
201 *zeroed_bus, AudioPowerMonitor::zero_power(), false);
204 static const float kMonoSilentNoise[] = {
205 0.01f, -0.01f
208 static const float kMonoMaxAmplitude[] = {
209 1.0f
212 static const float kMonoMaxAmplitude2[] = {
213 -1.0f, 1.0f
216 static const float kMonoHalfMaxAmplitude[] = {
217 0.5f, -0.5f, 0.5f, -0.5f
220 static const float kMonoAmplitudeClipped[] = {
221 2.0f, -2.0f
224 static const float kMonoMaxAmplitudeWithClip[] = {
225 2.0f, 0.0, 0.0f, 0.0f
228 static const float kMonoMaxAmplitudeWithClip2[] = {
229 4.0f, 0.0, 0.0f, 0.0f
232 static const float kStereoSilentNoise[] = {
233 // left channel
234 0.005f, -0.005f,
235 // right channel
236 0.005f, -0.005f
239 static const float kStereoMaxAmplitude[] = {
240 // left channel
241 1.0f, -1.0f,
242 // right channel
243 -1.0f, 1.0f
246 static const float kRightChannelMaxAmplitude[] = {
247 // left channel
248 0.0f, 0.0f, 0.0f, 0.0f,
249 // right channel
250 -1.0f, 1.0f, -1.0f, 1.0f
253 static const float kLeftChannelHalfMaxAmplitude[] = {
254 // left channel
255 0.5f, -0.5f, 0.5f, -0.5f,
256 // right channel
257 0.0f, 0.0f, 0.0f, 0.0f,
260 static const float kStereoMixed[] = {
261 // left channel
262 0.5f, -0.5f, 0.5f, -0.5f,
263 // right channel
264 -1.0f, 1.0f, -1.0f, 1.0f
267 static const float kStereoMixed2[] = {
268 // left channel
269 1.0f, -1.0f, 0.75f, -0.75f, 0.5f, -0.5f, 0.25f, -0.25f,
270 // right channel
271 0.25f, -0.25f, 0.5f, -0.5f, 0.75f, -0.75f, 1.0f, -1.0f
274 INSTANTIATE_TEST_CASE_P(
275 Scenarios, AudioPowerMonitorTest,
276 ::testing::Values(
277 TestScenario(kMonoSilentNoise, 1, 2, -40, false),
278 TestScenario(kMonoMaxAmplitude, 1, 1,
279 AudioPowerMonitor::max_power(), false),
280 TestScenario(kMonoMaxAmplitude2, 1, 2,
281 AudioPowerMonitor::max_power(), false),
282 TestScenario(kMonoHalfMaxAmplitude, 1, 4, -6, false),
283 TestScenario(kMonoAmplitudeClipped, 1, 2,
284 AudioPowerMonitor::max_power(), true),
285 TestScenario(kMonoMaxAmplitudeWithClip, 1, 4,
286 AudioPowerMonitor::max_power(), true),
287 TestScenario(kMonoMaxAmplitudeWithClip2, 1, 4,
288 AudioPowerMonitor::max_power(), true),
289 TestScenario(kMonoSilentNoise, 1, 2,
290 AudioPowerMonitor::zero_power(), false).
291 WithABadSample(std::numeric_limits<float>::infinity()),
292 TestScenario(kMonoHalfMaxAmplitude, 1, 4,
293 AudioPowerMonitor::zero_power(), false).
294 WithABadSample(std::numeric_limits<float>::quiet_NaN()),
295 TestScenario(kStereoSilentNoise, 2, 2, -46, false),
296 TestScenario(kStereoMaxAmplitude, 2, 2,
297 AudioPowerMonitor::max_power(), false),
298 TestScenario(kRightChannelMaxAmplitude, 2, 4, -3, false),
299 TestScenario(kLeftChannelHalfMaxAmplitude, 2, 4, -9, false),
300 TestScenario(kStereoMixed, 2, 4, -2, false),
301 TestScenario(kStereoMixed2, 2, 8, -3, false)));
303 } // namespace media