Chromium Code Reviews| 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/google_one_shot_remote_engine.h" |
| 9 #include "content/browser/speech/speech_recognizer_impl.h" | 9 #include "content/browser/speech/speech_recognizer_impl.h" |
| 10 #include "content/public/browser/speech_recognition_event_listener.h" | 10 #include "content/public/browser/speech_recognition_event_listener.h" |
| 11 #include "content/test/test_url_fetcher_factory.h" | 11 #include "content/test/test_url_fetcher_factory.h" |
| 12 #include "media/audio/audio_manager.h" | 12 #include "media/audio/audio_manager.h" |
| 13 #include "media/audio/fake_audio_input_stream.h" | 13 #include "media/audio/fake_audio_input_stream.h" |
| 14 #include "media/audio/fake_audio_output_stream.h" | 14 #include "media/audio/fake_audio_output_stream.h" |
| 15 #include "media/audio/test_audio_input_controller_factory.h" | 15 #include "media/audio/test_audio_input_controller_factory.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 using base::MessageLoopProxy; | |
| 20 using content::BrowserThread; | 21 using content::BrowserThread; |
| 21 using content::BrowserThreadImpl; | 22 using content::BrowserThreadImpl; |
| 22 using media::AudioInputController; | 23 using media::AudioInputController; |
| 23 using media::TestAudioInputController; | 24 using media::TestAudioInputController; |
| 24 using media::TestAudioInputControllerFactory; | 25 using media::TestAudioInputControllerFactory; |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 class MockAudioManager : public AudioManagerBase { | 29 class MockAudioManager : public AudioManagerBase { |
| 29 public: | 30 public: |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 | 87 |
| 87 | 88 |
| 88 namespace speech { | 89 namespace speech { |
| 89 | 90 |
| 90 class SpeechRecognizerImplTest : public content::SpeechRecognitionEventListener, | 91 class SpeechRecognizerImplTest : public content::SpeechRecognitionEventListener, |
| 91 public testing::Test { | 92 public testing::Test { |
| 92 public: | 93 public: |
| 93 SpeechRecognizerImplTest() | 94 SpeechRecognizerImplTest() |
| 94 : io_thread_(BrowserThread::IO, &message_loop_), | 95 : io_thread_(BrowserThread::IO, &message_loop_), |
| 95 audio_manager_(new MockAudioManager()), | 96 audio_manager_(new MockAudioManager()), |
| 96 audio_ended_(false), | 97 recognition_started_(false), |
| 97 recognition_ended_(false), | 98 recognition_ended_(false), |
| 98 result_received_(false), | 99 result_received_(false), |
| 99 audio_started_(false), | 100 audio_started_(false), |
| 101 audio_ended_(false), | |
| 102 sound_started_(false), | |
| 103 sound_ended_(false), | |
| 100 error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 104 error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
| 101 volume_(-1.0f) { | 105 volume_(-1.0f) { |
| 102 recognizer_ = new SpeechRecognizerImpl( | 106 GoogleOneShotRemoteEngine* sr_engine = |
| 103 this, 1, std::string(), std::string(), NULL, false, std::string(), | 107 new GoogleOneShotRemoteEngine(NULL /* URLRequestContextGetter */); |
| 104 std::string()); | 108 GoogleOneShotRemoteEngineConfig config; |
| 109 config.audio_num_bits_per_sample = | |
| 110 SpeechRecognizerImpl::kNumBitsPerAudioSample; | |
| 111 config.audio_sample_rate = SpeechRecognizerImpl::kAudioSampleRate; | |
| 112 config.filter_profanities = false; | |
| 113 sr_engine->SetConfig(config); | |
| 114 | |
| 115 recognizer_ = new SpeechRecognizerImpl(this, 1, sr_engine); | |
| 105 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); | 116 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); |
| 117 | |
| 106 int audio_packet_length_bytes = | 118 int audio_packet_length_bytes = |
| 107 (SpeechRecognizerImpl::kAudioSampleRate * | 119 (SpeechRecognizerImpl::kAudioSampleRate * |
| 108 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * | 120 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * |
| 109 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * | 121 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * |
| 110 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); | 122 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); |
| 111 audio_packet_.resize(audio_packet_length_bytes); | 123 audio_packet_.resize(audio_packet_length_bytes); |
| 112 } | 124 } |
| 113 | 125 |
| 126 void CheckEventsConsistency() { | |
| 127 // Note: "!x || y" == "x implies y". | |
| 128 EXPECT_TRUE(!recognition_ended_ || recognition_started_); | |
| 129 EXPECT_TRUE(!audio_ended_ || audio_started_); | |
| 130 EXPECT_TRUE(!sound_ended_ || sound_started_); | |
| 131 EXPECT_TRUE(!audio_started_ || recognition_started_); | |
| 132 EXPECT_TRUE(!sound_started_ || audio_started_); | |
| 133 EXPECT_TRUE(!audio_ended_ || (sound_ended_ || !sound_started_)); | |
| 134 EXPECT_TRUE(!recognition_ended_ || (audio_ended_ || !audio_started_)); | |
| 135 } | |
| 136 | |
| 137 void CheckFinalEventsConsistency() { | |
| 138 // Note: "!(x ^ y)" == "(x && y) || (!x && !x)". | |
| 139 EXPECT_FALSE(recognition_started_ ^ recognition_ended_); | |
| 140 EXPECT_FALSE(audio_started_ ^ audio_ended_); | |
| 141 EXPECT_FALSE(sound_started_ ^ sound_ended_); | |
| 142 } | |
| 143 | |
| 114 // Overridden from content::SpeechRecognitionEventListener: | 144 // Overridden from content::SpeechRecognitionEventListener: |
| 115 virtual void OnAudioStart(int caller_id) OVERRIDE { | 145 virtual void OnAudioStart(int caller_id) OVERRIDE { |
| 116 audio_started_ = true; | 146 audio_started_ = true; |
| 147 CheckEventsConsistency(); | |
| 117 } | 148 } |
| 118 | 149 |
| 119 virtual void OnAudioEnd(int caller_id) OVERRIDE { | 150 virtual void OnAudioEnd(int caller_id) OVERRIDE { |
| 120 audio_ended_ = true; | 151 audio_ended_ = true; |
| 152 CheckEventsConsistency(); | |
| 121 } | 153 } |
| 122 | 154 |
| 123 virtual void OnRecognitionResult( | 155 virtual void OnRecognitionResult( |
| 124 int caller_id, const content::SpeechRecognitionResult& result) OVERRIDE { | 156 int caller_id, const content::SpeechRecognitionResult& result) OVERRIDE { |
| 125 result_received_ = true; | 157 result_received_ = true; |
| 126 } | 158 } |
| 127 | 159 |
| 128 virtual void OnRecognitionError( | 160 virtual void OnRecognitionError( |
| 129 int caller_id, | 161 int caller_id, const content::SpeechRecognitionError& error) OVERRIDE { |
| 130 const content::SpeechRecognitionError& error) OVERRIDE { | 162 EXPECT_TRUE(recognition_started_); |
| 163 EXPECT_FALSE(recognition_ended_); | |
| 131 error_ = error.code; | 164 error_ = error.code; |
| 132 } | 165 } |
| 133 | 166 |
| 134 virtual void OnAudioLevelsChange(int caller_id, float volume, | 167 virtual void OnAudioLevelsChange(int caller_id, float volume, |
| 135 float noise_volume) OVERRIDE { | 168 float noise_volume) OVERRIDE { |
|
hans
2012/04/02 16:05:59
is there a space too much here?
Primiano Tucci (use gerrit)
2012/04/03 10:16:39
Done.
| |
| 136 volume_ = volume; | 169 volume_ = volume; |
| 137 noise_volume_ = noise_volume; | 170 noise_volume_ = noise_volume; |
| 138 } | 171 } |
| 139 | 172 |
| 140 virtual void OnRecognitionEnd(int caller_id) OVERRIDE { | 173 virtual void OnRecognitionEnd(int caller_id) OVERRIDE { |
| 141 recognition_ended_ = true; | 174 recognition_ended_ = true; |
| 175 CheckEventsConsistency(); | |
| 142 } | 176 } |
| 143 | 177 |
| 144 virtual void OnRecognitionStart(int caller_id) OVERRIDE {} | 178 virtual void OnRecognitionStart(int caller_id) OVERRIDE { |
| 179 recognition_started_ = true; | |
| 180 CheckEventsConsistency(); | |
| 181 } | |
| 182 | |
| 145 virtual void OnEnvironmentEstimationComplete(int caller_id) OVERRIDE {} | 183 virtual void OnEnvironmentEstimationComplete(int caller_id) OVERRIDE {} |
| 146 virtual void OnSoundStart(int caller_id) OVERRIDE {} | 184 |
| 147 virtual void OnSoundEnd(int caller_id) OVERRIDE {} | 185 virtual void OnSoundStart(int caller_id) OVERRIDE { |
| 186 sound_started_ = true; | |
| 187 CheckEventsConsistency(); | |
| 188 } | |
| 189 | |
| 190 virtual void OnSoundEnd(int caller_id) OVERRIDE { | |
| 191 sound_ended_ = true; | |
| 192 CheckEventsConsistency(); | |
| 193 } | |
| 148 | 194 |
| 149 // testing::Test methods. | 195 // testing::Test methods. |
| 150 virtual void SetUp() OVERRIDE { | 196 virtual void SetUp() OVERRIDE { |
| 151 AudioInputController::set_factory_for_testing( | 197 AudioInputController::set_factory_for_testing( |
| 152 &audio_input_controller_factory_); | 198 &audio_input_controller_factory_); |
| 153 } | 199 } |
| 154 | 200 |
| 155 virtual void TearDown() OVERRIDE { | 201 virtual void TearDown() OVERRIDE { |
| 156 AudioInputController::set_factory_for_testing(NULL); | 202 AudioInputController::set_factory_for_testing(NULL); |
| 157 } | 203 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 169 value += factor; | 215 value += factor; |
| 170 audio_packet_[i] = value % 100; | 216 audio_packet_[i] = value % 100; |
| 171 } | 217 } |
| 172 } | 218 } |
| 173 | 219 |
| 174 protected: | 220 protected: |
| 175 MessageLoopForIO message_loop_; | 221 MessageLoopForIO message_loop_; |
| 176 BrowserThreadImpl io_thread_; | 222 BrowserThreadImpl io_thread_; |
| 177 scoped_refptr<SpeechRecognizerImpl> recognizer_; | 223 scoped_refptr<SpeechRecognizerImpl> recognizer_; |
| 178 scoped_ptr<AudioManager> audio_manager_; | 224 scoped_ptr<AudioManager> audio_manager_; |
| 179 bool audio_ended_; | 225 bool recognition_started_; |
| 180 bool recognition_ended_; | 226 bool recognition_ended_; |
| 181 bool result_received_; | 227 bool result_received_; |
| 182 bool audio_started_; | 228 bool audio_started_; |
| 229 bool audio_ended_; | |
| 230 bool sound_started_; | |
| 231 bool sound_ended_; | |
| 183 content::SpeechRecognitionErrorCode error_; | 232 content::SpeechRecognitionErrorCode error_; |
| 184 TestURLFetcherFactory url_fetcher_factory_; | 233 TestURLFetcherFactory url_fetcher_factory_; |
| 185 TestAudioInputControllerFactory audio_input_controller_factory_; | 234 TestAudioInputControllerFactory audio_input_controller_factory_; |
| 186 std::vector<uint8> audio_packet_; | 235 std::vector<uint8> audio_packet_; |
| 187 float volume_; | 236 float volume_; |
| 188 float noise_volume_; | 237 float noise_volume_; |
| 189 }; | 238 }; |
| 190 | 239 |
| 191 TEST_F(SpeechRecognizerImplTest, StopNoData) { | 240 TEST_F(SpeechRecognizerImplTest, StopNoData) { |
| 192 // Check for callbacks when stopping record before any audio gets recorded. | 241 // Check for callbacks when stopping record before any audio gets recorded. |
| 193 recognizer_->StartRecognition(); | 242 recognizer_->StartRecognition(); |
| 194 recognizer_->AbortRecognition(); | 243 recognizer_->AbortRecognition(); |
| 195 EXPECT_FALSE(audio_ended_); | 244 MessageLoop::current()->RunAllPending(); |
| 196 EXPECT_FALSE(recognition_ended_); | 245 EXPECT_TRUE(recognition_started_); |
| 246 EXPECT_FALSE(audio_started_); | |
| 197 EXPECT_FALSE(result_received_); | 247 EXPECT_FALSE(result_received_); |
| 198 EXPECT_FALSE(audio_started_); | |
| 199 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 248 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 249 CheckFinalEventsConsistency(); | |
| 200 } | 250 } |
| 201 | 251 |
| 202 TEST_F(SpeechRecognizerImplTest, CancelNoData) { | 252 TEST_F(SpeechRecognizerImplTest, CancelNoData) { |
| 203 // Check for callbacks when canceling recognition before any audio gets | 253 // Check for callbacks when canceling recognition before any audio gets |
| 204 // recorded. | 254 // recorded. |
| 205 recognizer_->StartRecognition(); | 255 recognizer_->StartRecognition(); |
| 206 recognizer_->StopAudioCapture(); | 256 recognizer_->StopAudioCapture(); |
| 207 EXPECT_TRUE(audio_ended_); | 257 MessageLoop::current()->RunAllPending(); |
| 208 EXPECT_TRUE(recognition_ended_); | 258 EXPECT_TRUE(recognition_started_); |
| 259 EXPECT_FALSE(audio_started_); | |
| 209 EXPECT_FALSE(result_received_); | 260 EXPECT_FALSE(result_received_); |
| 210 EXPECT_FALSE(audio_started_); | |
| 211 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 261 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 262 CheckFinalEventsConsistency(); | |
| 212 } | 263 } |
| 213 | 264 |
| 214 TEST_F(SpeechRecognizerImplTest, StopWithData) { | 265 TEST_F(SpeechRecognizerImplTest, StopWithData) { |
| 215 // Start recording, give some data and then stop. This should wait for the | 266 // Start recording, give some data and then stop. This should wait for the |
| 216 // network callback to arrive before completion. | 267 // network callback to arrive before completion. |
| 217 recognizer_->StartRecognition(); | 268 recognizer_->StartRecognition(); |
| 269 MessageLoop::current()->RunAllPending(); | |
| 218 TestAudioInputController* controller = | 270 TestAudioInputController* controller = |
| 219 audio_input_controller_factory_.controller(); | 271 audio_input_controller_factory_.controller(); |
| 220 ASSERT_TRUE(controller); | 272 ASSERT_TRUE(controller); |
| 221 | 273 |
| 222 // Try sending 5 chunks of mock audio data and verify that each of them | 274 // Try sending 5 chunks of mock audio data and verify that each of them |
| 223 // resulted immediately in a packet sent out via the network. This verifies | 275 // resulted immediately in a packet sent out via the network. This verifies |
| 224 // that we are streaming out encoded data as chunks without waiting for the | 276 // that we are streaming out encoded data as chunks without waiting for the |
| 225 // full recording to complete. | 277 // full recording to complete. |
| 226 const size_t kNumChunks = 5; | 278 const size_t kNumChunks = 5; |
| 227 for (size_t i = 0; i < kNumChunks; ++i) { | 279 for (size_t i = 0; i < kNumChunks; ++i) { |
| 228 controller->event_handler()->OnData(controller, &audio_packet_[0], | 280 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 229 audio_packet_.size()); | 281 audio_packet_.size()); |
| 230 MessageLoop::current()->RunAllPending(); | 282 MessageLoop::current()->RunAllPending(); |
| 231 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 283 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 232 ASSERT_TRUE(fetcher); | 284 ASSERT_TRUE(fetcher); |
| 233 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); | 285 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); |
| 234 } | 286 } |
| 235 | 287 |
| 236 recognizer_->StopAudioCapture(); | 288 recognizer_->StopAudioCapture(); |
| 289 MessageLoop::current()->RunAllPending(); | |
| 237 EXPECT_TRUE(audio_started_); | 290 EXPECT_TRUE(audio_started_); |
| 238 EXPECT_TRUE(audio_ended_); | 291 EXPECT_TRUE(audio_ended_); |
| 239 EXPECT_FALSE(recognition_ended_); | 292 EXPECT_FALSE(recognition_ended_); |
| 240 EXPECT_FALSE(result_received_); | 293 EXPECT_FALSE(result_received_); |
| 241 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 294 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 242 | 295 |
| 243 // Issue the network callback to complete the process. | 296 // Issue the network callback to complete the process. |
| 244 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 297 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 245 ASSERT_TRUE(fetcher); | 298 ASSERT_TRUE(fetcher); |
| 246 | 299 |
| 247 fetcher->set_url(fetcher->GetOriginalURL()); | 300 fetcher->set_url(fetcher->GetOriginalURL()); |
| 248 net::URLRequestStatus status; | 301 net::URLRequestStatus status; |
| 249 status.set_status(net::URLRequestStatus::SUCCESS); | 302 status.set_status(net::URLRequestStatus::SUCCESS); |
| 250 fetcher->set_status(status); | 303 fetcher->set_status(status); |
| 251 fetcher->set_response_code(200); | 304 fetcher->set_response_code(200); |
| 252 fetcher->SetResponseString( | 305 fetcher->SetResponseString( |
| 253 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); | 306 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); |
| 254 fetcher->delegate()->OnURLFetchComplete(fetcher); | 307 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 255 | 308 MessageLoop::current()->RunAllPending(); |
| 256 EXPECT_TRUE(recognition_ended_); | 309 EXPECT_TRUE(recognition_ended_); |
| 257 EXPECT_TRUE(result_received_); | 310 EXPECT_TRUE(result_received_); |
| 258 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 311 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 312 CheckFinalEventsConsistency(); | |
| 259 } | 313 } |
| 260 | 314 |
| 261 TEST_F(SpeechRecognizerImplTest, CancelWithData) { | 315 TEST_F(SpeechRecognizerImplTest, CancelWithData) { |
| 262 // Start recording, give some data and then cancel. This should create | 316 // Start recording, give some data and then cancel. |
| 263 // a network request but give no callbacks. | |
| 264 recognizer_->StartRecognition(); | 317 recognizer_->StartRecognition(); |
| 318 MessageLoop::current()->RunAllPending(); | |
| 265 TestAudioInputController* controller = | 319 TestAudioInputController* controller = |
| 266 audio_input_controller_factory_.controller(); | 320 audio_input_controller_factory_.controller(); |
| 267 ASSERT_TRUE(controller); | 321 ASSERT_TRUE(controller); |
| 268 controller->event_handler()->OnData(controller, &audio_packet_[0], | 322 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 269 audio_packet_.size()); | 323 audio_packet_.size()); |
| 270 MessageLoop::current()->RunAllPending(); | 324 MessageLoop::current()->RunAllPending(); |
| 271 recognizer_->AbortRecognition(); | 325 recognizer_->AbortRecognition(); |
| 326 MessageLoop::current()->RunAllPending(); | |
| 272 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 327 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 328 EXPECT_TRUE(recognition_started_); | |
| 273 EXPECT_TRUE(audio_started_); | 329 EXPECT_TRUE(audio_started_); |
| 274 EXPECT_FALSE(audio_ended_); | |
| 275 EXPECT_FALSE(recognition_ended_); | |
| 276 EXPECT_FALSE(result_received_); | 330 EXPECT_FALSE(result_received_); |
| 277 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 331 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 332 CheckFinalEventsConsistency(); | |
| 278 } | 333 } |
| 279 | 334 |
| 280 TEST_F(SpeechRecognizerImplTest, ConnectionError) { | 335 TEST_F(SpeechRecognizerImplTest, ConnectionError) { |
| 281 // Start recording, give some data and then stop. Issue the network callback | 336 // Start recording, give some data and then stop. Issue the network callback |
| 282 // with a connection error and verify that the recognizer bubbles the error up | 337 // with a connection error and verify that the recognizer bubbles the error up |
| 283 recognizer_->StartRecognition(); | 338 recognizer_->StartRecognition(); |
| 339 MessageLoop::current()->RunAllPending(); | |
| 284 TestAudioInputController* controller = | 340 TestAudioInputController* controller = |
| 285 audio_input_controller_factory_.controller(); | 341 audio_input_controller_factory_.controller(); |
| 286 ASSERT_TRUE(controller); | 342 ASSERT_TRUE(controller); |
| 287 controller->event_handler()->OnData(controller, &audio_packet_[0], | 343 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 288 audio_packet_.size()); | 344 audio_packet_.size()); |
| 289 MessageLoop::current()->RunAllPending(); | 345 MessageLoop::current()->RunAllPending(); |
| 290 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 346 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 291 ASSERT_TRUE(fetcher); | 347 ASSERT_TRUE(fetcher); |
| 292 | 348 |
| 293 recognizer_->StopAudioCapture(); | 349 recognizer_->StopAudioCapture(); |
| 350 MessageLoop::current()->RunAllPending(); | |
| 294 EXPECT_TRUE(audio_started_); | 351 EXPECT_TRUE(audio_started_); |
| 295 EXPECT_TRUE(audio_ended_); | 352 EXPECT_TRUE(audio_ended_); |
| 296 EXPECT_FALSE(recognition_ended_); | 353 EXPECT_FALSE(recognition_ended_); |
| 297 EXPECT_FALSE(result_received_); | 354 EXPECT_FALSE(result_received_); |
| 298 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 355 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 299 | 356 |
| 300 // Issue the network callback to complete the process. | 357 // Issue the network callback to complete the process. |
| 301 fetcher->set_url(fetcher->GetOriginalURL()); | 358 fetcher->set_url(fetcher->GetOriginalURL()); |
| 302 net::URLRequestStatus status; | 359 net::URLRequestStatus status; |
| 303 status.set_status(net::URLRequestStatus::FAILED); | 360 status.set_status(net::URLRequestStatus::FAILED); |
| 304 status.set_error(net::ERR_CONNECTION_REFUSED); | 361 status.set_error(net::ERR_CONNECTION_REFUSED); |
| 305 fetcher->set_status(status); | 362 fetcher->set_status(status); |
| 306 fetcher->set_response_code(0); | 363 fetcher->set_response_code(0); |
| 307 fetcher->SetResponseString(""); | 364 fetcher->SetResponseString(""); |
| 308 fetcher->delegate()->OnURLFetchComplete(fetcher); | 365 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 309 | 366 MessageLoop::current()->RunAllPending(); |
| 310 EXPECT_FALSE(recognition_ended_); | 367 EXPECT_TRUE(recognition_ended_); |
| 311 EXPECT_FALSE(result_received_); | 368 EXPECT_FALSE(result_received_); |
| 312 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 369 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 370 CheckFinalEventsConsistency(); | |
| 313 } | 371 } |
| 314 | 372 |
| 315 TEST_F(SpeechRecognizerImplTest, ServerError) { | 373 TEST_F(SpeechRecognizerImplTest, ServerError) { |
| 316 // Start recording, give some data and then stop. Issue the network callback | 374 // Start recording, give some data and then stop. Issue the network callback |
| 317 // with a 500 error and verify that the recognizer bubbles the error up | 375 // with a 500 error and verify that the recognizer bubbles the error up |
| 318 recognizer_->StartRecognition(); | 376 recognizer_->StartRecognition(); |
| 377 MessageLoop::current()->RunAllPending(); | |
| 319 TestAudioInputController* controller = | 378 TestAudioInputController* controller = |
| 320 audio_input_controller_factory_.controller(); | 379 audio_input_controller_factory_.controller(); |
| 321 ASSERT_TRUE(controller); | 380 ASSERT_TRUE(controller); |
| 322 controller->event_handler()->OnData(controller, &audio_packet_[0], | 381 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 323 audio_packet_.size()); | 382 audio_packet_.size()); |
| 324 MessageLoop::current()->RunAllPending(); | 383 MessageLoop::current()->RunAllPending(); |
| 325 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 384 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 326 ASSERT_TRUE(fetcher); | 385 ASSERT_TRUE(fetcher); |
| 327 | 386 |
| 328 recognizer_->StopAudioCapture(); | 387 recognizer_->StopAudioCapture(); |
| 388 MessageLoop::current()->RunAllPending(); | |
| 329 EXPECT_TRUE(audio_started_); | 389 EXPECT_TRUE(audio_started_); |
| 330 EXPECT_TRUE(audio_ended_); | 390 EXPECT_TRUE(audio_ended_); |
| 331 EXPECT_FALSE(recognition_ended_); | 391 EXPECT_FALSE(recognition_ended_); |
| 332 EXPECT_FALSE(result_received_); | 392 EXPECT_FALSE(result_received_); |
| 333 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 393 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 334 | 394 |
| 335 // Issue the network callback to complete the process. | 395 // Issue the network callback to complete the process. |
| 336 fetcher->set_url(fetcher->GetOriginalURL()); | 396 fetcher->set_url(fetcher->GetOriginalURL()); |
| 337 net::URLRequestStatus status; | 397 net::URLRequestStatus status; |
| 338 status.set_status(net::URLRequestStatus::SUCCESS); | 398 status.set_status(net::URLRequestStatus::SUCCESS); |
| 339 fetcher->set_status(status); | 399 fetcher->set_status(status); |
| 340 fetcher->set_response_code(500); | 400 fetcher->set_response_code(500); |
| 341 fetcher->SetResponseString("Internal Server Error"); | 401 fetcher->SetResponseString("Internal Server Error"); |
| 342 fetcher->delegate()->OnURLFetchComplete(fetcher); | 402 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 343 | 403 MessageLoop::current()->RunAllPending(); |
| 344 EXPECT_FALSE(recognition_ended_); | 404 EXPECT_TRUE(recognition_ended_); |
| 345 EXPECT_FALSE(result_received_); | 405 EXPECT_FALSE(result_received_); |
| 346 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 406 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 407 CheckFinalEventsConsistency(); | |
| 347 } | 408 } |
| 348 | 409 |
| 349 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { | 410 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { |
| 350 // Check if things tear down properly if AudioInputController threw an error. | 411 // Check if things tear down properly if AudioInputController threw an error. |
| 351 recognizer_->StartRecognition(); | 412 recognizer_->StartRecognition(); |
| 413 MessageLoop::current()->RunAllPending(); | |
| 352 TestAudioInputController* controller = | 414 TestAudioInputController* controller = |
| 353 audio_input_controller_factory_.controller(); | 415 audio_input_controller_factory_.controller(); |
| 354 ASSERT_TRUE(controller); | 416 ASSERT_TRUE(controller); |
| 355 controller->event_handler()->OnError(controller, 0); | 417 controller->event_handler()->OnError(controller, 0); |
| 356 MessageLoop::current()->RunAllPending(); | 418 MessageLoop::current()->RunAllPending(); |
| 419 EXPECT_TRUE(recognition_started_); | |
| 357 EXPECT_FALSE(audio_started_); | 420 EXPECT_FALSE(audio_started_); |
| 358 EXPECT_FALSE(audio_ended_); | |
| 359 EXPECT_FALSE(recognition_ended_); | |
| 360 EXPECT_FALSE(result_received_); | 421 EXPECT_FALSE(result_received_); |
| 361 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 422 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 423 CheckFinalEventsConsistency(); | |
| 362 } | 424 } |
| 363 | 425 |
| 364 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { | 426 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { |
| 365 // Check if things tear down properly if AudioInputController threw an error | 427 // Check if things tear down properly if AudioInputController threw an error |
| 366 // after giving some audio data. | 428 // after giving some audio data. |
| 367 recognizer_->StartRecognition(); | 429 recognizer_->StartRecognition(); |
| 430 MessageLoop::current()->RunAllPending(); | |
| 368 TestAudioInputController* controller = | 431 TestAudioInputController* controller = |
| 369 audio_input_controller_factory_.controller(); | 432 audio_input_controller_factory_.controller(); |
| 370 ASSERT_TRUE(controller); | 433 ASSERT_TRUE(controller); |
| 371 controller->event_handler()->OnData(controller, &audio_packet_[0], | 434 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 372 audio_packet_.size()); | 435 audio_packet_.size()); |
| 373 controller->event_handler()->OnError(controller, 0); | 436 controller->event_handler()->OnError(controller, 0); |
| 374 MessageLoop::current()->RunAllPending(); | 437 MessageLoop::current()->RunAllPending(); |
| 375 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 438 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 439 EXPECT_TRUE(recognition_started_); | |
| 376 EXPECT_TRUE(audio_started_); | 440 EXPECT_TRUE(audio_started_); |
| 377 EXPECT_FALSE(audio_ended_); | |
| 378 EXPECT_FALSE(recognition_ended_); | |
| 379 EXPECT_FALSE(result_received_); | 441 EXPECT_FALSE(result_received_); |
| 380 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 442 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 443 CheckFinalEventsConsistency(); | |
| 381 } | 444 } |
| 382 | 445 |
| 383 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { | 446 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { |
| 384 // Start recording and give a lot of packets with audio samples set to zero. | 447 // Start recording and give a lot of packets with audio samples set to zero. |
| 385 // This should trigger the no-speech detector and issue a callback. | 448 // This should trigger the no-speech detector and issue a callback. |
| 386 recognizer_->StartRecognition(); | 449 recognizer_->StartRecognition(); |
| 450 MessageLoop::current()->RunAllPending(); | |
| 387 TestAudioInputController* controller = | 451 TestAudioInputController* controller = |
| 388 audio_input_controller_factory_.controller(); | 452 audio_input_controller_factory_.controller(); |
| 389 ASSERT_TRUE(controller); | 453 ASSERT_TRUE(controller); |
| 390 controller = audio_input_controller_factory_.controller(); | |
| 391 ASSERT_TRUE(controller); | |
| 392 | 454 |
| 393 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / | 455 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / |
| 394 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; | 456 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs + 1; |
| 395 // The vector is already filled with zero value samples on create. | 457 // The vector is already filled with zero value samples on create. |
| 396 for (int i = 0; i < num_packets; ++i) { | 458 for (int i = 0; i < num_packets; ++i) { |
| 397 controller->event_handler()->OnData(controller, &audio_packet_[0], | 459 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 398 audio_packet_.size()); | 460 audio_packet_.size()); |
| 399 } | 461 } |
| 400 MessageLoop::current()->RunAllPending(); | 462 MessageLoop::current()->RunAllPending(); |
| 463 EXPECT_TRUE(recognition_started_); | |
| 401 EXPECT_TRUE(audio_started_); | 464 EXPECT_TRUE(audio_started_); |
| 402 EXPECT_FALSE(audio_ended_); | |
| 403 EXPECT_FALSE(recognition_ended_); | |
| 404 EXPECT_FALSE(result_received_); | 465 EXPECT_FALSE(result_received_); |
| 405 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); | 466 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
| 467 CheckFinalEventsConsistency(); | |
| 406 } | 468 } |
| 407 | 469 |
| 408 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { | 470 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { |
| 409 // Start recording and give a lot of packets with audio samples set to zero | 471 // Start recording and give a lot of packets with audio samples set to zero |
| 410 // and then some more with reasonably loud audio samples. This should be | 472 // and then some more with reasonably loud audio samples. This should be |
| 411 // treated as normal speech input and the no-speech detector should not get | 473 // treated as normal speech input and the no-speech detector should not get |
| 412 // triggered. | 474 // triggered. |
| 413 recognizer_->StartRecognition(); | 475 recognizer_->StartRecognition(); |
| 476 MessageLoop::current()->RunAllPending(); | |
| 414 TestAudioInputController* controller = | 477 TestAudioInputController* controller = |
| 415 audio_input_controller_factory_.controller(); | 478 audio_input_controller_factory_.controller(); |
| 416 ASSERT_TRUE(controller); | 479 ASSERT_TRUE(controller); |
| 417 controller = audio_input_controller_factory_.controller(); | 480 controller = audio_input_controller_factory_.controller(); |
| 418 ASSERT_TRUE(controller); | 481 ASSERT_TRUE(controller); |
| 419 | 482 |
| 420 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / | 483 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / |
| 421 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; | 484 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; |
| 422 | 485 |
| 423 // The vector is already filled with zero value samples on create. | 486 // The vector is already filled with zero value samples on create. |
| 424 for (int i = 0; i < num_packets / 2; ++i) { | 487 for (int i = 0; i < num_packets / 2; ++i) { |
| 425 controller->event_handler()->OnData(controller, &audio_packet_[0], | 488 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 426 audio_packet_.size()); | 489 audio_packet_.size()); |
| 427 } | 490 } |
| 428 | 491 |
| 429 FillPacketWithTestWaveform(); | 492 FillPacketWithTestWaveform(); |
| 430 for (int i = 0; i < num_packets / 2; ++i) { | 493 for (int i = 0; i < num_packets / 2; ++i) { |
| 431 controller->event_handler()->OnData(controller, &audio_packet_[0], | 494 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 432 audio_packet_.size()); | 495 audio_packet_.size()); |
| 433 } | 496 } |
| 434 | 497 |
| 435 MessageLoop::current()->RunAllPending(); | 498 MessageLoop::current()->RunAllPending(); |
| 436 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 499 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 437 EXPECT_TRUE(audio_started_); | 500 EXPECT_TRUE(audio_started_); |
| 438 EXPECT_FALSE(audio_ended_); | 501 EXPECT_FALSE(audio_ended_); |
| 439 EXPECT_FALSE(recognition_ended_); | 502 EXPECT_FALSE(recognition_ended_); |
| 440 recognizer_->AbortRecognition(); | 503 recognizer_->AbortRecognition(); |
| 504 MessageLoop::current()->RunAllPending(); | |
| 505 CheckFinalEventsConsistency(); | |
| 441 } | 506 } |
| 442 | 507 |
| 443 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { | 508 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { |
| 444 // Start recording and give a lot of packets with audio samples set to zero | 509 // Start recording and give a lot of packets with audio samples set to zero |
| 445 // and then some more with reasonably loud audio samples. Check that we don't | 510 // and then some more with reasonably loud audio samples. Check that we don't |
| 446 // get the callback during estimation phase, then get zero for the silence | 511 // get the callback during estimation phase, then get zero for the silence |
| 447 // samples and proper volume for the loud audio. | 512 // samples and proper volume for the loud audio. |
| 448 recognizer_->StartRecognition(); | 513 recognizer_->StartRecognition(); |
| 514 MessageLoop::current()->RunAllPending(); | |
| 449 TestAudioInputController* controller = | 515 TestAudioInputController* controller = |
| 450 audio_input_controller_factory_.controller(); | 516 audio_input_controller_factory_.controller(); |
| 451 ASSERT_TRUE(controller); | 517 ASSERT_TRUE(controller); |
| 452 controller = audio_input_controller_factory_.controller(); | 518 controller = audio_input_controller_factory_.controller(); |
| 453 ASSERT_TRUE(controller); | 519 ASSERT_TRUE(controller); |
| 454 | 520 |
| 455 // Feed some samples to begin with for the endpointer to do noise estimation. | 521 // Feed some samples to begin with for the endpointer to do noise estimation. |
| 456 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / | 522 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / |
| 457 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; | 523 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; |
| 458 FillPacketWithNoise(); | 524 FillPacketWithNoise(); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 473 controller->event_handler()->OnData(controller, &audio_packet_[0], | 539 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 474 audio_packet_.size()); | 540 audio_packet_.size()); |
| 475 MessageLoop::current()->RunAllPending(); | 541 MessageLoop::current()->RunAllPending(); |
| 476 EXPECT_FLOAT_EQ(0.89926866f, volume_); | 542 EXPECT_FLOAT_EQ(0.89926866f, volume_); |
| 477 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); | 543 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); |
| 478 | 544 |
| 479 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 545 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 480 EXPECT_FALSE(audio_ended_); | 546 EXPECT_FALSE(audio_ended_); |
| 481 EXPECT_FALSE(recognition_ended_); | 547 EXPECT_FALSE(recognition_ended_); |
| 482 recognizer_->AbortRecognition(); | 548 recognizer_->AbortRecognition(); |
| 549 MessageLoop::current()->RunAllPending(); | |
| 550 CheckFinalEventsConsistency(); | |
| 483 } | 551 } |
| 484 | 552 |
| 485 } // namespace speech | 553 } // namespace speech |
| OLD | NEW |