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