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.h" | 9 #include "content/browser/speech/speech_recognizer.h" |
10 #include "content/public/browser/speech_recognition_event_listener.h" | 10 #include "content/public/browser/speech_recognition_event_listener.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 config.audio_num_bits_per_sample = SpeechRecognizer::kNumBitsPerAudioSample; | 49 config.audio_num_bits_per_sample = SpeechRecognizer::kNumBitsPerAudioSample; |
50 config.audio_sample_rate = SpeechRecognizer::kAudioSampleRate; | 50 config.audio_sample_rate = SpeechRecognizer::kAudioSampleRate; |
51 config.filter_profanities = false; | 51 config.filter_profanities = false; |
52 sr_engine->SetConfig(config); | 52 sr_engine->SetConfig(config); |
53 | 53 |
54 const int kTestingSessionId = 1; | 54 const int kTestingSessionId = 1; |
55 const bool kOneShotMode = true; | 55 const bool kOneShotMode = true; |
56 recognizer_ = new SpeechRecognizer( | 56 recognizer_ = new SpeechRecognizer( |
57 this, kTestingSessionId, kOneShotMode, sr_engine); | 57 this, kTestingSessionId, kOneShotMode, sr_engine); |
58 audio_manager_.reset(new media::MockAudioManager( | 58 audio_manager_.reset(new media::MockAudioManager( |
59 MessageLoop::current()->message_loop_proxy())); | 59 base::MessageLoop::current()->message_loop_proxy())); |
60 recognizer_->SetAudioManagerForTests(audio_manager_.get()); | 60 recognizer_->SetAudioManagerForTests(audio_manager_.get()); |
61 | 61 |
62 int audio_packet_length_bytes = | 62 int audio_packet_length_bytes = |
63 (SpeechRecognizer::kAudioSampleRate * | 63 (SpeechRecognizer::kAudioSampleRate * |
64 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * | 64 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * |
65 ChannelLayoutToChannelCount(SpeechRecognizer::kChannelLayout) * | 65 ChannelLayoutToChannelCount(SpeechRecognizer::kChannelLayout) * |
66 SpeechRecognizer::kNumBitsPerAudioSample) / (8 * 1000); | 66 SpeechRecognizer::kNumBitsPerAudioSample) / (8 * 1000); |
67 audio_packet_.resize(audio_packet_length_bytes); | 67 audio_packet_.resize(audio_packet_length_bytes); |
68 } | 68 } |
69 | 69 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 void FillPacketWithNoise() { | 155 void FillPacketWithNoise() { |
156 int value = 0; | 156 int value = 0; |
157 int factor = 175; | 157 int factor = 175; |
158 for (size_t i = 0; i < audio_packet_.size(); ++i) { | 158 for (size_t i = 0; i < audio_packet_.size(); ++i) { |
159 value += factor; | 159 value += factor; |
160 audio_packet_[i] = value % 100; | 160 audio_packet_[i] = value % 100; |
161 } | 161 } |
162 } | 162 } |
163 | 163 |
164 protected: | 164 protected: |
165 MessageLoopForIO message_loop_; | 165 base::MessageLoopForIO message_loop_; |
166 BrowserThreadImpl io_thread_; | 166 BrowserThreadImpl io_thread_; |
167 scoped_refptr<SpeechRecognizer> recognizer_; | 167 scoped_refptr<SpeechRecognizer> recognizer_; |
168 scoped_ptr<AudioManager> audio_manager_; | 168 scoped_ptr<AudioManager> audio_manager_; |
169 bool recognition_started_; | 169 bool recognition_started_; |
170 bool recognition_ended_; | 170 bool recognition_ended_; |
171 bool result_received_; | 171 bool result_received_; |
172 bool audio_started_; | 172 bool audio_started_; |
173 bool audio_ended_; | 173 bool audio_ended_; |
174 bool sound_started_; | 174 bool sound_started_; |
175 bool sound_ended_; | 175 bool sound_ended_; |
176 SpeechRecognitionErrorCode error_; | 176 SpeechRecognitionErrorCode error_; |
177 net::TestURLFetcherFactory url_fetcher_factory_; | 177 net::TestURLFetcherFactory url_fetcher_factory_; |
178 TestAudioInputControllerFactory audio_input_controller_factory_; | 178 TestAudioInputControllerFactory audio_input_controller_factory_; |
179 std::vector<uint8> audio_packet_; | 179 std::vector<uint8> audio_packet_; |
180 float volume_; | 180 float volume_; |
181 float noise_volume_; | 181 float noise_volume_; |
182 }; | 182 }; |
183 | 183 |
184 TEST_F(SpeechRecognizerTest, StopNoData) { | 184 TEST_F(SpeechRecognizerTest, StopNoData) { |
185 // Check for callbacks when stopping record before any audio gets recorded. | 185 // Check for callbacks when stopping record before any audio gets recorded. |
186 recognizer_->StartRecognition(); | 186 recognizer_->StartRecognition(); |
187 recognizer_->StopAudioCapture(); | 187 recognizer_->StopAudioCapture(); |
188 MessageLoop::current()->RunUntilIdle(); | 188 base::MessageLoop::current()->RunUntilIdle(); |
189 EXPECT_TRUE(recognition_started_); | 189 EXPECT_TRUE(recognition_started_); |
190 EXPECT_FALSE(audio_started_); | 190 EXPECT_FALSE(audio_started_); |
191 EXPECT_FALSE(result_received_); | 191 EXPECT_FALSE(result_received_); |
192 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 192 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
193 CheckFinalEventsConsistency(); | 193 CheckFinalEventsConsistency(); |
194 } | 194 } |
195 | 195 |
196 TEST_F(SpeechRecognizerTest, CancelNoData) { | 196 TEST_F(SpeechRecognizerTest, CancelNoData) { |
197 // Check for callbacks when canceling recognition before any audio gets | 197 // Check for callbacks when canceling recognition before any audio gets |
198 // recorded. | 198 // recorded. |
199 recognizer_->StartRecognition(); | 199 recognizer_->StartRecognition(); |
200 recognizer_->AbortRecognition(); | 200 recognizer_->AbortRecognition(); |
201 MessageLoop::current()->RunUntilIdle(); | 201 base::MessageLoop::current()->RunUntilIdle(); |
202 EXPECT_TRUE(recognition_started_); | 202 EXPECT_TRUE(recognition_started_); |
203 EXPECT_FALSE(audio_started_); | 203 EXPECT_FALSE(audio_started_); |
204 EXPECT_FALSE(result_received_); | 204 EXPECT_FALSE(result_received_); |
205 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); | 205 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); |
206 CheckFinalEventsConsistency(); | 206 CheckFinalEventsConsistency(); |
207 } | 207 } |
208 | 208 |
209 TEST_F(SpeechRecognizerTest, StopWithData) { | 209 TEST_F(SpeechRecognizerTest, StopWithData) { |
210 // Start recording, give some data and then stop. This should wait for the | 210 // Start recording, give some data and then stop. This should wait for the |
211 // network callback to arrive before completion. | 211 // network callback to arrive before completion. |
212 recognizer_->StartRecognition(); | 212 recognizer_->StartRecognition(); |
213 MessageLoop::current()->RunUntilIdle(); | 213 base::MessageLoop::current()->RunUntilIdle(); |
214 TestAudioInputController* controller = | 214 TestAudioInputController* controller = |
215 audio_input_controller_factory_.controller(); | 215 audio_input_controller_factory_.controller(); |
216 ASSERT_TRUE(controller); | 216 ASSERT_TRUE(controller); |
217 | 217 |
218 // Try sending 5 chunks of mock audio data and verify that each of them | 218 // Try sending 5 chunks of mock audio data and verify that each of them |
219 // resulted immediately in a packet sent out via the network. This verifies | 219 // resulted immediately in a packet sent out via the network. This verifies |
220 // that we are streaming out encoded data as chunks without waiting for the | 220 // that we are streaming out encoded data as chunks without waiting for the |
221 // full recording to complete. | 221 // full recording to complete. |
222 const size_t kNumChunks = 5; | 222 const size_t kNumChunks = 5; |
223 for (size_t i = 0; i < kNumChunks; ++i) { | 223 for (size_t i = 0; i < kNumChunks; ++i) { |
224 controller->event_handler()->OnData(controller, &audio_packet_[0], | 224 controller->event_handler()->OnData(controller, &audio_packet_[0], |
225 audio_packet_.size()); | 225 audio_packet_.size()); |
226 MessageLoop::current()->RunUntilIdle(); | 226 base::MessageLoop::current()->RunUntilIdle(); |
227 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 227 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
228 ASSERT_TRUE(fetcher); | 228 ASSERT_TRUE(fetcher); |
229 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); | 229 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); |
230 } | 230 } |
231 | 231 |
232 recognizer_->StopAudioCapture(); | 232 recognizer_->StopAudioCapture(); |
233 MessageLoop::current()->RunUntilIdle(); | 233 base::MessageLoop::current()->RunUntilIdle(); |
234 EXPECT_TRUE(audio_started_); | 234 EXPECT_TRUE(audio_started_); |
235 EXPECT_TRUE(audio_ended_); | 235 EXPECT_TRUE(audio_ended_); |
236 EXPECT_FALSE(recognition_ended_); | 236 EXPECT_FALSE(recognition_ended_); |
237 EXPECT_FALSE(result_received_); | 237 EXPECT_FALSE(result_received_); |
238 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 238 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
239 | 239 |
240 // Issue the network callback to complete the process. | 240 // Issue the network callback to complete the process. |
241 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 241 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
242 ASSERT_TRUE(fetcher); | 242 ASSERT_TRUE(fetcher); |
243 | 243 |
244 fetcher->set_url(fetcher->GetOriginalURL()); | 244 fetcher->set_url(fetcher->GetOriginalURL()); |
245 net::URLRequestStatus status; | 245 net::URLRequestStatus status; |
246 status.set_status(net::URLRequestStatus::SUCCESS); | 246 status.set_status(net::URLRequestStatus::SUCCESS); |
247 fetcher->set_status(status); | 247 fetcher->set_status(status); |
248 fetcher->set_response_code(200); | 248 fetcher->set_response_code(200); |
249 fetcher->SetResponseString( | 249 fetcher->SetResponseString( |
250 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); | 250 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); |
251 fetcher->delegate()->OnURLFetchComplete(fetcher); | 251 fetcher->delegate()->OnURLFetchComplete(fetcher); |
252 MessageLoop::current()->RunUntilIdle(); | 252 base::MessageLoop::current()->RunUntilIdle(); |
253 EXPECT_TRUE(recognition_ended_); | 253 EXPECT_TRUE(recognition_ended_); |
254 EXPECT_TRUE(result_received_); | 254 EXPECT_TRUE(result_received_); |
255 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 255 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
256 CheckFinalEventsConsistency(); | 256 CheckFinalEventsConsistency(); |
257 } | 257 } |
258 | 258 |
259 TEST_F(SpeechRecognizerTest, CancelWithData) { | 259 TEST_F(SpeechRecognizerTest, CancelWithData) { |
260 // Start recording, give some data and then cancel. | 260 // Start recording, give some data and then cancel. |
261 recognizer_->StartRecognition(); | 261 recognizer_->StartRecognition(); |
262 MessageLoop::current()->RunUntilIdle(); | 262 base::MessageLoop::current()->RunUntilIdle(); |
263 TestAudioInputController* controller = | 263 TestAudioInputController* controller = |
264 audio_input_controller_factory_.controller(); | 264 audio_input_controller_factory_.controller(); |
265 ASSERT_TRUE(controller); | 265 ASSERT_TRUE(controller); |
266 controller->event_handler()->OnData(controller, &audio_packet_[0], | 266 controller->event_handler()->OnData(controller, &audio_packet_[0], |
267 audio_packet_.size()); | 267 audio_packet_.size()); |
268 MessageLoop::current()->RunUntilIdle(); | 268 base::MessageLoop::current()->RunUntilIdle(); |
269 recognizer_->AbortRecognition(); | 269 recognizer_->AbortRecognition(); |
270 MessageLoop::current()->RunUntilIdle(); | 270 base::MessageLoop::current()->RunUntilIdle(); |
271 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 271 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
272 EXPECT_TRUE(recognition_started_); | 272 EXPECT_TRUE(recognition_started_); |
273 EXPECT_TRUE(audio_started_); | 273 EXPECT_TRUE(audio_started_); |
274 EXPECT_FALSE(result_received_); | 274 EXPECT_FALSE(result_received_); |
275 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); | 275 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); |
276 CheckFinalEventsConsistency(); | 276 CheckFinalEventsConsistency(); |
277 } | 277 } |
278 | 278 |
279 TEST_F(SpeechRecognizerTest, ConnectionError) { | 279 TEST_F(SpeechRecognizerTest, ConnectionError) { |
280 // 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 |
281 // 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 |
282 recognizer_->StartRecognition(); | 282 recognizer_->StartRecognition(); |
283 MessageLoop::current()->RunUntilIdle(); | 283 base::MessageLoop::current()->RunUntilIdle(); |
284 TestAudioInputController* controller = | 284 TestAudioInputController* controller = |
285 audio_input_controller_factory_.controller(); | 285 audio_input_controller_factory_.controller(); |
286 ASSERT_TRUE(controller); | 286 ASSERT_TRUE(controller); |
287 controller->event_handler()->OnData(controller, &audio_packet_[0], | 287 controller->event_handler()->OnData(controller, &audio_packet_[0], |
288 audio_packet_.size()); | 288 audio_packet_.size()); |
289 MessageLoop::current()->RunUntilIdle(); | 289 base::MessageLoop::current()->RunUntilIdle(); |
290 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 290 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
291 ASSERT_TRUE(fetcher); | 291 ASSERT_TRUE(fetcher); |
292 | 292 |
293 recognizer_->StopAudioCapture(); | 293 recognizer_->StopAudioCapture(); |
294 MessageLoop::current()->RunUntilIdle(); | 294 base::MessageLoop::current()->RunUntilIdle(); |
295 EXPECT_TRUE(audio_started_); | 295 EXPECT_TRUE(audio_started_); |
296 EXPECT_TRUE(audio_ended_); | 296 EXPECT_TRUE(audio_ended_); |
297 EXPECT_FALSE(recognition_ended_); | 297 EXPECT_FALSE(recognition_ended_); |
298 EXPECT_FALSE(result_received_); | 298 EXPECT_FALSE(result_received_); |
299 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 299 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
300 | 300 |
301 // Issue the network callback to complete the process. | 301 // Issue the network callback to complete the process. |
302 fetcher->set_url(fetcher->GetOriginalURL()); | 302 fetcher->set_url(fetcher->GetOriginalURL()); |
303 net::URLRequestStatus status; | 303 net::URLRequestStatus status; |
304 status.set_status(net::URLRequestStatus::FAILED); | 304 status.set_status(net::URLRequestStatus::FAILED); |
305 status.set_error(net::ERR_CONNECTION_REFUSED); | 305 status.set_error(net::ERR_CONNECTION_REFUSED); |
306 fetcher->set_status(status); | 306 fetcher->set_status(status); |
307 fetcher->set_response_code(0); | 307 fetcher->set_response_code(0); |
308 fetcher->SetResponseString(std::string()); | 308 fetcher->SetResponseString(std::string()); |
309 fetcher->delegate()->OnURLFetchComplete(fetcher); | 309 fetcher->delegate()->OnURLFetchComplete(fetcher); |
310 MessageLoop::current()->RunUntilIdle(); | 310 base::MessageLoop::current()->RunUntilIdle(); |
311 EXPECT_TRUE(recognition_ended_); | 311 EXPECT_TRUE(recognition_ended_); |
312 EXPECT_FALSE(result_received_); | 312 EXPECT_FALSE(result_received_); |
313 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 313 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
314 CheckFinalEventsConsistency(); | 314 CheckFinalEventsConsistency(); |
315 } | 315 } |
316 | 316 |
317 TEST_F(SpeechRecognizerTest, ServerError) { | 317 TEST_F(SpeechRecognizerTest, ServerError) { |
318 // Start recording, give some data and then stop. Issue the network callback | 318 // Start recording, give some data and then stop. Issue the network callback |
319 // with a 500 error and verify that the recognizer bubbles the error up | 319 // with a 500 error and verify that the recognizer bubbles the error up |
320 recognizer_->StartRecognition(); | 320 recognizer_->StartRecognition(); |
321 MessageLoop::current()->RunUntilIdle(); | 321 base::MessageLoop::current()->RunUntilIdle(); |
322 TestAudioInputController* controller = | 322 TestAudioInputController* controller = |
323 audio_input_controller_factory_.controller(); | 323 audio_input_controller_factory_.controller(); |
324 ASSERT_TRUE(controller); | 324 ASSERT_TRUE(controller); |
325 controller->event_handler()->OnData(controller, &audio_packet_[0], | 325 controller->event_handler()->OnData(controller, &audio_packet_[0], |
326 audio_packet_.size()); | 326 audio_packet_.size()); |
327 MessageLoop::current()->RunUntilIdle(); | 327 base::MessageLoop::current()->RunUntilIdle(); |
328 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 328 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
329 ASSERT_TRUE(fetcher); | 329 ASSERT_TRUE(fetcher); |
330 | 330 |
331 recognizer_->StopAudioCapture(); | 331 recognizer_->StopAudioCapture(); |
332 MessageLoop::current()->RunUntilIdle(); | 332 base::MessageLoop::current()->RunUntilIdle(); |
333 EXPECT_TRUE(audio_started_); | 333 EXPECT_TRUE(audio_started_); |
334 EXPECT_TRUE(audio_ended_); | 334 EXPECT_TRUE(audio_ended_); |
335 EXPECT_FALSE(recognition_ended_); | 335 EXPECT_FALSE(recognition_ended_); |
336 EXPECT_FALSE(result_received_); | 336 EXPECT_FALSE(result_received_); |
337 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 337 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
338 | 338 |
339 // Issue the network callback to complete the process. | 339 // Issue the network callback to complete the process. |
340 fetcher->set_url(fetcher->GetOriginalURL()); | 340 fetcher->set_url(fetcher->GetOriginalURL()); |
341 net::URLRequestStatus status; | 341 net::URLRequestStatus status; |
342 status.set_status(net::URLRequestStatus::SUCCESS); | 342 status.set_status(net::URLRequestStatus::SUCCESS); |
343 fetcher->set_status(status); | 343 fetcher->set_status(status); |
344 fetcher->set_response_code(500); | 344 fetcher->set_response_code(500); |
345 fetcher->SetResponseString("Internal Server Error"); | 345 fetcher->SetResponseString("Internal Server Error"); |
346 fetcher->delegate()->OnURLFetchComplete(fetcher); | 346 fetcher->delegate()->OnURLFetchComplete(fetcher); |
347 MessageLoop::current()->RunUntilIdle(); | 347 base::MessageLoop::current()->RunUntilIdle(); |
348 EXPECT_TRUE(recognition_ended_); | 348 EXPECT_TRUE(recognition_ended_); |
349 EXPECT_FALSE(result_received_); | 349 EXPECT_FALSE(result_received_); |
350 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 350 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
351 CheckFinalEventsConsistency(); | 351 CheckFinalEventsConsistency(); |
352 } | 352 } |
353 | 353 |
354 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { | 354 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { |
355 // Check if things tear down properly if AudioInputController threw an error. | 355 // Check if things tear down properly if AudioInputController threw an error. |
356 recognizer_->StartRecognition(); | 356 recognizer_->StartRecognition(); |
357 MessageLoop::current()->RunUntilIdle(); | 357 base::MessageLoop::current()->RunUntilIdle(); |
358 TestAudioInputController* controller = | 358 TestAudioInputController* controller = |
359 audio_input_controller_factory_.controller(); | 359 audio_input_controller_factory_.controller(); |
360 ASSERT_TRUE(controller); | 360 ASSERT_TRUE(controller); |
361 controller->event_handler()->OnError(controller); | 361 controller->event_handler()->OnError(controller); |
362 MessageLoop::current()->RunUntilIdle(); | 362 base::MessageLoop::current()->RunUntilIdle(); |
363 EXPECT_TRUE(recognition_started_); | 363 EXPECT_TRUE(recognition_started_); |
364 EXPECT_FALSE(audio_started_); | 364 EXPECT_FALSE(audio_started_); |
365 EXPECT_FALSE(result_received_); | 365 EXPECT_FALSE(result_received_); |
366 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 366 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
367 CheckFinalEventsConsistency(); | 367 CheckFinalEventsConsistency(); |
368 } | 368 } |
369 | 369 |
370 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { | 370 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { |
371 // Check if things tear down properly if AudioInputController threw an error | 371 // Check if things tear down properly if AudioInputController threw an error |
372 // after giving some audio data. | 372 // after giving some audio data. |
373 recognizer_->StartRecognition(); | 373 recognizer_->StartRecognition(); |
374 MessageLoop::current()->RunUntilIdle(); | 374 base::MessageLoop::current()->RunUntilIdle(); |
375 TestAudioInputController* controller = | 375 TestAudioInputController* controller = |
376 audio_input_controller_factory_.controller(); | 376 audio_input_controller_factory_.controller(); |
377 ASSERT_TRUE(controller); | 377 ASSERT_TRUE(controller); |
378 controller->event_handler()->OnData(controller, &audio_packet_[0], | 378 controller->event_handler()->OnData(controller, &audio_packet_[0], |
379 audio_packet_.size()); | 379 audio_packet_.size()); |
380 controller->event_handler()->OnError(controller); | 380 controller->event_handler()->OnError(controller); |
381 MessageLoop::current()->RunUntilIdle(); | 381 base::MessageLoop::current()->RunUntilIdle(); |
382 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 382 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
383 EXPECT_TRUE(recognition_started_); | 383 EXPECT_TRUE(recognition_started_); |
384 EXPECT_TRUE(audio_started_); | 384 EXPECT_TRUE(audio_started_); |
385 EXPECT_FALSE(result_received_); | 385 EXPECT_FALSE(result_received_); |
386 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 386 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
387 CheckFinalEventsConsistency(); | 387 CheckFinalEventsConsistency(); |
388 } | 388 } |
389 | 389 |
390 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { | 390 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { |
391 // Start recording and give a lot of packets with audio samples set to zero. | 391 // Start recording and give a lot of packets with audio samples set to zero. |
392 // This should trigger the no-speech detector and issue a callback. | 392 // This should trigger the no-speech detector and issue a callback. |
393 recognizer_->StartRecognition(); | 393 recognizer_->StartRecognition(); |
394 MessageLoop::current()->RunUntilIdle(); | 394 base::MessageLoop::current()->RunUntilIdle(); |
395 TestAudioInputController* controller = | 395 TestAudioInputController* controller = |
396 audio_input_controller_factory_.controller(); | 396 audio_input_controller_factory_.controller(); |
397 ASSERT_TRUE(controller); | 397 ASSERT_TRUE(controller); |
398 | 398 |
399 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutMs) / | 399 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutMs) / |
400 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs + 1; | 400 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs + 1; |
401 // The vector is already filled with zero value samples on create. | 401 // The vector is already filled with zero value samples on create. |
402 for (int i = 0; i < num_packets; ++i) { | 402 for (int i = 0; i < num_packets; ++i) { |
403 controller->event_handler()->OnData(controller, &audio_packet_[0], | 403 controller->event_handler()->OnData(controller, &audio_packet_[0], |
404 audio_packet_.size()); | 404 audio_packet_.size()); |
405 } | 405 } |
406 MessageLoop::current()->RunUntilIdle(); | 406 base::MessageLoop::current()->RunUntilIdle(); |
407 EXPECT_TRUE(recognition_started_); | 407 EXPECT_TRUE(recognition_started_); |
408 EXPECT_TRUE(audio_started_); | 408 EXPECT_TRUE(audio_started_); |
409 EXPECT_FALSE(result_received_); | 409 EXPECT_FALSE(result_received_); |
410 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); | 410 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
411 CheckFinalEventsConsistency(); | 411 CheckFinalEventsConsistency(); |
412 } | 412 } |
413 | 413 |
414 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { | 414 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { |
415 // Start recording and give a lot of packets with audio samples set to zero | 415 // Start recording and give a lot of packets with audio samples set to zero |
416 // and then some more with reasonably loud audio samples. This should be | 416 // and then some more with reasonably loud audio samples. This should be |
417 // treated as normal speech input and the no-speech detector should not get | 417 // treated as normal speech input and the no-speech detector should not get |
418 // triggered. | 418 // triggered. |
419 recognizer_->StartRecognition(); | 419 recognizer_->StartRecognition(); |
420 MessageLoop::current()->RunUntilIdle(); | 420 base::MessageLoop::current()->RunUntilIdle(); |
421 TestAudioInputController* controller = | 421 TestAudioInputController* controller = |
422 audio_input_controller_factory_.controller(); | 422 audio_input_controller_factory_.controller(); |
423 ASSERT_TRUE(controller); | 423 ASSERT_TRUE(controller); |
424 controller = audio_input_controller_factory_.controller(); | 424 controller = audio_input_controller_factory_.controller(); |
425 ASSERT_TRUE(controller); | 425 ASSERT_TRUE(controller); |
426 | 426 |
427 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutMs) / | 427 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutMs) / |
428 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; | 428 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; |
429 | 429 |
430 // The vector is already filled with zero value samples on create. | 430 // The vector is already filled with zero value samples on create. |
431 for (int i = 0; i < num_packets / 2; ++i) { | 431 for (int i = 0; i < num_packets / 2; ++i) { |
432 controller->event_handler()->OnData(controller, &audio_packet_[0], | 432 controller->event_handler()->OnData(controller, &audio_packet_[0], |
433 audio_packet_.size()); | 433 audio_packet_.size()); |
434 } | 434 } |
435 | 435 |
436 FillPacketWithTestWaveform(); | 436 FillPacketWithTestWaveform(); |
437 for (int i = 0; i < num_packets / 2; ++i) { | 437 for (int i = 0; i < num_packets / 2; ++i) { |
438 controller->event_handler()->OnData(controller, &audio_packet_[0], | 438 controller->event_handler()->OnData(controller, &audio_packet_[0], |
439 audio_packet_.size()); | 439 audio_packet_.size()); |
440 } | 440 } |
441 | 441 |
442 MessageLoop::current()->RunUntilIdle(); | 442 base::MessageLoop::current()->RunUntilIdle(); |
443 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 443 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
444 EXPECT_TRUE(audio_started_); | 444 EXPECT_TRUE(audio_started_); |
445 EXPECT_FALSE(audio_ended_); | 445 EXPECT_FALSE(audio_ended_); |
446 EXPECT_FALSE(recognition_ended_); | 446 EXPECT_FALSE(recognition_ended_); |
447 recognizer_->AbortRecognition(); | 447 recognizer_->AbortRecognition(); |
448 MessageLoop::current()->RunUntilIdle(); | 448 base::MessageLoop::current()->RunUntilIdle(); |
449 CheckFinalEventsConsistency(); | 449 CheckFinalEventsConsistency(); |
450 } | 450 } |
451 | 451 |
452 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { | 452 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { |
453 // Start recording and give a lot of packets with audio samples set to zero | 453 // Start recording and give a lot of packets with audio samples set to zero |
454 // and then some more with reasonably loud audio samples. Check that we don't | 454 // and then some more with reasonably loud audio samples. Check that we don't |
455 // get the callback during estimation phase, then get zero for the silence | 455 // get the callback during estimation phase, then get zero for the silence |
456 // samples and proper volume for the loud audio. | 456 // samples and proper volume for the loud audio. |
457 recognizer_->StartRecognition(); | 457 recognizer_->StartRecognition(); |
458 MessageLoop::current()->RunUntilIdle(); | 458 base::MessageLoop::current()->RunUntilIdle(); |
459 TestAudioInputController* controller = | 459 TestAudioInputController* controller = |
460 audio_input_controller_factory_.controller(); | 460 audio_input_controller_factory_.controller(); |
461 ASSERT_TRUE(controller); | 461 ASSERT_TRUE(controller); |
462 controller = audio_input_controller_factory_.controller(); | 462 controller = audio_input_controller_factory_.controller(); |
463 ASSERT_TRUE(controller); | 463 ASSERT_TRUE(controller); |
464 | 464 |
465 // Feed some samples to begin with for the endpointer to do noise estimation. | 465 // Feed some samples to begin with for the endpointer to do noise estimation. |
466 int num_packets = SpeechRecognizer::kEndpointerEstimationTimeMs / | 466 int num_packets = SpeechRecognizer::kEndpointerEstimationTimeMs / |
467 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; | 467 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; |
468 FillPacketWithNoise(); | 468 FillPacketWithNoise(); |
469 for (int i = 0; i < num_packets; ++i) { | 469 for (int i = 0; i < num_packets; ++i) { |
470 controller->event_handler()->OnData(controller, &audio_packet_[0], | 470 controller->event_handler()->OnData(controller, &audio_packet_[0], |
471 audio_packet_.size()); | 471 audio_packet_.size()); |
472 } | 472 } |
473 MessageLoop::current()->RunUntilIdle(); | 473 base::MessageLoop::current()->RunUntilIdle(); |
474 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. | 474 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. |
475 | 475 |
476 // The vector is already filled with zero value samples on create. | 476 // The vector is already filled with zero value samples on create. |
477 controller->event_handler()->OnData(controller, &audio_packet_[0], | 477 controller->event_handler()->OnData(controller, &audio_packet_[0], |
478 audio_packet_.size()); | 478 audio_packet_.size()); |
479 MessageLoop::current()->RunUntilIdle(); | 479 base::MessageLoop::current()->RunUntilIdle(); |
480 EXPECT_FLOAT_EQ(0.74939233f, volume_); | 480 EXPECT_FLOAT_EQ(0.74939233f, volume_); |
481 | 481 |
482 FillPacketWithTestWaveform(); | 482 FillPacketWithTestWaveform(); |
483 controller->event_handler()->OnData(controller, &audio_packet_[0], | 483 controller->event_handler()->OnData(controller, &audio_packet_[0], |
484 audio_packet_.size()); | 484 audio_packet_.size()); |
485 MessageLoop::current()->RunUntilIdle(); | 485 base::MessageLoop::current()->RunUntilIdle(); |
486 EXPECT_FLOAT_EQ(0.89926866f, volume_); | 486 EXPECT_FLOAT_EQ(0.89926866f, volume_); |
487 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); | 487 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); |
488 | 488 |
489 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); | 489 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
490 EXPECT_FALSE(audio_ended_); | 490 EXPECT_FALSE(audio_ended_); |
491 EXPECT_FALSE(recognition_ended_); | 491 EXPECT_FALSE(recognition_ended_); |
492 recognizer_->AbortRecognition(); | 492 recognizer_->AbortRecognition(); |
493 MessageLoop::current()->RunUntilIdle(); | 493 base::MessageLoop::current()->RunUntilIdle(); |
494 CheckFinalEventsConsistency(); | 494 CheckFinalEventsConsistency(); |
495 } | 495 } |
496 | 496 |
497 } // namespace content | 497 } // namespace content |
OLD | NEW |