Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Side by Side Diff: content/browser/speech/speech_recognizer_impl_unittest.cc

Issue 2675713002: Switch Speech Recognition to asynchronous callback-based AudioManager interactions. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"),
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
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 std::unique_ptr<media::MockAudioManager, media::AudioManagerDeleter>
222 audio_manager_;
223 std::unique_ptr<media::AudioSystem> audio_system_;
196 bool recognition_started_; 224 bool recognition_started_;
197 bool recognition_ended_; 225 bool recognition_ended_;
198 bool result_received_; 226 bool result_received_;
199 bool audio_started_; 227 bool audio_started_;
200 bool audio_ended_; 228 bool audio_ended_;
201 bool sound_started_; 229 bool sound_started_;
202 bool sound_ended_; 230 bool sound_ended_;
203 SpeechRecognitionErrorCode error_; 231 SpeechRecognitionErrorCode error_;
204 net::TestURLFetcherFactory url_fetcher_factory_; 232 net::TestURLFetcherFactory url_fetcher_factory_;
205 TestAudioInputControllerFactory audio_input_controller_factory_; 233 TestAudioInputControllerFactory audio_input_controller_factory_;
206 std::vector<uint8_t> audio_packet_; 234 std::vector<uint8_t> audio_packet_;
207 std::unique_ptr<media::AudioBus> audio_bus_; 235 std::unique_ptr<media::AudioBus> audio_bus_;
208 int bytes_per_sample_; 236 int bytes_per_sample_;
209 float volume_; 237 float volume_;
210 float noise_volume_; 238 float noise_volume_;
211 }; 239 };
212 240
241 TEST_F(SpeechRecognizerImplTest, StartNoInputDevices) {
242 // Check for callbacks when stopping record before any audio gets recorded.
243 audio_manager_->SetHasInputDevices(false);
244 recognizer_->StartRecognition(
245 media::AudioDeviceDescription::kDefaultDeviceId);
246 WaitForAudioThreadToPostDeviceInfo();
247 // recognizer_->StopAudioCapture();
248 base::RunLoop().RunUntilIdle();
249 EXPECT_TRUE(recognition_started_);
250 EXPECT_FALSE(audio_started_);
251 EXPECT_FALSE(result_received_);
252 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_);
253 CheckFinalEventsConsistency();
254 }
255
256 TEST_F(SpeechRecognizerImplTest, StopBeforeDeviceInfoReceived) {
257 // Check for callbacks when stopping record before reply is received from
258 // AudioSystem.
259 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
260 base::WaitableEvent::InitialState::NOT_SIGNALED);
261
262 // Block audio thread.
263 audio_thread_.task_runner()->PostTask(
264 FROM_HERE,
265 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&event)));
266
267 recognizer_->StartRecognition(
268 media::AudioDeviceDescription::kDefaultDeviceId);
269 recognizer_->StopAudioCapture();
270 base::RunLoop().RunUntilIdle();
271
272 // Release audio thread and receive a callback from it.
273 event.Signal();
274 WaitForAudioThreadToPostDeviceInfo();
275 base::RunLoop().RunUntilIdle();
276
277 EXPECT_TRUE(recognition_started_);
278 EXPECT_FALSE(audio_started_);
279 EXPECT_FALSE(result_received_);
280 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
281 CheckFinalEventsConsistency();
282 }
283
284 TEST_F(SpeechRecognizerImplTest, CancelBeforeDeviceInfoReceived) {
285 // Check for callbacks when stopping record before reply is received from
286 // AudioSystem.
287 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
288 base::WaitableEvent::InitialState::NOT_SIGNALED);
289
290 // Block audio thread.
291 audio_thread_.task_runner()->PostTask(
292 FROM_HERE,
293 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&event)));
294
295 recognizer_->StartRecognition(
296 media::AudioDeviceDescription::kDefaultDeviceId);
297 recognizer_->AbortRecognition();
298 base::RunLoop().RunUntilIdle();
299
300 // Release audio thread and receive a callback from it.
301 event.Signal();
302 WaitForAudioThreadToPostDeviceInfo();
303 base::RunLoop().RunUntilIdle();
304
305 EXPECT_TRUE(recognition_started_);
306 EXPECT_FALSE(audio_started_);
307 EXPECT_FALSE(result_received_);
308 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
309 CheckFinalEventsConsistency();
310 }
311
213 TEST_F(SpeechRecognizerImplTest, StopNoData) { 312 TEST_F(SpeechRecognizerImplTest, StopNoData) {
214 // Check for callbacks when stopping record before any audio gets recorded. 313 // Check for callbacks when stopping record before any audio gets recorded.
215 recognizer_->StartRecognition( 314 recognizer_->StartRecognition(
216 media::AudioDeviceDescription::kDefaultDeviceId); 315 media::AudioDeviceDescription::kDefaultDeviceId);
316 WaitForAudioThreadToPostDeviceInfo();
217 recognizer_->StopAudioCapture(); 317 recognizer_->StopAudioCapture();
218 base::RunLoop().RunUntilIdle(); 318 base::RunLoop().RunUntilIdle();
219 EXPECT_TRUE(recognition_started_); 319 EXPECT_TRUE(recognition_started_);
220 EXPECT_FALSE(audio_started_); 320 EXPECT_FALSE(audio_started_);
221 EXPECT_FALSE(result_received_); 321 EXPECT_FALSE(result_received_);
222 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); 322 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
223 CheckFinalEventsConsistency(); 323 CheckFinalEventsConsistency();
224 } 324 }
225 325
226 TEST_F(SpeechRecognizerImplTest, CancelNoData) { 326 TEST_F(SpeechRecognizerImplTest, CancelNoData) {
227 // Check for callbacks when canceling recognition before any audio gets 327 // Check for callbacks when canceling recognition before any audio gets
228 // recorded. 328 // recorded.
229 recognizer_->StartRecognition( 329 recognizer_->StartRecognition(
230 media::AudioDeviceDescription::kDefaultDeviceId); 330 media::AudioDeviceDescription::kDefaultDeviceId);
331 WaitForAudioThreadToPostDeviceInfo();
231 recognizer_->AbortRecognition(); 332 recognizer_->AbortRecognition();
232 base::RunLoop().RunUntilIdle(); 333 base::RunLoop().RunUntilIdle();
233 EXPECT_TRUE(recognition_started_); 334 EXPECT_TRUE(recognition_started_);
234 EXPECT_FALSE(audio_started_); 335 EXPECT_FALSE(audio_started_);
235 EXPECT_FALSE(result_received_); 336 EXPECT_FALSE(result_received_);
236 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); 337 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_);
237 CheckFinalEventsConsistency(); 338 CheckFinalEventsConsistency();
238 } 339 }
239 340
240 TEST_F(SpeechRecognizerImplTest, StopWithData) { 341 TEST_F(SpeechRecognizerImplTest, StopWithData) {
241 // Start recording, give some data and then stop. This should wait for the 342 // Start recording, give some data and then stop. This should wait for the
242 // network callback to arrive before completion. 343 // network callback to arrive before completion.
243 recognizer_->StartRecognition( 344 recognizer_->StartRecognition(
244 media::AudioDeviceDescription::kDefaultDeviceId); 345 media::AudioDeviceDescription::kDefaultDeviceId);
346 WaitForAudioThreadToPostDeviceInfo();
245 base::RunLoop().RunUntilIdle(); 347 base::RunLoop().RunUntilIdle();
246 TestAudioInputController* controller = 348 TestAudioInputController* controller =
247 audio_input_controller_factory_.controller(); 349 audio_input_controller_factory_.controller();
248 ASSERT_TRUE(controller); 350 ASSERT_TRUE(controller);
249 351
250 // Try sending 5 chunks of mock audio data and verify that each of them 352 // 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 353 // 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 354 // that we are streaming out encoded data as chunks without waiting for the
253 // full recording to complete. 355 // full recording to complete.
254 const size_t kNumChunks = 5; 356 const size_t kNumChunks = 5;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 EXPECT_TRUE(recognition_ended_); 399 EXPECT_TRUE(recognition_ended_);
298 EXPECT_TRUE(result_received_); 400 EXPECT_TRUE(result_received_);
299 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); 401 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
300 CheckFinalEventsConsistency(); 402 CheckFinalEventsConsistency();
301 } 403 }
302 404
303 TEST_F(SpeechRecognizerImplTest, CancelWithData) { 405 TEST_F(SpeechRecognizerImplTest, CancelWithData) {
304 // Start recording, give some data and then cancel. 406 // Start recording, give some data and then cancel.
305 recognizer_->StartRecognition( 407 recognizer_->StartRecognition(
306 media::AudioDeviceDescription::kDefaultDeviceId); 408 media::AudioDeviceDescription::kDefaultDeviceId);
409 WaitForAudioThreadToPostDeviceInfo();
307 base::RunLoop().RunUntilIdle(); 410 base::RunLoop().RunUntilIdle();
308 TestAudioInputController* controller = 411 TestAudioInputController* controller =
309 audio_input_controller_factory_.controller(); 412 audio_input_controller_factory_.controller();
310 ASSERT_TRUE(controller); 413 ASSERT_TRUE(controller);
311 OnData(audio_bus_.get()); 414 OnData(audio_bus_.get());
312 base::RunLoop().RunUntilIdle(); 415 base::RunLoop().RunUntilIdle();
313 recognizer_->AbortRecognition(); 416 recognizer_->AbortRecognition();
314 base::RunLoop().RunUntilIdle(); 417 base::RunLoop().RunUntilIdle();
315 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 418 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
316 EXPECT_TRUE(recognition_started_); 419 EXPECT_TRUE(recognition_started_);
317 EXPECT_TRUE(audio_started_); 420 EXPECT_TRUE(audio_started_);
318 EXPECT_FALSE(result_received_); 421 EXPECT_FALSE(result_received_);
319 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); 422 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_);
320 CheckFinalEventsConsistency(); 423 CheckFinalEventsConsistency();
321 } 424 }
322 425
323 TEST_F(SpeechRecognizerImplTest, ConnectionError) { 426 TEST_F(SpeechRecognizerImplTest, ConnectionError) {
324 // Start recording, give some data and then stop. Issue the network callback 427 // 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 428 // with a connection error and verify that the recognizer bubbles the error up
326 recognizer_->StartRecognition( 429 recognizer_->StartRecognition(
327 media::AudioDeviceDescription::kDefaultDeviceId); 430 media::AudioDeviceDescription::kDefaultDeviceId);
431 WaitForAudioThreadToPostDeviceInfo();
328 base::RunLoop().RunUntilIdle(); 432 base::RunLoop().RunUntilIdle();
329 TestAudioInputController* controller = 433 TestAudioInputController* controller =
330 audio_input_controller_factory_.controller(); 434 audio_input_controller_factory_.controller();
331 ASSERT_TRUE(controller); 435 ASSERT_TRUE(controller);
332 OnData(audio_bus_.get()); 436 OnData(audio_bus_.get());
333 base::RunLoop().RunUntilIdle(); 437 base::RunLoop().RunUntilIdle();
334 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 438 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
335 ASSERT_TRUE(fetcher); 439 ASSERT_TRUE(fetcher);
336 440
337 recognizer_->StopAudioCapture(); 441 recognizer_->StopAudioCapture();
(...skipping 16 matching lines...) Expand all
354 EXPECT_FALSE(result_received_); 458 EXPECT_FALSE(result_received_);
355 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); 459 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_);
356 CheckFinalEventsConsistency(); 460 CheckFinalEventsConsistency();
357 } 461 }
358 462
359 TEST_F(SpeechRecognizerImplTest, ServerError) { 463 TEST_F(SpeechRecognizerImplTest, ServerError) {
360 // Start recording, give some data and then stop. Issue the network callback 464 // 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 465 // with a 500 error and verify that the recognizer bubbles the error up
362 recognizer_->StartRecognition( 466 recognizer_->StartRecognition(
363 media::AudioDeviceDescription::kDefaultDeviceId); 467 media::AudioDeviceDescription::kDefaultDeviceId);
468 WaitForAudioThreadToPostDeviceInfo();
364 base::RunLoop().RunUntilIdle(); 469 base::RunLoop().RunUntilIdle();
365 TestAudioInputController* controller = 470 TestAudioInputController* controller =
366 audio_input_controller_factory_.controller(); 471 audio_input_controller_factory_.controller();
367 ASSERT_TRUE(controller); 472 ASSERT_TRUE(controller);
368 OnData(audio_bus_.get()); 473 OnData(audio_bus_.get());
369 base::RunLoop().RunUntilIdle(); 474 base::RunLoop().RunUntilIdle();
370 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 475 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
371 ASSERT_TRUE(fetcher); 476 ASSERT_TRUE(fetcher);
372 477
373 recognizer_->StopAudioCapture(); 478 recognizer_->StopAudioCapture();
(...skipping 14 matching lines...) Expand all
388 EXPECT_TRUE(recognition_ended_); 493 EXPECT_TRUE(recognition_ended_);
389 EXPECT_FALSE(result_received_); 494 EXPECT_FALSE(result_received_);
390 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); 495 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_);
391 CheckFinalEventsConsistency(); 496 CheckFinalEventsConsistency();
392 } 497 }
393 498
394 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { 499 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) {
395 // Check if things tear down properly if AudioInputController threw an error. 500 // Check if things tear down properly if AudioInputController threw an error.
396 recognizer_->StartRecognition( 501 recognizer_->StartRecognition(
397 media::AudioDeviceDescription::kDefaultDeviceId); 502 media::AudioDeviceDescription::kDefaultDeviceId);
503 WaitForAudioThreadToPostDeviceInfo();
398 base::RunLoop().RunUntilIdle(); 504 base::RunLoop().RunUntilIdle();
399 TestAudioInputController* controller = 505 TestAudioInputController* controller =
400 audio_input_controller_factory_.controller(); 506 audio_input_controller_factory_.controller();
401 ASSERT_TRUE(controller); 507 ASSERT_TRUE(controller);
402 controller->event_handler()->OnError(controller, 508 controller->event_handler()->OnError(controller,
403 AudioInputController::UNKNOWN_ERROR); 509 AudioInputController::UNKNOWN_ERROR);
404 base::RunLoop().RunUntilIdle(); 510 base::RunLoop().RunUntilIdle();
405 EXPECT_TRUE(recognition_started_); 511 EXPECT_TRUE(recognition_started_);
406 EXPECT_FALSE(audio_started_); 512 EXPECT_FALSE(audio_started_);
407 EXPECT_FALSE(result_received_); 513 EXPECT_FALSE(result_received_);
408 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); 514 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_);
409 CheckFinalEventsConsistency(); 515 CheckFinalEventsConsistency();
410 } 516 }
411 517
412 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { 518 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) {
413 // Check if things tear down properly if AudioInputController threw an error 519 // Check if things tear down properly if AudioInputController threw an error
414 // after giving some audio data. 520 // after giving some audio data.
415 recognizer_->StartRecognition( 521 recognizer_->StartRecognition(
416 media::AudioDeviceDescription::kDefaultDeviceId); 522 media::AudioDeviceDescription::kDefaultDeviceId);
523 WaitForAudioThreadToPostDeviceInfo();
417 base::RunLoop().RunUntilIdle(); 524 base::RunLoop().RunUntilIdle();
418 TestAudioInputController* controller = 525 TestAudioInputController* controller =
419 audio_input_controller_factory_.controller(); 526 audio_input_controller_factory_.controller();
420 ASSERT_TRUE(controller); 527 ASSERT_TRUE(controller);
421 OnData(audio_bus_.get()); 528 OnData(audio_bus_.get());
422 controller->event_handler()->OnError(controller, 529 controller->event_handler()->OnError(controller,
423 AudioInputController::UNKNOWN_ERROR); 530 AudioInputController::UNKNOWN_ERROR);
424 base::RunLoop().RunUntilIdle(); 531 base::RunLoop().RunUntilIdle();
425 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 532 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
426 EXPECT_TRUE(recognition_started_); 533 EXPECT_TRUE(recognition_started_);
427 EXPECT_TRUE(audio_started_); 534 EXPECT_TRUE(audio_started_);
428 EXPECT_FALSE(result_received_); 535 EXPECT_FALSE(result_received_);
429 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_); 536 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO_CAPTURE, error_);
430 CheckFinalEventsConsistency(); 537 CheckFinalEventsConsistency();
431 } 538 }
432 539
433 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { 540 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) {
434 // Start recording and give a lot of packets with audio samples set to zero. 541 // 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. 542 // This should trigger the no-speech detector and issue a callback.
436 recognizer_->StartRecognition( 543 recognizer_->StartRecognition(
437 media::AudioDeviceDescription::kDefaultDeviceId); 544 media::AudioDeviceDescription::kDefaultDeviceId);
545 WaitForAudioThreadToPostDeviceInfo();
438 base::RunLoop().RunUntilIdle(); 546 base::RunLoop().RunUntilIdle();
439 TestAudioInputController* controller = 547 TestAudioInputController* controller =
440 audio_input_controller_factory_.controller(); 548 audio_input_controller_factory_.controller();
441 ASSERT_TRUE(controller); 549 ASSERT_TRUE(controller);
442 550
443 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / 551 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
444 SpeechRecognitionEngine::kAudioPacketIntervalMs + 1; 552 SpeechRecognitionEngine::kAudioPacketIntervalMs + 1;
445 // The vector is already filled with zero value samples on create. 553 // The vector is already filled with zero value samples on create.
446 for (int i = 0; i < num_packets; ++i) { 554 for (int i = 0; i < num_packets; ++i) {
447 OnData(audio_bus_.get()); 555 OnData(audio_bus_.get());
448 } 556 }
449 base::RunLoop().RunUntilIdle(); 557 base::RunLoop().RunUntilIdle();
450 EXPECT_TRUE(recognition_started_); 558 EXPECT_TRUE(recognition_started_);
451 EXPECT_TRUE(audio_started_); 559 EXPECT_TRUE(audio_started_);
452 EXPECT_FALSE(result_received_); 560 EXPECT_FALSE(result_received_);
453 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); 561 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_);
454 CheckFinalEventsConsistency(); 562 CheckFinalEventsConsistency();
455 } 563 }
456 564
457 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { 565 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) {
458 // Start recording and give a lot of packets with audio samples set to zero 566 // 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 567 // 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 568 // treated as normal speech input and the no-speech detector should not get
461 // triggered. 569 // triggered.
462 recognizer_->StartRecognition( 570 recognizer_->StartRecognition(
463 media::AudioDeviceDescription::kDefaultDeviceId); 571 media::AudioDeviceDescription::kDefaultDeviceId);
572 WaitForAudioThreadToPostDeviceInfo();
464 base::RunLoop().RunUntilIdle(); 573 base::RunLoop().RunUntilIdle();
465 TestAudioInputController* controller = 574 TestAudioInputController* controller =
466 audio_input_controller_factory_.controller(); 575 audio_input_controller_factory_.controller();
467 ASSERT_TRUE(controller); 576 ASSERT_TRUE(controller);
468 controller = audio_input_controller_factory_.controller(); 577 controller = audio_input_controller_factory_.controller();
469 ASSERT_TRUE(controller); 578 ASSERT_TRUE(controller);
470 579
471 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / 580 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
472 SpeechRecognitionEngine::kAudioPacketIntervalMs; 581 SpeechRecognitionEngine::kAudioPacketIntervalMs;
473 582
(...skipping 17 matching lines...) Expand all
491 CheckFinalEventsConsistency(); 600 CheckFinalEventsConsistency();
492 } 601 }
493 602
494 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { 603 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) {
495 // Start recording and give a lot of packets with audio samples set to zero 604 // 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 605 // 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 606 // get the callback during estimation phase, then get zero for the silence
498 // samples and proper volume for the loud audio. 607 // samples and proper volume for the loud audio.
499 recognizer_->StartRecognition( 608 recognizer_->StartRecognition(
500 media::AudioDeviceDescription::kDefaultDeviceId); 609 media::AudioDeviceDescription::kDefaultDeviceId);
610 WaitForAudioThreadToPostDeviceInfo();
501 base::RunLoop().RunUntilIdle(); 611 base::RunLoop().RunUntilIdle();
502 TestAudioInputController* controller = 612 TestAudioInputController* controller =
503 audio_input_controller_factory_.controller(); 613 audio_input_controller_factory_.controller();
504 ASSERT_TRUE(controller); 614 ASSERT_TRUE(controller);
505 controller = audio_input_controller_factory_.controller(); 615 controller = audio_input_controller_factory_.controller();
506 ASSERT_TRUE(controller); 616 ASSERT_TRUE(controller);
507 617
508 // Feed some samples to begin with for the endpointer to do noise estimation. 618 // Feed some samples to begin with for the endpointer to do noise estimation.
509 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / 619 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs /
510 SpeechRecognitionEngine::kAudioPacketIntervalMs; 620 SpeechRecognitionEngine::kAudioPacketIntervalMs;
(...skipping 17 matching lines...) Expand all
528 638
529 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); 639 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
530 EXPECT_FALSE(audio_ended_); 640 EXPECT_FALSE(audio_ended_);
531 EXPECT_FALSE(recognition_ended_); 641 EXPECT_FALSE(recognition_ended_);
532 recognizer_->AbortRecognition(); 642 recognizer_->AbortRecognition();
533 base::RunLoop().RunUntilIdle(); 643 base::RunLoop().RunUntilIdle();
534 CheckFinalEventsConsistency(); 644 CheckFinalEventsConsistency();
535 } 645 }
536 646
537 } // namespace content 647 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698