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_METHOD1(OnError, void(AudioInputController* controller));
55 MOCK_METHOD3(OnData, void(AudioInputController* controller,
56 const uint8* data, uint32 size));
59 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler);
63 class AudioInputControllerTest : public testing::Test {
65 AudioInputControllerTest() {}
66 virtual ~AudioInputControllerTest() {}
69 base::MessageLoop message_loop_;
72 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest);
75 // Test AudioInputController for create and close without recording audio.
76 TEST_F(AudioInputControllerTest, CreateAndClose) {
77 MockAudioInputControllerEventHandler event_handler;
79 // OnCreated() will be posted once.
80 EXPECT_CALL(event_handler, OnCreated(NotNull()))
81 .WillOnce(QuitMessageLoop(&message_loop_));
83 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
84 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
85 kSampleRate, kBitsPerSample, kSamplesPerPacket);
87 scoped_refptr<AudioInputController> controller =
88 AudioInputController::Create(audio_manager.get(),
91 AudioManagerBase::kDefaultDeviceId,
93 ASSERT_TRUE(controller.get());
95 // Wait for OnCreated() to fire.
98 // Close the AudioInputController synchronously.
99 CloseAudioController(controller.get());
102 // Test a normal call sequence of create, record and close.
103 TEST_F(AudioInputControllerTest, RecordAndClose) {
104 MockAudioInputControllerEventHandler event_handler;
107 // OnCreated() will be called once.
108 EXPECT_CALL(event_handler, OnCreated(NotNull()))
111 // OnRecording() will be called only once.
112 EXPECT_CALL(event_handler, OnRecording(NotNull()))
115 // OnData() shall be called ten times.
116 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull(), _))
118 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10,
119 message_loop_.message_loop_proxy()));
121 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
122 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
123 kSampleRate, kBitsPerSample, kSamplesPerPacket);
125 // Creating the AudioInputController should render an OnCreated() call.
126 scoped_refptr<AudioInputController> controller =
127 AudioInputController::Create(audio_manager.get(),
130 AudioManagerBase::kDefaultDeviceId,
132 ASSERT_TRUE(controller.get());
134 // Start recording and trigger one OnRecording() call.
135 controller->Record();
137 // Record and wait until ten OnData() callbacks are received.
140 // Close the AudioInputController synchronously.
141 CloseAudioController(controller.get());
144 // Test that the AudioInputController reports an error when the input stream
145 // stops. This can happen when the underlying audio layer stops feeding data as
146 // a result of a removed microphone device.
147 TEST_F(AudioInputControllerTest, RecordAndError) {
148 MockAudioInputControllerEventHandler event_handler;
151 // OnCreated() will be called once.
152 EXPECT_CALL(event_handler, OnCreated(NotNull()))
155 // OnRecording() will be called only once.
156 EXPECT_CALL(event_handler, OnRecording(NotNull()))
159 // OnData() shall be called ten times.
160 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull(), _))
162 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10,
163 message_loop_.message_loop_proxy()));
165 // OnError() will be called after the data stream stops while the
166 // controller is in a recording state.
167 EXPECT_CALL(event_handler, OnError(NotNull()))
169 .WillOnce(QuitMessageLoop(&message_loop_));
171 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
172 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
173 kSampleRate, kBitsPerSample, kSamplesPerPacket);
175 // Creating the AudioInputController should render an OnCreated() call.
176 scoped_refptr<AudioInputController> controller =
177 AudioInputController::Create(audio_manager.get(),
180 AudioManagerBase::kDefaultDeviceId,
182 ASSERT_TRUE(controller.get());
184 // Start recording and trigger one OnRecording() call.
185 controller->Record();
187 // Record and wait until ten OnData() callbacks are received.
190 // Stop the stream and verify that OnError() is posted.
191 AudioInputStream* stream = controller->stream_for_testing();
195 // Close the AudioInputController synchronously.
196 CloseAudioController(controller.get());
199 // Test that AudioInputController rejects insanely large packet sizes.
200 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) {
201 // Create an audio device with a very large packet size.
202 MockAudioInputControllerEventHandler event_handler;
204 // OnCreated() shall not be called in this test.
205 EXPECT_CALL(event_handler, OnCreated(NotNull()))
208 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
209 AudioParameters params(AudioParameters::AUDIO_FAKE,
213 kSamplesPerPacket * 1000);
214 scoped_refptr<AudioInputController> controller =
215 AudioInputController::Create(audio_manager.get(),
218 AudioManagerBase::kDefaultDeviceId,
220 ASSERT_FALSE(controller.get());
223 // Test calling AudioInputController::Close multiple times.
224 TEST_F(AudioInputControllerTest, CloseTwice) {
225 MockAudioInputControllerEventHandler event_handler;
227 // OnRecording() will be called only once.
228 EXPECT_CALL(event_handler, OnCreated(NotNull()));
230 // OnRecording() will be called only once.
231 EXPECT_CALL(event_handler, OnRecording(NotNull()))
234 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting());
235 AudioParameters params(AudioParameters::AUDIO_FAKE,
240 scoped_refptr<AudioInputController> controller =
241 AudioInputController::Create(audio_manager.get(),
244 AudioManagerBase::kDefaultDeviceId,
246 ASSERT_TRUE(controller.get());
248 controller->Record();
250 controller->Close(base::MessageLoop::QuitClosure());
251 base::MessageLoop::current()->Run();
253 controller->Close(base::MessageLoop::QuitClosure());
254 base::MessageLoop::current()->Run();