Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: media/audio/audio_input_unittest.cc

Issue 2784433002: Ensures that audio tasks cannot run after AudioManager is deleted. (Closed)
Patch Set: cleanup Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/test/test_message_loop.h" 11 #include "base/test/test_message_loop.h"
12 #include "base/threading/platform_thread.h" 12 #include "base/threading/platform_thread.h"
13 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "media/audio/audio_device_description.h" 15 #include "media/audio/audio_device_description.h"
16 #include "media/audio/audio_io.h" 16 #include "media/audio/audio_io.h"
17 #include "media/audio/audio_manager.h" 17 #include "media/audio/audio_manager.h"
18 #include "media/audio/audio_unittest_util.h" 18 #include "media/audio/audio_unittest_util.h"
19 #include "media/audio/test_audio_thread.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 namespace media { 22 namespace media {
22 23
23 // This class allows to find out if the callbacks are occurring as 24 // This class allows to find out if the callbacks are occurring as
24 // expected and if any error has been reported. 25 // expected and if any error has been reported.
25 class TestInputCallback : public AudioInputStream::AudioInputCallback { 26 class TestInputCallback : public AudioInputStream::AudioInputCallback {
26 public: 27 public:
27 explicit TestInputCallback() 28 explicit TestInputCallback()
28 : callback_count_(0), 29 : callback_count_(0),
(...skipping 18 matching lines...) Expand all
47 private: 48 private:
48 int callback_count_; 49 int callback_count_;
49 int had_error_; 50 int had_error_;
50 }; 51 };
51 52
52 class AudioInputTest : public testing::Test { 53 class AudioInputTest : public testing::Test {
53 public: 54 public:
54 AudioInputTest() 55 AudioInputTest()
55 : message_loop_(base::MessageLoop::TYPE_UI), 56 : message_loop_(base::MessageLoop::TYPE_UI),
56 audio_manager_(AudioManager::CreateForTesting( 57 audio_manager_(AudioManager::CreateForTesting(
57 base::ThreadTaskRunnerHandle::Get())), 58 base::MakeUnique<TestAudioThread>())),
58 audio_input_stream_(NULL) { 59 audio_input_stream_(NULL) {
59 base::RunLoop().RunUntilIdle(); 60 base::RunLoop().RunUntilIdle();
60 } 61 }
61 62
62 ~AudioInputTest() override {} 63 ~AudioInputTest() override { audio_manager_->Shutdown(); }
63 64
64 protected: 65 protected:
65 bool InputDevicesAvailable() { 66 bool InputDevicesAvailable() {
66 return audio_manager_->HasAudioInputDevices(); 67 return audio_manager_->HasAudioInputDevices();
67 } 68 }
68 69
69 void MakeAudioInputStreamOnAudioThread() { 70 void MakeAudioInputStreamOnAudioThread() {
70 RunOnAudioThread( 71 RunOnAudioThread(
71 base::Bind(&AudioInputTest::MakeAudioInputStream, 72 base::Bind(&AudioInputTest::MakeAudioInputStream,
72 base::Unretained(this))); 73 base::Unretained(this)));
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 146
146 // Synchronously runs the provided callback/closure on the audio thread. 147 // Synchronously runs the provided callback/closure on the audio thread.
147 void RunOnAudioThread(const base::Closure& closure) { 148 void RunOnAudioThread(const base::Closure& closure) {
148 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); 149 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
149 closure.Run(); 150 closure.Run();
150 } 151 }
151 152
152 void OnLogMessage(const std::string& message) {} 153 void OnLogMessage(const std::string& message) {}
153 154
154 base::TestMessageLoop message_loop_; 155 base::TestMessageLoop message_loop_;
155 ScopedAudioManagerPtr audio_manager_; 156 std::unique_ptr<AudioManager> audio_manager_;
156 AudioInputStream* audio_input_stream_; 157 AudioInputStream* audio_input_stream_;
157 158
158 private: 159 private:
159 DISALLOW_COPY_AND_ASSIGN(AudioInputTest); 160 DISALLOW_COPY_AND_ASSIGN(AudioInputTest);
160 }; 161 };
161 162
162 // Test create and close of an AudioInputStream without recording audio. 163 // Test create and close of an AudioInputStream without recording audio.
163 TEST_F(AudioInputTest, CreateAndClose) { 164 TEST_F(AudioInputTest, CreateAndClose) {
164 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); 165 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
165 MakeAudioInputStreamOnAudioThread(); 166 MakeAudioInputStreamOnAudioThread();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 FROM_HERE, run_loop.QuitClosure(), 214 FROM_HERE, run_loop.QuitClosure(),
214 base::TimeDelta::FromMilliseconds(500)); 215 base::TimeDelta::FromMilliseconds(500));
215 run_loop.Run(); 216 run_loop.Run();
216 EXPECT_GE(test_callback.callback_count(), 2); 217 EXPECT_GE(test_callback.callback_count(), 2);
217 EXPECT_FALSE(test_callback.had_error()); 218 EXPECT_FALSE(test_callback.had_error());
218 219
219 StopAndCloseAudioInputStreamOnAudioThread(); 220 StopAndCloseAudioInputStreamOnAudioThread();
220 } 221 }
221 222
222 } // namespace media 223 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698