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/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 "media/audio/audio_manager_base.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 using ::testing::AtLeast
;
17 using ::testing::Exactly
;
18 using ::testing::InvokeWithoutArgs
;
19 using ::testing::NotNull
;
23 static const int kSampleRate
= AudioParameters::kAudioCDSampleRate
;
24 static const int kBitsPerSample
= 16;
25 static const ChannelLayout kChannelLayout
= CHANNEL_LAYOUT_STEREO
;
26 static const int kSamplesPerPacket
= kSampleRate
/ 10;
28 // Posts base::MessageLoop::QuitClosure() on specified message loop.
29 ACTION_P(QuitMessageLoop
, loop_or_proxy
) {
30 loop_or_proxy
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
33 // Posts base::MessageLoop::QuitClosure() on specified message loop after a
34 // certain number of calls given by |limit|.
35 ACTION_P3(CheckCountAndPostQuitTask
, count
, limit
, loop_or_proxy
) {
36 if (++*count
>= limit
) {
37 loop_or_proxy
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
41 // Closes AudioOutputController synchronously.
42 static void CloseAudioController(AudioInputController
* controller
) {
43 controller
->Close(base::MessageLoop::QuitClosure());
44 base::MessageLoop::current()->Run();
47 class MockAudioInputControllerEventHandler
48 : public AudioInputController::EventHandler
{
50 MockAudioInputControllerEventHandler() {}
52 MOCK_METHOD1(OnCreated
, void(AudioInputController
* controller
));
53 MOCK_METHOD1(OnRecording
, void(AudioInputController
* controller
));
54 MOCK_METHOD2(OnError
, void(AudioInputController
* controller
,
55 AudioInputController::ErrorCode error_code
));
57 void(AudioInputController
* controller
, const AudioBus
* data
));
59 void(AudioInputController
* controller
,
60 const std::string
& message
));
63 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler
);
67 class AudioInputControllerTest
: public testing::Test
{
69 AudioInputControllerTest() {}
70 ~AudioInputControllerTest() override
{}
73 base::MessageLoop message_loop_
;
76 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest
);
79 // Test AudioInputController for create and close without recording audio.
80 TEST_F(AudioInputControllerTest
, CreateAndClose
) {
81 MockAudioInputControllerEventHandler event_handler
;
83 // OnCreated() will be posted once.
84 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
85 .WillOnce(QuitMessageLoop(&message_loop_
));
87 scoped_ptr
<AudioManager
> audio_manager(AudioManager::CreateForTesting());
88 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
89 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
);
91 scoped_refptr
<AudioInputController
> controller
=
92 AudioInputController::Create(audio_manager
.get(),
95 AudioManagerBase::kDefaultDeviceId
,
97 ASSERT_TRUE(controller
.get());
99 // Wait for OnCreated() to fire.
102 // Close the AudioInputController synchronously.
103 CloseAudioController(controller
.get());
106 // Test a normal call sequence of create, record and close.
107 TEST_F(AudioInputControllerTest
, RecordAndClose
) {
108 MockAudioInputControllerEventHandler event_handler
;
111 // OnCreated() will be called once.
112 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
115 // OnRecording() will be called only once.
116 EXPECT_CALL(event_handler
, OnRecording(NotNull()))
119 // OnData() shall be called ten times.
120 EXPECT_CALL(event_handler
, OnData(NotNull(), NotNull()))
122 .WillRepeatedly(CheckCountAndPostQuitTask(
123 &count
, 10, message_loop_
.message_loop_proxy()));
125 scoped_ptr
<AudioManager
> audio_manager(AudioManager::CreateForTesting());
126 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
127 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
);
129 // Creating the AudioInputController should render an OnCreated() call.
130 scoped_refptr
<AudioInputController
> controller
=
131 AudioInputController::Create(audio_manager
.get(),
134 AudioManagerBase::kDefaultDeviceId
,
136 ASSERT_TRUE(controller
.get());
138 // Start recording and trigger one OnRecording() call.
139 controller
->Record();
141 // Record and wait until ten OnData() callbacks are received.
144 // Close the AudioInputController synchronously.
145 CloseAudioController(controller
.get());
148 // Test that the AudioInputController reports an error when the input stream
149 // stops. This can happen when the underlying audio layer stops feeding data as
150 // a result of a removed microphone device.
151 // Disabled due to crbug.com/357569 and crbug.com/357501.
152 // TODO(henrika): Remove the test when the timer workaround has been removed.
153 TEST_F(AudioInputControllerTest
, DISABLED_RecordAndError
) {
154 MockAudioInputControllerEventHandler event_handler
;
157 // OnCreated() will be called once.
158 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
161 // OnRecording() will be called only once.
162 EXPECT_CALL(event_handler
, OnRecording(NotNull()))
165 // OnData() shall be called ten times.
166 EXPECT_CALL(event_handler
, OnData(NotNull(), NotNull()))
168 .WillRepeatedly(CheckCountAndPostQuitTask(
169 &count
, 10, message_loop_
.message_loop_proxy()));
171 // OnError() will be called after the data stream stops while the
172 // controller is in a recording state.
173 EXPECT_CALL(event_handler
, OnError(NotNull(),
174 AudioInputController::NO_DATA_ERROR
))
176 .WillOnce(QuitMessageLoop(&message_loop_
));
178 scoped_ptr
<AudioManager
> audio_manager(AudioManager::CreateForTesting());
179 AudioParameters
params(AudioParameters::AUDIO_FAKE
, kChannelLayout
,
180 kSampleRate
, kBitsPerSample
, kSamplesPerPacket
);
182 // Creating the AudioInputController should render an OnCreated() call.
183 scoped_refptr
<AudioInputController
> controller
=
184 AudioInputController::Create(audio_manager
.get(),
187 AudioManagerBase::kDefaultDeviceId
,
189 ASSERT_TRUE(controller
.get());
191 // Start recording and trigger one OnRecording() call.
192 controller
->Record();
194 // Record and wait until ten OnData() callbacks are received.
197 // Stop the stream and verify that OnError() is posted.
198 AudioInputStream
* stream
= controller
->stream_for_testing();
202 // Close the AudioInputController synchronously.
203 CloseAudioController(controller
.get());
206 // Test that AudioInputController rejects insanely large packet sizes.
207 TEST_F(AudioInputControllerTest
, SamplesPerPacketTooLarge
) {
208 // Create an audio device with a very large packet size.
209 MockAudioInputControllerEventHandler event_handler
;
211 // OnCreated() shall not be called in this test.
212 EXPECT_CALL(event_handler
, OnCreated(NotNull()))
215 scoped_ptr
<AudioManager
> audio_manager(AudioManager::CreateForTesting());
216 AudioParameters
params(AudioParameters::AUDIO_FAKE
,
220 kSamplesPerPacket
* 1000);
221 scoped_refptr
<AudioInputController
> controller
=
222 AudioInputController::Create(audio_manager
.get(),
225 AudioManagerBase::kDefaultDeviceId
,
227 ASSERT_FALSE(controller
.get());
230 // Test calling AudioInputController::Close multiple times.
231 TEST_F(AudioInputControllerTest
, CloseTwice
) {
232 MockAudioInputControllerEventHandler event_handler
;
234 // OnRecording() will be called only once.
235 EXPECT_CALL(event_handler
, OnCreated(NotNull()));
237 // OnRecording() will be called only once.
238 EXPECT_CALL(event_handler
, OnRecording(NotNull()))
241 scoped_ptr
<AudioManager
> audio_manager(AudioManager::CreateForTesting());
242 AudioParameters
params(AudioParameters::AUDIO_FAKE
,
247 scoped_refptr
<AudioInputController
> controller
=
248 AudioInputController::Create(audio_manager
.get(),
251 AudioManagerBase::kDefaultDeviceId
,
253 ASSERT_TRUE(controller
.get());
255 controller
->Record();
257 controller
->Close(base::MessageLoop::QuitClosure());
258 base::MessageLoop::current()->Run();
260 controller
->Close(base::MessageLoop::QuitClosure());
261 base::MessageLoop::current()->Run();