Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / media / audio / audio_input_controller_unittest.cc
blob743e7b4308324aed8161da76e1e416f9d77550b1
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"
6 #include "base/bind.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"
14 using ::testing::_;
15 using ::testing::AtLeast;
16 using ::testing::Exactly;
17 using ::testing::InvokeWithoutArgs;
18 using ::testing::NotNull;
20 namespace media {
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 {
48 public:
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));
57 private:
58 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler);
61 // Test fixture.
62 class AudioInputControllerTest : public testing::Test {
63 public:
64 AudioInputControllerTest() {}
65 virtual ~AudioInputControllerTest() {}
67 protected:
68 MessageLoop message_loop_;
70 private:
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.
90 message_loop_.Run();
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;
99 int count = 0;
101 // OnCreated() will be called once.
102 EXPECT_CALL(event_handler, OnCreated(NotNull()))
103 .Times(Exactly(1));
105 // OnRecording() will be called only once.
106 EXPECT_CALL(event_handler, OnRecording(NotNull()))
107 .Times(Exactly(1));
109 // OnData() shall be called ten times.
110 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull(), _))
111 .Times(AtLeast(10))
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.
128 message_loop_.Run();
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;
139 int count = 0;
141 // OnCreated() will be called once.
142 EXPECT_CALL(event_handler, OnCreated(NotNull()))
143 .Times(Exactly(1));
145 // OnRecording() will be called only once.
146 EXPECT_CALL(event_handler, OnRecording(NotNull()))
147 .Times(Exactly(1));
149 // OnData() shall be called ten times.
150 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull(), _))
151 .Times(AtLeast(10))
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()))
158 .Times(Exactly(1))
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.
174 message_loop_.Run();
176 // Stop the stream and verify that OnError() is posted.
177 AudioInputStream* stream = controller->stream_for_testing();
178 stream->Stop();
179 message_loop_.Run();
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()))
192 .Times(Exactly(0));
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()))
211 .Times(Exactly(1));
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();
229 } // namespace media