| 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_recognizer_delegate.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/test_audio_input_controller_factory.h" | 12 #include "media/audio/test_audio_input_controller_factory.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/url_request/url_request_status.h" | 14 #include "net/url_request/url_request_status.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using content::BrowserThread; | 17 using content::BrowserThread; |
| 18 using content::BrowserThreadImpl; | 18 using content::BrowserThreadImpl; |
| 19 using media::AudioInputController; | 19 using media::AudioInputController; |
| 20 using media::TestAudioInputController; | 20 using media::TestAudioInputController; |
| 21 using media::TestAudioInputControllerFactory; | 21 using media::TestAudioInputControllerFactory; |
| 22 | 22 |
| 23 namespace speech_input { | 23 namespace speech { |
| 24 | 24 |
| 25 class SpeechRecognizerTest : public content::SpeechRecognizerDelegate, | 25 class SpeechRecognizerTest : public content::SpeechRecognizerDelegate, |
| 26 public testing::Test { | 26 public testing::Test { |
| 27 public: | 27 public: |
| 28 SpeechRecognizerTest() | 28 SpeechRecognizerTest() |
| 29 : io_thread_(BrowserThread::IO, &message_loop_), | 29 : io_thread_(BrowserThread::IO, &message_loop_), |
| 30 audio_manager_(AudioManager::Create()), | 30 audio_manager_(AudioManager::Create()), |
| 31 recording_complete_(false), | 31 recording_complete_(false), |
| 32 recognition_complete_(false), | 32 recognition_complete_(false), |
| 33 result_received_(false), | 33 result_received_(false), |
| 34 audio_received_(false), | 34 audio_received_(false), |
| 35 error_(content::SPEECH_INPUT_ERROR_NONE), | 35 error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
| 36 volume_(-1.0f) { | 36 volume_(-1.0f) { |
| 37 recognizer_ = new SpeechRecognizerImpl( | 37 recognizer_ = new SpeechRecognizerImpl( |
| 38 this, 1, std::string(), std::string(), NULL, false, std::string(), | 38 this, 1, std::string(), std::string(), NULL, false, std::string(), |
| 39 std::string()); | 39 std::string()); |
| 40 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); | 40 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); |
| 41 int audio_packet_length_bytes = | 41 int audio_packet_length_bytes = |
| 42 (SpeechRecognizerImpl::kAudioSampleRate * | 42 (SpeechRecognizerImpl::kAudioSampleRate * |
| 43 SpeechRecognizerImpl::kAudioPacketIntervalMs * | 43 SpeechRecognizerImpl::kAudioPacketIntervalMs * |
| 44 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * | 44 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * |
| 45 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); | 45 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); |
| 46 audio_packet_.resize(audio_packet_length_bytes); | 46 audio_packet_.resize(audio_packet_length_bytes); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Overridden from content::SpeechRecognizerDelegate: | 49 // Overridden from content::SpeechRecognizerDelegate: |
| 50 virtual void SetRecognitionResult( | 50 virtual void SetRecognitionResult( |
| 51 int caller_id, | 51 int caller_id, |
| 52 const content::SpeechInputResult& result) OVERRIDE { | 52 const content::SpeechRecognitionResult& result) OVERRIDE { |
| 53 result_received_ = true; | 53 result_received_ = true; |
| 54 } | 54 } |
| 55 | 55 |
| 56 virtual void DidCompleteRecording(int caller_id) OVERRIDE { | 56 virtual void DidCompleteRecording(int caller_id) OVERRIDE { |
| 57 recording_complete_ = true; | 57 recording_complete_ = true; |
| 58 } | 58 } |
| 59 | 59 |
| 60 virtual void DidCompleteRecognition(int caller_id) OVERRIDE { | 60 virtual void DidCompleteRecognition(int caller_id) OVERRIDE { |
| 61 recognition_complete_ = true; | 61 recognition_complete_ = true; |
| 62 } | 62 } |
| 63 | 63 |
| 64 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE { | 64 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE { |
| 65 } | 65 } |
| 66 | 66 |
| 67 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE { | 67 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE { |
| 68 audio_received_ = true; | 68 audio_received_ = true; |
| 69 } | 69 } |
| 70 | 70 |
| 71 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE { | 71 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE { |
| 72 } | 72 } |
| 73 | 73 |
| 74 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE { | 74 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE { |
| 75 } | 75 } |
| 76 | 76 |
| 77 virtual void OnRecognizerError(int caller_id, | 77 virtual void OnRecognizerError( |
| 78 content::SpeechInputError error) OVERRIDE { | 78 int caller_id, content::SpeechRecognitionErrorCode error) OVERRIDE { |
| 79 error_ = error; | 79 error_ = error; |
| 80 } | 80 } |
| 81 | 81 |
| 82 virtual void SetInputVolume(int caller_id, float volume, | 82 virtual void SetInputVolume(int caller_id, float volume, |
| 83 float noise_volume) OVERRIDE { | 83 float noise_volume) OVERRIDE { |
| 84 volume_ = volume; | 84 volume_ = volume; |
| 85 noise_volume_ = noise_volume; | 85 noise_volume_ = noise_volume; |
| 86 } | 86 } |
| 87 | 87 |
| 88 // testing::Test methods. | 88 // testing::Test methods. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 112 | 112 |
| 113 protected: | 113 protected: |
| 114 MessageLoopForIO message_loop_; | 114 MessageLoopForIO message_loop_; |
| 115 BrowserThreadImpl io_thread_; | 115 BrowserThreadImpl io_thread_; |
| 116 scoped_refptr<SpeechRecognizerImpl> recognizer_; | 116 scoped_refptr<SpeechRecognizerImpl> recognizer_; |
| 117 scoped_ptr<AudioManager> audio_manager_; | 117 scoped_ptr<AudioManager> audio_manager_; |
| 118 bool recording_complete_; | 118 bool recording_complete_; |
| 119 bool recognition_complete_; | 119 bool recognition_complete_; |
| 120 bool result_received_; | 120 bool result_received_; |
| 121 bool audio_received_; | 121 bool audio_received_; |
| 122 content::SpeechInputError error_; | 122 content::SpeechRecognitionErrorCode error_; |
| 123 TestURLFetcherFactory url_fetcher_factory_; | 123 TestURLFetcherFactory url_fetcher_factory_; |
| 124 TestAudioInputControllerFactory audio_input_controller_factory_; | 124 TestAudioInputControllerFactory audio_input_controller_factory_; |
| 125 std::vector<uint8> audio_packet_; | 125 std::vector<uint8> audio_packet_; |
| 126 float volume_; | 126 float volume_; |
| 127 float noise_volume_; | 127 float noise_volume_; |
| 128 }; | 128 }; |
| 129 | 129 |
| 130 TEST_F(SpeechRecognizerTest, StopNoData) { | 130 TEST_F(SpeechRecognizerTest, StopNoData) { |
| 131 // Check for callbacks when stopping record before any audio gets recorded. | 131 // Check for callbacks when stopping record before any audio gets recorded. |
| 132 EXPECT_TRUE(recognizer_->StartRecording()); | 132 EXPECT_TRUE(recognizer_->StartRecording()); |
| 133 recognizer_->CancelRecognition(); | 133 recognizer_->CancelRecognition(); |
| 134 EXPECT_FALSE(recording_complete_); | 134 EXPECT_FALSE(recording_complete_); |
| 135 EXPECT_FALSE(recognition_complete_); | 135 EXPECT_FALSE(recognition_complete_); |
| 136 EXPECT_FALSE(result_received_); | 136 EXPECT_FALSE(result_received_); |
| 137 EXPECT_FALSE(audio_received_); | 137 EXPECT_FALSE(audio_received_); |
| 138 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 138 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 139 } | 139 } |
| 140 | 140 |
| 141 TEST_F(SpeechRecognizerTest, CancelNoData) { | 141 TEST_F(SpeechRecognizerTest, CancelNoData) { |
| 142 // Check for callbacks when canceling recognition before any audio gets | 142 // Check for callbacks when canceling recognition before any audio gets |
| 143 // recorded. | 143 // recorded. |
| 144 EXPECT_TRUE(recognizer_->StartRecording()); | 144 EXPECT_TRUE(recognizer_->StartRecording()); |
| 145 recognizer_->StopRecording(); | 145 recognizer_->StopRecording(); |
| 146 EXPECT_TRUE(recording_complete_); | 146 EXPECT_TRUE(recording_complete_); |
| 147 EXPECT_TRUE(recognition_complete_); | 147 EXPECT_TRUE(recognition_complete_); |
| 148 EXPECT_FALSE(result_received_); | 148 EXPECT_FALSE(result_received_); |
| 149 EXPECT_FALSE(audio_received_); | 149 EXPECT_FALSE(audio_received_); |
| 150 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 150 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 151 } | 151 } |
| 152 | 152 |
| 153 TEST_F(SpeechRecognizerTest, StopWithData) { | 153 TEST_F(SpeechRecognizerTest, StopWithData) { |
| 154 // Start recording, give some data and then stop. This should wait for the | 154 // Start recording, give some data and then stop. This should wait for the |
| 155 // network callback to arrive before completion. | 155 // network callback to arrive before completion. |
| 156 EXPECT_TRUE(recognizer_->StartRecording()); | 156 EXPECT_TRUE(recognizer_->StartRecording()); |
| 157 TestAudioInputController* controller = | 157 TestAudioInputController* controller = |
| 158 audio_input_controller_factory_.controller(); | 158 audio_input_controller_factory_.controller(); |
| 159 ASSERT_TRUE(controller); | 159 ASSERT_TRUE(controller); |
| 160 | 160 |
| 161 // Try sending 5 chunks of mock audio data and verify that each of them | 161 // Try sending 5 chunks of mock audio data and verify that each of them |
| 162 // resulted immediately in a packet sent out via the network. This verifies | 162 // resulted immediately in a packet sent out via the network. This verifies |
| 163 // that we are streaming out encoded data as chunks without waiting for the | 163 // that we are streaming out encoded data as chunks without waiting for the |
| 164 // full recording to complete. | 164 // full recording to complete. |
| 165 const size_t kNumChunks = 5; | 165 const size_t kNumChunks = 5; |
| 166 for (size_t i = 0; i < kNumChunks; ++i) { | 166 for (size_t i = 0; i < kNumChunks; ++i) { |
| 167 controller->event_handler()->OnData(controller, &audio_packet_[0], | 167 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 168 audio_packet_.size()); | 168 audio_packet_.size()); |
| 169 MessageLoop::current()->RunAllPending(); | 169 MessageLoop::current()->RunAllPending(); |
| 170 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 170 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 171 ASSERT_TRUE(fetcher); | 171 ASSERT_TRUE(fetcher); |
| 172 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); | 172 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); |
| 173 } | 173 } |
| 174 | 174 |
| 175 recognizer_->StopRecording(); | 175 recognizer_->StopRecording(); |
| 176 EXPECT_TRUE(audio_received_); | 176 EXPECT_TRUE(audio_received_); |
| 177 EXPECT_TRUE(recording_complete_); | 177 EXPECT_TRUE(recording_complete_); |
| 178 EXPECT_FALSE(recognition_complete_); | 178 EXPECT_FALSE(recognition_complete_); |
| 179 EXPECT_FALSE(result_received_); | 179 EXPECT_FALSE(result_received_); |
| 180 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 180 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 181 | 181 |
| 182 // Issue the network callback to complete the process. | 182 // Issue the network callback to complete the process. |
| 183 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 183 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 184 ASSERT_TRUE(fetcher); | 184 ASSERT_TRUE(fetcher); |
| 185 | 185 |
| 186 fetcher->set_url(fetcher->GetOriginalURL()); | 186 fetcher->set_url(fetcher->GetOriginalURL()); |
| 187 net::URLRequestStatus status; | 187 net::URLRequestStatus status; |
| 188 status.set_status(net::URLRequestStatus::SUCCESS); | 188 status.set_status(net::URLRequestStatus::SUCCESS); |
| 189 fetcher->set_status(status); | 189 fetcher->set_status(status); |
| 190 fetcher->set_response_code(200); | 190 fetcher->set_response_code(200); |
| 191 fetcher->SetResponseString( | 191 fetcher->SetResponseString( |
| 192 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); | 192 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); |
| 193 fetcher->delegate()->OnURLFetchComplete(fetcher); | 193 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 194 | 194 |
| 195 EXPECT_TRUE(recognition_complete_); | 195 EXPECT_TRUE(recognition_complete_); |
| 196 EXPECT_TRUE(result_received_); | 196 EXPECT_TRUE(result_received_); |
| 197 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 197 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 198 } | 198 } |
| 199 | 199 |
| 200 TEST_F(SpeechRecognizerTest, CancelWithData) { | 200 TEST_F(SpeechRecognizerTest, CancelWithData) { |
| 201 // Start recording, give some data and then cancel. This should create | 201 // Start recording, give some data and then cancel. This should create |
| 202 // a network request but give no callbacks. | 202 // a network request but give no callbacks. |
| 203 EXPECT_TRUE(recognizer_->StartRecording()); | 203 EXPECT_TRUE(recognizer_->StartRecording()); |
| 204 TestAudioInputController* controller = | 204 TestAudioInputController* controller = |
| 205 audio_input_controller_factory_.controller(); | 205 audio_input_controller_factory_.controller(); |
| 206 ASSERT_TRUE(controller); | 206 ASSERT_TRUE(controller); |
| 207 controller->event_handler()->OnData(controller, &audio_packet_[0], | 207 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 208 audio_packet_.size()); | 208 audio_packet_.size()); |
| 209 MessageLoop::current()->RunAllPending(); | 209 MessageLoop::current()->RunAllPending(); |
| 210 recognizer_->CancelRecognition(); | 210 recognizer_->CancelRecognition(); |
| 211 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 211 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 212 EXPECT_TRUE(audio_received_); | 212 EXPECT_TRUE(audio_received_); |
| 213 EXPECT_FALSE(recording_complete_); | 213 EXPECT_FALSE(recording_complete_); |
| 214 EXPECT_FALSE(recognition_complete_); | 214 EXPECT_FALSE(recognition_complete_); |
| 215 EXPECT_FALSE(result_received_); | 215 EXPECT_FALSE(result_received_); |
| 216 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 216 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 217 } | 217 } |
| 218 | 218 |
| 219 TEST_F(SpeechRecognizerTest, ConnectionError) { | 219 TEST_F(SpeechRecognizerTest, ConnectionError) { |
| 220 // Start recording, give some data and then stop. Issue the network callback | 220 // Start recording, give some data and then stop. Issue the network callback |
| 221 // with a connection error and verify that the recognizer bubbles the error up | 221 // with a connection error and verify that the recognizer bubbles the error up |
| 222 EXPECT_TRUE(recognizer_->StartRecording()); | 222 EXPECT_TRUE(recognizer_->StartRecording()); |
| 223 TestAudioInputController* controller = | 223 TestAudioInputController* controller = |
| 224 audio_input_controller_factory_.controller(); | 224 audio_input_controller_factory_.controller(); |
| 225 ASSERT_TRUE(controller); | 225 ASSERT_TRUE(controller); |
| 226 controller->event_handler()->OnData(controller, &audio_packet_[0], | 226 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 227 audio_packet_.size()); | 227 audio_packet_.size()); |
| 228 MessageLoop::current()->RunAllPending(); | 228 MessageLoop::current()->RunAllPending(); |
| 229 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 229 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 230 ASSERT_TRUE(fetcher); | 230 ASSERT_TRUE(fetcher); |
| 231 | 231 |
| 232 recognizer_->StopRecording(); | 232 recognizer_->StopRecording(); |
| 233 EXPECT_TRUE(audio_received_); | 233 EXPECT_TRUE(audio_received_); |
| 234 EXPECT_TRUE(recording_complete_); | 234 EXPECT_TRUE(recording_complete_); |
| 235 EXPECT_FALSE(recognition_complete_); | 235 EXPECT_FALSE(recognition_complete_); |
| 236 EXPECT_FALSE(result_received_); | 236 EXPECT_FALSE(result_received_); |
| 237 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 237 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 238 | 238 |
| 239 // Issue the network callback to complete the process. | 239 // Issue the network callback to complete the process. |
| 240 fetcher->set_url(fetcher->GetOriginalURL()); | 240 fetcher->set_url(fetcher->GetOriginalURL()); |
| 241 net::URLRequestStatus status; | 241 net::URLRequestStatus status; |
| 242 status.set_status(net::URLRequestStatus::FAILED); | 242 status.set_status(net::URLRequestStatus::FAILED); |
| 243 status.set_error(net::ERR_CONNECTION_REFUSED); | 243 status.set_error(net::ERR_CONNECTION_REFUSED); |
| 244 fetcher->set_status(status); | 244 fetcher->set_status(status); |
| 245 fetcher->set_response_code(0); | 245 fetcher->set_response_code(0); |
| 246 fetcher->SetResponseString(""); | 246 fetcher->SetResponseString(""); |
| 247 fetcher->delegate()->OnURLFetchComplete(fetcher); | 247 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 248 | 248 |
| 249 EXPECT_FALSE(recognition_complete_); | 249 EXPECT_FALSE(recognition_complete_); |
| 250 EXPECT_FALSE(result_received_); | 250 EXPECT_FALSE(result_received_); |
| 251 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NETWORK, error_); | 251 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 252 } | 252 } |
| 253 | 253 |
| 254 TEST_F(SpeechRecognizerTest, ServerError) { | 254 TEST_F(SpeechRecognizerTest, ServerError) { |
| 255 // Start recording, give some data and then stop. Issue the network callback | 255 // Start recording, give some data and then stop. Issue the network callback |
| 256 // with a 500 error and verify that the recognizer bubbles the error up | 256 // with a 500 error and verify that the recognizer bubbles the error up |
| 257 EXPECT_TRUE(recognizer_->StartRecording()); | 257 EXPECT_TRUE(recognizer_->StartRecording()); |
| 258 TestAudioInputController* controller = | 258 TestAudioInputController* controller = |
| 259 audio_input_controller_factory_.controller(); | 259 audio_input_controller_factory_.controller(); |
| 260 ASSERT_TRUE(controller); | 260 ASSERT_TRUE(controller); |
| 261 controller->event_handler()->OnData(controller, &audio_packet_[0], | 261 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 262 audio_packet_.size()); | 262 audio_packet_.size()); |
| 263 MessageLoop::current()->RunAllPending(); | 263 MessageLoop::current()->RunAllPending(); |
| 264 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 264 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 265 ASSERT_TRUE(fetcher); | 265 ASSERT_TRUE(fetcher); |
| 266 | 266 |
| 267 recognizer_->StopRecording(); | 267 recognizer_->StopRecording(); |
| 268 EXPECT_TRUE(audio_received_); | 268 EXPECT_TRUE(audio_received_); |
| 269 EXPECT_TRUE(recording_complete_); | 269 EXPECT_TRUE(recording_complete_); |
| 270 EXPECT_FALSE(recognition_complete_); | 270 EXPECT_FALSE(recognition_complete_); |
| 271 EXPECT_FALSE(result_received_); | 271 EXPECT_FALSE(result_received_); |
| 272 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 272 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 273 | 273 |
| 274 // Issue the network callback to complete the process. | 274 // Issue the network callback to complete the process. |
| 275 fetcher->set_url(fetcher->GetOriginalURL()); | 275 fetcher->set_url(fetcher->GetOriginalURL()); |
| 276 net::URLRequestStatus status; | 276 net::URLRequestStatus status; |
| 277 status.set_status(net::URLRequestStatus::SUCCESS); | 277 status.set_status(net::URLRequestStatus::SUCCESS); |
| 278 fetcher->set_status(status); | 278 fetcher->set_status(status); |
| 279 fetcher->set_response_code(500); | 279 fetcher->set_response_code(500); |
| 280 fetcher->SetResponseString("Internal Server Error"); | 280 fetcher->SetResponseString("Internal Server Error"); |
| 281 fetcher->delegate()->OnURLFetchComplete(fetcher); | 281 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 282 | 282 |
| 283 EXPECT_FALSE(recognition_complete_); | 283 EXPECT_FALSE(recognition_complete_); |
| 284 EXPECT_FALSE(result_received_); | 284 EXPECT_FALSE(result_received_); |
| 285 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NETWORK, error_); | 285 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
| 286 } | 286 } |
| 287 | 287 |
| 288 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { | 288 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { |
| 289 // Check if things tear down properly if AudioInputController threw an error. | 289 // Check if things tear down properly if AudioInputController threw an error. |
| 290 EXPECT_TRUE(recognizer_->StartRecording()); | 290 EXPECT_TRUE(recognizer_->StartRecording()); |
| 291 TestAudioInputController* controller = | 291 TestAudioInputController* controller = |
| 292 audio_input_controller_factory_.controller(); | 292 audio_input_controller_factory_.controller(); |
| 293 ASSERT_TRUE(controller); | 293 ASSERT_TRUE(controller); |
| 294 controller->event_handler()->OnError(controller, 0); | 294 controller->event_handler()->OnError(controller, 0); |
| 295 MessageLoop::current()->RunAllPending(); | 295 MessageLoop::current()->RunAllPending(); |
| 296 EXPECT_FALSE(audio_received_); | 296 EXPECT_FALSE(audio_received_); |
| 297 EXPECT_FALSE(recording_complete_); | 297 EXPECT_FALSE(recording_complete_); |
| 298 EXPECT_FALSE(recognition_complete_); | 298 EXPECT_FALSE(recognition_complete_); |
| 299 EXPECT_FALSE(result_received_); | 299 EXPECT_FALSE(result_received_); |
| 300 EXPECT_EQ(content::SPEECH_INPUT_ERROR_AUDIO, error_); | 300 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 301 } | 301 } |
| 302 | 302 |
| 303 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { | 303 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { |
| 304 // Check if things tear down properly if AudioInputController threw an error | 304 // Check if things tear down properly if AudioInputController threw an error |
| 305 // after giving some audio data. | 305 // after giving some audio data. |
| 306 EXPECT_TRUE(recognizer_->StartRecording()); | 306 EXPECT_TRUE(recognizer_->StartRecording()); |
| 307 TestAudioInputController* controller = | 307 TestAudioInputController* controller = |
| 308 audio_input_controller_factory_.controller(); | 308 audio_input_controller_factory_.controller(); |
| 309 ASSERT_TRUE(controller); | 309 ASSERT_TRUE(controller); |
| 310 controller->event_handler()->OnData(controller, &audio_packet_[0], | 310 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 311 audio_packet_.size()); | 311 audio_packet_.size()); |
| 312 controller->event_handler()->OnError(controller, 0); | 312 controller->event_handler()->OnError(controller, 0); |
| 313 MessageLoop::current()->RunAllPending(); | 313 MessageLoop::current()->RunAllPending(); |
| 314 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 314 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 315 EXPECT_TRUE(audio_received_); | 315 EXPECT_TRUE(audio_received_); |
| 316 EXPECT_FALSE(recording_complete_); | 316 EXPECT_FALSE(recording_complete_); |
| 317 EXPECT_FALSE(recognition_complete_); | 317 EXPECT_FALSE(recognition_complete_); |
| 318 EXPECT_FALSE(result_received_); | 318 EXPECT_FALSE(result_received_); |
| 319 EXPECT_EQ(content::SPEECH_INPUT_ERROR_AUDIO, error_); | 319 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
| 320 } | 320 } |
| 321 | 321 |
| 322 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { | 322 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { |
| 323 // Start recording and give a lot of packets with audio samples set to zero. | 323 // Start recording and give a lot of packets with audio samples set to zero. |
| 324 // This should trigger the no-speech detector and issue a callback. | 324 // This should trigger the no-speech detector and issue a callback. |
| 325 EXPECT_TRUE(recognizer_->StartRecording()); | 325 EXPECT_TRUE(recognizer_->StartRecording()); |
| 326 TestAudioInputController* controller = | 326 TestAudioInputController* controller = |
| 327 audio_input_controller_factory_.controller(); | 327 audio_input_controller_factory_.controller(); |
| 328 ASSERT_TRUE(controller); | 328 ASSERT_TRUE(controller); |
| 329 controller = audio_input_controller_factory_.controller(); | 329 controller = audio_input_controller_factory_.controller(); |
| 330 ASSERT_TRUE(controller); | 330 ASSERT_TRUE(controller); |
| 331 | 331 |
| 332 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / | 332 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / |
| 333 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 333 SpeechRecognizerImpl::kAudioPacketIntervalMs; |
| 334 // The vector is already filled with zero value samples on create. | 334 // The vector is already filled with zero value samples on create. |
| 335 for (int i = 0; i < num_packets; ++i) { | 335 for (int i = 0; i < num_packets; ++i) { |
| 336 controller->event_handler()->OnData(controller, &audio_packet_[0], | 336 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 337 audio_packet_.size()); | 337 audio_packet_.size()); |
| 338 } | 338 } |
| 339 MessageLoop::current()->RunAllPending(); | 339 MessageLoop::current()->RunAllPending(); |
| 340 EXPECT_TRUE(audio_received_); | 340 EXPECT_TRUE(audio_received_); |
| 341 EXPECT_FALSE(recording_complete_); | 341 EXPECT_FALSE(recording_complete_); |
| 342 EXPECT_FALSE(recognition_complete_); | 342 EXPECT_FALSE(recognition_complete_); |
| 343 EXPECT_FALSE(result_received_); | 343 EXPECT_FALSE(result_received_); |
| 344 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NO_SPEECH, error_); | 344 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
| 345 } | 345 } |
| 346 | 346 |
| 347 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { | 347 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { |
| 348 // Start recording and give a lot of packets with audio samples set to zero | 348 // Start recording and give a lot of packets with audio samples set to zero |
| 349 // and then some more with reasonably loud audio samples. This should be | 349 // and then some more with reasonably loud audio samples. This should be |
| 350 // treated as normal speech input and the no-speech detector should not get | 350 // treated as normal speech input and the no-speech detector should not get |
| 351 // triggered. | 351 // triggered. |
| 352 EXPECT_TRUE(recognizer_->StartRecording()); | 352 EXPECT_TRUE(recognizer_->StartRecording()); |
| 353 TestAudioInputController* controller = | 353 TestAudioInputController* controller = |
| 354 audio_input_controller_factory_.controller(); | 354 audio_input_controller_factory_.controller(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 365 audio_packet_.size()); | 365 audio_packet_.size()); |
| 366 } | 366 } |
| 367 | 367 |
| 368 FillPacketWithTestWaveform(); | 368 FillPacketWithTestWaveform(); |
| 369 for (int i = 0; i < num_packets / 2; ++i) { | 369 for (int i = 0; i < num_packets / 2; ++i) { |
| 370 controller->event_handler()->OnData(controller, &audio_packet_[0], | 370 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 371 audio_packet_.size()); | 371 audio_packet_.size()); |
| 372 } | 372 } |
| 373 | 373 |
| 374 MessageLoop::current()->RunAllPending(); | 374 MessageLoop::current()->RunAllPending(); |
| 375 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 375 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 376 EXPECT_TRUE(audio_received_); | 376 EXPECT_TRUE(audio_received_); |
| 377 EXPECT_FALSE(recording_complete_); | 377 EXPECT_FALSE(recording_complete_); |
| 378 EXPECT_FALSE(recognition_complete_); | 378 EXPECT_FALSE(recognition_complete_); |
| 379 recognizer_->CancelRecognition(); | 379 recognizer_->CancelRecognition(); |
| 380 } | 380 } |
| 381 | 381 |
| 382 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { | 382 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { |
| 383 // 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 |
| 384 // and then some more with reasonably loud audio samples. Check that we don't | 384 // and then some more with reasonably loud audio samples. Check that we don't |
| 385 // get the callback during estimation phase, then get zero for the silence | 385 // get the callback during estimation phase, then get zero for the silence |
| (...skipping 22 matching lines...) Expand all Loading... |
| 408 MessageLoop::current()->RunAllPending(); | 408 MessageLoop::current()->RunAllPending(); |
| 409 EXPECT_FLOAT_EQ(0.74939233f, volume_); | 409 EXPECT_FLOAT_EQ(0.74939233f, volume_); |
| 410 | 410 |
| 411 FillPacketWithTestWaveform(); | 411 FillPacketWithTestWaveform(); |
| 412 controller->event_handler()->OnData(controller, &audio_packet_[0], | 412 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 413 audio_packet_.size()); | 413 audio_packet_.size()); |
| 414 MessageLoop::current()->RunAllPending(); | 414 MessageLoop::current()->RunAllPending(); |
| 415 EXPECT_FLOAT_EQ(0.89926866f, volume_); | 415 EXPECT_FLOAT_EQ(0.89926866f, volume_); |
| 416 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); | 416 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); |
| 417 | 417 |
| 418 EXPECT_EQ(content::SPEECH_INPUT_ERROR_NONE, error_); | 418 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 419 EXPECT_FALSE(recording_complete_); | 419 EXPECT_FALSE(recording_complete_); |
| 420 EXPECT_FALSE(recognition_complete_); | 420 EXPECT_FALSE(recognition_complete_); |
| 421 recognizer_->CancelRecognition(); | 421 recognizer_->CancelRecognition(); |
| 422 } | 422 } |
| 423 | 423 |
| 424 } // namespace speech_input | 424 } // namespace speech |
| OLD | NEW |