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