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

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

Powered by Google App Engine
This is Rietveld 408576698