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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
10 #include "media/audio/audio_input_controller.h" | 10 #include "media/audio/audio_input_controller.h" |
11 #include "media/audio/audio_manager_base.h" | 11 #include "media/audio/audio_manager_base.h" |
| 12 #include "media/audio/fake_audio_log_factory.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 using ::testing::_; | 16 using ::testing::_; |
16 using ::testing::AtLeast; | 17 using ::testing::AtLeast; |
17 using ::testing::Exactly; | 18 using ::testing::Exactly; |
18 using ::testing::InvokeWithoutArgs; | 19 using ::testing::InvokeWithoutArgs; |
19 using ::testing::NotNull; | 20 using ::testing::NotNull; |
20 | 21 |
21 namespace media { | 22 namespace media { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 void(AudioInputController* controller, | 61 void(AudioInputController* controller, |
61 const std::string& message)); | 62 const std::string& message)); |
62 | 63 |
63 private: | 64 private: |
64 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); | 65 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); |
65 }; | 66 }; |
66 | 67 |
67 // Test fixture. | 68 // Test fixture. |
68 class AudioInputControllerTest : public testing::Test { | 69 class AudioInputControllerTest : public testing::Test { |
69 public: | 70 public: |
70 AudioInputControllerTest() {} | 71 AudioInputControllerTest() |
| 72 : audio_manager_(AudioManager::Create(nullptr, |
| 73 nullptr, |
| 74 nullptr, |
| 75 &fake_audio_log_factory_)) {} |
71 ~AudioInputControllerTest() override {} | 76 ~AudioInputControllerTest() override {} |
72 | 77 |
73 protected: | 78 protected: |
74 base::MessageLoop message_loop_; | 79 base::MessageLoop message_loop_; |
| 80 FakeAudioLogFactory fake_audio_log_factory_; |
| 81 ScopedAudioManagerPtr audio_manager_; |
75 | 82 |
76 private: | 83 private: |
77 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); | 84 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); |
78 }; | 85 }; |
79 | 86 |
80 // Test AudioInputController for create and close without recording audio. | 87 // Test AudioInputController for create and close without recording audio. |
81 TEST_F(AudioInputControllerTest, CreateAndClose) { | 88 TEST_F(AudioInputControllerTest, CreateAndClose) { |
82 MockAudioInputControllerEventHandler event_handler; | 89 MockAudioInputControllerEventHandler event_handler; |
83 | 90 |
84 // OnCreated() will be posted once. | 91 // OnCreated() will be posted once. |
85 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 92 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
86 .WillOnce(QuitMessageLoop(&message_loop_)); | 93 .WillOnce(QuitMessageLoop(&message_loop_)); |
87 | 94 |
88 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
89 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 95 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
90 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 96 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
91 | 97 |
92 scoped_refptr<AudioInputController> controller = | 98 scoped_refptr<AudioInputController> controller = |
93 AudioInputController::Create(audio_manager.get(), | 99 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
94 &event_handler, | 100 AudioManagerBase::kDefaultDeviceId, NULL); |
95 params, | |
96 AudioManagerBase::kDefaultDeviceId, | |
97 NULL); | |
98 ASSERT_TRUE(controller.get()); | 101 ASSERT_TRUE(controller.get()); |
99 | 102 |
100 // Wait for OnCreated() to fire. | 103 // Wait for OnCreated() to fire. |
101 message_loop_.Run(); | 104 message_loop_.Run(); |
102 | 105 |
103 // Close the AudioInputController synchronously. | 106 // Close the AudioInputController synchronously. |
104 CloseAudioController(controller.get()); | 107 CloseAudioController(controller.get()); |
105 } | 108 } |
106 | 109 |
107 // Test a normal call sequence of create, record and close. | 110 // Test a normal call sequence of create, record and close. |
108 TEST_F(AudioInputControllerTest, RecordAndClose) { | 111 TEST_F(AudioInputControllerTest, RecordAndClose) { |
109 MockAudioInputControllerEventHandler event_handler; | 112 MockAudioInputControllerEventHandler event_handler; |
110 int count = 0; | 113 int count = 0; |
111 | 114 |
112 // OnCreated() will be called once. | 115 // OnCreated() will be called once. |
113 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 116 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
114 .Times(Exactly(1)); | 117 .Times(Exactly(1)); |
115 | 118 |
116 // OnRecording() will be called only once. | 119 // OnRecording() will be called only once. |
117 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 120 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
118 .Times(Exactly(1)); | 121 .Times(Exactly(1)); |
119 | 122 |
120 // OnData() shall be called ten times. | 123 // OnData() shall be called ten times. |
121 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) | 124 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) |
122 .Times(AtLeast(10)) | 125 .Times(AtLeast(10)) |
123 .WillRepeatedly(CheckCountAndPostQuitTask( | 126 .WillRepeatedly(CheckCountAndPostQuitTask( |
124 &count, 10, message_loop_.task_runner())); | 127 &count, 10, message_loop_.task_runner())); |
125 | 128 |
126 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
127 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 129 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
128 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 130 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
129 | 131 |
130 // Creating the AudioInputController should render an OnCreated() call. | 132 // Creating the AudioInputController should render an OnCreated() call. |
131 scoped_refptr<AudioInputController> controller = | 133 scoped_refptr<AudioInputController> controller = |
132 AudioInputController::Create(audio_manager.get(), | 134 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
133 &event_handler, | 135 AudioManagerBase::kDefaultDeviceId, NULL); |
134 params, | |
135 AudioManagerBase::kDefaultDeviceId, | |
136 NULL); | |
137 ASSERT_TRUE(controller.get()); | 136 ASSERT_TRUE(controller.get()); |
138 | 137 |
139 // Start recording and trigger one OnRecording() call. | 138 // Start recording and trigger one OnRecording() call. |
140 controller->Record(); | 139 controller->Record(); |
141 | 140 |
142 // Record and wait until ten OnData() callbacks are received. | 141 // Record and wait until ten OnData() callbacks are received. |
143 message_loop_.Run(); | 142 message_loop_.Run(); |
144 | 143 |
145 // Close the AudioInputController synchronously. | 144 // Close the AudioInputController synchronously. |
146 CloseAudioController(controller.get()); | 145 CloseAudioController(controller.get()); |
(...skipping 22 matching lines...) Expand all Loading... |
169 .WillRepeatedly(CheckCountAndPostQuitTask( | 168 .WillRepeatedly(CheckCountAndPostQuitTask( |
170 &count, 10, message_loop_.task_runner())); | 169 &count, 10, message_loop_.task_runner())); |
171 | 170 |
172 // OnError() will be called after the data stream stops while the | 171 // OnError() will be called after the data stream stops while the |
173 // controller is in a recording state. | 172 // controller is in a recording state. |
174 EXPECT_CALL(event_handler, OnError(NotNull(), | 173 EXPECT_CALL(event_handler, OnError(NotNull(), |
175 AudioInputController::NO_DATA_ERROR)) | 174 AudioInputController::NO_DATA_ERROR)) |
176 .Times(Exactly(1)) | 175 .Times(Exactly(1)) |
177 .WillOnce(QuitMessageLoop(&message_loop_)); | 176 .WillOnce(QuitMessageLoop(&message_loop_)); |
178 | 177 |
179 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
180 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 178 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
181 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 179 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
182 | 180 |
183 // Creating the AudioInputController should render an OnCreated() call. | 181 // Creating the AudioInputController should render an OnCreated() call. |
184 scoped_refptr<AudioInputController> controller = | 182 scoped_refptr<AudioInputController> controller = |
185 AudioInputController::Create(audio_manager.get(), | 183 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
186 &event_handler, | 184 AudioManagerBase::kDefaultDeviceId, NULL); |
187 params, | |
188 AudioManagerBase::kDefaultDeviceId, | |
189 NULL); | |
190 ASSERT_TRUE(controller.get()); | 185 ASSERT_TRUE(controller.get()); |
191 | 186 |
192 // Start recording and trigger one OnRecording() call. | 187 // Start recording and trigger one OnRecording() call. |
193 controller->Record(); | 188 controller->Record(); |
194 | 189 |
195 // Record and wait until ten OnData() callbacks are received. | 190 // Record and wait until ten OnData() callbacks are received. |
196 message_loop_.Run(); | 191 message_loop_.Run(); |
197 | 192 |
198 // Stop the stream and verify that OnError() is posted. | 193 // Stop the stream and verify that OnError() is posted. |
199 AudioInputStream* stream = controller->stream_for_testing(); | 194 AudioInputStream* stream = controller->stream_for_testing(); |
200 stream->Stop(); | 195 stream->Stop(); |
201 message_loop_.Run(); | 196 message_loop_.Run(); |
202 | 197 |
203 // Close the AudioInputController synchronously. | 198 // Close the AudioInputController synchronously. |
204 CloseAudioController(controller.get()); | 199 CloseAudioController(controller.get()); |
205 } | 200 } |
206 | 201 |
207 // Test that AudioInputController rejects insanely large packet sizes. | 202 // Test that AudioInputController rejects insanely large packet sizes. |
208 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { | 203 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { |
209 // Create an audio device with a very large packet size. | 204 // Create an audio device with a very large packet size. |
210 MockAudioInputControllerEventHandler event_handler; | 205 MockAudioInputControllerEventHandler event_handler; |
211 | 206 |
212 // OnCreated() shall not be called in this test. | 207 // OnCreated() shall not be called in this test. |
213 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 208 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
214 .Times(Exactly(0)); | 209 .Times(Exactly(0)); |
215 | 210 |
216 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
217 AudioParameters params(AudioParameters::AUDIO_FAKE, | 211 AudioParameters params(AudioParameters::AUDIO_FAKE, |
218 kChannelLayout, | 212 kChannelLayout, |
219 kSampleRate, | 213 kSampleRate, |
220 kBitsPerSample, | 214 kBitsPerSample, |
221 kSamplesPerPacket * 1000); | 215 kSamplesPerPacket * 1000); |
222 scoped_refptr<AudioInputController> controller = | 216 scoped_refptr<AudioInputController> controller = |
223 AudioInputController::Create(audio_manager.get(), | 217 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
224 &event_handler, | 218 AudioManagerBase::kDefaultDeviceId, NULL); |
225 params, | |
226 AudioManagerBase::kDefaultDeviceId, | |
227 NULL); | |
228 ASSERT_FALSE(controller.get()); | 219 ASSERT_FALSE(controller.get()); |
229 } | 220 } |
230 | 221 |
231 // Test calling AudioInputController::Close multiple times. | 222 // Test calling AudioInputController::Close multiple times. |
232 TEST_F(AudioInputControllerTest, CloseTwice) { | 223 TEST_F(AudioInputControllerTest, CloseTwice) { |
233 MockAudioInputControllerEventHandler event_handler; | 224 MockAudioInputControllerEventHandler event_handler; |
234 | 225 |
235 // OnRecording() will be called only once. | 226 // OnRecording() will be called only once. |
236 EXPECT_CALL(event_handler, OnCreated(NotNull())); | 227 EXPECT_CALL(event_handler, OnCreated(NotNull())); |
237 | 228 |
238 // OnRecording() will be called only once. | 229 // OnRecording() will be called only once. |
239 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 230 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
240 .Times(Exactly(1)); | 231 .Times(Exactly(1)); |
241 | 232 |
242 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
243 AudioParameters params(AudioParameters::AUDIO_FAKE, | 233 AudioParameters params(AudioParameters::AUDIO_FAKE, |
244 kChannelLayout, | 234 kChannelLayout, |
245 kSampleRate, | 235 kSampleRate, |
246 kBitsPerSample, | 236 kBitsPerSample, |
247 kSamplesPerPacket); | 237 kSamplesPerPacket); |
248 scoped_refptr<AudioInputController> controller = | 238 scoped_refptr<AudioInputController> controller = |
249 AudioInputController::Create(audio_manager.get(), | 239 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
250 &event_handler, | 240 AudioManagerBase::kDefaultDeviceId, NULL); |
251 params, | |
252 AudioManagerBase::kDefaultDeviceId, | |
253 NULL); | |
254 ASSERT_TRUE(controller.get()); | 241 ASSERT_TRUE(controller.get()); |
255 | 242 |
256 controller->Record(); | 243 controller->Record(); |
257 | 244 |
258 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 245 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
259 base::MessageLoop::current()->Run(); | 246 base::MessageLoop::current()->Run(); |
260 | 247 |
261 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 248 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
262 base::MessageLoop::current()->Run(); | 249 base::MessageLoop::current()->Run(); |
263 } | 250 } |
264 | 251 |
265 } // namespace media | 252 } // namespace media |
OLD | NEW |