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