| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "content/browser/browser_thread_impl.h" | 7 #include "content/browser/browser_thread_impl.h" |
| 8 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 8 #include "content/browser/speech/speech_recognizer_impl.h" | 9 #include "content/browser/speech/speech_recognizer_impl.h" |
| 9 #include "content/public/browser/speech_recognition_event_listener.h" | 10 #include "content/public/browser/speech_recognition_event_listener.h" |
| 10 #include "content/test/test_url_fetcher_factory.h" | 11 #include "content/test/test_url_fetcher_factory.h" |
| 11 #include "media/audio/audio_manager.h" | 12 #include "media/audio/audio_manager.h" |
| 12 #include "media/audio/fake_audio_input_stream.h" | 13 #include "media/audio/fake_audio_input_stream.h" |
| 13 #include "media/audio/fake_audio_output_stream.h" | 14 #include "media/audio/fake_audio_output_stream.h" |
| 14 #include "media/audio/test_audio_input_controller_factory.h" | 15 #include "media/audio/test_audio_input_controller_factory.h" |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 result_received_(false), | 98 result_received_(false), |
| 98 audio_started_(false), | 99 audio_started_(false), |
| 99 error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 100 error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
| 100 volume_(-1.0f) { | 101 volume_(-1.0f) { |
| 101 recognizer_ = new SpeechRecognizerImpl( | 102 recognizer_ = new SpeechRecognizerImpl( |
| 102 this, 1, std::string(), std::string(), NULL, false, std::string(), | 103 this, 1, std::string(), std::string(), NULL, false, std::string(), |
| 103 std::string()); | 104 std::string()); |
| 104 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); | 105 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); |
| 105 int audio_packet_length_bytes = | 106 int audio_packet_length_bytes = |
| 106 (SpeechRecognizerImpl::kAudioSampleRate * | 107 (SpeechRecognizerImpl::kAudioSampleRate * |
| 107 SpeechRecognizerImpl::kAudioPacketIntervalMs * | 108 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * |
| 108 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * | 109 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * |
| 109 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); | 110 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); |
| 110 audio_packet_.resize(audio_packet_length_bytes); | 111 audio_packet_.resize(audio_packet_length_bytes); |
| 111 } | 112 } |
| 112 | 113 |
| 113 // Overridden from content::SpeechRecognitionEventListener: | 114 // Overridden from content::SpeechRecognitionEventListener: |
| 114 virtual void OnAudioStart(int caller_id) OVERRIDE { | 115 virtual void OnAudioStart(int caller_id) OVERRIDE { |
| 115 audio_started_ = true; | 116 audio_started_ = true; |
| 116 } | 117 } |
| 117 | 118 |
| 118 virtual void OnAudioEnd(int caller_id) OVERRIDE { | 119 virtual void OnAudioEnd(int caller_id) OVERRIDE { |
| 119 audio_ended_ = true; | 120 audio_ended_ = true; |
| 120 } | 121 } |
| 121 | 122 |
| 122 virtual void OnRecognitionResult( | 123 virtual void OnRecognitionResult( |
| 123 int caller_id, const content::SpeechRecognitionResult& result) OVERRIDE { | 124 int caller_id, const content::SpeechRecognitionResult& result) OVERRIDE { |
| 124 result_received_ = true; | 125 result_received_ = true; |
| 125 } | 126 } |
| 126 | 127 |
| 127 virtual void OnRecognitionError( | 128 virtual void OnRecognitionError( |
| 128 int caller_id, | 129 int caller_id, |
| 129 const content::SpeechRecognitionErrorCode& error) OVERRIDE { | 130 const content::SpeechRecognitionError& error) OVERRIDE { |
| 130 error_ = error; | 131 error_ = error.code; |
| 131 } | 132 } |
| 132 | 133 |
| 133 virtual void OnAudioLevelsChange(int caller_id, float volume, | 134 virtual void OnAudioLevelsChange(int caller_id, float volume, |
| 134 float noise_volume) OVERRIDE { | 135 float noise_volume) OVERRIDE { |
| 135 volume_ = volume; | 136 volume_ = volume; |
| 136 noise_volume_ = noise_volume; | 137 noise_volume_ = noise_volume; |
| 137 } | 138 } |
| 138 | 139 |
| 139 virtual void OnRecognitionEnd(int caller_id) OVERRIDE { | 140 virtual void OnRecognitionEnd(int caller_id) OVERRIDE { |
| 140 recognition_ended_ = true; | 141 recognition_ended_ = true; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 content::SpeechRecognitionErrorCode error_; | 183 content::SpeechRecognitionErrorCode error_; |
| 183 TestURLFetcherFactory url_fetcher_factory_; | 184 TestURLFetcherFactory url_fetcher_factory_; |
| 184 TestAudioInputControllerFactory audio_input_controller_factory_; | 185 TestAudioInputControllerFactory audio_input_controller_factory_; |
| 185 std::vector<uint8> audio_packet_; | 186 std::vector<uint8> audio_packet_; |
| 186 float volume_; | 187 float volume_; |
| 187 float noise_volume_; | 188 float noise_volume_; |
| 188 }; | 189 }; |
| 189 | 190 |
| 190 TEST_F(SpeechRecognizerImplTest, StopNoData) { | 191 TEST_F(SpeechRecognizerImplTest, StopNoData) { |
| 191 // Check for callbacks when stopping record before any audio gets recorded. | 192 // Check for callbacks when stopping record before any audio gets recorded. |
| 192 EXPECT_TRUE(recognizer_->StartRecognition()); | 193 recognizer_->StartRecognition(); |
| 193 recognizer_->AbortRecognition(); | 194 recognizer_->AbortRecognition(); |
| 194 EXPECT_FALSE(audio_ended_); | 195 EXPECT_FALSE(audio_ended_); |
| 195 EXPECT_FALSE(recognition_ended_); | 196 EXPECT_FALSE(recognition_ended_); |
| 196 EXPECT_FALSE(result_received_); | 197 EXPECT_FALSE(result_received_); |
| 197 EXPECT_FALSE(audio_started_); | 198 EXPECT_FALSE(audio_started_); |
| 198 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 199 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 199 } | 200 } |
| 200 | 201 |
| 201 TEST_F(SpeechRecognizerImplTest, CancelNoData) { | 202 TEST_F(SpeechRecognizerImplTest, CancelNoData) { |
| 202 // Check for callbacks when canceling recognition before any audio gets | 203 // Check for callbacks when canceling recognition before any audio gets |
| 203 // recorded. | 204 // recorded. |
| 204 EXPECT_TRUE(recognizer_->StartRecognition()); | 205 recognizer_->StartRecognition(); |
| 205 recognizer_->StopAudioCapture(); | 206 recognizer_->StopAudioCapture(); |
| 206 EXPECT_TRUE(audio_ended_); | 207 EXPECT_TRUE(audio_ended_); |
| 207 EXPECT_TRUE(recognition_ended_); | 208 EXPECT_TRUE(recognition_ended_); |
| 208 EXPECT_FALSE(result_received_); | 209 EXPECT_FALSE(result_received_); |
| 209 EXPECT_FALSE(audio_started_); | 210 EXPECT_FALSE(audio_started_); |
| 210 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 211 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 211 } | 212 } |
| 212 | 213 |
| 213 TEST_F(SpeechRecognizerImplTest, StopWithData) { | 214 TEST_F(SpeechRecognizerImplTest, StopWithData) { |
| 214 // Start recording, give some data and then stop. This should wait for the | 215 // Start recording, give some data and then stop. This should wait for the |
| 215 // network callback to arrive before completion. | 216 // network callback to arrive before completion. |
| 216 EXPECT_TRUE(recognizer_->StartRecognition()); | 217 recognizer_->StartRecognition(); |
| 217 TestAudioInputController* controller = | 218 TestAudioInputController* controller = |
| 218 audio_input_controller_factory_.controller(); | 219 audio_input_controller_factory_.controller(); |
| 219 ASSERT_TRUE(controller); | 220 ASSERT_TRUE(controller); |
| 220 | 221 |
| 221 // Try sending 5 chunks of mock audio data and verify that each of them | 222 // Try sending 5 chunks of mock audio data and verify that each of them |
| 222 // resulted immediately in a packet sent out via the network. This verifies | 223 // resulted immediately in a packet sent out via the network. This verifies |
| 223 // that we are streaming out encoded data as chunks without waiting for the | 224 // that we are streaming out encoded data as chunks without waiting for the |
| 224 // full recording to complete. | 225 // full recording to complete. |
| 225 const size_t kNumChunks = 5; | 226 const size_t kNumChunks = 5; |
| 226 for (size_t i = 0; i < kNumChunks; ++i) { | 227 for (size_t i = 0; i < kNumChunks; ++i) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 253 fetcher->delegate()->OnURLFetchComplete(fetcher); | 254 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 254 | 255 |
| 255 EXPECT_TRUE(recognition_ended_); | 256 EXPECT_TRUE(recognition_ended_); |
| 256 EXPECT_TRUE(result_received_); | 257 EXPECT_TRUE(result_received_); |
| 257 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 258 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 258 } | 259 } |
| 259 | 260 |
| 260 TEST_F(SpeechRecognizerImplTest, CancelWithData) { | 261 TEST_F(SpeechRecognizerImplTest, CancelWithData) { |
| 261 // Start recording, give some data and then cancel. This should create | 262 // Start recording, give some data and then cancel. This should create |
| 262 // a network request but give no callbacks. | 263 // a network request but give no callbacks. |
| 263 EXPECT_TRUE(recognizer_->StartRecognition()); | 264 recognizer_->StartRecognition(); |
| 264 TestAudioInputController* controller = | 265 TestAudioInputController* controller = |
| 265 audio_input_controller_factory_.controller(); | 266 audio_input_controller_factory_.controller(); |
| 266 ASSERT_TRUE(controller); | 267 ASSERT_TRUE(controller); |
| 267 controller->event_handler()->OnData(controller, &audio_packet_[0], | 268 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 268 audio_packet_.size()); | 269 audio_packet_.size()); |
| 269 MessageLoop::current()->RunAllPending(); | 270 MessageLoop::current()->RunAllPending(); |
| 270 recognizer_->AbortRecognition(); | 271 recognizer_->AbortRecognition(); |
| 271 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 272 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 272 EXPECT_TRUE(audio_started_); | 273 EXPECT_TRUE(audio_started_); |
| 273 EXPECT_FALSE(audio_ended_); | 274 EXPECT_FALSE(audio_ended_); |
| 274 EXPECT_FALSE(recognition_ended_); | 275 EXPECT_FALSE(recognition_ended_); |
| 275 EXPECT_FALSE(result_received_); | 276 EXPECT_FALSE(result_received_); |
| 276 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 277 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 277 } | 278 } |
| 278 | 279 |
| 279 TEST_F(SpeechRecognizerImplTest, ConnectionError) { | 280 TEST_F(SpeechRecognizerImplTest, ConnectionError) { |
| 280 // Start recording, give some data and then stop. Issue the network callback | 281 // Start recording, give some data and then stop. Issue the network callback |
| 281 // with a connection error and verify that the recognizer bubbles the error up | 282 // with a connection error and verify that the recognizer bubbles the error up |
| 282 EXPECT_TRUE(recognizer_->StartRecognition()); | 283 recognizer_->StartRecognition(); |
| 283 TestAudioInputController* controller = | 284 TestAudioInputController* controller = |
| 284 audio_input_controller_factory_.controller(); | 285 audio_input_controller_factory_.controller(); |
| 285 ASSERT_TRUE(controller); | 286 ASSERT_TRUE(controller); |
| 286 controller->event_handler()->OnData(controller, &audio_packet_[0], | 287 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 287 audio_packet_.size()); | 288 audio_packet_.size()); |
| 288 MessageLoop::current()->RunAllPending(); | 289 MessageLoop::current()->RunAllPending(); |
| 289 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 290 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 290 ASSERT_TRUE(fetcher); | 291 ASSERT_TRUE(fetcher); |
| 291 | 292 |
| 292 recognizer_->StopAudioCapture(); | 293 recognizer_->StopAudioCapture(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 307 fetcher->delegate()->OnURLFetchComplete(fetcher); | 308 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 308 | 309 |
| 309 EXPECT_FALSE(recognition_ended_); | 310 EXPECT_FALSE(recognition_ended_); |
| 310 EXPECT_FALSE(result_received_); | 311 EXPECT_FALSE(result_received_); |
| 311 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 312 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 312 } | 313 } |
| 313 | 314 |
| 314 TEST_F(SpeechRecognizerImplTest, ServerError) { | 315 TEST_F(SpeechRecognizerImplTest, ServerError) { |
| 315 // Start recording, give some data and then stop. Issue the network callback | 316 // Start recording, give some data and then stop. Issue the network callback |
| 316 // with a 500 error and verify that the recognizer bubbles the error up | 317 // with a 500 error and verify that the recognizer bubbles the error up |
| 317 EXPECT_TRUE(recognizer_->StartRecognition()); | 318 recognizer_->StartRecognition(); |
| 318 TestAudioInputController* controller = | 319 TestAudioInputController* controller = |
| 319 audio_input_controller_factory_.controller(); | 320 audio_input_controller_factory_.controller(); |
| 320 ASSERT_TRUE(controller); | 321 ASSERT_TRUE(controller); |
| 321 controller->event_handler()->OnData(controller, &audio_packet_[0], | 322 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 322 audio_packet_.size()); | 323 audio_packet_.size()); |
| 323 MessageLoop::current()->RunAllPending(); | 324 MessageLoop::current()->RunAllPending(); |
| 324 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 325 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 325 ASSERT_TRUE(fetcher); | 326 ASSERT_TRUE(fetcher); |
| 326 | 327 |
| 327 recognizer_->StopAudioCapture(); | 328 recognizer_->StopAudioCapture(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 340 fetcher->SetResponseString("Internal Server Error"); | 341 fetcher->SetResponseString("Internal Server Error"); |
| 341 fetcher->delegate()->OnURLFetchComplete(fetcher); | 342 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 342 | 343 |
| 343 EXPECT_FALSE(recognition_ended_); | 344 EXPECT_FALSE(recognition_ended_); |
| 344 EXPECT_FALSE(result_received_); | 345 EXPECT_FALSE(result_received_); |
| 345 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 346 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 346 } | 347 } |
| 347 | 348 |
| 348 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { | 349 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { |
| 349 // Check if things tear down properly if AudioInputController threw an error. | 350 // Check if things tear down properly if AudioInputController threw an error. |
| 350 EXPECT_TRUE(recognizer_->StartRecognition()); | 351 recognizer_->StartRecognition(); |
| 351 TestAudioInputController* controller = | 352 TestAudioInputController* controller = |
| 352 audio_input_controller_factory_.controller(); | 353 audio_input_controller_factory_.controller(); |
| 353 ASSERT_TRUE(controller); | 354 ASSERT_TRUE(controller); |
| 354 controller->event_handler()->OnError(controller, 0); | 355 controller->event_handler()->OnError(controller, 0); |
| 355 MessageLoop::current()->RunAllPending(); | 356 MessageLoop::current()->RunAllPending(); |
| 356 EXPECT_FALSE(audio_started_); | 357 EXPECT_FALSE(audio_started_); |
| 357 EXPECT_FALSE(audio_ended_); | 358 EXPECT_FALSE(audio_ended_); |
| 358 EXPECT_FALSE(recognition_ended_); | 359 EXPECT_FALSE(recognition_ended_); |
| 359 EXPECT_FALSE(result_received_); | 360 EXPECT_FALSE(result_received_); |
| 360 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 361 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 361 } | 362 } |
| 362 | 363 |
| 363 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { | 364 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { |
| 364 // Check if things tear down properly if AudioInputController threw an error | 365 // Check if things tear down properly if AudioInputController threw an error |
| 365 // after giving some audio data. | 366 // after giving some audio data. |
| 366 EXPECT_TRUE(recognizer_->StartRecognition()); | 367 recognizer_->StartRecognition(); |
| 367 TestAudioInputController* controller = | 368 TestAudioInputController* controller = |
| 368 audio_input_controller_factory_.controller(); | 369 audio_input_controller_factory_.controller(); |
| 369 ASSERT_TRUE(controller); | 370 ASSERT_TRUE(controller); |
| 370 controller->event_handler()->OnData(controller, &audio_packet_[0], | 371 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 371 audio_packet_.size()); | 372 audio_packet_.size()); |
| 372 controller->event_handler()->OnError(controller, 0); | 373 controller->event_handler()->OnError(controller, 0); |
| 373 MessageLoop::current()->RunAllPending(); | 374 MessageLoop::current()->RunAllPending(); |
| 374 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 375 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 375 EXPECT_TRUE(audio_started_); | 376 EXPECT_TRUE(audio_started_); |
| 376 EXPECT_FALSE(audio_ended_); | 377 EXPECT_FALSE(audio_ended_); |
| 377 EXPECT_FALSE(recognition_ended_); | 378 EXPECT_FALSE(recognition_ended_); |
| 378 EXPECT_FALSE(result_received_); | 379 EXPECT_FALSE(result_received_); |
| 379 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 380 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 380 } | 381 } |
| 381 | 382 |
| 382 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { | 383 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { |
| 383 // Start recording and give a lot of packets with audio samples set to zero. | 384 // Start recording and give a lot of packets with audio samples set to zero. |
| 384 // This should trigger the no-speech detector and issue a callback. | 385 // This should trigger the no-speech detector and issue a callback. |
| 385 EXPECT_TRUE(recognizer_->StartRecognition()); | 386 recognizer_->StartRecognition(); |
| 386 TestAudioInputController* controller = | 387 TestAudioInputController* controller = |
| 387 audio_input_controller_factory_.controller(); | 388 audio_input_controller_factory_.controller(); |
| 388 ASSERT_TRUE(controller); | 389 ASSERT_TRUE(controller); |
| 389 controller = audio_input_controller_factory_.controller(); | 390 controller = audio_input_controller_factory_.controller(); |
| 390 ASSERT_TRUE(controller); | 391 ASSERT_TRUE(controller); |
| 391 | 392 |
| 392 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / | 393 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / |
| 393 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 394 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; |
| 394 // The vector is already filled with zero value samples on create. | 395 // The vector is already filled with zero value samples on create. |
| 395 for (int i = 0; i < num_packets; ++i) { | 396 for (int i = 0; i < num_packets; ++i) { |
| 396 controller->event_handler()->OnData(controller, &audio_packet_[0], | 397 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 397 audio_packet_.size()); | 398 audio_packet_.size()); |
| 398 } | 399 } |
| 399 MessageLoop::current()->RunAllPending(); | 400 MessageLoop::current()->RunAllPending(); |
| 400 EXPECT_TRUE(audio_started_); | 401 EXPECT_TRUE(audio_started_); |
| 401 EXPECT_FALSE(audio_ended_); | 402 EXPECT_FALSE(audio_ended_); |
| 402 EXPECT_FALSE(recognition_ended_); | 403 EXPECT_FALSE(recognition_ended_); |
| 403 EXPECT_FALSE(result_received_); | 404 EXPECT_FALSE(result_received_); |
| 404 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); | 405 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
| 405 } | 406 } |
| 406 | 407 |
| 407 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { | 408 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { |
| 408 // Start recording and give a lot of packets with audio samples set to zero | 409 // Start recording and give a lot of packets with audio samples set to zero |
| 409 // and then some more with reasonably loud audio samples. This should be | 410 // and then some more with reasonably loud audio samples. This should be |
| 410 // treated as normal speech input and the no-speech detector should not get | 411 // treated as normal speech input and the no-speech detector should not get |
| 411 // triggered. | 412 // triggered. |
| 412 EXPECT_TRUE(recognizer_->StartRecognition()); | 413 recognizer_->StartRecognition(); |
| 413 TestAudioInputController* controller = | 414 TestAudioInputController* controller = |
| 414 audio_input_controller_factory_.controller(); | 415 audio_input_controller_factory_.controller(); |
| 415 ASSERT_TRUE(controller); | 416 ASSERT_TRUE(controller); |
| 416 controller = audio_input_controller_factory_.controller(); | 417 controller = audio_input_controller_factory_.controller(); |
| 417 ASSERT_TRUE(controller); | 418 ASSERT_TRUE(controller); |
| 418 | 419 |
| 419 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / | 420 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / |
| 420 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 421 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; |
| 421 | 422 |
| 422 // The vector is already filled with zero value samples on create. | 423 // The vector is already filled with zero value samples on create. |
| 423 for (int i = 0; i < num_packets / 2; ++i) { | 424 for (int i = 0; i < num_packets / 2; ++i) { |
| 424 controller->event_handler()->OnData(controller, &audio_packet_[0], | 425 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 425 audio_packet_.size()); | 426 audio_packet_.size()); |
| 426 } | 427 } |
| 427 | 428 |
| 428 FillPacketWithTestWaveform(); | 429 FillPacketWithTestWaveform(); |
| 429 for (int i = 0; i < num_packets / 2; ++i) { | 430 for (int i = 0; i < num_packets / 2; ++i) { |
| 430 controller->event_handler()->OnData(controller, &audio_packet_[0], | 431 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 431 audio_packet_.size()); | 432 audio_packet_.size()); |
| 432 } | 433 } |
| 433 | 434 |
| 434 MessageLoop::current()->RunAllPending(); | 435 MessageLoop::current()->RunAllPending(); |
| 435 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 436 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 436 EXPECT_TRUE(audio_started_); | 437 EXPECT_TRUE(audio_started_); |
| 437 EXPECT_FALSE(audio_ended_); | 438 EXPECT_FALSE(audio_ended_); |
| 438 EXPECT_FALSE(recognition_ended_); | 439 EXPECT_FALSE(recognition_ended_); |
| 439 recognizer_->AbortRecognition(); | 440 recognizer_->AbortRecognition(); |
| 440 } | 441 } |
| 441 | 442 |
| 442 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { | 443 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { |
| 443 // Start recording and give a lot of packets with audio samples set to zero | 444 // Start recording and give a lot of packets with audio samples set to zero |
| 444 // and then some more with reasonably loud audio samples. Check that we don't | 445 // and then some more with reasonably loud audio samples. Check that we don't |
| 445 // get the callback during estimation phase, then get zero for the silence | 446 // get the callback during estimation phase, then get zero for the silence |
| 446 // samples and proper volume for the loud audio. | 447 // samples and proper volume for the loud audio. |
| 447 EXPECT_TRUE(recognizer_->StartRecognition()); | 448 recognizer_->StartRecognition(); |
| 448 TestAudioInputController* controller = | 449 TestAudioInputController* controller = |
| 449 audio_input_controller_factory_.controller(); | 450 audio_input_controller_factory_.controller(); |
| 450 ASSERT_TRUE(controller); | 451 ASSERT_TRUE(controller); |
| 451 controller = audio_input_controller_factory_.controller(); | 452 controller = audio_input_controller_factory_.controller(); |
| 452 ASSERT_TRUE(controller); | 453 ASSERT_TRUE(controller); |
| 453 | 454 |
| 454 // Feed some samples to begin with for the endpointer to do noise estimation. | 455 // Feed some samples to begin with for the endpointer to do noise estimation. |
| 455 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / | 456 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / |
| 456 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 457 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; |
| 457 FillPacketWithNoise(); | 458 FillPacketWithNoise(); |
| 458 for (int i = 0; i < num_packets; ++i) { | 459 for (int i = 0; i < num_packets; ++i) { |
| 459 controller->event_handler()->OnData(controller, &audio_packet_[0], | 460 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 460 audio_packet_.size()); | 461 audio_packet_.size()); |
| 461 } | 462 } |
| 462 MessageLoop::current()->RunAllPending(); | 463 MessageLoop::current()->RunAllPending(); |
| 463 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. | 464 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. |
| 464 | 465 |
| 465 // The vector is already filled with zero value samples on create. | 466 // The vector is already filled with zero value samples on create. |
| 466 controller->event_handler()->OnData(controller, &audio_packet_[0], | 467 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 467 audio_packet_.size()); | 468 audio_packet_.size()); |
| 468 MessageLoop::current()->RunAllPending(); | 469 MessageLoop::current()->RunAllPending(); |
| 469 EXPECT_FLOAT_EQ(0.74939233f, volume_); | 470 EXPECT_FLOAT_EQ(0.74939233f, volume_); |
| 470 | 471 |
| 471 FillPacketWithTestWaveform(); | 472 FillPacketWithTestWaveform(); |
| 472 controller->event_handler()->OnData(controller, &audio_packet_[0], | 473 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 473 audio_packet_.size()); | 474 audio_packet_.size()); |
| 474 MessageLoop::current()->RunAllPending(); | 475 MessageLoop::current()->RunAllPending(); |
| 475 EXPECT_FLOAT_EQ(0.89926866f, volume_); | 476 EXPECT_FLOAT_EQ(0.89926866f, volume_); |
| 476 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); | 477 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); |
| 477 | 478 |
| 478 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 479 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 479 EXPECT_FALSE(audio_ended_); | 480 EXPECT_FALSE(audio_ended_); |
| 480 EXPECT_FALSE(recognition_ended_); | 481 EXPECT_FALSE(recognition_ended_); |
| 481 recognizer_->AbortRecognition(); | 482 recognizer_->AbortRecognition(); |
| 482 } | 483 } |
| 483 | 484 |
| 484 } // namespace speech | 485 } // namespace speech |
| OLD | NEW |