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