| 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/speech_recognizer_impl.h" | 8 #include "content/browser/speech/speech_recognizer_impl.h" |
| 9 #include "content/public/browser/speech_recognizer_delegate.h" | 9 #include "content/public/browser/speech_recognition_event_listener.h" |
| 10 #include "content/test/test_url_fetcher_factory.h" | 10 #include "content/test/test_url_fetcher_factory.h" |
| 11 #include "media/audio/audio_manager.h" | 11 #include "media/audio/audio_manager.h" |
| 12 #include "media/audio/fake_audio_input_stream.h" | 12 #include "media/audio/fake_audio_input_stream.h" |
| 13 #include "media/audio/fake_audio_output_stream.h" | 13 #include "media/audio/fake_audio_output_stream.h" |
| 14 #include "media/audio/test_audio_input_controller_factory.h" | 14 #include "media/audio/test_audio_input_controller_factory.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 using content::BrowserThread; | 19 using content::BrowserThread; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 44 virtual AudioOutputStream* MakeAudioOutputStreamProxy( | 44 virtual AudioOutputStream* MakeAudioOutputStreamProxy( |
| 45 const AudioParameters& params) OVERRIDE { | 45 const AudioParameters& params) OVERRIDE { |
| 46 NOTREACHED(); | 46 NOTREACHED(); |
| 47 return NULL; | 47 return NULL; |
| 48 } | 48 } |
| 49 virtual AudioInputStream* MakeAudioInputStream( | 49 virtual AudioInputStream* MakeAudioInputStream( |
| 50 const AudioParameters& params, const std::string& device_id) OVERRIDE { | 50 const AudioParameters& params, const std::string& device_id) OVERRIDE { |
| 51 return FakeAudioInputStream::MakeFakeStream(this, params); | 51 return FakeAudioInputStream::MakeFakeStream(this, params); |
| 52 } | 52 } |
| 53 virtual AudioOutputStream* MakeLinearOutputStream( | 53 virtual AudioOutputStream* MakeLinearOutputStream( |
| 54 const AudioParameters& params) OVERRIDE { return NULL; } | 54 const AudioParameters& params) OVERRIDE { |
| 55 NOTREACHED(); |
| 56 return NULL; |
| 57 } |
| 55 virtual AudioOutputStream* MakeLowLatencyOutputStream( | 58 virtual AudioOutputStream* MakeLowLatencyOutputStream( |
| 56 const AudioParameters& params) OVERRIDE { return NULL; } | 59 const AudioParameters& params) OVERRIDE { |
| 60 NOTREACHED(); |
| 61 return NULL; |
| 62 } |
| 57 virtual AudioInputStream* MakeLinearInputStream( | 63 virtual AudioInputStream* MakeLinearInputStream( |
| 58 const AudioParameters& params, const std::string& device_id) OVERRIDE { | 64 const AudioParameters& params, const std::string& device_id) OVERRIDE { |
| 65 NOTREACHED(); |
| 59 return NULL; | 66 return NULL; |
| 60 } | 67 } |
| 61 virtual AudioInputStream* MakeLowLatencyInputStream( | 68 virtual AudioInputStream* MakeLowLatencyInputStream( |
| 62 const AudioParameters& params, const std::string& device_id) OVERRIDE { | 69 const AudioParameters& params, const std::string& device_id) OVERRIDE { |
| 70 NOTREACHED(); |
| 63 return NULL; | 71 return NULL; |
| 64 } | 72 } |
| 65 virtual void MuteAll() OVERRIDE {} | 73 virtual void MuteAll() OVERRIDE {} |
| 66 virtual void UnMuteAll() OVERRIDE {} | 74 virtual void UnMuteAll() OVERRIDE {} |
| 67 virtual bool IsRecordingInProcess() OVERRIDE { return false; } | 75 virtual bool IsRecordingInProcess() OVERRIDE { return false; } |
| 68 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE { | 76 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE { |
| 69 return audio_thread_->message_loop_proxy(); | 77 return audio_thread_->message_loop_proxy(); |
| 70 } | 78 } |
| 71 virtual void Init() OVERRIDE {}; | 79 virtual void Init() OVERRIDE {}; |
| 72 private: | 80 private: |
| 73 scoped_ptr<base::Thread> audio_thread_; | 81 scoped_ptr<base::Thread> audio_thread_; |
| 74 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); | 82 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); |
| 75 }; | 83 }; |
| 76 } // namespace | 84 } // namespace |
| 77 | 85 |
| 78 | 86 |
| 79 namespace speech { | 87 namespace speech { |
| 80 | 88 |
| 81 class SpeechRecognizerTest : public content::SpeechRecognizerDelegate, | 89 class SpeechRecognizerImplTest : public content::SpeechRecognitionEventListener, |
| 82 public testing::Test { | 90 public testing::Test { |
| 83 public: | 91 public: |
| 84 SpeechRecognizerTest() | 92 SpeechRecognizerImplTest() |
| 85 : io_thread_(BrowserThread::IO, &message_loop_), | 93 : io_thread_(BrowserThread::IO, &message_loop_), |
| 86 audio_manager_(new MockAudioManager()), | 94 audio_manager_(new MockAudioManager()), |
| 87 recording_complete_(false), | 95 audio_ended_(false), |
| 88 recognition_complete_(false), | 96 recognition_ended_(false), |
| 89 result_received_(false), | 97 result_received_(false), |
| 90 audio_received_(false), | 98 audio_started_(false), |
| 91 error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 99 error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
| 92 volume_(-1.0f) { | 100 volume_(-1.0f) { |
| 93 recognizer_ = new SpeechRecognizerImpl( | 101 recognizer_ = new SpeechRecognizerImpl( |
| 94 this, 1, std::string(), std::string(), NULL, false, std::string(), | 102 this, 1, std::string(), std::string(), NULL, false, std::string(), |
| 95 std::string()); | 103 std::string()); |
| 96 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); | 104 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); |
| 97 int audio_packet_length_bytes = | 105 int audio_packet_length_bytes = |
| 98 (SpeechRecognizerImpl::kAudioSampleRate * | 106 (SpeechRecognizerImpl::kAudioSampleRate * |
| 99 SpeechRecognizerImpl::kAudioPacketIntervalMs * | 107 SpeechRecognizerImpl::kAudioPacketIntervalMs * |
| 100 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * | 108 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * |
| 101 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); | 109 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); |
| 102 audio_packet_.resize(audio_packet_length_bytes); | 110 audio_packet_.resize(audio_packet_length_bytes); |
| 103 } | 111 } |
| 104 | 112 |
| 105 // Overridden from content::SpeechRecognizerDelegate: | 113 // Overridden from content::SpeechRecognitionEventListener: |
| 106 virtual void SetRecognitionResult( | 114 virtual void OnAudioStart(int caller_id) OVERRIDE { |
| 107 int caller_id, | 115 audio_started_ = true; |
| 108 const content::SpeechRecognitionResult& result) OVERRIDE { | 116 } |
| 117 |
| 118 virtual void OnAudioEnd(int caller_id) OVERRIDE { |
| 119 audio_ended_ = true; |
| 120 } |
| 121 |
| 122 virtual void OnRecognitionResult( |
| 123 int caller_id, const content::SpeechRecognitionResult& result) OVERRIDE { |
| 109 result_received_ = true; | 124 result_received_ = true; |
| 110 } | 125 } |
| 111 | 126 |
| 112 virtual void DidCompleteRecording(int caller_id) OVERRIDE { | 127 virtual void OnRecognitionError( |
| 113 recording_complete_ = true; | 128 int caller_id, |
| 114 } | 129 const content::SpeechRecognitionErrorCode& error) OVERRIDE { |
| 115 | |
| 116 virtual void DidCompleteRecognition(int caller_id) OVERRIDE { | |
| 117 recognition_complete_ = true; | |
| 118 } | |
| 119 | |
| 120 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE { | |
| 121 } | |
| 122 | |
| 123 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE { | |
| 124 audio_received_ = true; | |
| 125 } | |
| 126 | |
| 127 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE { | |
| 128 } | |
| 129 | |
| 130 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE { | |
| 131 } | |
| 132 | |
| 133 virtual void OnRecognizerError( | |
| 134 int caller_id, content::SpeechRecognitionErrorCode error) OVERRIDE { | |
| 135 error_ = error; | 130 error_ = error; |
| 136 } | 131 } |
| 137 | 132 |
| 138 virtual void SetInputVolume(int caller_id, float volume, | 133 virtual void OnAudioLevelsChange(int caller_id, float volume, |
| 139 float noise_volume) OVERRIDE { | 134 float noise_volume) OVERRIDE { |
| 140 volume_ = volume; | 135 volume_ = volume; |
| 141 noise_volume_ = noise_volume; | 136 noise_volume_ = noise_volume; |
| 142 } | 137 } |
| 143 | 138 |
| 139 virtual void OnRecognitionEnd(int caller_id) OVERRIDE { |
| 140 recognition_ended_ = true; |
| 141 } |
| 142 |
| 143 virtual void OnRecognitionStart(int caller_id) OVERRIDE {} |
| 144 virtual void OnEnvironmentEstimationComplete(int caller_id) OVERRIDE {} |
| 145 virtual void OnSoundStart(int caller_id) OVERRIDE {} |
| 146 virtual void OnSoundEnd(int caller_id) OVERRIDE {} |
| 147 |
| 144 // testing::Test methods. | 148 // testing::Test methods. |
| 145 virtual void SetUp() OVERRIDE { | 149 virtual void SetUp() OVERRIDE { |
| 146 AudioInputController::set_factory_for_testing( | 150 AudioInputController::set_factory_for_testing( |
| 147 &audio_input_controller_factory_); | 151 &audio_input_controller_factory_); |
| 148 } | 152 } |
| 149 | 153 |
| 150 virtual void TearDown() OVERRIDE { | 154 virtual void TearDown() OVERRIDE { |
| 151 AudioInputController::set_factory_for_testing(NULL); | 155 AudioInputController::set_factory_for_testing(NULL); |
| 152 } | 156 } |
| 153 | 157 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 164 value += factor; | 168 value += factor; |
| 165 audio_packet_[i] = value % 100; | 169 audio_packet_[i] = value % 100; |
| 166 } | 170 } |
| 167 } | 171 } |
| 168 | 172 |
| 169 protected: | 173 protected: |
| 170 MessageLoopForIO message_loop_; | 174 MessageLoopForIO message_loop_; |
| 171 BrowserThreadImpl io_thread_; | 175 BrowserThreadImpl io_thread_; |
| 172 scoped_refptr<SpeechRecognizerImpl> recognizer_; | 176 scoped_refptr<SpeechRecognizerImpl> recognizer_; |
| 173 scoped_ptr<AudioManager> audio_manager_; | 177 scoped_ptr<AudioManager> audio_manager_; |
| 174 bool recording_complete_; | 178 bool audio_ended_; |
| 175 bool recognition_complete_; | 179 bool recognition_ended_; |
| 176 bool result_received_; | 180 bool result_received_; |
| 177 bool audio_received_; | 181 bool audio_started_; |
| 178 content::SpeechRecognitionErrorCode error_; | 182 content::SpeechRecognitionErrorCode error_; |
| 179 TestURLFetcherFactory url_fetcher_factory_; | 183 TestURLFetcherFactory url_fetcher_factory_; |
| 180 TestAudioInputControllerFactory audio_input_controller_factory_; | 184 TestAudioInputControllerFactory audio_input_controller_factory_; |
| 181 std::vector<uint8> audio_packet_; | 185 std::vector<uint8> audio_packet_; |
| 182 float volume_; | 186 float volume_; |
| 183 float noise_volume_; | 187 float noise_volume_; |
| 184 }; | 188 }; |
| 185 | 189 |
| 186 TEST_F(SpeechRecognizerTest, StopNoData) { | 190 TEST_F(SpeechRecognizerImplTest, StopNoData) { |
| 187 // Check for callbacks when stopping record before any audio gets recorded. | 191 // Check for callbacks when stopping record before any audio gets recorded. |
| 188 EXPECT_TRUE(recognizer_->StartRecording()); | 192 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 189 recognizer_->CancelRecognition(); | 193 recognizer_->AbortRecognition(); |
| 190 EXPECT_FALSE(recording_complete_); | 194 EXPECT_FALSE(audio_ended_); |
| 191 EXPECT_FALSE(recognition_complete_); | 195 EXPECT_FALSE(recognition_ended_); |
| 192 EXPECT_FALSE(result_received_); | 196 EXPECT_FALSE(result_received_); |
| 193 EXPECT_FALSE(audio_received_); | 197 EXPECT_FALSE(audio_started_); |
| 194 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 198 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 195 } | 199 } |
| 196 | 200 |
| 197 TEST_F(SpeechRecognizerTest, CancelNoData) { | 201 TEST_F(SpeechRecognizerImplTest, CancelNoData) { |
| 198 // Check for callbacks when canceling recognition before any audio gets | 202 // Check for callbacks when canceling recognition before any audio gets |
| 199 // recorded. | 203 // recorded. |
| 200 EXPECT_TRUE(recognizer_->StartRecording()); | 204 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 201 recognizer_->StopRecording(); | 205 recognizer_->StopAudioCapture(); |
| 202 EXPECT_TRUE(recording_complete_); | 206 EXPECT_TRUE(audio_ended_); |
| 203 EXPECT_TRUE(recognition_complete_); | 207 EXPECT_TRUE(recognition_ended_); |
| 204 EXPECT_FALSE(result_received_); | 208 EXPECT_FALSE(result_received_); |
| 205 EXPECT_FALSE(audio_received_); | 209 EXPECT_FALSE(audio_started_); |
| 206 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 210 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 207 } | 211 } |
| 208 | 212 |
| 209 TEST_F(SpeechRecognizerTest, StopWithData) { | 213 TEST_F(SpeechRecognizerImplTest, StopWithData) { |
| 210 // Start recording, give some data and then stop. This should wait for the | 214 // Start recording, give some data and then stop. This should wait for the |
| 211 // network callback to arrive before completion. | 215 // network callback to arrive before completion. |
| 212 EXPECT_TRUE(recognizer_->StartRecording()); | 216 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 213 TestAudioInputController* controller = | 217 TestAudioInputController* controller = |
| 214 audio_input_controller_factory_.controller(); | 218 audio_input_controller_factory_.controller(); |
| 215 ASSERT_TRUE(controller); | 219 ASSERT_TRUE(controller); |
| 216 | 220 |
| 217 // Try sending 5 chunks of mock audio data and verify that each of them | 221 // Try sending 5 chunks of mock audio data and verify that each of them |
| 218 // resulted immediately in a packet sent out via the network. This verifies | 222 // resulted immediately in a packet sent out via the network. This verifies |
| 219 // that we are streaming out encoded data as chunks without waiting for the | 223 // that we are streaming out encoded data as chunks without waiting for the |
| 220 // full recording to complete. | 224 // full recording to complete. |
| 221 const size_t kNumChunks = 5; | 225 const size_t kNumChunks = 5; |
| 222 for (size_t i = 0; i < kNumChunks; ++i) { | 226 for (size_t i = 0; i < kNumChunks; ++i) { |
| 223 controller->event_handler()->OnData(controller, &audio_packet_[0], | 227 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 224 audio_packet_.size()); | 228 audio_packet_.size()); |
| 225 MessageLoop::current()->RunAllPending(); | 229 MessageLoop::current()->RunAllPending(); |
| 226 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 230 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 227 ASSERT_TRUE(fetcher); | 231 ASSERT_TRUE(fetcher); |
| 228 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); | 232 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); |
| 229 } | 233 } |
| 230 | 234 |
| 231 recognizer_->StopRecording(); | 235 recognizer_->StopAudioCapture(); |
| 232 EXPECT_TRUE(audio_received_); | 236 EXPECT_TRUE(audio_started_); |
| 233 EXPECT_TRUE(recording_complete_); | 237 EXPECT_TRUE(audio_ended_); |
| 234 EXPECT_FALSE(recognition_complete_); | 238 EXPECT_FALSE(recognition_ended_); |
| 235 EXPECT_FALSE(result_received_); | 239 EXPECT_FALSE(result_received_); |
| 236 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 240 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 237 | 241 |
| 238 // Issue the network callback to complete the process. | 242 // Issue the network callback to complete the process. |
| 239 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 243 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 240 ASSERT_TRUE(fetcher); | 244 ASSERT_TRUE(fetcher); |
| 241 | 245 |
| 242 fetcher->set_url(fetcher->GetOriginalURL()); | 246 fetcher->set_url(fetcher->GetOriginalURL()); |
| 243 net::URLRequestStatus status; | 247 net::URLRequestStatus status; |
| 244 status.set_status(net::URLRequestStatus::SUCCESS); | 248 status.set_status(net::URLRequestStatus::SUCCESS); |
| 245 fetcher->set_status(status); | 249 fetcher->set_status(status); |
| 246 fetcher->set_response_code(200); | 250 fetcher->set_response_code(200); |
| 247 fetcher->SetResponseString( | 251 fetcher->SetResponseString( |
| 248 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); | 252 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); |
| 249 fetcher->delegate()->OnURLFetchComplete(fetcher); | 253 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 250 | 254 |
| 251 EXPECT_TRUE(recognition_complete_); | 255 EXPECT_TRUE(recognition_ended_); |
| 252 EXPECT_TRUE(result_received_); | 256 EXPECT_TRUE(result_received_); |
| 253 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 257 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 254 } | 258 } |
| 255 | 259 |
| 256 TEST_F(SpeechRecognizerTest, CancelWithData) { | 260 TEST_F(SpeechRecognizerImplTest, CancelWithData) { |
| 257 // Start recording, give some data and then cancel. This should create | 261 // Start recording, give some data and then cancel. This should create |
| 258 // a network request but give no callbacks. | 262 // a network request but give no callbacks. |
| 259 EXPECT_TRUE(recognizer_->StartRecording()); | 263 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 260 TestAudioInputController* controller = | 264 TestAudioInputController* controller = |
| 261 audio_input_controller_factory_.controller(); | 265 audio_input_controller_factory_.controller(); |
| 262 ASSERT_TRUE(controller); | 266 ASSERT_TRUE(controller); |
| 263 controller->event_handler()->OnData(controller, &audio_packet_[0], | 267 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 264 audio_packet_.size()); | 268 audio_packet_.size()); |
| 265 MessageLoop::current()->RunAllPending(); | 269 MessageLoop::current()->RunAllPending(); |
| 266 recognizer_->CancelRecognition(); | 270 recognizer_->AbortRecognition(); |
| 267 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 271 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 268 EXPECT_TRUE(audio_received_); | 272 EXPECT_TRUE(audio_started_); |
| 269 EXPECT_FALSE(recording_complete_); | 273 EXPECT_FALSE(audio_ended_); |
| 270 EXPECT_FALSE(recognition_complete_); | 274 EXPECT_FALSE(recognition_ended_); |
| 271 EXPECT_FALSE(result_received_); | 275 EXPECT_FALSE(result_received_); |
| 272 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 276 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 273 } | 277 } |
| 274 | 278 |
| 275 TEST_F(SpeechRecognizerTest, ConnectionError) { | 279 TEST_F(SpeechRecognizerImplTest, ConnectionError) { |
| 276 // Start recording, give some data and then stop. Issue the network callback | 280 // Start recording, give some data and then stop. Issue the network callback |
| 277 // with a connection error and verify that the recognizer bubbles the error up | 281 // with a connection error and verify that the recognizer bubbles the error up |
| 278 EXPECT_TRUE(recognizer_->StartRecording()); | 282 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 279 TestAudioInputController* controller = | 283 TestAudioInputController* controller = |
| 280 audio_input_controller_factory_.controller(); | 284 audio_input_controller_factory_.controller(); |
| 281 ASSERT_TRUE(controller); | 285 ASSERT_TRUE(controller); |
| 282 controller->event_handler()->OnData(controller, &audio_packet_[0], | 286 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 283 audio_packet_.size()); | 287 audio_packet_.size()); |
| 284 MessageLoop::current()->RunAllPending(); | 288 MessageLoop::current()->RunAllPending(); |
| 285 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 289 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 286 ASSERT_TRUE(fetcher); | 290 ASSERT_TRUE(fetcher); |
| 287 | 291 |
| 288 recognizer_->StopRecording(); | 292 recognizer_->StopAudioCapture(); |
| 289 EXPECT_TRUE(audio_received_); | 293 EXPECT_TRUE(audio_started_); |
| 290 EXPECT_TRUE(recording_complete_); | 294 EXPECT_TRUE(audio_ended_); |
| 291 EXPECT_FALSE(recognition_complete_); | 295 EXPECT_FALSE(recognition_ended_); |
| 292 EXPECT_FALSE(result_received_); | 296 EXPECT_FALSE(result_received_); |
| 293 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 297 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 294 | 298 |
| 295 // Issue the network callback to complete the process. | 299 // Issue the network callback to complete the process. |
| 296 fetcher->set_url(fetcher->GetOriginalURL()); | 300 fetcher->set_url(fetcher->GetOriginalURL()); |
| 297 net::URLRequestStatus status; | 301 net::URLRequestStatus status; |
| 298 status.set_status(net::URLRequestStatus::FAILED); | 302 status.set_status(net::URLRequestStatus::FAILED); |
| 299 status.set_error(net::ERR_CONNECTION_REFUSED); | 303 status.set_error(net::ERR_CONNECTION_REFUSED); |
| 300 fetcher->set_status(status); | 304 fetcher->set_status(status); |
| 301 fetcher->set_response_code(0); | 305 fetcher->set_response_code(0); |
| 302 fetcher->SetResponseString(""); | 306 fetcher->SetResponseString(""); |
| 303 fetcher->delegate()->OnURLFetchComplete(fetcher); | 307 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 304 | 308 |
| 305 EXPECT_FALSE(recognition_complete_); | 309 EXPECT_FALSE(recognition_ended_); |
| 306 EXPECT_FALSE(result_received_); | 310 EXPECT_FALSE(result_received_); |
| 307 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 311 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 308 } | 312 } |
| 309 | 313 |
| 310 TEST_F(SpeechRecognizerTest, ServerError) { | 314 TEST_F(SpeechRecognizerImplTest, ServerError) { |
| 311 // Start recording, give some data and then stop. Issue the network callback | 315 // Start recording, give some data and then stop. Issue the network callback |
| 312 // with a 500 error and verify that the recognizer bubbles the error up | 316 // with a 500 error and verify that the recognizer bubbles the error up |
| 313 EXPECT_TRUE(recognizer_->StartRecording()); | 317 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 314 TestAudioInputController* controller = | 318 TestAudioInputController* controller = |
| 315 audio_input_controller_factory_.controller(); | 319 audio_input_controller_factory_.controller(); |
| 316 ASSERT_TRUE(controller); | 320 ASSERT_TRUE(controller); |
| 317 controller->event_handler()->OnData(controller, &audio_packet_[0], | 321 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 318 audio_packet_.size()); | 322 audio_packet_.size()); |
| 319 MessageLoop::current()->RunAllPending(); | 323 MessageLoop::current()->RunAllPending(); |
| 320 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 324 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 321 ASSERT_TRUE(fetcher); | 325 ASSERT_TRUE(fetcher); |
| 322 | 326 |
| 323 recognizer_->StopRecording(); | 327 recognizer_->StopAudioCapture(); |
| 324 EXPECT_TRUE(audio_received_); | 328 EXPECT_TRUE(audio_started_); |
| 325 EXPECT_TRUE(recording_complete_); | 329 EXPECT_TRUE(audio_ended_); |
| 326 EXPECT_FALSE(recognition_complete_); | 330 EXPECT_FALSE(recognition_ended_); |
| 327 EXPECT_FALSE(result_received_); | 331 EXPECT_FALSE(result_received_); |
| 328 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 332 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 329 | 333 |
| 330 // Issue the network callback to complete the process. | 334 // Issue the network callback to complete the process. |
| 331 fetcher->set_url(fetcher->GetOriginalURL()); | 335 fetcher->set_url(fetcher->GetOriginalURL()); |
| 332 net::URLRequestStatus status; | 336 net::URLRequestStatus status; |
| 333 status.set_status(net::URLRequestStatus::SUCCESS); | 337 status.set_status(net::URLRequestStatus::SUCCESS); |
| 334 fetcher->set_status(status); | 338 fetcher->set_status(status); |
| 335 fetcher->set_response_code(500); | 339 fetcher->set_response_code(500); |
| 336 fetcher->SetResponseString("Internal Server Error"); | 340 fetcher->SetResponseString("Internal Server Error"); |
| 337 fetcher->delegate()->OnURLFetchComplete(fetcher); | 341 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 338 | 342 |
| 339 EXPECT_FALSE(recognition_complete_); | 343 EXPECT_FALSE(recognition_ended_); |
| 340 EXPECT_FALSE(result_received_); | 344 EXPECT_FALSE(result_received_); |
| 341 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 345 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 342 } | 346 } |
| 343 | 347 |
| 344 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { | 348 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { |
| 345 // Check if things tear down properly if AudioInputController threw an error. | 349 // Check if things tear down properly if AudioInputController threw an error. |
| 346 EXPECT_TRUE(recognizer_->StartRecording()); | 350 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 347 TestAudioInputController* controller = | 351 TestAudioInputController* controller = |
| 348 audio_input_controller_factory_.controller(); | 352 audio_input_controller_factory_.controller(); |
| 349 ASSERT_TRUE(controller); | 353 ASSERT_TRUE(controller); |
| 350 controller->event_handler()->OnError(controller, 0); | 354 controller->event_handler()->OnError(controller, 0); |
| 351 MessageLoop::current()->RunAllPending(); | 355 MessageLoop::current()->RunAllPending(); |
| 352 EXPECT_FALSE(audio_received_); | 356 EXPECT_FALSE(audio_started_); |
| 353 EXPECT_FALSE(recording_complete_); | 357 EXPECT_FALSE(audio_ended_); |
| 354 EXPECT_FALSE(recognition_complete_); | 358 EXPECT_FALSE(recognition_ended_); |
| 355 EXPECT_FALSE(result_received_); | 359 EXPECT_FALSE(result_received_); |
| 356 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 360 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 357 } | 361 } |
| 358 | 362 |
| 359 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { | 363 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { |
| 360 // Check if things tear down properly if AudioInputController threw an error | 364 // Check if things tear down properly if AudioInputController threw an error |
| 361 // after giving some audio data. | 365 // after giving some audio data. |
| 362 EXPECT_TRUE(recognizer_->StartRecording()); | 366 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 363 TestAudioInputController* controller = | 367 TestAudioInputController* controller = |
| 364 audio_input_controller_factory_.controller(); | 368 audio_input_controller_factory_.controller(); |
| 365 ASSERT_TRUE(controller); | 369 ASSERT_TRUE(controller); |
| 366 controller->event_handler()->OnData(controller, &audio_packet_[0], | 370 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 367 audio_packet_.size()); | 371 audio_packet_.size()); |
| 368 controller->event_handler()->OnError(controller, 0); | 372 controller->event_handler()->OnError(controller, 0); |
| 369 MessageLoop::current()->RunAllPending(); | 373 MessageLoop::current()->RunAllPending(); |
| 370 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 374 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 371 EXPECT_TRUE(audio_received_); | 375 EXPECT_TRUE(audio_started_); |
| 372 EXPECT_FALSE(recording_complete_); | 376 EXPECT_FALSE(audio_ended_); |
| 373 EXPECT_FALSE(recognition_complete_); | 377 EXPECT_FALSE(recognition_ended_); |
| 374 EXPECT_FALSE(result_received_); | 378 EXPECT_FALSE(result_received_); |
| 375 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 379 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 376 } | 380 } |
| 377 | 381 |
| 378 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { | 382 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { |
| 379 // Start recording and give a lot of packets with audio samples set to zero. | 383 // Start recording and give a lot of packets with audio samples set to zero. |
| 380 // This should trigger the no-speech detector and issue a callback. | 384 // This should trigger the no-speech detector and issue a callback. |
| 381 EXPECT_TRUE(recognizer_->StartRecording()); | 385 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 382 TestAudioInputController* controller = | 386 TestAudioInputController* controller = |
| 383 audio_input_controller_factory_.controller(); | 387 audio_input_controller_factory_.controller(); |
| 384 ASSERT_TRUE(controller); | 388 ASSERT_TRUE(controller); |
| 385 controller = audio_input_controller_factory_.controller(); | 389 controller = audio_input_controller_factory_.controller(); |
| 386 ASSERT_TRUE(controller); | 390 ASSERT_TRUE(controller); |
| 387 | 391 |
| 388 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / | 392 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / |
| 389 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 393 SpeechRecognizerImpl::kAudioPacketIntervalMs; |
| 390 // The vector is already filled with zero value samples on create. | 394 // The vector is already filled with zero value samples on create. |
| 391 for (int i = 0; i < num_packets; ++i) { | 395 for (int i = 0; i < num_packets; ++i) { |
| 392 controller->event_handler()->OnData(controller, &audio_packet_[0], | 396 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 393 audio_packet_.size()); | 397 audio_packet_.size()); |
| 394 } | 398 } |
| 395 MessageLoop::current()->RunAllPending(); | 399 MessageLoop::current()->RunAllPending(); |
| 396 EXPECT_TRUE(audio_received_); | 400 EXPECT_TRUE(audio_started_); |
| 397 EXPECT_FALSE(recording_complete_); | 401 EXPECT_FALSE(audio_ended_); |
| 398 EXPECT_FALSE(recognition_complete_); | 402 EXPECT_FALSE(recognition_ended_); |
| 399 EXPECT_FALSE(result_received_); | 403 EXPECT_FALSE(result_received_); |
| 400 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); | 404 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
| 401 } | 405 } |
| 402 | 406 |
| 403 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { | 407 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { |
| 404 // Start recording and give a lot of packets with audio samples set to zero | 408 // Start recording and give a lot of packets with audio samples set to zero |
| 405 // and then some more with reasonably loud audio samples. This should be | 409 // and then some more with reasonably loud audio samples. This should be |
| 406 // treated as normal speech input and the no-speech detector should not get | 410 // treated as normal speech input and the no-speech detector should not get |
| 407 // triggered. | 411 // triggered. |
| 408 EXPECT_TRUE(recognizer_->StartRecording()); | 412 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 409 TestAudioInputController* controller = | 413 TestAudioInputController* controller = |
| 410 audio_input_controller_factory_.controller(); | 414 audio_input_controller_factory_.controller(); |
| 411 ASSERT_TRUE(controller); | 415 ASSERT_TRUE(controller); |
| 412 controller = audio_input_controller_factory_.controller(); | 416 controller = audio_input_controller_factory_.controller(); |
| 413 ASSERT_TRUE(controller); | 417 ASSERT_TRUE(controller); |
| 414 | 418 |
| 415 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / | 419 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / |
| 416 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 420 SpeechRecognizerImpl::kAudioPacketIntervalMs; |
| 417 | 421 |
| 418 // The vector is already filled with zero value samples on create. | 422 // The vector is already filled with zero value samples on create. |
| 419 for (int i = 0; i < num_packets / 2; ++i) { | 423 for (int i = 0; i < num_packets / 2; ++i) { |
| 420 controller->event_handler()->OnData(controller, &audio_packet_[0], | 424 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 421 audio_packet_.size()); | 425 audio_packet_.size()); |
| 422 } | 426 } |
| 423 | 427 |
| 424 FillPacketWithTestWaveform(); | 428 FillPacketWithTestWaveform(); |
| 425 for (int i = 0; i < num_packets / 2; ++i) { | 429 for (int i = 0; i < num_packets / 2; ++i) { |
| 426 controller->event_handler()->OnData(controller, &audio_packet_[0], | 430 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 427 audio_packet_.size()); | 431 audio_packet_.size()); |
| 428 } | 432 } |
| 429 | 433 |
| 430 MessageLoop::current()->RunAllPending(); | 434 MessageLoop::current()->RunAllPending(); |
| 431 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 435 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 432 EXPECT_TRUE(audio_received_); | 436 EXPECT_TRUE(audio_started_); |
| 433 EXPECT_FALSE(recording_complete_); | 437 EXPECT_FALSE(audio_ended_); |
| 434 EXPECT_FALSE(recognition_complete_); | 438 EXPECT_FALSE(recognition_ended_); |
| 435 recognizer_->CancelRecognition(); | 439 recognizer_->AbortRecognition(); |
| 436 } | 440 } |
| 437 | 441 |
| 438 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { | 442 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { |
| 439 // Start recording and give a lot of packets with audio samples set to zero | 443 // Start recording and give a lot of packets with audio samples set to zero |
| 440 // and then some more with reasonably loud audio samples. Check that we don't | 444 // and then some more with reasonably loud audio samples. Check that we don't |
| 441 // get the callback during estimation phase, then get zero for the silence | 445 // get the callback during estimation phase, then get zero for the silence |
| 442 // samples and proper volume for the loud audio. | 446 // samples and proper volume for the loud audio. |
| 443 EXPECT_TRUE(recognizer_->StartRecording()); | 447 EXPECT_TRUE(recognizer_->StartRecognition()); |
| 444 TestAudioInputController* controller = | 448 TestAudioInputController* controller = |
| 445 audio_input_controller_factory_.controller(); | 449 audio_input_controller_factory_.controller(); |
| 446 ASSERT_TRUE(controller); | 450 ASSERT_TRUE(controller); |
| 447 controller = audio_input_controller_factory_.controller(); | 451 controller = audio_input_controller_factory_.controller(); |
| 448 ASSERT_TRUE(controller); | 452 ASSERT_TRUE(controller); |
| 449 | 453 |
| 450 // Feed some samples to begin with for the endpointer to do noise estimation. | 454 // Feed some samples to begin with for the endpointer to do noise estimation. |
| 451 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / | 455 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / |
| 452 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 456 SpeechRecognizerImpl::kAudioPacketIntervalMs; |
| 453 FillPacketWithNoise(); | 457 FillPacketWithNoise(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 465 EXPECT_FLOAT_EQ(0.74939233f, volume_); | 469 EXPECT_FLOAT_EQ(0.74939233f, volume_); |
| 466 | 470 |
| 467 FillPacketWithTestWaveform(); | 471 FillPacketWithTestWaveform(); |
| 468 controller->event_handler()->OnData(controller, &audio_packet_[0], | 472 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 469 audio_packet_.size()); | 473 audio_packet_.size()); |
| 470 MessageLoop::current()->RunAllPending(); | 474 MessageLoop::current()->RunAllPending(); |
| 471 EXPECT_FLOAT_EQ(0.89926866f, volume_); | 475 EXPECT_FLOAT_EQ(0.89926866f, volume_); |
| 472 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); | 476 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); |
| 473 | 477 |
| 474 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 478 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 475 EXPECT_FALSE(recording_complete_); | 479 EXPECT_FALSE(audio_ended_); |
| 476 EXPECT_FALSE(recognition_complete_); | 480 EXPECT_FALSE(recognition_ended_); |
| 477 recognizer_->CancelRecognition(); | 481 recognizer_->AbortRecognition(); |
| 478 } | 482 } |
| 479 | 483 |
| 480 } // namespace speech | 484 } // namespace speech |
| OLD | NEW |