cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / media / audio / mac / audio_auhal_mac_unittest.cc
blobb4cf8c64cc65d2e8549ce156cc7ba00920028c26
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 "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "media/audio/audio_io.h"
8 #include "media/audio/audio_manager.h"
9 #include "media/audio/simple_sources.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using ::testing::_;
14 using ::testing::AnyNumber;
15 using ::testing::DoAll;
16 using ::testing::Field;
17 using ::testing::InSequence;
18 using ::testing::Invoke;
19 using ::testing::NiceMock;
20 using ::testing::NotNull;
21 using ::testing::Return;
23 static const int kBitsPerSample = 16;
25 // TODO(crogers): Most of these tests can be made platform agnostic.
26 // http://crbug.com/223242
28 namespace media {
30 class MockAudioSourceCallback : public AudioOutputStream::AudioSourceCallback {
31 public:
32 MOCK_METHOD2(OnMoreData, int(AudioBus* audio_bus,
33 AudioBuffersState buffers_state));
34 MOCK_METHOD3(OnMoreIOData, int(AudioBus* source,
35 AudioBus* dest,
36 AudioBuffersState buffers_state));
37 MOCK_METHOD1(OnError, void(AudioOutputStream* stream));
40 // Convenience method which creates a default AudioOutputStream object but
41 // also allows the user to modify the default settings.
42 class AudioOutputStreamWrapper {
43 public:
44 explicit AudioOutputStreamWrapper()
45 : audio_man_(AudioManager::Create()),
46 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY),
47 bits_per_sample_(kBitsPerSample) {
48 AudioParameters preferred_params =
49 audio_man_->GetDefaultOutputStreamParameters();
50 channel_layout_ = preferred_params.channel_layout();
51 channels_ = preferred_params.channels();
52 sample_rate_ = preferred_params.sample_rate();
53 samples_per_packet_ = preferred_params.frames_per_buffer();
56 ~AudioOutputStreamWrapper() {}
58 // Creates AudioOutputStream object using default parameters.
59 AudioOutputStream* Create() {
60 return CreateOutputStream();
63 // Creates AudioOutputStream object using non-default parameters where the
64 // frame size is modified.
65 AudioOutputStream* Create(int samples_per_packet) {
66 samples_per_packet_ = samples_per_packet;
67 return CreateOutputStream();
70 // Creates AudioOutputStream object using non-default parameters where the
71 // sample rate is modified.
72 AudioOutputStream* CreateWithSampleRate(int sample_rate) {
73 sample_rate_ = sample_rate;
74 return CreateOutputStream();
77 // Creates AudioOutputStream object using non-default parameters where the
78 // channel layout is modified.
79 AudioOutputStream* CreateWithLayout(ChannelLayout layout) {
80 channel_layout_ = layout;
81 channels_ = ChannelLayoutToChannelCount(layout);
82 return CreateOutputStream();
85 AudioParameters::Format format() const { return format_; }
86 int channels() const { return ChannelLayoutToChannelCount(channel_layout_); }
87 int bits_per_sample() const { return bits_per_sample_; }
88 int sample_rate() const { return sample_rate_; }
89 int samples_per_packet() const { return samples_per_packet_; }
91 bool CanRunAudioTests() {
92 return audio_man_->HasAudioOutputDevices();
95 private:
96 AudioOutputStream* CreateOutputStream() {
97 AudioParameters params;
98 params.Reset(format_, channel_layout_,
99 channels_, 0,
100 sample_rate_, bits_per_sample_,
101 samples_per_packet_);
103 AudioOutputStream* aos = audio_man_->MakeAudioOutputStream(params,
104 std::string());
105 EXPECT_TRUE(aos);
106 return aos;
109 scoped_ptr<AudioManager> audio_man_;
111 AudioParameters::Format format_;
112 ChannelLayout channel_layout_;
113 int channels_;
114 int bits_per_sample_;
115 int sample_rate_;
116 int samples_per_packet_;
119 // Test that we can get the hardware sample-rate.
120 TEST(AUHALStreamTest, HardwareSampleRate) {
121 AudioOutputStreamWrapper aosw;
122 if (!aosw.CanRunAudioTests())
123 return;
125 int sample_rate = aosw.sample_rate();
126 EXPECT_GE(sample_rate, 16000);
127 EXPECT_LE(sample_rate, 192000);
130 // Test Create(), Close() calling sequence.
131 TEST(AUHALStreamTest, CreateAndClose) {
132 AudioOutputStreamWrapper aosw;
133 if (!aosw.CanRunAudioTests())
134 return;
136 AudioOutputStream* aos = aosw.Create();
137 aos->Close();
140 // Test Open(), Close() calling sequence.
141 TEST(AUHALStreamTest, OpenAndClose) {
142 AudioOutputStreamWrapper aosw;
143 if (!aosw.CanRunAudioTests())
144 return;
146 AudioOutputStream* aos = aosw.Create();
147 EXPECT_TRUE(aos->Open());
148 aos->Close();
151 // Test Open(), Start(), Close() calling sequence.
152 TEST(AUHALStreamTest, OpenStartAndClose) {
153 AudioOutputStreamWrapper aosw;
154 if (!aosw.CanRunAudioTests())
155 return;
157 AudioOutputStream* aos = aosw.Create();
158 EXPECT_TRUE(aos->Open());
159 MockAudioSourceCallback source;
160 EXPECT_CALL(source, OnError(aos))
161 .Times(0);
162 aos->Start(&source);
163 aos->Close();
166 // Test Open(), Start(), Stop(), Close() calling sequence.
167 TEST(AUHALStreamTest, OpenStartStopAndClose) {
168 AudioOutputStreamWrapper aosw;
169 if (!aosw.CanRunAudioTests())
170 return;
172 AudioOutputStream* aos = aosw.Create();
173 EXPECT_TRUE(aos->Open());
174 MockAudioSourceCallback source;
175 EXPECT_CALL(source, OnError(aos))
176 .Times(0);
177 aos->Start(&source);
178 aos->Stop();
179 aos->Close();
182 // This test produces actual audio for 0.5 seconds on the default audio device
183 // at the hardware sample-rate (usually 44.1KHz).
184 // Parameters have been chosen carefully so you should not hear
185 // pops or noises while the sound is playing.
186 TEST(AUHALStreamTest, AUHALStreamPlay200HzTone) {
187 AudioOutputStreamWrapper aosw;
188 if (!aosw.CanRunAudioTests())
189 return;
191 AudioOutputStream* aos = aosw.CreateWithLayout(CHANNEL_LAYOUT_MONO);
193 EXPECT_TRUE(aos->Open());
195 SineWaveAudioSource source(1, 200.0, aosw.sample_rate());
196 aos->Start(&source);
197 usleep(500000);
199 aos->Stop();
200 aos->Close();
203 // Test that Open() will fail with a sample-rate which isn't the hardware
204 // sample-rate.
205 TEST(AUHALStreamTest, AUHALStreamInvalidSampleRate) {
206 AudioOutputStreamWrapper aosw;
207 if (!aosw.CanRunAudioTests())
208 return;
210 int non_default_sample_rate = aosw.sample_rate() == 44100 ?
211 48000 : 44100;
212 AudioOutputStream* aos = aosw.CreateWithSampleRate(non_default_sample_rate);
214 EXPECT_FALSE(aos->Open());
216 aos->Close();
219 } // namespace media