OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/synchronization/waitable_event.h" | |
11 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
13 #include "base/threading/thread.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
13 #include "content/browser/speech/proto/google_streaming_api.pb.h" | 15 #include "content/browser/speech/proto/google_streaming_api.pb.h" |
14 #include "content/browser/speech/speech_recognition_engine.h" | 16 #include "content/browser/speech/speech_recognition_engine.h" |
15 #include "content/browser/speech/speech_recognizer_impl.h" | 17 #include "content/browser/speech/speech_recognizer_impl.h" |
16 #include "content/public/browser/speech_recognition_event_listener.h" | 18 #include "content/public/browser/speech_recognition_event_listener.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "media/audio/audio_device_description.h" | 20 #include "media/audio/audio_device_description.h" |
21 #include "media/audio/audio_system_impl.h" | |
19 #include "media/audio/fake_audio_input_stream.h" | 22 #include "media/audio/fake_audio_input_stream.h" |
20 #include "media/audio/fake_audio_output_stream.h" | 23 #include "media/audio/fake_audio_output_stream.h" |
21 #include "media/audio/mock_audio_manager.h" | 24 #include "media/audio/mock_audio_manager.h" |
22 #include "media/audio/test_audio_input_controller_factory.h" | 25 #include "media/audio/test_audio_input_controller_factory.h" |
23 #include "media/base/audio_bus.h" | 26 #include "media/base/audio_bus.h" |
27 #include "media/base/test_helpers.h" | |
24 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
25 #include "net/url_request/test_url_fetcher_factory.h" | 29 #include "net/url_request/test_url_fetcher_factory.h" |
26 #include "net/url_request/url_request_status.h" | 30 #include "net/url_request/url_request_status.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
28 | 32 |
29 using media::AudioInputController; | 33 using media::AudioInputController; |
30 using media::AudioInputStream; | 34 using media::AudioInputStream; |
31 using media::AudioOutputStream; | 35 using media::AudioOutputStream; |
32 using media::AudioParameters; | 36 using media::AudioParameters; |
33 using media::TestAudioInputController; | 37 using media::TestAudioInputController; |
34 using media::TestAudioInputControllerFactory; | 38 using media::TestAudioInputControllerFactory; |
35 | 39 |
36 namespace content { | 40 namespace content { |
37 | 41 |
38 class SpeechRecognizerImplTest : public SpeechRecognitionEventListener, | 42 class SpeechRecognizerImplTest : public SpeechRecognitionEventListener, |
39 public testing::Test { | 43 public testing::Test { |
40 public: | 44 public: |
41 SpeechRecognizerImplTest() | 45 SpeechRecognizerImplTest() |
42 : recognition_started_(false), | 46 : audio_thread_("AudioThread"), |
tommi (sloooow) - chröme
2017/02/05 20:14:54
nit: SpeechAudioThread? Nice improvement to reall
o1ka
2017/02/06 12:06:08
Done.
| |
47 recognition_started_(false), | |
43 recognition_ended_(false), | 48 recognition_ended_(false), |
44 result_received_(false), | 49 result_received_(false), |
45 audio_started_(false), | 50 audio_started_(false), |
46 audio_ended_(false), | 51 audio_ended_(false), |
47 sound_started_(false), | 52 sound_started_(false), |
48 sound_ended_(false), | 53 sound_ended_(false), |
49 error_(SPEECH_RECOGNITION_ERROR_NONE), | 54 error_(SPEECH_RECOGNITION_ERROR_NONE), |
50 volume_(-1.0f) { | 55 volume_(-1.0f) { |
51 // SpeechRecognizer takes ownership of sr_engine. | 56 // SpeechRecognizer takes ownership of sr_engine. |
52 SpeechRecognitionEngine* sr_engine = | 57 SpeechRecognitionEngine* sr_engine = |
53 new SpeechRecognitionEngine(NULL /* URLRequestContextGetter */); | 58 new SpeechRecognitionEngine(NULL /* URLRequestContextGetter */); |
54 SpeechRecognitionEngine::Config config; | 59 SpeechRecognitionEngine::Config config; |
55 config.audio_num_bits_per_sample = | 60 config.audio_num_bits_per_sample = |
56 SpeechRecognizerImpl::kNumBitsPerAudioSample; | 61 SpeechRecognizerImpl::kNumBitsPerAudioSample; |
57 config.audio_sample_rate = SpeechRecognizerImpl::kAudioSampleRate; | 62 config.audio_sample_rate = SpeechRecognizerImpl::kAudioSampleRate; |
58 config.filter_profanities = false; | 63 config.filter_profanities = false; |
59 sr_engine->SetConfig(config); | 64 sr_engine->SetConfig(config); |
60 | 65 |
61 const int kTestingSessionId = 1; | 66 const int kTestingSessionId = 1; |
67 | |
68 audio_thread_.StartAndWaitForTesting(); | |
69 audio_manager_.reset( | |
70 new media::MockAudioManager(audio_thread_.task_runner())); | |
71 audio_manager_->SetInputStreamParameters( | |
72 media::AudioParameters::UnavailableDeviceParams()); | |
73 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | |
62 recognizer_ = new SpeechRecognizerImpl( | 74 recognizer_ = new SpeechRecognizerImpl( |
63 this, kTestingSessionId, false, false, sr_engine); | 75 this, audio_system_.get(), kTestingSessionId, false, false, sr_engine); |
64 audio_manager_.reset( | |
65 new media::MockAudioManager(base::ThreadTaskRunnerHandle::Get().get())); | |
66 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); | |
67 | 76 |
68 int audio_packet_length_bytes = | 77 int audio_packet_length_bytes = |
69 (SpeechRecognizerImpl::kAudioSampleRate * | 78 (SpeechRecognizerImpl::kAudioSampleRate * |
70 SpeechRecognitionEngine::kAudioPacketIntervalMs * | 79 SpeechRecognitionEngine::kAudioPacketIntervalMs * |
71 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * | 80 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * |
72 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); | 81 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); |
73 audio_packet_.resize(audio_packet_length_bytes); | 82 audio_packet_.resize(audio_packet_length_bytes); |
74 | 83 |
75 const int channels = | 84 const int channels = |
76 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout); | 85 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout); |
77 bytes_per_sample_ = SpeechRecognizerImpl::kNumBitsPerAudioSample / 8; | 86 bytes_per_sample_ = SpeechRecognizerImpl::kNumBitsPerAudioSample / 8; |
78 const int frames = audio_packet_length_bytes / channels / bytes_per_sample_; | 87 const int frames = audio_packet_length_bytes / channels / bytes_per_sample_; |
79 audio_bus_ = media::AudioBus::Create(channels, frames); | 88 audio_bus_ = media::AudioBus::Create(channels, frames); |
80 audio_bus_->Zero(); | 89 audio_bus_->Zero(); |
81 } | 90 } |
82 | 91 |
92 ~SpeechRecognizerImplTest() override { | |
93 // Deleting |audio_manager_| on audio thread. | |
94 audio_manager_.reset(); | |
95 audio_thread_.Stop(); | |
96 } | |
97 | |
83 void CheckEventsConsistency() { | 98 void CheckEventsConsistency() { |
84 // Note: "!x || y" == "x implies y". | 99 // Note: "!x || y" == "x implies y". |
85 EXPECT_TRUE(!recognition_ended_ || recognition_started_); | 100 EXPECT_TRUE(!recognition_ended_ || recognition_started_); |
86 EXPECT_TRUE(!audio_ended_ || audio_started_); | 101 EXPECT_TRUE(!audio_ended_ || audio_started_); |
87 EXPECT_TRUE(!sound_ended_ || sound_started_); | 102 EXPECT_TRUE(!sound_ended_ || sound_started_); |
88 EXPECT_TRUE(!audio_started_ || recognition_started_); | 103 EXPECT_TRUE(!audio_started_ || recognition_started_); |
89 EXPECT_TRUE(!sound_started_ || audio_started_); | 104 EXPECT_TRUE(!sound_started_ || audio_started_); |
90 EXPECT_TRUE(!audio_ended_ || (sound_ended_ || !sound_started_)); | 105 EXPECT_TRUE(!audio_ended_ || (sound_ended_ || !sound_started_)); |
91 EXPECT_TRUE(!recognition_ended_ || (audio_ended_ || !audio_started_)); | 106 EXPECT_TRUE(!recognition_ended_ || (audio_ended_ || !audio_started_)); |
92 } | 107 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 } | 197 } |
183 CopyPacketToAudioBus(); | 198 CopyPacketToAudioBus(); |
184 } | 199 } |
185 | 200 |
186 void OnData(media::AudioBus* data) { | 201 void OnData(media::AudioBus* data) { |
187 auto* writer = | 202 auto* writer = |
188 static_cast<AudioInputController::SyncWriter*>(recognizer_.get()); | 203 static_cast<AudioInputController::SyncWriter*>(recognizer_.get()); |
189 writer->Write(data, 0.0, false, 0); | 204 writer->Write(data, 0.0, false, 0); |
190 } | 205 } |
191 | 206 |
207 void WaitForAudioThreadToPostDeviceInfo() { | |
208 media::WaitableMessageLoopEvent event; | |
209 audio_thread_.task_runner()->PostTaskAndReply( | |
210 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); | |
211 // Runs the loop and waits for the |audio_thread_| to call event's closure, | |
212 // which means AudioSystem reply containing device parameters is already | |
213 // queued on the main thread. | |
214 event.RunAndWait(); | |
215 } | |
216 | |
192 protected: | 217 protected: |
193 TestBrowserThreadBundle thread_bundle_; | 218 TestBrowserThreadBundle thread_bundle_; |
194 scoped_refptr<SpeechRecognizerImpl> recognizer_; | 219 scoped_refptr<SpeechRecognizerImpl> recognizer_; |
195 media::ScopedAudioManagerPtr audio_manager_; | 220 base::Thread audio_thread_; |
221 media::MockAudioManager::UniquePtr audio_manager_; | |
222 std::unique_ptr<media::AudioSystem> audio_system_; | |
196 bool recognition_started_; | 223 bool recognition_started_; |
197 bool recognition_ended_; | 224 bool recognition_ended_; |
198 bool result_received_; | 225 bool result_received_; |
199 bool audio_started_; | 226 bool audio_started_; |
200 bool audio_ended_; | 227 bool audio_ended_; |
201 bool sound_started_; | 228 bool sound_started_; |
202 bool sound_ended_; | 229 bool sound_ended_; |
203 SpeechRecognitionErrorCode error_; | 230 SpeechRecognitionErrorCode error_; |
204 net::TestURLFetcherFactory url_fetcher_factory_; | 231 net::TestURLFetcherFactory url_fetcher_factory_; |
205 TestAudioInputControllerFactory audio_input_controller_factory_; | 232 TestAudioInputControllerFactory audio_input_controller_factory_; |
206 std::vector<uint8_t> audio_packet_; | 233 std::vector<uint8_t> audio_packet_; |
207 std::unique_ptr<media::AudioBus> audio_bus_; | 234 std::unique_ptr<media::AudioBus> audio_bus_; |
208 int bytes_per_sample_; | 235 int bytes_per_sample_; |
209 float volume_; | 236 float volume_; |
210 float noise_volume_; | 237 float noise_volume_; |
211 }; | 238 }; |
212 | 239 |
240 TEST_F(SpeechRecognizerImplTest, StartNoInputDevices) { | |
241 // Check for callbacks when stopping record before any audio gets recorded. | |
242 audio_manager_->SetHasInputDevices(false); | |
243 recognizer_->StartRecognition( | |
244 media::AudioDeviceDescription::kDefaultDeviceId); | |
245 WaitForAudioThreadToPostDeviceInfo(); | |
246 // recognizer_->StopAudioCapture(); | |
tommi (sloooow) - chröme
2017/02/05 20:14:54
remove?
o1ka
2017/02/06 12:06:08
Done.
| |
247 base::RunLoop().RunUntilIdle(); | |
248 EXPECT_TRUE(recognition_started_); | |
249 EXPECT_FALSE(audio_started_); | |
250 EXPECT_FALSE(result_received_); | |
251 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); | |
252 CheckFinalEventsConsistency(); | |
253 } | |
254 | |
255 TEST_F(SpeechRecognizerImplTest, StopBeforeDeviceInfoReceived) { | |
256 // Check for callbacks when stopping record before reply is received from | |
257 // AudioSystem. | |
258 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
259 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
260 | |
261 // Block audio thread. | |
262 audio_thread_.task_runner()->PostTask( | |
263 FROM_HERE, | |
264 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&event))); | |
265 | |
266 recognizer_->StartRecognition( | |
267 media::AudioDeviceDescription::kDefaultDeviceId); | |
268 recognizer_->StopAudioCapture(); | |
269 base::RunLoop().RunUntilIdle(); | |
270 | |
271 // Release audio thread and receive a callback from it. | |
272 event.Signal(); | |
273 WaitForAudioThreadToPostDeviceInfo(); | |
274 base::RunLoop().RunUntilIdle(); | |
275 | |
276 EXPECT_TRUE(recognition_started_); | |
277 EXPECT_FALSE(audio_started_); | |
278 EXPECT_FALSE(result_received_); | |
279 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | |
280 CheckFinalEventsConsistency(); | |
281 } | |
282 | |
283 TEST_F(SpeechRecognizerImplTest, CancelBeforeDeviceInfoReceived) { | |
284 // Check for callbacks when stopping record before reply is received from | |
285 // AudioSystem. | |
286 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
287 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
288 | |
289 // Block audio thread. | |
290 audio_thread_.task_runner()->PostTask( | |
291 FROM_HERE, | |
292 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&event))); | |
293 | |
294 recognizer_->StartRecognition( | |
295 media::AudioDeviceDescription::kDefaultDeviceId); | |
296 recognizer_->AbortRecognition(); | |
297 base::RunLoop().RunUntilIdle(); | |
298 | |
299 // Release audio thread and receive a callback from it. | |
300 event.Signal(); | |
301 WaitForAudioThreadToPostDeviceInfo(); | |
302 base::RunLoop().RunUntilIdle(); | |
303 | |
304 EXPECT_TRUE(recognition_started_); | |
305 EXPECT_FALSE(audio_started_); | |
306 EXPECT_FALSE(result_received_); | |
307 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | |
308 CheckFinalEventsConsistency(); | |
309 } | |
310 | |
213 TEST_F(SpeechRecognizerImplTest, StopNoData) { | 311 TEST_F(SpeechRecognizerImplTest, StopNoData) { |
214 // Check for callbacks when stopping record before any audio gets recorded. | 312 // Check for callbacks when stopping record before any audio gets recorded. |
215 recognizer_->StartRecognition( | 313 recognizer_->StartRecognition( |
216 media::AudioDeviceDescription::kDefaultDeviceId); | 314 media::AudioDeviceDescription::kDefaultDeviceId); |
315 WaitForAudioThreadToPostDeviceInfo(); | |
217 recognizer_->StopAudioCapture(); | 316 recognizer_->StopAudioCapture(); |
218 base::RunLoop().RunUntilIdle(); | 317 base::RunLoop().RunUntilIdle(); |
219 EXPECT_TRUE(recognition_started_); | 318 EXPECT_TRUE(recognition_started_); |
220 EXPECT_FALSE(audio_started_); | 319 EXPECT_FALSE(audio_started_); |
221 EXPECT_FALSE(result_received_); | 320 EXPECT_FALSE(result_received_); |
222 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 321 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
223 CheckFinalEventsConsistency(); | 322 CheckFinalEventsConsistency(); |
224 } | 323 } |
225 | 324 |
226 TEST_F(SpeechRecognizerImplTest, CancelNoData) { | 325 TEST_F(SpeechRecognizerImplTest, CancelNoData) { |
227 // Check for callbacks when canceling recognition before any audio gets | 326 // Check for callbacks when canceling recognition before any audio gets |
228 // recorded. | 327 // recorded. |
229 recognizer_->StartRecognition( | 328 recognizer_->StartRecognition( |
230 media::AudioDeviceDescription::kDefaultDeviceId); | 329 media::AudioDeviceDescription::kDefaultDeviceId); |
330 WaitForAudioThreadToPostDeviceInfo(); | |
231 recognizer_->AbortRecognition(); | 331 recognizer_->AbortRecognition(); |
232 base::RunLoop().RunUntilIdle(); | 332 base::RunLoop().RunUntilIdle(); |
233 EXPECT_TRUE(recognition_started_); | 333 EXPECT_TRUE(recognition_started_); |
234 EXPECT_FALSE(audio_started_); | 334 EXPECT_FALSE(audio_started_); |
235 EXPECT_FALSE(result_received_); | 335 EXPECT_FALSE(result_received_); |
236 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); | 336 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); |
237 CheckFinalEventsConsistency(); | 337 CheckFinalEventsConsistency(); |
238 } | 338 } |
239 | 339 |
240 TEST_F(SpeechRecognizerImplTest, StopWithData) { | 340 TEST_F(SpeechRecognizerImplTest, StopWithData) { |
241 // Start recording, give some data and then stop. This should wait for the | 341 // Start recording, give some data and then stop. This should wait for the |
242 // network callback to arrive before completion. | 342 // network callback to arrive before completion. |
243 recognizer_->StartRecognition( | 343 recognizer_->StartRecognition( |
244 media::AudioDeviceDescription::kDefaultDeviceId); | 344 media::AudioDeviceDescription::kDefaultDeviceId); |
345 WaitForAudioThreadToPostDeviceInfo(); | |
245 base::RunLoop().RunUntilIdle(); | 346 base::RunLoop().RunUntilIdle(); |
246 TestAudioInputController* controller = | 347 TestAudioInputController* controller = |
247 audio_input_controller_factory_.controller(); | 348 audio_input_controller_factory_.controller(); |
248 ASSERT_TRUE(controller); | 349 ASSERT_TRUE(controller); |
249 | 350 |
250 // Try sending 5 chunks of mock audio data and verify that each of them | 351 // Try sending 5 chunks of mock audio data and verify that each of them |
251 // resulted immediately in a packet sent out via the network. This verifies | 352 // resulted immediately in a packet sent out via the network. This verifies |
252 // that we are streaming out encoded data as chunks without waiting for the | 353 // that we are streaming out encoded data as chunks without waiting for the |
253 // full recording to complete. | 354 // full recording to complete. |
254 const size_t kNumChunks = 5; | 355 const size_t kNumChunks = 5; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 EXPECT_TRUE(recognition_ended_); | 398 EXPECT_TRUE(recognition_ended_); |
298 EXPECT_TRUE(result_received_); | 399 EXPECT_TRUE(result_received_); |
299 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 400 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
300 CheckFinalEventsConsistency(); | 401 CheckFinalEventsConsistency(); |
301 } | 402 } |
302 | 403 |
303 TEST_F(SpeechRecognizerImplTest, CancelWithData) { | 404 TEST_F(SpeechRecognizerImplTest, CancelWithData) { |
304 // Start recording, give some data and then cancel. | 405 // Start recording, give some data and then cancel. |
305 recognizer_->StartRecognition( | 406 recognizer_->StartRecognition( |
306 media::AudioDeviceDescription::kDefaultDeviceId); | 407 media::AudioDeviceDescription::kDefaultDeviceId); |
408 WaitForAudioThreadToPostDeviceInfo(); | |
307 base::RunLoop().RunUntilIdle(); | 409 base::RunLoop().RunUntilIdle(); |
308 TestAudioInputController* controller = | 410 TestAudioInputController* controller = |
309 audio_input_controller_factory_.controller(); | 411 audio_input_controller_factory_.controller(); |
310 ASSERT_TRUE(controller); | 412 ASSERT_TRUE(controller); |
311 OnData(audio_bus_.get()); | 413 OnData(audio_bus_.get()); |
312 base::RunLoop().RunUntilIdle(); | 414 base::RunLoop().RunUntilIdle(); |
313 recognizer_->AbortRecognition(); | 415 recognizer_->AbortRecognition(); |
314 base::RunLoop().RunUntilIdle(); | 416 base::RunLoop().RunUntilIdle(); |
315 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 417 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
316 EXPECT_TRUE(recognition_started_); | 418 EXPECT_TRUE(recognition_started_); |
317 EXPECT_TRUE(audio_started_); | 419 EXPECT_TRUE(audio_started_); |
318 EXPECT_FALSE(result_received_); | 420 EXPECT_FALSE(result_received_); |
319 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); | 421 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); |
320 CheckFinalEventsConsistency(); | 422 CheckFinalEventsConsistency(); |
321 } | 423 } |
322 | 424 |
323 TEST_F(SpeechRecognizerImplTest, ConnectionError) { | 425 TEST_F(SpeechRecognizerImplTest, ConnectionError) { |
324 // Start recording, give some data and then stop. Issue the network callback | 426 // Start recording, give some data and then stop. Issue the network callback |
325 // with a connection error and verify that the recognizer bubbles the error up | 427 // with a connection error and verify that the recognizer bubbles the error up |
326 recognizer_->StartRecognition( | 428 recognizer_->StartRecognition( |
327 media::AudioDeviceDescription::kDefaultDeviceId); | 429 media::AudioDeviceDescription::kDefaultDeviceId); |
430 WaitForAudioThreadToPostDeviceInfo(); | |
328 base::RunLoop().RunUntilIdle(); | 431 base::RunLoop().RunUntilIdle(); |
329 TestAudioInputController* controller = | 432 TestAudioInputController* controller = |
330 audio_input_controller_factory_.controller(); | 433 audio_input_controller_factory_.controller(); |
331 ASSERT_TRUE(controller); | 434 ASSERT_TRUE(controller); |
332 OnData(audio_bus_.get()); | 435 OnData(audio_bus_.get()); |
333 base::RunLoop().RunUntilIdle(); | 436 base::RunLoop().RunUntilIdle(); |
334 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 437 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
335 ASSERT_TRUE(fetcher); | 438 ASSERT_TRUE(fetcher); |
336 | 439 |
337 recognizer_->StopAudioCapture(); | 440 recognizer_->StopAudioCapture(); |
(...skipping 16 matching lines...) Expand all Loading... | |
354 EXPECT_FALSE(result_received_); | 457 EXPECT_FALSE(result_received_); |
355 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 458 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
356 CheckFinalEventsConsistency(); | 459 CheckFinalEventsConsistency(); |
357 } | 460 } |
358 | 461 |
359 TEST_F(SpeechRecognizerImplTest, ServerError) { | 462 TEST_F(SpeechRecognizerImplTest, ServerError) { |
360 // Start recording, give some data and then stop. Issue the network callback | 463 // Start recording, give some data and then stop. Issue the network callback |
361 // with a 500 error and verify that the recognizer bubbles the error up | 464 // with a 500 error and verify that the recognizer bubbles the error up |
362 recognizer_->StartRecognition( | 465 recognizer_->StartRecognition( |
363 media::AudioDeviceDescription::kDefaultDeviceId); | 466 media::AudioDeviceDescription::kDefaultDeviceId); |
467 WaitForAudioThreadToPostDeviceInfo(); | |
364 base::RunLoop().RunUntilIdle(); | 468 base::RunLoop().RunUntilIdle(); |
365 TestAudioInputController* controller = | 469 TestAudioInputController* controller = |
366 audio_input_controller_factory_.controller(); | 470 audio_input_controller_factory_.controller(); |
367 ASSERT_TRUE(controller); | 471 ASSERT_TRUE(controller); |
368 OnData(audio_bus_.get()); | 472 OnData(audio_bus_.get()); |
369 base::RunLoop().RunUntilIdle(); | 473 base::RunLoop().RunUntilIdle(); |
370 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 474 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
371 ASSERT_TRUE(fetcher); | 475 ASSERT_TRUE(fetcher); |
372 | 476 |
373 recognizer_->StopAudioCapture(); | 477 recognizer_->StopAudioCapture(); |
(...skipping 14 matching lines...) Expand all Loading... | |
388 EXPECT_TRUE(recognition_ended_); | 492 EXPECT_TRUE(recognition_ended_); |
389 EXPECT_FALSE(result_received_); | 493 EXPECT_FALSE(result_received_); |
390 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 494 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
391 CheckFinalEventsConsistency(); | 495 CheckFinalEventsConsistency(); |
392 } | 496 } |
393 | 497 |
394 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { | 498 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { |
395 // Check if things tear down properly if AudioInputController threw an error. | 499 // Check if things tear down properly if AudioInputController threw an error. |
396 recognizer_->StartRecognition( | 500 recognizer_->StartRecognition( |
397 media::AudioDeviceDescription::kDefaultDeviceId); | 501 media::AudioDeviceDescription::kDefaultDeviceId); |
502 WaitForAudioThreadToPostDeviceInfo(); | |
398 base::RunLoop().RunUntilIdle(); | 503 base::RunLoop().RunUntilIdle(); |
399 TestAudioInputController* controller = | 504 TestAudioInputController* controller = |
400 audio_input_controller_factory_.controller(); | 505 audio_input_controller_factory_.controller(); |
401 ASSERT_TRUE(controller); | 506 ASSERT_TRUE(controller); |
402 controller->event_handler()->OnError(controller, | 507 controller->event_handler()->OnError(controller, |
403 AudioInputController::UNKNOWN_ERROR); | 508 AudioInputController::UNKNOWN_ERROR); |
404 base::RunLoop().RunUntilIdle(); | 509 base::RunLoop().RunUntilIdle(); |
405 EXPECT_TRUE(recognition_started_); | 510 EXPECT_TRUE(recognition_started_); |
406 EXPECT_FALSE(audio_started_); | 511 EXPECT_FALSE(audio_started_); |
407 EXPECT_FALSE(result_received_); | 512 EXPECT_FALSE(result_received_); |
408 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); | 513 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); |
409 CheckFinalEventsConsistency(); | 514 CheckFinalEventsConsistency(); |
410 } | 515 } |
411 | 516 |
412 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { | 517 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { |
413 // Check if things tear down properly if AudioInputController threw an error | 518 // Check if things tear down properly if AudioInputController threw an error |
414 // after giving some audio data. | 519 // after giving some audio data. |
415 recognizer_->StartRecognition( | 520 recognizer_->StartRecognition( |
416 media::AudioDeviceDescription::kDefaultDeviceId); | 521 media::AudioDeviceDescription::kDefaultDeviceId); |
522 WaitForAudioThreadToPostDeviceInfo(); | |
417 base::RunLoop().RunUntilIdle(); | 523 base::RunLoop().RunUntilIdle(); |
418 TestAudioInputController* controller = | 524 TestAudioInputController* controller = |
419 audio_input_controller_factory_.controller(); | 525 audio_input_controller_factory_.controller(); |
420 ASSERT_TRUE(controller); | 526 ASSERT_TRUE(controller); |
421 OnData(audio_bus_.get()); | 527 OnData(audio_bus_.get()); |
422 controller->event_handler()->OnError(controller, | 528 controller->event_handler()->OnError(controller, |
423 AudioInputController::UNKNOWN_ERROR); | 529 AudioInputController::UNKNOWN_ERROR); |
424 base::RunLoop().RunUntilIdle(); | 530 base::RunLoop().RunUntilIdle(); |
425 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 531 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
426 EXPECT_TRUE(recognition_started_); | 532 EXPECT_TRUE(recognition_started_); |
427 EXPECT_TRUE(audio_started_); | 533 EXPECT_TRUE(audio_started_); |
428 EXPECT_FALSE(result_received_); | 534 EXPECT_FALSE(result_received_); |
429 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); | 535 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); |
430 CheckFinalEventsConsistency(); | 536 CheckFinalEventsConsistency(); |
431 } | 537 } |
432 | 538 |
433 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { | 539 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { |
434 // Start recording and give a lot of packets with audio samples set to zero. | 540 // Start recording and give a lot of packets with audio samples set to zero. |
435 // This should trigger the no-speech detector and issue a callback. | 541 // This should trigger the no-speech detector and issue a callback. |
436 recognizer_->StartRecognition( | 542 recognizer_->StartRecognition( |
437 media::AudioDeviceDescription::kDefaultDeviceId); | 543 media::AudioDeviceDescription::kDefaultDeviceId); |
544 WaitForAudioThreadToPostDeviceInfo(); | |
438 base::RunLoop().RunUntilIdle(); | 545 base::RunLoop().RunUntilIdle(); |
439 TestAudioInputController* controller = | 546 TestAudioInputController* controller = |
440 audio_input_controller_factory_.controller(); | 547 audio_input_controller_factory_.controller(); |
441 ASSERT_TRUE(controller); | 548 ASSERT_TRUE(controller); |
442 | 549 |
443 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / | 550 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / |
444 SpeechRecognitionEngine::kAudioPacketIntervalMs + 1; | 551 SpeechRecognitionEngine::kAudioPacketIntervalMs + 1; |
445 // The vector is already filled with zero value samples on create. | 552 // The vector is already filled with zero value samples on create. |
446 for (int i = 0; i < num_packets; ++i) { | 553 for (int i = 0; i < num_packets; ++i) { |
447 OnData(audio_bus_.get()); | 554 OnData(audio_bus_.get()); |
448 } | 555 } |
449 base::RunLoop().RunUntilIdle(); | 556 base::RunLoop().RunUntilIdle(); |
450 EXPECT_TRUE(recognition_started_); | 557 EXPECT_TRUE(recognition_started_); |
451 EXPECT_TRUE(audio_started_); | 558 EXPECT_TRUE(audio_started_); |
452 EXPECT_FALSE(result_received_); | 559 EXPECT_FALSE(result_received_); |
453 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); | 560 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
454 CheckFinalEventsConsistency(); | 561 CheckFinalEventsConsistency(); |
455 } | 562 } |
456 | 563 |
457 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { | 564 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { |
458 // Start recording and give a lot of packets with audio samples set to zero | 565 // Start recording and give a lot of packets with audio samples set to zero |
459 // and then some more with reasonably loud audio samples. This should be | 566 // and then some more with reasonably loud audio samples. This should be |
460 // treated as normal speech input and the no-speech detector should not get | 567 // treated as normal speech input and the no-speech detector should not get |
461 // triggered. | 568 // triggered. |
462 recognizer_->StartRecognition( | 569 recognizer_->StartRecognition( |
463 media::AudioDeviceDescription::kDefaultDeviceId); | 570 media::AudioDeviceDescription::kDefaultDeviceId); |
571 WaitForAudioThreadToPostDeviceInfo(); | |
464 base::RunLoop().RunUntilIdle(); | 572 base::RunLoop().RunUntilIdle(); |
465 TestAudioInputController* controller = | 573 TestAudioInputController* controller = |
466 audio_input_controller_factory_.controller(); | 574 audio_input_controller_factory_.controller(); |
467 ASSERT_TRUE(controller); | 575 ASSERT_TRUE(controller); |
468 controller = audio_input_controller_factory_.controller(); | 576 controller = audio_input_controller_factory_.controller(); |
469 ASSERT_TRUE(controller); | 577 ASSERT_TRUE(controller); |
470 | 578 |
471 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / | 579 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / |
472 SpeechRecognitionEngine::kAudioPacketIntervalMs; | 580 SpeechRecognitionEngine::kAudioPacketIntervalMs; |
473 | 581 |
(...skipping 17 matching lines...) Expand all Loading... | |
491 CheckFinalEventsConsistency(); | 599 CheckFinalEventsConsistency(); |
492 } | 600 } |
493 | 601 |
494 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { | 602 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { |
495 // Start recording and give a lot of packets with audio samples set to zero | 603 // Start recording and give a lot of packets with audio samples set to zero |
496 // and then some more with reasonably loud audio samples. Check that we don't | 604 // and then some more with reasonably loud audio samples. Check that we don't |
497 // get the callback during estimation phase, then get zero for the silence | 605 // get the callback during estimation phase, then get zero for the silence |
498 // samples and proper volume for the loud audio. | 606 // samples and proper volume for the loud audio. |
499 recognizer_->StartRecognition( | 607 recognizer_->StartRecognition( |
500 media::AudioDeviceDescription::kDefaultDeviceId); | 608 media::AudioDeviceDescription::kDefaultDeviceId); |
609 WaitForAudioThreadToPostDeviceInfo(); | |
501 base::RunLoop().RunUntilIdle(); | 610 base::RunLoop().RunUntilIdle(); |
502 TestAudioInputController* controller = | 611 TestAudioInputController* controller = |
503 audio_input_controller_factory_.controller(); | 612 audio_input_controller_factory_.controller(); |
504 ASSERT_TRUE(controller); | 613 ASSERT_TRUE(controller); |
505 controller = audio_input_controller_factory_.controller(); | 614 controller = audio_input_controller_factory_.controller(); |
506 ASSERT_TRUE(controller); | 615 ASSERT_TRUE(controller); |
507 | 616 |
508 // Feed some samples to begin with for the endpointer to do noise estimation. | 617 // Feed some samples to begin with for the endpointer to do noise estimation. |
509 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / | 618 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / |
510 SpeechRecognitionEngine::kAudioPacketIntervalMs; | 619 SpeechRecognitionEngine::kAudioPacketIntervalMs; |
(...skipping 17 matching lines...) Expand all Loading... | |
528 | 637 |
529 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 638 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
530 EXPECT_FALSE(audio_ended_); | 639 EXPECT_FALSE(audio_ended_); |
531 EXPECT_FALSE(recognition_ended_); | 640 EXPECT_FALSE(recognition_ended_); |
532 recognizer_->AbortRecognition(); | 641 recognizer_->AbortRecognition(); |
533 base::RunLoop().RunUntilIdle(); | 642 base::RunLoop().RunUntilIdle(); |
534 CheckFinalEventsConsistency(); | 643 CheckFinalEventsConsistency(); |
535 } | 644 } |
536 | 645 |
537 } // namespace content | 646 } // namespace content |
OLD | NEW |