Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / media / audio / cras / cras_input_unittest.cc
blob6c5a40cd4c6e2a7bb0021eae7174a44374da028f
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_METHOD4(OnData,
32 void(AudioInputStream*, const AudioBus*, 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 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 return CreateStream(layout, samples_per_packet,
69 AudioManagerBase::kDefaultDeviceId);
72 CrasInputStream* CreateStream(ChannelLayout layout,
73 int32 samples_per_packet,
74 const std::string& device_id) {
75 AudioParameters params(kTestFormat,
76 layout,
77 kTestSampleRate,
78 kTestBitsPerSample,
79 samples_per_packet);
80 return new CrasInputStream(params, mock_manager_.get(), device_id);
83 void CaptureSomeFrames(const AudioParameters &params,
84 unsigned int duration_ms) {
85 CrasInputStream* test_stream = new CrasInputStream(
86 params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
88 ASSERT_TRUE(test_stream->Open());
90 // Allow 8 frames variance for SRC in the callback. Different numbers of
91 // samples can be provided when doing non-integer SRC. For example
92 // converting from 192k to 44.1k is a ratio of 4.35 to 1.
93 MockAudioInputCallback mock_callback;
94 base::WaitableEvent event(false, false);
96 EXPECT_CALL(mock_callback, OnData(test_stream, _, _, _))
97 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal));
99 test_stream->Start(&mock_callback);
101 // Wait for samples to be captured.
102 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
104 test_stream->Stop();
105 test_stream->Close();
108 static const unsigned int kTestBitsPerSample;
109 static const unsigned int kTestCaptureDurationMs;
110 static const ChannelLayout kTestChannelLayout;
111 static const AudioParameters::Format kTestFormat;
112 static const uint32 kTestFramesPerPacket;
113 static const int kTestSampleRate;
115 scoped_ptr<StrictMock<MockAudioManagerCrasInput> > mock_manager_;
117 private:
118 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest);
121 const unsigned int CrasInputStreamTest::kTestBitsPerSample = 16;
122 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs = 250;
123 const ChannelLayout CrasInputStreamTest::kTestChannelLayout =
124 CHANNEL_LAYOUT_STEREO;
125 const AudioParameters::Format CrasInputStreamTest::kTestFormat =
126 AudioParameters::AUDIO_PCM_LINEAR;
127 const uint32 CrasInputStreamTest::kTestFramesPerPacket = 1000;
128 const int CrasInputStreamTest::kTestSampleRate = 44100;
130 TEST_F(CrasInputStreamTest, OpenMono) {
131 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
132 EXPECT_TRUE(test_stream->Open());
133 test_stream->Close();
136 TEST_F(CrasInputStreamTest, OpenStereo) {
137 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_STEREO);
138 EXPECT_TRUE(test_stream->Open());
139 test_stream->Close();
142 TEST_F(CrasInputStreamTest, BadBitsPerSample) {
143 AudioParameters bad_bps_params(kTestFormat,
144 kTestChannelLayout,
145 kTestSampleRate,
146 kTestBitsPerSample - 1,
147 kTestFramesPerPacket);
148 CrasInputStream* test_stream = new CrasInputStream(
149 bad_bps_params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
150 EXPECT_FALSE(test_stream->Open());
151 test_stream->Close();
154 TEST_F(CrasInputStreamTest, BadSampleRate) {
155 AudioParameters bad_rate_params(kTestFormat,
156 kTestChannelLayout,
158 kTestBitsPerSample,
159 kTestFramesPerPacket);
160 CrasInputStream* test_stream = new CrasInputStream(
161 bad_rate_params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
162 EXPECT_FALSE(test_stream->Open());
163 test_stream->Close();
166 TEST_F(CrasInputStreamTest, SetGetVolume) {
167 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
168 EXPECT_TRUE(test_stream->Open());
170 double max_volume = test_stream->GetMaxVolume();
171 EXPECT_GE(max_volume, 1.0);
173 test_stream->SetVolume(max_volume / 2);
175 double new_volume = test_stream->GetVolume();
177 EXPECT_GE(new_volume, 0.0);
178 EXPECT_LE(new_volume, max_volume);
180 test_stream->Close();
183 TEST_F(CrasInputStreamTest, CaptureFrames) {
184 const unsigned int rates[] =
185 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000};
187 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) {
188 SCOPED_TRACE(testing::Message() << "Mono " << rates[i] << "Hz");
189 AudioParameters params_mono(kTestFormat,
190 CHANNEL_LAYOUT_MONO,
191 rates[i],
192 kTestBitsPerSample,
193 kTestFramesPerPacket);
194 CaptureSomeFrames(params_mono, kTestCaptureDurationMs);
197 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) {
198 SCOPED_TRACE(testing::Message() << "Stereo " << rates[i] << "Hz");
199 AudioParameters params_stereo(kTestFormat,
200 CHANNEL_LAYOUT_STEREO,
201 rates[i],
202 kTestBitsPerSample,
203 kTestFramesPerPacket);
204 CaptureSomeFrames(params_stereo, kTestCaptureDurationMs);
208 TEST_F(CrasInputStreamTest, CaptureLoopback) {
209 CrasInputStream* test_stream = CreateStream(
210 CHANNEL_LAYOUT_STEREO,
211 kTestFramesPerPacket,
212 AudioManagerBase::kLoopbackInputDeviceId);
213 EXPECT_TRUE(test_stream->Open());
214 test_stream->Close();
217 } // namespace media