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" |
11 #include "media/audio/mock_audio_manager.h" | 11 #include "media/audio/mock_audio_manager.h" |
12 #include "media/audio/fake_audio_input_stream.h" | 12 #include "media/audio/fake_audio_input_stream.h" |
13 #include "media/audio/fake_audio_output_stream.h" | 13 #include "media/audio/fake_audio_output_stream.h" |
14 #include "media/audio/test_audio_input_controller_factory.h" | 14 #include "media/audio/test_audio_input_controller_factory.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.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 base::MessageLoopProxy; |
21 using content::BrowserThread; | |
22 using content::BrowserThreadImpl; | |
23 using media::AudioInputController; | 21 using media::AudioInputController; |
24 using media::AudioInputStream; | 22 using media::AudioInputStream; |
25 using media::AudioManager; | 23 using media::AudioManager; |
26 using media::AudioOutputStream; | 24 using media::AudioOutputStream; |
27 using media::AudioParameters; | 25 using media::AudioParameters; |
28 using media::TestAudioInputController; | 26 using media::TestAudioInputController; |
29 using media::TestAudioInputControllerFactory; | 27 using media::TestAudioInputControllerFactory; |
30 | 28 |
31 namespace speech { | 29 namespace content { |
32 | 30 |
33 class SpeechRecognizerTest : public content::SpeechRecognitionEventListener, | 31 class SpeechRecognizerTest : public SpeechRecognitionEventListener, |
34 public testing::Test { | 32 public testing::Test { |
35 public: | 33 public: |
36 SpeechRecognizerTest() | 34 SpeechRecognizerTest() |
37 : io_thread_(BrowserThread::IO, &message_loop_), | 35 : io_thread_(BrowserThread::IO, &message_loop_), |
38 recognition_started_(false), | 36 recognition_started_(false), |
39 recognition_ended_(false), | 37 recognition_ended_(false), |
40 result_received_(false), | 38 result_received_(false), |
41 audio_started_(false), | 39 audio_started_(false), |
42 audio_ended_(false), | 40 audio_ended_(false), |
43 sound_started_(false), | 41 sound_started_(false), |
44 sound_ended_(false), | 42 sound_ended_(false), |
45 error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 43 error_(SPEECH_RECOGNITION_ERROR_NONE), |
46 volume_(-1.0f) { | 44 volume_(-1.0f) { |
47 // SpeechRecognizer takes ownership of sr_engine. | 45 // SpeechRecognizer takes ownership of sr_engine. |
48 SpeechRecognitionEngine* sr_engine = | 46 SpeechRecognitionEngine* sr_engine = |
49 new GoogleOneShotRemoteEngine(NULL /* URLRequestContextGetter */); | 47 new GoogleOneShotRemoteEngine(NULL /* URLRequestContextGetter */); |
50 SpeechRecognitionEngineConfig config; | 48 SpeechRecognitionEngineConfig config; |
51 config.audio_num_bits_per_sample = SpeechRecognizer::kNumBitsPerAudioSample; | 49 config.audio_num_bits_per_sample = SpeechRecognizer::kNumBitsPerAudioSample; |
52 config.audio_sample_rate = SpeechRecognizer::kAudioSampleRate; | 50 config.audio_sample_rate = SpeechRecognizer::kAudioSampleRate; |
53 config.filter_profanities = false; | 51 config.filter_profanities = false; |
54 sr_engine->SetConfig(config); | 52 sr_engine->SetConfig(config); |
55 | 53 |
(...skipping 24 matching lines...) Expand all Loading... |
80 EXPECT_TRUE(!recognition_ended_ || (audio_ended_ || !audio_started_)); | 78 EXPECT_TRUE(!recognition_ended_ || (audio_ended_ || !audio_started_)); |
81 } | 79 } |
82 | 80 |
83 void CheckFinalEventsConsistency() { | 81 void CheckFinalEventsConsistency() { |
84 // Note: "!(x ^ y)" == "(x && y) || (!x && !x)". | 82 // Note: "!(x ^ y)" == "(x && y) || (!x && !x)". |
85 EXPECT_FALSE(recognition_started_ ^ recognition_ended_); | 83 EXPECT_FALSE(recognition_started_ ^ recognition_ended_); |
86 EXPECT_FALSE(audio_started_ ^ audio_ended_); | 84 EXPECT_FALSE(audio_started_ ^ audio_ended_); |
87 EXPECT_FALSE(sound_started_ ^ sound_ended_); | 85 EXPECT_FALSE(sound_started_ ^ sound_ended_); |
88 } | 86 } |
89 | 87 |
90 // Overridden from content::SpeechRecognitionEventListener: | 88 // Overridden from SpeechRecognitionEventListener: |
91 virtual void OnAudioStart(int session_id) OVERRIDE { | 89 virtual void OnAudioStart(int session_id) OVERRIDE { |
92 audio_started_ = true; | 90 audio_started_ = true; |
93 CheckEventsConsistency(); | 91 CheckEventsConsistency(); |
94 } | 92 } |
95 | 93 |
96 virtual void OnAudioEnd(int session_id) OVERRIDE { | 94 virtual void OnAudioEnd(int session_id) OVERRIDE { |
97 audio_ended_ = true; | 95 audio_ended_ = true; |
98 CheckEventsConsistency(); | 96 CheckEventsConsistency(); |
99 } | 97 } |
100 | 98 |
101 virtual void OnRecognitionResult( | 99 virtual void OnRecognitionResult( |
102 int session_id, const content::SpeechRecognitionResult& result) OVERRIDE { | 100 int session_id, const SpeechRecognitionResult& result) OVERRIDE { |
103 result_received_ = true; | 101 result_received_ = true; |
104 } | 102 } |
105 | 103 |
106 virtual void OnRecognitionError( | 104 virtual void OnRecognitionError( |
107 int session_id, const content::SpeechRecognitionError& error) OVERRIDE { | 105 int session_id, const SpeechRecognitionError& error) OVERRIDE { |
108 EXPECT_TRUE(recognition_started_); | 106 EXPECT_TRUE(recognition_started_); |
109 EXPECT_FALSE(recognition_ended_); | 107 EXPECT_FALSE(recognition_ended_); |
110 error_ = error.code; | 108 error_ = error.code; |
111 } | 109 } |
112 | 110 |
113 virtual void OnAudioLevelsChange(int session_id, float volume, | 111 virtual void OnAudioLevelsChange(int session_id, float volume, |
114 float noise_volume) OVERRIDE { | 112 float noise_volume) OVERRIDE { |
115 volume_ = volume; | 113 volume_ = volume; |
116 noise_volume_ = noise_volume; | 114 noise_volume_ = noise_volume; |
117 } | 115 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 BrowserThreadImpl io_thread_; | 166 BrowserThreadImpl io_thread_; |
169 scoped_refptr<SpeechRecognizer> recognizer_; | 167 scoped_refptr<SpeechRecognizer> recognizer_; |
170 scoped_ptr<AudioManager> audio_manager_; | 168 scoped_ptr<AudioManager> audio_manager_; |
171 bool recognition_started_; | 169 bool recognition_started_; |
172 bool recognition_ended_; | 170 bool recognition_ended_; |
173 bool result_received_; | 171 bool result_received_; |
174 bool audio_started_; | 172 bool audio_started_; |
175 bool audio_ended_; | 173 bool audio_ended_; |
176 bool sound_started_; | 174 bool sound_started_; |
177 bool sound_ended_; | 175 bool sound_ended_; |
178 content::SpeechRecognitionErrorCode error_; | 176 SpeechRecognitionErrorCode error_; |
179 net::TestURLFetcherFactory url_fetcher_factory_; | 177 net::TestURLFetcherFactory url_fetcher_factory_; |
180 TestAudioInputControllerFactory audio_input_controller_factory_; | 178 TestAudioInputControllerFactory audio_input_controller_factory_; |
181 std::vector<uint8> audio_packet_; | 179 std::vector<uint8> audio_packet_; |
182 float volume_; | 180 float volume_; |
183 float noise_volume_; | 181 float noise_volume_; |
184 }; | 182 }; |
185 | 183 |
186 TEST_F(SpeechRecognizerTest, StopNoData) { | 184 TEST_F(SpeechRecognizerTest, StopNoData) { |
187 // Check for callbacks when stopping record before any audio gets recorded. | 185 // Check for callbacks when stopping record before any audio gets recorded. |
188 recognizer_->StartRecognition(); | 186 recognizer_->StartRecognition(); |
189 recognizer_->StopAudioCapture(); | 187 recognizer_->StopAudioCapture(); |
190 MessageLoop::current()->RunAllPending(); | 188 MessageLoop::current()->RunAllPending(); |
191 EXPECT_TRUE(recognition_started_); | 189 EXPECT_TRUE(recognition_started_); |
192 EXPECT_FALSE(audio_started_); | 190 EXPECT_FALSE(audio_started_); |
193 EXPECT_FALSE(result_received_); | 191 EXPECT_FALSE(result_received_); |
194 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 192 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
195 CheckFinalEventsConsistency(); | 193 CheckFinalEventsConsistency(); |
196 } | 194 } |
197 | 195 |
198 TEST_F(SpeechRecognizerTest, CancelNoData) { | 196 TEST_F(SpeechRecognizerTest, CancelNoData) { |
199 // Check for callbacks when canceling recognition before any audio gets | 197 // Check for callbacks when canceling recognition before any audio gets |
200 // recorded. | 198 // recorded. |
201 recognizer_->StartRecognition(); | 199 recognizer_->StartRecognition(); |
202 recognizer_->AbortRecognition(); | 200 recognizer_->AbortRecognition(); |
203 MessageLoop::current()->RunAllPending(); | 201 MessageLoop::current()->RunAllPending(); |
204 EXPECT_TRUE(recognition_started_); | 202 EXPECT_TRUE(recognition_started_); |
205 EXPECT_FALSE(audio_started_); | 203 EXPECT_FALSE(audio_started_); |
206 EXPECT_FALSE(result_received_); | 204 EXPECT_FALSE(result_received_); |
207 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_ABORTED, error_); | 205 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); |
208 CheckFinalEventsConsistency(); | 206 CheckFinalEventsConsistency(); |
209 } | 207 } |
210 | 208 |
211 TEST_F(SpeechRecognizerTest, StopWithData) { | 209 TEST_F(SpeechRecognizerTest, StopWithData) { |
212 // 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 |
213 // network callback to arrive before completion. | 211 // network callback to arrive before completion. |
214 recognizer_->StartRecognition(); | 212 recognizer_->StartRecognition(); |
215 MessageLoop::current()->RunAllPending(); | 213 MessageLoop::current()->RunAllPending(); |
216 TestAudioInputController* controller = | 214 TestAudioInputController* controller = |
217 audio_input_controller_factory_.controller(); | 215 audio_input_controller_factory_.controller(); |
(...skipping 12 matching lines...) Expand all Loading... |
230 ASSERT_TRUE(fetcher); | 228 ASSERT_TRUE(fetcher); |
231 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); | 229 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); |
232 } | 230 } |
233 | 231 |
234 recognizer_->StopAudioCapture(); | 232 recognizer_->StopAudioCapture(); |
235 MessageLoop::current()->RunAllPending(); | 233 MessageLoop::current()->RunAllPending(); |
236 EXPECT_TRUE(audio_started_); | 234 EXPECT_TRUE(audio_started_); |
237 EXPECT_TRUE(audio_ended_); | 235 EXPECT_TRUE(audio_ended_); |
238 EXPECT_FALSE(recognition_ended_); | 236 EXPECT_FALSE(recognition_ended_); |
239 EXPECT_FALSE(result_received_); | 237 EXPECT_FALSE(result_received_); |
240 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 238 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
241 | 239 |
242 // Issue the network callback to complete the process. | 240 // Issue the network callback to complete the process. |
243 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 241 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
244 ASSERT_TRUE(fetcher); | 242 ASSERT_TRUE(fetcher); |
245 | 243 |
246 fetcher->set_url(fetcher->GetOriginalURL()); | 244 fetcher->set_url(fetcher->GetOriginalURL()); |
247 net::URLRequestStatus status; | 245 net::URLRequestStatus status; |
248 status.set_status(net::URLRequestStatus::SUCCESS); | 246 status.set_status(net::URLRequestStatus::SUCCESS); |
249 fetcher->set_status(status); | 247 fetcher->set_status(status); |
250 fetcher->set_response_code(200); | 248 fetcher->set_response_code(200); |
251 fetcher->SetResponseString( | 249 fetcher->SetResponseString( |
252 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); | 250 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); |
253 fetcher->delegate()->OnURLFetchComplete(fetcher); | 251 fetcher->delegate()->OnURLFetchComplete(fetcher); |
254 MessageLoop::current()->RunAllPending(); | 252 MessageLoop::current()->RunAllPending(); |
255 EXPECT_TRUE(recognition_ended_); | 253 EXPECT_TRUE(recognition_ended_); |
256 EXPECT_TRUE(result_received_); | 254 EXPECT_TRUE(result_received_); |
257 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 255 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
258 CheckFinalEventsConsistency(); | 256 CheckFinalEventsConsistency(); |
259 } | 257 } |
260 | 258 |
261 TEST_F(SpeechRecognizerTest, CancelWithData) { | 259 TEST_F(SpeechRecognizerTest, CancelWithData) { |
262 // Start recording, give some data and then cancel. | 260 // Start recording, give some data and then cancel. |
263 recognizer_->StartRecognition(); | 261 recognizer_->StartRecognition(); |
264 MessageLoop::current()->RunAllPending(); | 262 MessageLoop::current()->RunAllPending(); |
265 TestAudioInputController* controller = | 263 TestAudioInputController* controller = |
266 audio_input_controller_factory_.controller(); | 264 audio_input_controller_factory_.controller(); |
267 ASSERT_TRUE(controller); | 265 ASSERT_TRUE(controller); |
268 controller->event_handler()->OnData(controller, &audio_packet_[0], | 266 controller->event_handler()->OnData(controller, &audio_packet_[0], |
269 audio_packet_.size()); | 267 audio_packet_.size()); |
270 MessageLoop::current()->RunAllPending(); | 268 MessageLoop::current()->RunAllPending(); |
271 recognizer_->AbortRecognition(); | 269 recognizer_->AbortRecognition(); |
272 MessageLoop::current()->RunAllPending(); | 270 MessageLoop::current()->RunAllPending(); |
273 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 271 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
274 EXPECT_TRUE(recognition_started_); | 272 EXPECT_TRUE(recognition_started_); |
275 EXPECT_TRUE(audio_started_); | 273 EXPECT_TRUE(audio_started_); |
276 EXPECT_FALSE(result_received_); | 274 EXPECT_FALSE(result_received_); |
277 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_ABORTED, error_); | 275 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_); |
278 CheckFinalEventsConsistency(); | 276 CheckFinalEventsConsistency(); |
279 } | 277 } |
280 | 278 |
281 TEST_F(SpeechRecognizerTest, ConnectionError) { | 279 TEST_F(SpeechRecognizerTest, ConnectionError) { |
282 // 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 |
283 // 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 |
284 recognizer_->StartRecognition(); | 282 recognizer_->StartRecognition(); |
285 MessageLoop::current()->RunAllPending(); | 283 MessageLoop::current()->RunAllPending(); |
286 TestAudioInputController* controller = | 284 TestAudioInputController* controller = |
287 audio_input_controller_factory_.controller(); | 285 audio_input_controller_factory_.controller(); |
288 ASSERT_TRUE(controller); | 286 ASSERT_TRUE(controller); |
289 controller->event_handler()->OnData(controller, &audio_packet_[0], | 287 controller->event_handler()->OnData(controller, &audio_packet_[0], |
290 audio_packet_.size()); | 288 audio_packet_.size()); |
291 MessageLoop::current()->RunAllPending(); | 289 MessageLoop::current()->RunAllPending(); |
292 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 290 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
293 ASSERT_TRUE(fetcher); | 291 ASSERT_TRUE(fetcher); |
294 | 292 |
295 recognizer_->StopAudioCapture(); | 293 recognizer_->StopAudioCapture(); |
296 MessageLoop::current()->RunAllPending(); | 294 MessageLoop::current()->RunAllPending(); |
297 EXPECT_TRUE(audio_started_); | 295 EXPECT_TRUE(audio_started_); |
298 EXPECT_TRUE(audio_ended_); | 296 EXPECT_TRUE(audio_ended_); |
299 EXPECT_FALSE(recognition_ended_); | 297 EXPECT_FALSE(recognition_ended_); |
300 EXPECT_FALSE(result_received_); | 298 EXPECT_FALSE(result_received_); |
301 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 299 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
302 | 300 |
303 // Issue the network callback to complete the process. | 301 // Issue the network callback to complete the process. |
304 fetcher->set_url(fetcher->GetOriginalURL()); | 302 fetcher->set_url(fetcher->GetOriginalURL()); |
305 net::URLRequestStatus status; | 303 net::URLRequestStatus status; |
306 status.set_status(net::URLRequestStatus::FAILED); | 304 status.set_status(net::URLRequestStatus::FAILED); |
307 status.set_error(net::ERR_CONNECTION_REFUSED); | 305 status.set_error(net::ERR_CONNECTION_REFUSED); |
308 fetcher->set_status(status); | 306 fetcher->set_status(status); |
309 fetcher->set_response_code(0); | 307 fetcher->set_response_code(0); |
310 fetcher->SetResponseString(""); | 308 fetcher->SetResponseString(""); |
311 fetcher->delegate()->OnURLFetchComplete(fetcher); | 309 fetcher->delegate()->OnURLFetchComplete(fetcher); |
312 MessageLoop::current()->RunAllPending(); | 310 MessageLoop::current()->RunAllPending(); |
313 EXPECT_TRUE(recognition_ended_); | 311 EXPECT_TRUE(recognition_ended_); |
314 EXPECT_FALSE(result_received_); | 312 EXPECT_FALSE(result_received_); |
315 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 313 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
316 CheckFinalEventsConsistency(); | 314 CheckFinalEventsConsistency(); |
317 } | 315 } |
318 | 316 |
319 TEST_F(SpeechRecognizerTest, ServerError) { | 317 TEST_F(SpeechRecognizerTest, ServerError) { |
320 // 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 |
321 // 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 |
322 recognizer_->StartRecognition(); | 320 recognizer_->StartRecognition(); |
323 MessageLoop::current()->RunAllPending(); | 321 MessageLoop::current()->RunAllPending(); |
324 TestAudioInputController* controller = | 322 TestAudioInputController* controller = |
325 audio_input_controller_factory_.controller(); | 323 audio_input_controller_factory_.controller(); |
326 ASSERT_TRUE(controller); | 324 ASSERT_TRUE(controller); |
327 controller->event_handler()->OnData(controller, &audio_packet_[0], | 325 controller->event_handler()->OnData(controller, &audio_packet_[0], |
328 audio_packet_.size()); | 326 audio_packet_.size()); |
329 MessageLoop::current()->RunAllPending(); | 327 MessageLoop::current()->RunAllPending(); |
330 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 328 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
331 ASSERT_TRUE(fetcher); | 329 ASSERT_TRUE(fetcher); |
332 | 330 |
333 recognizer_->StopAudioCapture(); | 331 recognizer_->StopAudioCapture(); |
334 MessageLoop::current()->RunAllPending(); | 332 MessageLoop::current()->RunAllPending(); |
335 EXPECT_TRUE(audio_started_); | 333 EXPECT_TRUE(audio_started_); |
336 EXPECT_TRUE(audio_ended_); | 334 EXPECT_TRUE(audio_ended_); |
337 EXPECT_FALSE(recognition_ended_); | 335 EXPECT_FALSE(recognition_ended_); |
338 EXPECT_FALSE(result_received_); | 336 EXPECT_FALSE(result_received_); |
339 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 337 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
340 | 338 |
341 // Issue the network callback to complete the process. | 339 // Issue the network callback to complete the process. |
342 fetcher->set_url(fetcher->GetOriginalURL()); | 340 fetcher->set_url(fetcher->GetOriginalURL()); |
343 net::URLRequestStatus status; | 341 net::URLRequestStatus status; |
344 status.set_status(net::URLRequestStatus::SUCCESS); | 342 status.set_status(net::URLRequestStatus::SUCCESS); |
345 fetcher->set_status(status); | 343 fetcher->set_status(status); |
346 fetcher->set_response_code(500); | 344 fetcher->set_response_code(500); |
347 fetcher->SetResponseString("Internal Server Error"); | 345 fetcher->SetResponseString("Internal Server Error"); |
348 fetcher->delegate()->OnURLFetchComplete(fetcher); | 346 fetcher->delegate()->OnURLFetchComplete(fetcher); |
349 MessageLoop::current()->RunAllPending(); | 347 MessageLoop::current()->RunAllPending(); |
350 EXPECT_TRUE(recognition_ended_); | 348 EXPECT_TRUE(recognition_ended_); |
351 EXPECT_FALSE(result_received_); | 349 EXPECT_FALSE(result_received_); |
352 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 350 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
353 CheckFinalEventsConsistency(); | 351 CheckFinalEventsConsistency(); |
354 } | 352 } |
355 | 353 |
356 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { | 354 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { |
357 // Check if things tear down properly if AudioInputController threw an error. | 355 // Check if things tear down properly if AudioInputController threw an error. |
358 recognizer_->StartRecognition(); | 356 recognizer_->StartRecognition(); |
359 MessageLoop::current()->RunAllPending(); | 357 MessageLoop::current()->RunAllPending(); |
360 TestAudioInputController* controller = | 358 TestAudioInputController* controller = |
361 audio_input_controller_factory_.controller(); | 359 audio_input_controller_factory_.controller(); |
362 ASSERT_TRUE(controller); | 360 ASSERT_TRUE(controller); |
363 controller->event_handler()->OnError(controller, 0); | 361 controller->event_handler()->OnError(controller, 0); |
364 MessageLoop::current()->RunAllPending(); | 362 MessageLoop::current()->RunAllPending(); |
365 EXPECT_TRUE(recognition_started_); | 363 EXPECT_TRUE(recognition_started_); |
366 EXPECT_FALSE(audio_started_); | 364 EXPECT_FALSE(audio_started_); |
367 EXPECT_FALSE(result_received_); | 365 EXPECT_FALSE(result_received_); |
368 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 366 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
369 CheckFinalEventsConsistency(); | 367 CheckFinalEventsConsistency(); |
370 } | 368 } |
371 | 369 |
372 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { | 370 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { |
373 // Check if things tear down properly if AudioInputController threw an error | 371 // Check if things tear down properly if AudioInputController threw an error |
374 // after giving some audio data. | 372 // after giving some audio data. |
375 recognizer_->StartRecognition(); | 373 recognizer_->StartRecognition(); |
376 MessageLoop::current()->RunAllPending(); | 374 MessageLoop::current()->RunAllPending(); |
377 TestAudioInputController* controller = | 375 TestAudioInputController* controller = |
378 audio_input_controller_factory_.controller(); | 376 audio_input_controller_factory_.controller(); |
379 ASSERT_TRUE(controller); | 377 ASSERT_TRUE(controller); |
380 controller->event_handler()->OnData(controller, &audio_packet_[0], | 378 controller->event_handler()->OnData(controller, &audio_packet_[0], |
381 audio_packet_.size()); | 379 audio_packet_.size()); |
382 controller->event_handler()->OnError(controller, 0); | 380 controller->event_handler()->OnError(controller, 0); |
383 MessageLoop::current()->RunAllPending(); | 381 MessageLoop::current()->RunAllPending(); |
384 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 382 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
385 EXPECT_TRUE(recognition_started_); | 383 EXPECT_TRUE(recognition_started_); |
386 EXPECT_TRUE(audio_started_); | 384 EXPECT_TRUE(audio_started_); |
387 EXPECT_FALSE(result_received_); | 385 EXPECT_FALSE(result_received_); |
388 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 386 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
389 CheckFinalEventsConsistency(); | 387 CheckFinalEventsConsistency(); |
390 } | 388 } |
391 | 389 |
392 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { | 390 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { |
393 // 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. |
394 // This should trigger the no-speech detector and issue a callback. | 392 // This should trigger the no-speech detector and issue a callback. |
395 recognizer_->StartRecognition(); | 393 recognizer_->StartRecognition(); |
396 MessageLoop::current()->RunAllPending(); | 394 MessageLoop::current()->RunAllPending(); |
397 TestAudioInputController* controller = | 395 TestAudioInputController* controller = |
398 audio_input_controller_factory_.controller(); | 396 audio_input_controller_factory_.controller(); |
399 ASSERT_TRUE(controller); | 397 ASSERT_TRUE(controller); |
400 | 398 |
401 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutMs) / | 399 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutMs) / |
402 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs + 1; | 400 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs + 1; |
403 // The vector is already filled with zero value samples on create. | 401 // The vector is already filled with zero value samples on create. |
404 for (int i = 0; i < num_packets; ++i) { | 402 for (int i = 0; i < num_packets; ++i) { |
405 controller->event_handler()->OnData(controller, &audio_packet_[0], | 403 controller->event_handler()->OnData(controller, &audio_packet_[0], |
406 audio_packet_.size()); | 404 audio_packet_.size()); |
407 } | 405 } |
408 MessageLoop::current()->RunAllPending(); | 406 MessageLoop::current()->RunAllPending(); |
409 EXPECT_TRUE(recognition_started_); | 407 EXPECT_TRUE(recognition_started_); |
410 EXPECT_TRUE(audio_started_); | 408 EXPECT_TRUE(audio_started_); |
411 EXPECT_FALSE(result_received_); | 409 EXPECT_FALSE(result_received_); |
412 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); | 410 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
413 CheckFinalEventsConsistency(); | 411 CheckFinalEventsConsistency(); |
414 } | 412 } |
415 | 413 |
416 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { | 414 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { |
417 // 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 |
418 // 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 |
419 // 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 |
420 // triggered. | 418 // triggered. |
421 recognizer_->StartRecognition(); | 419 recognizer_->StartRecognition(); |
422 MessageLoop::current()->RunAllPending(); | 420 MessageLoop::current()->RunAllPending(); |
(...skipping 12 matching lines...) Expand all Loading... |
435 audio_packet_.size()); | 433 audio_packet_.size()); |
436 } | 434 } |
437 | 435 |
438 FillPacketWithTestWaveform(); | 436 FillPacketWithTestWaveform(); |
439 for (int i = 0; i < num_packets / 2; ++i) { | 437 for (int i = 0; i < num_packets / 2; ++i) { |
440 controller->event_handler()->OnData(controller, &audio_packet_[0], | 438 controller->event_handler()->OnData(controller, &audio_packet_[0], |
441 audio_packet_.size()); | 439 audio_packet_.size()); |
442 } | 440 } |
443 | 441 |
444 MessageLoop::current()->RunAllPending(); | 442 MessageLoop::current()->RunAllPending(); |
445 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 443 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
446 EXPECT_TRUE(audio_started_); | 444 EXPECT_TRUE(audio_started_); |
447 EXPECT_FALSE(audio_ended_); | 445 EXPECT_FALSE(audio_ended_); |
448 EXPECT_FALSE(recognition_ended_); | 446 EXPECT_FALSE(recognition_ended_); |
449 recognizer_->AbortRecognition(); | 447 recognizer_->AbortRecognition(); |
450 MessageLoop::current()->RunAllPending(); | 448 MessageLoop::current()->RunAllPending(); |
451 CheckFinalEventsConsistency(); | 449 CheckFinalEventsConsistency(); |
452 } | 450 } |
453 | 451 |
454 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { | 452 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { |
455 // 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 |
(...skipping 25 matching lines...) Expand all Loading... |
481 MessageLoop::current()->RunAllPending(); | 479 MessageLoop::current()->RunAllPending(); |
482 EXPECT_FLOAT_EQ(0.74939233f, volume_); | 480 EXPECT_FLOAT_EQ(0.74939233f, volume_); |
483 | 481 |
484 FillPacketWithTestWaveform(); | 482 FillPacketWithTestWaveform(); |
485 controller->event_handler()->OnData(controller, &audio_packet_[0], | 483 controller->event_handler()->OnData(controller, &audio_packet_[0], |
486 audio_packet_.size()); | 484 audio_packet_.size()); |
487 MessageLoop::current()->RunAllPending(); | 485 MessageLoop::current()->RunAllPending(); |
488 EXPECT_FLOAT_EQ(0.89926866f, volume_); | 486 EXPECT_FLOAT_EQ(0.89926866f, volume_); |
489 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); | 487 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); |
490 | 488 |
491 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 489 EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_); |
492 EXPECT_FALSE(audio_ended_); | 490 EXPECT_FALSE(audio_ended_); |
493 EXPECT_FALSE(recognition_ended_); | 491 EXPECT_FALSE(recognition_ended_); |
494 recognizer_->AbortRecognition(); | 492 recognizer_->AbortRecognition(); |
495 MessageLoop::current()->RunAllPending(); | 493 MessageLoop::current()->RunAllPending(); |
496 CheckFinalEventsConsistency(); | 494 CheckFinalEventsConsistency(); |
497 } | 495 } |
498 | 496 |
499 } // namespace speech | 497 } // namespace content |
OLD | NEW |