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 "media/audio/audio_input_controller.h" |
5 #include "base/bind.h" | 6 #include "base/bind.h" |
6 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
7 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
10 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
11 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
12 #include "media/audio/audio_device_description.h" | 14 #include "media/audio/audio_device_description.h" |
13 #include "media/audio/audio_input_controller.h" | 15 #include "media/audio/audio_thread_impl.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 using ::testing::_; | 19 using ::testing::_; |
18 using ::testing::AnyNumber; | 20 using ::testing::AnyNumber; |
19 using ::testing::AtLeast; | 21 using ::testing::AtLeast; |
20 using ::testing::Exactly; | 22 using ::testing::Exactly; |
21 using ::testing::InvokeWithoutArgs; | 23 using ::testing::InvokeWithoutArgs; |
22 using ::testing::NotNull; | 24 using ::testing::NotNull; |
23 using base::WaitableEvent; | 25 using base::WaitableEvent; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 double volume, | 75 double volume, |
74 bool key_pressed, | 76 bool key_pressed, |
75 uint32_t hardware_delay_bytes)); | 77 uint32_t hardware_delay_bytes)); |
76 MOCK_METHOD0(Close, void()); | 78 MOCK_METHOD0(Close, void()); |
77 }; | 79 }; |
78 | 80 |
79 // Test fixture. | 81 // Test fixture. |
80 class AudioInputControllerTest : public testing::Test { | 82 class AudioInputControllerTest : public testing::Test { |
81 public: | 83 public: |
82 AudioInputControllerTest() | 84 AudioInputControllerTest() |
83 : audio_thread_("AudioThread"), | 85 : suspend_event_(WaitableEvent::ResetPolicy::AUTOMATIC, |
84 suspend_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | |
85 WaitableEvent::InitialState::NOT_SIGNALED) { | 86 WaitableEvent::InitialState::NOT_SIGNALED) { |
86 audio_thread_.StartAndWaitForTesting(); | |
87 audio_manager_ = | 87 audio_manager_ = |
88 AudioManager::CreateForTesting(audio_thread_.task_runner()); | 88 AudioManager::CreateForTesting(base::MakeUnique<AudioThreadImpl>()); |
89 } | 89 } |
90 ~AudioInputControllerTest() override { | 90 ~AudioInputControllerTest() override { audio_manager_->Shutdown(); } |
91 audio_task_runner()->PostTask( | |
92 FROM_HERE, base::Bind(&AudioInputControllerTest::DeleteAudioManager, | |
93 base::Unretained(this))); | |
94 audio_thread_.Stop(); | |
95 } | |
96 | 91 |
97 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner() const { | 92 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner() const { |
98 return audio_thread_.task_runner(); | 93 return audio_manager_->GetTaskRunner(); |
99 } | 94 } |
100 | 95 |
101 void SuspendAudioThread() { | 96 void SuspendAudioThread() { |
102 audio_task_runner()->PostTask( | 97 audio_task_runner()->PostTask( |
103 FROM_HERE, base::Bind(&AudioInputControllerTest::WaitForResume, | 98 FROM_HERE, base::Bind(&AudioInputControllerTest::WaitForResume, |
104 base::Unretained(this))); | 99 base::Unretained(this))); |
105 } | 100 } |
106 | 101 |
107 void ResumeAudioThread() { suspend_event_.Signal(); } | 102 void ResumeAudioThread() { suspend_event_.Signal(); } |
108 | 103 |
109 private: | 104 private: |
110 void DeleteAudioManager() { audio_manager_.reset(); } | |
111 void WaitForResume() { suspend_event_.Wait(); } | 105 void WaitForResume() { suspend_event_.Wait(); } |
112 | 106 |
113 protected: | 107 protected: |
114 base::Thread audio_thread_; | |
115 base::MessageLoop message_loop_; | 108 base::MessageLoop message_loop_; |
116 ScopedAudioManagerPtr audio_manager_; | 109 std::unique_ptr<AudioManager> audio_manager_; |
117 WaitableEvent suspend_event_; | 110 WaitableEvent suspend_event_; |
118 | 111 |
119 private: | 112 private: |
120 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); | 113 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); |
121 }; | 114 }; |
122 | 115 |
123 // Test AudioInputController for create and close without recording audio. | 116 // Test AudioInputController for create and close without recording audio. |
124 TEST_F(AudioInputControllerTest, CreateAndClose) { | 117 TEST_F(AudioInputControllerTest, CreateAndClose) { |
125 base::RunLoop run_loop; | 118 base::RunLoop run_loop; |
126 | 119 |
127 MockAudioInputControllerEventHandler event_handler; | 120 MockAudioInputControllerEventHandler event_handler; |
128 MockSyncWriter sync_writer; | 121 MockSyncWriter sync_writer; |
129 scoped_refptr<AudioInputController> controller; | 122 scoped_refptr<AudioInputController> controller; |
130 | 123 |
131 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 124 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
132 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 125 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
133 | 126 |
134 SuspendAudioThread(); | 127 SuspendAudioThread(); |
135 controller = AudioInputController::Create( | 128 controller = AudioInputController::Create( |
136 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, | 129 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, |
137 AudioDeviceDescription::kDefaultDeviceId, false, audio_task_runner()); | 130 AudioDeviceDescription::kDefaultDeviceId, false, audio_task_runner()); |
138 ASSERT_TRUE(controller.get()); | 131 ASSERT_TRUE(controller.get()); |
139 EXPECT_CALL(event_handler, OnCreated(controller.get())).Times(Exactly(1)); | 132 EXPECT_CALL(event_handler, OnCreated(controller.get())).Times(Exactly(1)); |
140 EXPECT_CALL(event_handler, OnLog(controller.get(), _)); | 133 EXPECT_CALL(event_handler, OnLog(controller.get(), _)); |
141 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); | 134 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); |
142 ResumeAudioThread(); | 135 ResumeAudioThread(); |
143 | 136 |
144 CloseAudioController(controller.get()); | 137 CloseAudioController(controller.get()); |
145 | |
146 audio_thread_.FlushForTesting(); | |
147 } | 138 } |
148 | 139 |
149 // Test a normal call sequence of create, record and close. | 140 // Test a normal call sequence of create, record and close. |
150 TEST_F(AudioInputControllerTest, RecordAndClose) { | 141 TEST_F(AudioInputControllerTest, RecordAndClose) { |
151 MockAudioInputControllerEventHandler event_handler; | 142 MockAudioInputControllerEventHandler event_handler; |
152 MockSyncWriter sync_writer; | 143 MockSyncWriter sync_writer; |
153 int count = 0; | 144 int count = 0; |
154 | 145 |
155 // OnCreated() will be called once. | 146 // OnCreated() will be called once. |
156 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 147 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 controller->Record(); | 216 controller->Record(); |
226 | 217 |
227 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 218 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
228 base::RunLoop().Run(); | 219 base::RunLoop().Run(); |
229 | 220 |
230 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 221 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
231 base::RunLoop().Run(); | 222 base::RunLoop().Run(); |
232 } | 223 } |
233 | 224 |
234 } // namespace media | 225 } // namespace media |
OLD | NEW |