Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / media / audio / cras / cras_input_unittest.cc
blob77f951cbb28c464277839d61df57b06c91c208d4
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 <string>
7 #include "base/synchronization/waitable_event.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/time/time.h"
10 #include "media/audio/cras/audio_manager_cras.h"
11 #include "media/audio/fake_audio_log_factory.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 // cras_util.h defines custom min/max macros which break compilation, so ensure
16 // it's not included until last. #if avoids presubmit errors.
17 #if defined(USE_CRAS)
18 #include "media/audio/cras/cras_input.h"
19 #endif
21 using testing::_;
22 using testing::AtLeast;
23 using testing::Ge;
24 using testing::InvokeWithoutArgs;
25 using testing::StrictMock;
27 namespace media {
29 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
30 public:
31 MOCK_METHOD5(OnData, void(
32 AudioInputStream*, const uint8*, uint32, uint32, double));
33 MOCK_METHOD1(OnError, void(AudioInputStream*));
36 class MockAudioManagerCrasInput : public AudioManagerCras {
37 public:
38 MockAudioManagerCrasInput() : AudioManagerCras(&fake_audio_log_factory_) {}
40 // We need to override this function in order to skip checking the number
41 // of active output streams. It is because the number of active streams
42 // is managed inside MakeAudioInputStream, and we don't use
43 // MakeAudioInputStream to create the stream in the tests.
44 virtual void ReleaseInputStream(AudioInputStream* stream) OVERRIDE {
45 DCHECK(stream);
46 delete stream;
49 private:
50 FakeAudioLogFactory fake_audio_log_factory_;
53 class CrasInputStreamTest : public testing::Test {
54 protected:
55 CrasInputStreamTest() {
56 mock_manager_.reset(new StrictMock<MockAudioManagerCrasInput>());
59 virtual ~CrasInputStreamTest() {
62 CrasInputStream* CreateStream(ChannelLayout layout) {
63 return CreateStream(layout, kTestFramesPerPacket);
66 CrasInputStream* CreateStream(ChannelLayout layout,
67 int32 samples_per_packet) {
68 AudioParameters params(kTestFormat,
69 layout,
70 kTestSampleRate,
71 kTestBitsPerSample,
72 samples_per_packet);
73 return new CrasInputStream(params, mock_manager_.get(),
74 AudioManagerBase::kDefaultDeviceId);
77 void CaptureSomeFrames(const AudioParameters &params,
78 unsigned int duration_ms) {
79 CrasInputStream* test_stream = new CrasInputStream(
80 params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
82 ASSERT_TRUE(test_stream->Open());
84 // Allow 8 frames variance for SRC in the callback. Different numbers of
85 // samples can be provided when doing non-integer SRC. For example
86 // converting from 192k to 44.1k is a ratio of 4.35 to 1.
87 MockAudioInputCallback mock_callback;
88 unsigned int expected_size = (kTestFramesPerPacket - 8) *
89 params.channels() *
90 params.bits_per_sample() / 8;
92 base::WaitableEvent event(false, false);
94 EXPECT_CALL(mock_callback,
95 OnData(test_stream, _, Ge(expected_size), _, _))
96 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal));
98 test_stream->Start(&mock_callback);
100 // Wait for samples to be captured.
101 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
103 test_stream->Stop();
104 test_stream->Close();
107 static const unsigned int kTestBitsPerSample;
108 static const unsigned int kTestCaptureDurationMs;
109 static const ChannelLayout kTestChannelLayout;
110 static const AudioParameters::Format kTestFormat;
111 static const uint32 kTestFramesPerPacket;
112 static const int kTestSampleRate;
114 scoped_ptr<StrictMock<MockAudioManagerCrasInput> > mock_manager_;
116 private:
117 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest);
120 const unsigned int CrasInputStreamTest::kTestBitsPerSample = 16;
121 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs = 250;
122 const ChannelLayout CrasInputStreamTest::kTestChannelLayout =
123 CHANNEL_LAYOUT_STEREO;
124 const AudioParameters::Format CrasInputStreamTest::kTestFormat =
125 AudioParameters::AUDIO_PCM_LINEAR;
126 const uint32 CrasInputStreamTest::kTestFramesPerPacket = 1000;
127 const int CrasInputStreamTest::kTestSampleRate = 44100;
129 TEST_F(CrasInputStreamTest, OpenMono) {
130 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
131 EXPECT_TRUE(test_stream->Open());
132 test_stream->Close();
135 TEST_F(CrasInputStreamTest, OpenStereo) {
136 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_STEREO);
137 EXPECT_TRUE(test_stream->Open());
138 test_stream->Close();
141 TEST_F(CrasInputStreamTest, BadBitsPerSample) {
142 AudioParameters bad_bps_params(kTestFormat,
143 kTestChannelLayout,
144 kTestSampleRate,
145 kTestBitsPerSample - 1,
146 kTestFramesPerPacket);
147 CrasInputStream* test_stream = new CrasInputStream(
148 bad_bps_params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
149 EXPECT_FALSE(test_stream->Open());
150 test_stream->Close();
153 TEST_F(CrasInputStreamTest, BadFormat) {
154 AudioParameters bad_format_params(AudioParameters::AUDIO_LAST_FORMAT,
155 kTestChannelLayout,
156 kTestSampleRate,
157 kTestBitsPerSample,
158 kTestFramesPerPacket);
159 CrasInputStream* test_stream = new CrasInputStream(
160 bad_format_params, mock_manager_.get(),
161 AudioManagerBase::kDefaultDeviceId);
162 EXPECT_FALSE(test_stream->Open());
163 test_stream->Close();
166 TEST_F(CrasInputStreamTest, BadSampleRate) {
167 AudioParameters bad_rate_params(kTestFormat,
168 kTestChannelLayout,
170 kTestBitsPerSample,
171 kTestFramesPerPacket);
172 CrasInputStream* test_stream = new CrasInputStream(
173 bad_rate_params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
174 EXPECT_FALSE(test_stream->Open());
175 test_stream->Close();
178 TEST_F(CrasInputStreamTest, SetGetVolume) {
179 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
180 EXPECT_TRUE(test_stream->Open());
182 double max_volume = test_stream->GetMaxVolume();
183 EXPECT_GE(max_volume, 1.0);
185 test_stream->SetVolume(max_volume / 2);
187 double new_volume = test_stream->GetVolume();
189 EXPECT_GE(new_volume, 0.0);
190 EXPECT_LE(new_volume, max_volume);
192 test_stream->Close();
195 TEST_F(CrasInputStreamTest, CaptureFrames) {
196 const unsigned int rates[] =
197 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000};
199 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) {
200 SCOPED_TRACE(testing::Message() << "Mono " << rates[i] << "Hz");
201 AudioParameters params_mono(kTestFormat,
202 CHANNEL_LAYOUT_MONO,
203 rates[i],
204 kTestBitsPerSample,
205 kTestFramesPerPacket);
206 CaptureSomeFrames(params_mono, kTestCaptureDurationMs);
209 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) {
210 SCOPED_TRACE(testing::Message() << "Stereo " << rates[i] << "Hz");
211 AudioParameters params_stereo(kTestFormat,
212 CHANNEL_LAYOUT_STEREO,
213 rates[i],
214 kTestBitsPerSample,
215 kTestFramesPerPacket);
216 CaptureSomeFrames(params_stereo, kTestCaptureDurationMs);
220 } // namespace media