Make callers of CommandLine use it via the base:: namespace.
[chromium-blink-merge.git] / media / audio / audio_input_unittest.cc
blobd959680f6de473f7f07cf77e46dcb8fba13342f4
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/environment.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/platform_thread.h"
13 #include "media/audio/audio_io.h"
14 #include "media/audio/audio_manager_base.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace media {
19 // This class allows to find out if the callbacks are occurring as
20 // expected and if any error has been reported.
21 class TestInputCallback : public AudioInputStream::AudioInputCallback {
22 public:
23 explicit TestInputCallback()
24 : callback_count_(0),
25 had_error_(0) {
27 void OnData(AudioInputStream* stream,
28 const AudioBus* source,
29 uint32 hardware_delay_bytes,
30 double volume) override {
31 ++callback_count_;
33 void OnError(AudioInputStream* stream) override { ++had_error_; }
34 // Returns how many times OnData() has been called.
35 int callback_count() const {
36 return callback_count_;
38 // Returns how many times the OnError callback was called.
39 int had_error() const {
40 return had_error_;
43 private:
44 int callback_count_;
45 int had_error_;
48 class AudioInputTest : public testing::Test {
49 public:
50 AudioInputTest() :
51 message_loop_(base::MessageLoop::TYPE_UI),
52 audio_manager_(AudioManager::CreateForTesting()),
53 audio_input_stream_(NULL) {
54 // Wait for the AudioManager to finish any initialization on the audio loop.
55 base::RunLoop().RunUntilIdle();
58 ~AudioInputTest() override { base::RunLoop().RunUntilIdle(); }
60 protected:
61 AudioManager* audio_manager() { return audio_manager_.get(); }
63 bool CanRunAudioTests() {
64 bool has_input = audio_manager()->HasAudioInputDevices();
65 LOG_IF(WARNING, !has_input) << "No input devices detected";
66 return has_input;
69 void MakeAudioInputStreamOnAudioThread() {
70 RunOnAudioThread(
71 base::Bind(&AudioInputTest::MakeAudioInputStream,
72 base::Unretained(this)));
75 void CloseAudioInputStreamOnAudioThread() {
76 RunOnAudioThread(
77 base::Bind(&AudioInputStream::Close,
78 base::Unretained(audio_input_stream_)));
79 audio_input_stream_ = NULL;
82 void OpenAndCloseAudioInputStreamOnAudioThread() {
83 RunOnAudioThread(
84 base::Bind(&AudioInputTest::OpenAndClose,
85 base::Unretained(this)));
88 void OpenStopAndCloseAudioInputStreamOnAudioThread() {
89 RunOnAudioThread(
90 base::Bind(&AudioInputTest::OpenStopAndClose,
91 base::Unretained(this)));
94 void OpenAndStartAudioInputStreamOnAudioThread(
95 AudioInputStream::AudioInputCallback* sink) {
96 RunOnAudioThread(
97 base::Bind(&AudioInputTest::OpenAndStart,
98 base::Unretained(this),
99 sink));
102 void StopAndCloseAudioInputStreamOnAudioThread() {
103 RunOnAudioThread(
104 base::Bind(&AudioInputTest::StopAndClose,
105 base::Unretained(this)));
108 void MakeAudioInputStream() {
109 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
110 AudioParameters params = audio_manager()->GetInputStreamParameters(
111 AudioManagerBase::kDefaultDeviceId);
112 audio_input_stream_ = audio_manager()->MakeAudioInputStream(params,
113 AudioManagerBase::kDefaultDeviceId);
114 EXPECT_TRUE(audio_input_stream_);
117 void OpenAndClose() {
118 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
119 EXPECT_TRUE(audio_input_stream_->Open());
120 audio_input_stream_->Close();
121 audio_input_stream_ = NULL;
124 void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
125 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
126 EXPECT_TRUE(audio_input_stream_->Open());
127 audio_input_stream_->Start(sink);
130 void OpenStopAndClose() {
131 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
132 EXPECT_TRUE(audio_input_stream_->Open());
133 audio_input_stream_->Stop();
134 audio_input_stream_->Close();
135 audio_input_stream_ = NULL;
138 void StopAndClose() {
139 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
140 audio_input_stream_->Stop();
141 audio_input_stream_->Close();
142 audio_input_stream_ = NULL;
145 // Synchronously runs the provided callback/closure on the audio thread.
146 void RunOnAudioThread(const base::Closure& closure) {
147 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
148 base::WaitableEvent event(false, false);
149 audio_manager()->GetTaskRunner()->PostTask(
150 FROM_HERE,
151 base::Bind(&AudioInputTest::RunOnAudioThreadImpl,
152 base::Unretained(this),
153 closure,
154 &event));
155 event.Wait();
156 } else {
157 closure.Run();
161 void RunOnAudioThreadImpl(const base::Closure& closure,
162 base::WaitableEvent* event) {
163 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
164 closure.Run();
165 event->Signal();
168 base::MessageLoop message_loop_;
169 scoped_ptr<AudioManager> audio_manager_;
170 AudioInputStream* audio_input_stream_;
172 private:
173 DISALLOW_COPY_AND_ASSIGN(AudioInputTest);
176 // Test create and close of an AudioInputStream without recording audio.
177 TEST_F(AudioInputTest, CreateAndClose) {
178 if (!CanRunAudioTests())
179 return;
180 MakeAudioInputStreamOnAudioThread();
181 CloseAudioInputStreamOnAudioThread();
184 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
185 // This test is failing on ARM linux: http://crbug.com/238490
186 #define MAYBE_OpenAndClose DISABLED_OpenAndClose
187 #else
188 #define MAYBE_OpenAndClose OpenAndClose
189 #endif
190 // Test create, open and close of an AudioInputStream without recording audio.
191 TEST_F(AudioInputTest, MAYBE_OpenAndClose) {
192 if (!CanRunAudioTests())
193 return;
194 MakeAudioInputStreamOnAudioThread();
195 OpenAndCloseAudioInputStreamOnAudioThread();
198 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
199 // This test is failing on ARM linux: http://crbug.com/238490
200 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose
201 #else
202 #define MAYBE_OpenStopAndClose OpenStopAndClose
203 #endif
204 // Test create, open, stop and close of an AudioInputStream without recording.
205 TEST_F(AudioInputTest, MAYBE_OpenStopAndClose) {
206 if (!CanRunAudioTests())
207 return;
208 MakeAudioInputStreamOnAudioThread();
209 OpenStopAndCloseAudioInputStreamOnAudioThread();
212 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
213 // This test is failing on ARM linux: http://crbug.com/238490
214 #define MAYBE_Record DISABLED_Record
215 #else
216 #define MAYBE_Record Record
217 #endif
218 // Test a normal recording sequence using an AudioInputStream.
219 // Very simple test which starts capturing during half a second and verifies
220 // that recording starts.
221 TEST_F(AudioInputTest, MAYBE_Record) {
222 if (!CanRunAudioTests())
223 return;
224 MakeAudioInputStreamOnAudioThread();
226 TestInputCallback test_callback;
227 OpenAndStartAudioInputStreamOnAudioThread(&test_callback);
229 message_loop_.PostDelayedTask(
230 FROM_HERE,
231 base::MessageLoop::QuitClosure(),
232 base::TimeDelta::FromMilliseconds(500));
233 message_loop_.Run();
234 EXPECT_GE(test_callback.callback_count(), 2);
235 EXPECT_FALSE(test_callback.had_error());
237 StopAndCloseAudioInputStreamOnAudioThread();
240 } // namespace media