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