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.
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 "media/audio/mock_audio_source_callback.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 // cras_util.h defines custom min/max macros which break compilation, so ensure
17 // it's not included until last. #if avoids presubmit errors.
19 #include "media/audio/cras/cras_unified.h"
24 using testing::InvokeWithoutArgs
;
25 using testing::Return
;
26 using testing::SetArgumentPointee
;
27 using testing::StrictMock
;
31 class MockAudioManagerCras
: public AudioManagerCras
{
33 MockAudioManagerCras() : AudioManagerCras(&fake_audio_log_factory_
) {}
35 MOCK_METHOD0(Init
, void());
36 MOCK_METHOD0(HasAudioOutputDevices
, bool());
37 MOCK_METHOD0(HasAudioInputDevices
, bool());
38 MOCK_METHOD1(MakeLinearOutputStream
, AudioOutputStream
*(
39 const AudioParameters
& params
));
40 MOCK_METHOD2(MakeLowLatencyOutputStream
,
41 AudioOutputStream
*(const AudioParameters
& params
,
42 const std::string
& device_id
));
43 MOCK_METHOD2(MakeLinearOutputStream
, AudioInputStream
*(
44 const AudioParameters
& params
, const std::string
& device_id
));
45 MOCK_METHOD2(MakeLowLatencyInputStream
, AudioInputStream
*(
46 const AudioParameters
& params
, const std::string
& device_id
));
48 // We need to override this function in order to skip the checking the number
49 // of active output streams. It is because the number of active streams
50 // is managed inside MakeAudioOutputStream, and we don't use
51 // MakeAudioOutputStream to create the stream in the tests.
52 void ReleaseOutputStream(AudioOutputStream
* stream
) override
{
58 FakeAudioLogFactory fake_audio_log_factory_
;
61 class CrasUnifiedStreamTest
: public testing::Test
{
63 CrasUnifiedStreamTest() {
64 mock_manager_
.reset(new StrictMock
<MockAudioManagerCras
>());
67 virtual ~CrasUnifiedStreamTest() {
70 CrasUnifiedStream
* CreateStream(ChannelLayout layout
) {
71 return CreateStream(layout
, kTestFramesPerPacket
);
74 CrasUnifiedStream
* CreateStream(ChannelLayout layout
,
75 int32 samples_per_packet
) {
76 AudioParameters
params(kTestFormat
, layout
, kTestSampleRate
,
77 kTestBitsPerSample
, samples_per_packet
);
78 return new CrasUnifiedStream(params
, mock_manager_
.get());
81 MockAudioManagerCras
& mock_manager() {
82 return *(mock_manager_
.get());
85 static const ChannelLayout kTestChannelLayout
;
86 static const int kTestSampleRate
;
87 static const int kTestBitsPerSample
;
88 static const AudioParameters::Format kTestFormat
;
89 static const uint32 kTestFramesPerPacket
;
91 scoped_ptr
<StrictMock
<MockAudioManagerCras
> > mock_manager_
;
94 DISALLOW_COPY_AND_ASSIGN(CrasUnifiedStreamTest
);
97 const ChannelLayout
CrasUnifiedStreamTest::kTestChannelLayout
=
98 CHANNEL_LAYOUT_STEREO
;
99 const int CrasUnifiedStreamTest::kTestSampleRate
=
100 AudioParameters::kAudioCDSampleRate
;
101 const int CrasUnifiedStreamTest::kTestBitsPerSample
= 16;
102 const AudioParameters::Format
CrasUnifiedStreamTest::kTestFormat
=
103 AudioParameters::AUDIO_PCM_LINEAR
;
104 const uint32
CrasUnifiedStreamTest::kTestFramesPerPacket
= 1000;
106 TEST_F(CrasUnifiedStreamTest
, ConstructedState
) {
107 CrasUnifiedStream
* test_stream
= CreateStream(kTestChannelLayout
);
108 EXPECT_TRUE(test_stream
->Open());
109 test_stream
->Close();
111 // Should support mono.
112 test_stream
= CreateStream(CHANNEL_LAYOUT_MONO
);
113 EXPECT_TRUE(test_stream
->Open());
114 test_stream
->Close();
116 // Should support multi-channel.
117 test_stream
= CreateStream(CHANNEL_LAYOUT_SURROUND
);
118 EXPECT_TRUE(test_stream
->Open());
119 test_stream
->Close();
121 // Bad bits per sample.
122 AudioParameters
bad_bps_params(kTestFormat
, kTestChannelLayout
,
123 kTestSampleRate
, kTestBitsPerSample
- 1,
124 kTestFramesPerPacket
);
125 test_stream
= new CrasUnifiedStream(bad_bps_params
, mock_manager_
.get());
126 EXPECT_FALSE(test_stream
->Open());
127 test_stream
->Close();
130 AudioParameters
bad_rate_params(kTestFormat
, kTestChannelLayout
,
131 0, kTestBitsPerSample
, kTestFramesPerPacket
);
132 test_stream
= new CrasUnifiedStream(bad_rate_params
, mock_manager_
.get());
133 EXPECT_FALSE(test_stream
->Open());
134 test_stream
->Close();
137 TEST_F(CrasUnifiedStreamTest
, RenderFrames
) {
138 CrasUnifiedStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_MONO
);
139 MockAudioSourceCallback mock_callback
;
141 ASSERT_TRUE(test_stream
->Open());
143 base::WaitableEvent
event(false, false);
145 EXPECT_CALL(mock_callback
, OnMoreData(_
, _
))
146 .WillRepeatedly(DoAll(
147 InvokeWithoutArgs(&event
, &base::WaitableEvent::Signal
),
148 Return(kTestFramesPerPacket
)));
150 test_stream
->Start(&mock_callback
);
152 // Wait for samples to be captured.
153 EXPECT_TRUE(event
.TimedWait(TestTimeouts::action_timeout()));
157 test_stream
->Close();