OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/environment.h" | 8 #include "base/environment.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
11 #include "base/test/test_message_loop.h" | 13 #include "base/synchronization/waitable_event.h" |
12 #include "base/thread_task_runner_handle.h" | |
13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
15 #include "media/audio/audio_io.h" | 16 #include "media/audio/audio_io.h" |
16 #include "media/audio/audio_manager_base.h" | 17 #include "media/audio/audio_manager_base.h" |
17 #include "media/audio/audio_unittest_util.h" | 18 #include "media/audio/audio_unittest_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 // This class allows to find out if the callbacks are occurring as | 23 // This class allows to find out if the callbacks are occurring as |
(...skipping 19 matching lines...) Expand all Loading... |
42 int had_error() const { | 43 int had_error() const { |
43 return had_error_; | 44 return had_error_; |
44 } | 45 } |
45 | 46 |
46 private: | 47 private: |
47 int callback_count_; | 48 int callback_count_; |
48 int had_error_; | 49 int had_error_; |
49 }; | 50 }; |
50 | 51 |
51 class AudioInputTest : public testing::Test { | 52 class AudioInputTest : public testing::Test { |
52 public: | 53 public: |
53 AudioInputTest() | 54 AudioInputTest() : |
54 : message_loop_(base::MessageLoop::TYPE_UI), | 55 message_loop_(base::MessageLoop::TYPE_UI), |
55 audio_manager_(AudioManager::CreateForTesting( | 56 audio_manager_(AudioManager::CreateForTesting()), |
56 base::ThreadTaskRunnerHandle::Get())), | 57 audio_input_stream_(NULL) { |
57 audio_input_stream_(NULL) { | 58 // Wait for the AudioManager to finish any initialization on the audio loop. |
58 base::RunLoop().RunUntilIdle(); | 59 base::RunLoop().RunUntilIdle(); |
59 } | 60 } |
60 | 61 |
61 ~AudioInputTest() override {} | 62 ~AudioInputTest() override { base::RunLoop().RunUntilIdle(); } |
62 | 63 |
63 protected: | 64 protected: |
64 bool InputDevicesAvailable() { | 65 bool InputDevicesAvailable() { |
65 return audio_manager_->HasAudioInputDevices(); | 66 return audio_manager_->HasAudioInputDevices(); |
66 } | 67 } |
67 | 68 |
68 void MakeAudioInputStreamOnAudioThread() { | 69 void MakeAudioInputStreamOnAudioThread() { |
69 RunOnAudioThread( | 70 RunOnAudioThread( |
70 base::Bind(&AudioInputTest::MakeAudioInputStream, | 71 base::Bind(&AudioInputTest::MakeAudioInputStream, |
71 base::Unretained(this))); | 72 base::Unretained(this))); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 137 |
137 void StopAndClose() { | 138 void StopAndClose() { |
138 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 139 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
139 audio_input_stream_->Stop(); | 140 audio_input_stream_->Stop(); |
140 audio_input_stream_->Close(); | 141 audio_input_stream_->Close(); |
141 audio_input_stream_ = NULL; | 142 audio_input_stream_ = NULL; |
142 } | 143 } |
143 | 144 |
144 // Synchronously runs the provided callback/closure on the audio thread. | 145 // Synchronously runs the provided callback/closure on the audio thread. |
145 void RunOnAudioThread(const base::Closure& closure) { | 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(); |
| 158 } |
| 159 } |
| 160 |
| 161 void RunOnAudioThreadImpl(const base::Closure& closure, |
| 162 base::WaitableEvent* event) { |
146 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | 163 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); |
147 closure.Run(); | 164 closure.Run(); |
| 165 event->Signal(); |
148 } | 166 } |
149 | 167 |
150 base::TestMessageLoop message_loop_; | 168 base::MessageLoop message_loop_; |
151 ScopedAudioManagerPtr audio_manager_; | 169 scoped_ptr<AudioManager> audio_manager_; |
152 AudioInputStream* audio_input_stream_; | 170 AudioInputStream* audio_input_stream_; |
153 | 171 |
154 private: | 172 private: |
155 DISALLOW_COPY_AND_ASSIGN(AudioInputTest); | 173 DISALLOW_COPY_AND_ASSIGN(AudioInputTest); |
156 }; | 174 }; |
157 | 175 |
158 // Test create and close of an AudioInputStream without recording audio. | 176 // Test create and close of an AudioInputStream without recording audio. |
159 TEST_F(AudioInputTest, CreateAndClose) { | 177 TEST_F(AudioInputTest, CreateAndClose) { |
160 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); | 178 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
161 MakeAudioInputStreamOnAudioThread(); | 179 MakeAudioInputStreamOnAudioThread(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // Test a normal recording sequence using an AudioInputStream. | 215 // Test a normal recording sequence using an AudioInputStream. |
198 // Very simple test which starts capturing during half a second and verifies | 216 // Very simple test which starts capturing during half a second and verifies |
199 // that recording starts. | 217 // that recording starts. |
200 TEST_F(AudioInputTest, MAYBE_Record) { | 218 TEST_F(AudioInputTest, MAYBE_Record) { |
201 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); | 219 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
202 MakeAudioInputStreamOnAudioThread(); | 220 MakeAudioInputStreamOnAudioThread(); |
203 | 221 |
204 TestInputCallback test_callback; | 222 TestInputCallback test_callback; |
205 OpenAndStartAudioInputStreamOnAudioThread(&test_callback); | 223 OpenAndStartAudioInputStreamOnAudioThread(&test_callback); |
206 | 224 |
207 base::RunLoop run_loop; | 225 message_loop_.PostDelayedTask(FROM_HERE, |
208 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 226 base::MessageLoop::QuitWhenIdleClosure(), |
209 FROM_HERE, run_loop.QuitClosure(), | 227 base::TimeDelta::FromMilliseconds(500)); |
210 base::TimeDelta::FromMilliseconds(500)); | 228 message_loop_.Run(); |
211 run_loop.Run(); | |
212 EXPECT_GE(test_callback.callback_count(), 2); | 229 EXPECT_GE(test_callback.callback_count(), 2); |
213 EXPECT_FALSE(test_callback.had_error()); | 230 EXPECT_FALSE(test_callback.had_error()); |
214 | 231 |
215 StopAndCloseAudioInputStreamOnAudioThread(); | 232 StopAndCloseAudioInputStreamOnAudioThread(); |
216 } | 233 } |
217 | 234 |
218 } // namespace media | 235 } // namespace media |
OLD | NEW |