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 "base/basictypes.h"
7 #include "base/message_loop.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "base/test/test_timeouts.h"
10 #include "media/audio/audio_input_controller.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::AtLeast
;
16 using ::testing::Exactly
;
17 using ::testing::InvokeWithoutArgs
;
18 using ::testing::NotNull
;
22 static const int kSampleRate
= AudioParameters::kAudioCDSampleRate
;
23 static const int kBitsPerSample
= 16;
24 static const ChannelLayout kChannelLayout
= CHANNEL_LAYOUT_STEREO
;
25 static const int kSamplesPerPacket
= kSampleRate
/ 10;
27 // Posts MessageLoop::QuitClosure() on specified message loop.
28 ACTION_P(QuitMessageLoop
, loop_or_proxy
) {
29 loop_or_proxy
->PostTask(FROM_HERE
, MessageLoop::QuitClosure());
32 // Posts MessageLoop::QuitClosure() on specified message loop after a certain
33 // number of calls given by |limit|.
34 ACTION_P3(CheckCountAndPostQuitTask
, count
, limit
, loop_or_proxy
) {
35 if (++*count
>= limit
) {
36 loop_or_proxy
->PostTask(FROM_HERE
, MessageLoop::QuitClosure());
40 // Closes AudioOutputController synchronously.
41 static void CloseAudioController(AudioInputController
* controller
) {
42 controller
->Close(MessageLoop::QuitClosure());
43 MessageLoop::current()->Run();
46 class MockAudioInputControllerEventHandler
47 : public AudioInputController::EventHandler
{
49 MockAudioInputControllerEventHandler() {}
51 MOCK_METHOD1(OnCreated
, void(AudioInputController
* controller
));
52 MOCK_METHOD1(OnRecording
, void(AudioInputController
* controller
));
53 MOCK_METHOD1(OnError
, void(AudioInputController
* controller
));
54 MOCK_METHOD3(OnData
, void(AudioInputController
* controller
,
55 const uint8
* data
, uint32 size
));
58 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler
);
62 class AudioInputControllerTest
: public testing::Test
{
64 AudioInputControllerTest() {}
65 virtual ~AudioInputControllerTest() {}
68 MessageLoop message_loop_
;
71 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest
);
74 // Test AudioInputController for create and close without recording audio.
75 TEST_F(AudioInputControllerTest
, CreateAndClose
) {
76 MockAudioInputControllerEventHandler event_handler
;
78 // OnCreated() will be posted once.
79 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
80 .WillOnce(QuitMessageLoop(&message_loop_
));
82 scoped_ptr
<AudioManager
> audio_manager(AudioManager::Create());
83 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
84 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
);
85 scoped_refptr
<AudioInputController
> controller
=
86 AudioInputController::Create(audio_manager
.get(), &event_handler
, params
);
87 ASSERT_TRUE(controller
.get());
89 // Wait for OnCreated() to fire.
92 // Close the AudioInputController synchronously.
93 CloseAudioController(controller
);
96 // Test a normal call sequence of create, record and close.
97 TEST_F(AudioInputControllerTest
, RecordAndClose
) {
98 MockAudioInputControllerEventHandler event_handler
;
101 // OnCreated() will be called once.
102 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
105 // OnRecording() will be called only once.
106 EXPECT_CALL(event_handler
, OnRecording(NotNull()))
109 // OnData() shall be called ten times.
110 EXPECT_CALL(event_handler
, OnData(NotNull(), NotNull(), _
))
112 .WillRepeatedly(CheckCountAndPostQuitTask(&count
, 10,
113 message_loop_
.message_loop_proxy()));
115 scoped_ptr
<AudioManager
> audio_manager(AudioManager::Create());
116 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
117 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
);
119 // Creating the AudioInputController should render an OnCreated() call.
120 scoped_refptr
<AudioInputController
> controller
=
121 AudioInputController::Create(audio_manager
.get(), &event_handler
, params
);
122 ASSERT_TRUE(controller
.get());
124 // Start recording and trigger one OnRecording() call.
125 controller
->Record();
127 // Record and wait until ten OnData() callbacks are received.
130 // Close the AudioInputController synchronously.
131 CloseAudioController(controller
);
134 // Test that the AudioInputController reports an error when the input stream
135 // stops without an OnClose() callback. This can happen when the underlying
136 // audio layer stops feeding data as a result of a removed microphone device.
137 TEST_F(AudioInputControllerTest
, RecordAndError
) {
138 MockAudioInputControllerEventHandler event_handler
;
141 // OnCreated() will be called once.
142 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
145 // OnRecording() will be called only once.
146 EXPECT_CALL(event_handler
, OnRecording(NotNull()))
149 // OnData() shall be called ten times.
150 EXPECT_CALL(event_handler
, OnData(NotNull(), NotNull(), _
))
152 .WillRepeatedly(CheckCountAndPostQuitTask(&count
, 10,
153 message_loop_
.message_loop_proxy()));
155 // OnError() will be called after the data stream stops while the
156 // controller is in a recording state.
157 EXPECT_CALL(event_handler
, OnError(NotNull()))
159 .WillOnce(QuitMessageLoop(&message_loop_
));
161 scoped_ptr
<AudioManager
> audio_manager(AudioManager::Create());
162 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
163 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
);
165 // Creating the AudioInputController should render an OnCreated() call.
166 scoped_refptr
<AudioInputController
> controller
=
167 AudioInputController::Create(audio_manager
.get(), &event_handler
, params
);
168 ASSERT_TRUE(controller
.get());
170 // Start recording and trigger one OnRecording() call.
171 controller
->Record();
173 // Record and wait until ten OnData() callbacks are received.
176 // Stop the stream and verify that OnError() is posted.
177 AudioInputStream
* stream
= controller
->stream_for_testing();
181 // Close the AudioInputController synchronously.
182 CloseAudioController(controller
);
185 // Test that AudioInputController rejects insanely large packet sizes.
186 TEST_F(AudioInputControllerTest
, SamplesPerPacketTooLarge
) {
187 // Create an audio device with a very large packet size.
188 MockAudioInputControllerEventHandler event_handler
;
190 // OnCreated() shall not be called in this test.
191 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
194 scoped_ptr
<AudioManager
> audio_manager(AudioManager::Create());
195 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
196 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
* 1000);
197 scoped_refptr
<AudioInputController
> controller
=
198 AudioInputController::Create(audio_manager
.get(), &event_handler
, params
);
199 ASSERT_FALSE(controller
);
202 // Test calling AudioInputController::Close multiple times.
203 TEST_F(AudioInputControllerTest
, CloseTwice
) {
204 MockAudioInputControllerEventHandler event_handler
;
206 // OnRecording() will be called only once.
207 EXPECT_CALL(event_handler
, OnCreated(NotNull()));
209 // OnRecording() will be called only once.
210 EXPECT_CALL(event_handler
, OnRecording(NotNull()))
213 scoped_ptr
<AudioManager
> audio_manager(AudioManager::Create());
214 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
215 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
);
216 scoped_refptr
<AudioInputController
> controller
=
217 AudioInputController::Create(audio_manager
.get(), &event_handler
, params
);
218 ASSERT_TRUE(controller
.get());
220 controller
->Record();
222 controller
->Close(MessageLoop::QuitClosure());
223 MessageLoop::current()->Run();
225 controller
->Close(MessageLoop::QuitClosure());
226 MessageLoop::current()->Run();