Fix vectorized icon for PPAPI broker website setting icon
[chromium-blink-merge.git] / media / audio / audio_input_unittest.cc
blob97b7bf0c9df82ca8aa289960528111a2417b587b
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 "media/audio/audio_unittest_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace media {
20 // This class allows to find out if the callbacks are occurring as
21 // expected and if any error has been reported.
22 class TestInputCallback : public AudioInputStream::AudioInputCallback {
23 public:
24 explicit TestInputCallback()
25 : callback_count_(0),
26 had_error_(0) {
28 void OnData(AudioInputStream* stream,
29 const AudioBus* source,
30 uint32 hardware_delay_bytes,
31 double volume) override {
32 ++callback_count_;
34 void OnError(AudioInputStream* stream) override { ++had_error_; }
35 // Returns how many times OnData() has been called.
36 int callback_count() const {
37 return callback_count_;
39 // Returns how many times the OnError callback was called.
40 int had_error() const {
41 return had_error_;
44 private:
45 int callback_count_;
46 int had_error_;
49 class AudioInputTest : public testing::Test {
50 public:
51 AudioInputTest() :
52 message_loop_(base::MessageLoop::TYPE_UI),
53 audio_manager_(AudioManager::CreateForTesting()),
54 audio_input_stream_(NULL) {
55 // Wait for the AudioManager to finish any initialization on the audio loop.
56 base::RunLoop().RunUntilIdle();
59 ~AudioInputTest() override { base::RunLoop().RunUntilIdle(); }
61 protected:
62 bool InputDevicesAvailable() {
63 return audio_manager_->HasAudioInputDevices();
66 void MakeAudioInputStreamOnAudioThread() {
67 RunOnAudioThread(
68 base::Bind(&AudioInputTest::MakeAudioInputStream,
69 base::Unretained(this)));
72 void CloseAudioInputStreamOnAudioThread() {
73 RunOnAudioThread(
74 base::Bind(&AudioInputStream::Close,
75 base::Unretained(audio_input_stream_)));
76 audio_input_stream_ = NULL;
79 void OpenAndCloseAudioInputStreamOnAudioThread() {
80 RunOnAudioThread(
81 base::Bind(&AudioInputTest::OpenAndClose,
82 base::Unretained(this)));
85 void OpenStopAndCloseAudioInputStreamOnAudioThread() {
86 RunOnAudioThread(
87 base::Bind(&AudioInputTest::OpenStopAndClose,
88 base::Unretained(this)));
91 void OpenAndStartAudioInputStreamOnAudioThread(
92 AudioInputStream::AudioInputCallback* sink) {
93 RunOnAudioThread(
94 base::Bind(&AudioInputTest::OpenAndStart,
95 base::Unretained(this),
96 sink));
99 void StopAndCloseAudioInputStreamOnAudioThread() {
100 RunOnAudioThread(
101 base::Bind(&AudioInputTest::StopAndClose,
102 base::Unretained(this)));
105 void MakeAudioInputStream() {
106 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
107 AudioParameters params = audio_manager_->GetInputStreamParameters(
108 AudioManagerBase::kDefaultDeviceId);
109 audio_input_stream_ = audio_manager_->MakeAudioInputStream(params,
110 AudioManagerBase::kDefaultDeviceId);
111 EXPECT_TRUE(audio_input_stream_);
114 void OpenAndClose() {
115 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
116 EXPECT_TRUE(audio_input_stream_->Open());
117 audio_input_stream_->Close();
118 audio_input_stream_ = NULL;
121 void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
122 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
123 EXPECT_TRUE(audio_input_stream_->Open());
124 audio_input_stream_->Start(sink);
127 void OpenStopAndClose() {
128 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
129 EXPECT_TRUE(audio_input_stream_->Open());
130 audio_input_stream_->Stop();
131 audio_input_stream_->Close();
132 audio_input_stream_ = NULL;
135 void StopAndClose() {
136 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
137 audio_input_stream_->Stop();
138 audio_input_stream_->Close();
139 audio_input_stream_ = NULL;
142 // Synchronously runs the provided callback/closure on the audio thread.
143 void RunOnAudioThread(const base::Closure& closure) {
144 if (!audio_manager_->GetTaskRunner()->BelongsToCurrentThread()) {
145 base::WaitableEvent event(false, false);
146 audio_manager_->GetTaskRunner()->PostTask(
147 FROM_HERE,
148 base::Bind(&AudioInputTest::RunOnAudioThreadImpl,
149 base::Unretained(this),
150 closure,
151 &event));
152 event.Wait();
153 } else {
154 closure.Run();
158 void RunOnAudioThreadImpl(const base::Closure& closure,
159 base::WaitableEvent* event) {
160 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
161 closure.Run();
162 event->Signal();
165 base::MessageLoop message_loop_;
166 scoped_ptr<AudioManager> audio_manager_;
167 AudioInputStream* audio_input_stream_;
169 private:
170 DISALLOW_COPY_AND_ASSIGN(AudioInputTest);
173 // Test create and close of an AudioInputStream without recording audio.
174 TEST_F(AudioInputTest, CreateAndClose) {
175 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
176 MakeAudioInputStreamOnAudioThread();
177 CloseAudioInputStreamOnAudioThread();
180 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
181 // This test is failing on ARM linux: http://crbug.com/238490
182 #define MAYBE_OpenAndClose DISABLED_OpenAndClose
183 #else
184 #define MAYBE_OpenAndClose OpenAndClose
185 #endif
186 // Test create, open and close of an AudioInputStream without recording audio.
187 TEST_F(AudioInputTest, MAYBE_OpenAndClose) {
188 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
189 MakeAudioInputStreamOnAudioThread();
190 OpenAndCloseAudioInputStreamOnAudioThread();
193 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
194 // This test is failing on ARM linux: http://crbug.com/238490
195 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose
196 #else
197 #define MAYBE_OpenStopAndClose OpenStopAndClose
198 #endif
199 // Test create, open, stop and close of an AudioInputStream without recording.
200 TEST_F(AudioInputTest, MAYBE_OpenStopAndClose) {
201 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
202 MakeAudioInputStreamOnAudioThread();
203 OpenStopAndCloseAudioInputStreamOnAudioThread();
206 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
207 // This test is failing on ARM linux: http://crbug.com/238490
208 #define MAYBE_Record DISABLED_Record
209 #else
210 #define MAYBE_Record Record
211 #endif
212 // Test a normal recording sequence using an AudioInputStream.
213 // Very simple test which starts capturing during half a second and verifies
214 // that recording starts.
215 TEST_F(AudioInputTest, MAYBE_Record) {
216 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
217 MakeAudioInputStreamOnAudioThread();
219 TestInputCallback test_callback;
220 OpenAndStartAudioInputStreamOnAudioThread(&test_callback);
222 message_loop_.PostDelayedTask(
223 FROM_HERE,
224 base::MessageLoop::QuitClosure(),
225 base::TimeDelta::FromMilliseconds(500));
226 message_loop_.Run();
227 EXPECT_GE(test_callback.callback_count(), 2);
228 EXPECT_FALSE(test_callback.had_error());
230 StopAndCloseAudioInputStreamOnAudioThread();
233 } // namespace media