Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: content/browser/speech/speech_recognizer_impl_unittest.cc

Issue 9835049: Speech refactoring: Reimplemented speech_recognizer as a FSM. (CL1.5) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased from master. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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::TestAudioInputController; 24 using media::TestAudioInputController;
24 using media::TestAudioInputControllerFactory; 25 using media::TestAudioInputControllerFactory;
25 26
26 namespace { 27 namespace {
27 28
28 class MockAudioManager : public AudioManagerBase { 29 class MockAudioManager : public AudioManagerBase {
29 public: 30 public:
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 public: 93 public:
93 SpeechRecognizerImplTest() 94 SpeechRecognizerImplTest()
94 : io_thread_(BrowserThread::IO, &message_loop_), 95 : io_thread_(BrowserThread::IO, &message_loop_),
95 audio_manager_(new MockAudioManager()), 96 audio_manager_(new MockAudioManager()),
96 audio_ended_(false), 97 audio_ended_(false),
97 recognition_ended_(false), 98 recognition_ended_(false),
98 result_received_(false), 99 result_received_(false),
99 audio_started_(false), 100 audio_started_(false),
100 error_(content::SPEECH_RECOGNITION_ERROR_NONE), 101 error_(content::SPEECH_RECOGNITION_ERROR_NONE),
101 volume_(-1.0f) { 102 volume_(-1.0f) {
102 recognizer_ = new SpeechRecognizerImpl( 103 GoogleOneShotRemoteEngine* sr_engine =
103 this, 1, std::string(), std::string(), NULL, false, std::string(), 104 new GoogleOneShotRemoteEngine(NULL /* URLRequestContextGetter */);
104 std::string()); 105 GoogleOneShotRemoteEngineConfig config;
106 config.audio_num_bits_per_sample =
107 SpeechRecognizerImpl::kNumBitsPerAudioSample;
108 config.audio_sample_rate = SpeechRecognizerImpl::kAudioSampleRate;
109 config.filter_profanities = false;
110 sr_engine->SetConfig(config);
111
112 recognizer_ = new SpeechRecognizerImpl(this, 1, sr_engine);
105 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); 113 recognizer_->SetAudioManagerForTesting(audio_manager_.get());
114
106 int audio_packet_length_bytes = 115 int audio_packet_length_bytes =
107 (SpeechRecognizerImpl::kAudioSampleRate * 116 (SpeechRecognizerImpl::kAudioSampleRate *
108 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * 117 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs *
109 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * 118 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) *
110 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); 119 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000);
111 audio_packet_.resize(audio_packet_length_bytes); 120 audio_packet_.resize(audio_packet_length_bytes);
112 } 121 }
113 122
114 // Overridden from content::SpeechRecognitionEventListener: 123 // Overridden from content::SpeechRecognitionEventListener:
115 virtual void OnAudioStart(int caller_id) OVERRIDE { 124 virtual void OnAudioStart(int caller_id) OVERRIDE {
116 audio_started_ = true; 125 audio_started_ = true;
117 } 126 }
118 127
119 virtual void OnAudioEnd(int caller_id) OVERRIDE { 128 virtual void OnAudioEnd(int caller_id) OVERRIDE {
120 audio_ended_ = true; 129 audio_ended_ = true;
121 } 130 }
122 131
123 virtual void OnRecognitionResult( 132 virtual void OnRecognitionResult(
124 int caller_id, const content::SpeechRecognitionResult& result) OVERRIDE { 133 int caller_id, const content::SpeechRecognitionResult& result) OVERRIDE {
125 result_received_ = true; 134 result_received_ = true;
126 } 135 }
127 136
128 virtual void OnRecognitionError( 137 virtual void OnRecognitionError(
129 int caller_id, 138 int caller_id, const content::SpeechRecognitionError& error) OVERRIDE {
130 const content::SpeechRecognitionError& error) OVERRIDE {
131 error_ = error.code; 139 error_ = error.code;
132 } 140 }
133 141
134 virtual void OnAudioLevelsChange(int caller_id, float volume, 142 virtual void OnAudioLevelsChange(int caller_id, float volume,
135 float noise_volume) OVERRIDE { 143 float noise_volume) OVERRIDE {
136 volume_ = volume; 144 volume_ = volume;
137 noise_volume_ = noise_volume; 145 noise_volume_ = noise_volume;
138 } 146 }
139 147
140 virtual void OnRecognitionEnd(int caller_id) OVERRIDE { 148 virtual void OnRecognitionEnd(int caller_id) OVERRIDE {
141 recognition_ended_ = true; 149 recognition_ended_ = true;
142 } 150 }
143 151
144 virtual void OnRecognitionStart(int caller_id) OVERRIDE {} 152 virtual void OnRecognitionStart(int caller_id) OVERRIDE {}
145 virtual void OnEnvironmentEstimationComplete(int caller_id) OVERRIDE {} 153 virtual void OnEnvironmentEstimationComplete(int caller_id) OVERRIDE {}
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 TestAudioInputControllerFactory audio_input_controller_factory_; 193 TestAudioInputControllerFactory audio_input_controller_factory_;
186 std::vector<uint8> audio_packet_; 194 std::vector<uint8> audio_packet_;
187 float volume_; 195 float volume_;
188 float noise_volume_; 196 float noise_volume_;
189 }; 197 };
190 198
191 TEST_F(SpeechRecognizerImplTest, StopNoData) { 199 TEST_F(SpeechRecognizerImplTest, StopNoData) {
192 // Check for callbacks when stopping record before any audio gets recorded. 200 // Check for callbacks when stopping record before any audio gets recorded.
193 recognizer_->StartRecognition(); 201 recognizer_->StartRecognition();
194 recognizer_->AbortRecognition(); 202 recognizer_->AbortRecognition();
203 MessageLoop::current()->RunAllPending();
195 EXPECT_FALSE(audio_ended_); 204 EXPECT_FALSE(audio_ended_);
196 EXPECT_FALSE(recognition_ended_); 205 EXPECT_TRUE(recognition_ended_);
197 EXPECT_FALSE(result_received_); 206 EXPECT_FALSE(result_received_);
198 EXPECT_FALSE(audio_started_); 207 EXPECT_FALSE(audio_started_);
199 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 208 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
200 } 209 }
201 210
202 TEST_F(SpeechRecognizerImplTest, CancelNoData) { 211 TEST_F(SpeechRecognizerImplTest, CancelNoData) {
203 // Check for callbacks when canceling recognition before any audio gets 212 // Check for callbacks when canceling recognition before any audio gets
204 // recorded. 213 // recorded.
205 recognizer_->StartRecognition(); 214 recognizer_->StartRecognition();
206 recognizer_->StopAudioCapture(); 215 recognizer_->StopAudioCapture();
207 EXPECT_TRUE(audio_ended_); 216 MessageLoop::current()->RunAllPending();
217 EXPECT_FALSE(audio_ended_);
208 EXPECT_TRUE(recognition_ended_); 218 EXPECT_TRUE(recognition_ended_);
209 EXPECT_FALSE(result_received_); 219 EXPECT_FALSE(result_received_);
210 EXPECT_FALSE(audio_started_); 220 EXPECT_FALSE(audio_started_);
211 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 221 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
212 } 222 }
213 223
214 TEST_F(SpeechRecognizerImplTest, StopWithData) { 224 TEST_F(SpeechRecognizerImplTest, StopWithData) {
215 // Start recording, give some data and then stop. This should wait for the 225 // Start recording, give some data and then stop. This should wait for the
216 // network callback to arrive before completion. 226 // network callback to arrive before completion.
217 recognizer_->StartRecognition(); 227 recognizer_->StartRecognition();
228 MessageLoop::current()->RunAllPending();
218 TestAudioInputController* controller = 229 TestAudioInputController* controller =
219 audio_input_controller_factory_.controller(); 230 audio_input_controller_factory_.controller();
220 ASSERT_TRUE(controller); 231 ASSERT_TRUE(controller);
221 232
222 // Try sending 5 chunks of mock audio data and verify that each of them 233 // Try sending 5 chunks of mock audio data and verify that each of them
223 // resulted immediately in a packet sent out via the network. This verifies 234 // resulted immediately in a packet sent out via the network. This verifies
224 // that we are streaming out encoded data as chunks without waiting for the 235 // that we are streaming out encoded data as chunks without waiting for the
225 // full recording to complete. 236 // full recording to complete.
226 const size_t kNumChunks = 5; 237 const size_t kNumChunks = 5;
227 for (size_t i = 0; i < kNumChunks; ++i) { 238 for (size_t i = 0; i < kNumChunks; ++i) {
228 controller->event_handler()->OnData(controller, &audio_packet_[0], 239 controller->event_handler()->OnData(controller, &audio_packet_[0],
229 audio_packet_.size()); 240 audio_packet_.size());
230 MessageLoop::current()->RunAllPending(); 241 MessageLoop::current()->RunAllPending();
231 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 242 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
232 ASSERT_TRUE(fetcher); 243 ASSERT_TRUE(fetcher);
233 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); 244 EXPECT_EQ(i + 1, fetcher->upload_chunks().size());
234 } 245 }
235 246
236 recognizer_->StopAudioCapture(); 247 recognizer_->StopAudioCapture();
248 MessageLoop::current()->RunAllPending();
237 EXPECT_TRUE(audio_started_); 249 EXPECT_TRUE(audio_started_);
238 EXPECT_TRUE(audio_ended_); 250 EXPECT_TRUE(audio_ended_);
239 EXPECT_FALSE(recognition_ended_); 251 EXPECT_FALSE(recognition_ended_);
240 EXPECT_FALSE(result_received_); 252 EXPECT_FALSE(result_received_);
241 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 253 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
242 254
243 // Issue the network callback to complete the process. 255 // Issue the network callback to complete the process.
244 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 256 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
245 ASSERT_TRUE(fetcher); 257 ASSERT_TRUE(fetcher);
246 258
247 fetcher->set_url(fetcher->GetOriginalURL()); 259 fetcher->set_url(fetcher->GetOriginalURL());
248 net::URLRequestStatus status; 260 net::URLRequestStatus status;
249 status.set_status(net::URLRequestStatus::SUCCESS); 261 status.set_status(net::URLRequestStatus::SUCCESS);
250 fetcher->set_status(status); 262 fetcher->set_status(status);
251 fetcher->set_response_code(200); 263 fetcher->set_response_code(200);
252 fetcher->SetResponseString( 264 fetcher->SetResponseString(
253 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); 265 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}");
254 fetcher->delegate()->OnURLFetchComplete(fetcher); 266 fetcher->delegate()->OnURLFetchComplete(fetcher);
255 267 MessageLoop::current()->RunAllPending();
256 EXPECT_TRUE(recognition_ended_); 268 EXPECT_TRUE(recognition_ended_);
257 EXPECT_TRUE(result_received_); 269 EXPECT_TRUE(result_received_);
258 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 270 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
259 } 271 }
260 272
261 TEST_F(SpeechRecognizerImplTest, CancelWithData) { 273 TEST_F(SpeechRecognizerImplTest, CancelWithData) {
262 // Start recording, give some data and then cancel. This should create 274 // Start recording, give some data and then cancel.
263 // a network request but give no callbacks.
264 recognizer_->StartRecognition(); 275 recognizer_->StartRecognition();
276 MessageLoop::current()->RunAllPending();
265 TestAudioInputController* controller = 277 TestAudioInputController* controller =
266 audio_input_controller_factory_.controller(); 278 audio_input_controller_factory_.controller();
267 ASSERT_TRUE(controller); 279 ASSERT_TRUE(controller);
268 controller->event_handler()->OnData(controller, &audio_packet_[0], 280 controller->event_handler()->OnData(controller, &audio_packet_[0],
269 audio_packet_.size()); 281 audio_packet_.size());
270 MessageLoop::current()->RunAllPending(); 282 MessageLoop::current()->RunAllPending();
271 recognizer_->AbortRecognition(); 283 recognizer_->AbortRecognition();
284 MessageLoop::current()->RunAllPending();
272 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 285 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
273 EXPECT_TRUE(audio_started_); 286 EXPECT_TRUE(audio_started_);
274 EXPECT_FALSE(audio_ended_); 287 EXPECT_TRUE(audio_ended_);
275 EXPECT_FALSE(recognition_ended_); 288 EXPECT_TRUE(recognition_ended_);
276 EXPECT_FALSE(result_received_); 289 EXPECT_FALSE(result_received_);
277 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 290 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
278 } 291 }
279 292
280 TEST_F(SpeechRecognizerImplTest, ConnectionError) { 293 TEST_F(SpeechRecognizerImplTest, ConnectionError) {
281 // Start recording, give some data and then stop. Issue the network callback 294 // Start recording, give some data and then stop. Issue the network callback
282 // with a connection error and verify that the recognizer bubbles the error up 295 // with a connection error and verify that the recognizer bubbles the error up
283 recognizer_->StartRecognition(); 296 recognizer_->StartRecognition();
297 MessageLoop::current()->RunAllPending();
284 TestAudioInputController* controller = 298 TestAudioInputController* controller =
285 audio_input_controller_factory_.controller(); 299 audio_input_controller_factory_.controller();
286 ASSERT_TRUE(controller); 300 ASSERT_TRUE(controller);
287 controller->event_handler()->OnData(controller, &audio_packet_[0], 301 controller->event_handler()->OnData(controller, &audio_packet_[0],
288 audio_packet_.size()); 302 audio_packet_.size());
289 MessageLoop::current()->RunAllPending(); 303 MessageLoop::current()->RunAllPending();
290 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 304 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
291 ASSERT_TRUE(fetcher); 305 ASSERT_TRUE(fetcher);
292 306
293 recognizer_->StopAudioCapture(); 307 recognizer_->StopAudioCapture();
308 MessageLoop::current()->RunAllPending();
294 EXPECT_TRUE(audio_started_); 309 EXPECT_TRUE(audio_started_);
295 EXPECT_TRUE(audio_ended_); 310 EXPECT_TRUE(audio_ended_);
296 EXPECT_FALSE(recognition_ended_); 311 EXPECT_FALSE(recognition_ended_);
297 EXPECT_FALSE(result_received_); 312 EXPECT_FALSE(result_received_);
298 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 313 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
299 314
300 // Issue the network callback to complete the process. 315 // Issue the network callback to complete the process.
301 fetcher->set_url(fetcher->GetOriginalURL()); 316 fetcher->set_url(fetcher->GetOriginalURL());
302 net::URLRequestStatus status; 317 net::URLRequestStatus status;
303 status.set_status(net::URLRequestStatus::FAILED); 318 status.set_status(net::URLRequestStatus::FAILED);
304 status.set_error(net::ERR_CONNECTION_REFUSED); 319 status.set_error(net::ERR_CONNECTION_REFUSED);
305 fetcher->set_status(status); 320 fetcher->set_status(status);
306 fetcher->set_response_code(0); 321 fetcher->set_response_code(0);
307 fetcher->SetResponseString(""); 322 fetcher->SetResponseString("");
308 fetcher->delegate()->OnURLFetchComplete(fetcher); 323 fetcher->delegate()->OnURLFetchComplete(fetcher);
309 324 MessageLoop::current()->RunAllPending();
310 EXPECT_FALSE(recognition_ended_); 325 EXPECT_TRUE(recognition_ended_);
311 EXPECT_FALSE(result_received_); 326 EXPECT_FALSE(result_received_);
312 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); 327 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_);
313 } 328 }
314 329
315 TEST_F(SpeechRecognizerImplTest, ServerError) { 330 TEST_F(SpeechRecognizerImplTest, ServerError) {
316 // Start recording, give some data and then stop. Issue the network callback 331 // Start recording, give some data and then stop. Issue the network callback
317 // with a 500 error and verify that the recognizer bubbles the error up 332 // with a 500 error and verify that the recognizer bubbles the error up
318 recognizer_->StartRecognition(); 333 recognizer_->StartRecognition();
334 MessageLoop::current()->RunAllPending();
319 TestAudioInputController* controller = 335 TestAudioInputController* controller =
320 audio_input_controller_factory_.controller(); 336 audio_input_controller_factory_.controller();
321 ASSERT_TRUE(controller); 337 ASSERT_TRUE(controller);
322 controller->event_handler()->OnData(controller, &audio_packet_[0], 338 controller->event_handler()->OnData(controller, &audio_packet_[0],
323 audio_packet_.size()); 339 audio_packet_.size());
324 MessageLoop::current()->RunAllPending(); 340 MessageLoop::current()->RunAllPending();
325 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 341 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
326 ASSERT_TRUE(fetcher); 342 ASSERT_TRUE(fetcher);
327 343
328 recognizer_->StopAudioCapture(); 344 recognizer_->StopAudioCapture();
345 MessageLoop::current()->RunAllPending();
329 EXPECT_TRUE(audio_started_); 346 EXPECT_TRUE(audio_started_);
330 EXPECT_TRUE(audio_ended_); 347 EXPECT_TRUE(audio_ended_);
331 EXPECT_FALSE(recognition_ended_); 348 EXPECT_FALSE(recognition_ended_);
332 EXPECT_FALSE(result_received_); 349 EXPECT_FALSE(result_received_);
333 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 350 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
334 351
335 // Issue the network callback to complete the process. 352 // Issue the network callback to complete the process.
336 fetcher->set_url(fetcher->GetOriginalURL()); 353 fetcher->set_url(fetcher->GetOriginalURL());
337 net::URLRequestStatus status; 354 net::URLRequestStatus status;
338 status.set_status(net::URLRequestStatus::SUCCESS); 355 status.set_status(net::URLRequestStatus::SUCCESS);
339 fetcher->set_status(status); 356 fetcher->set_status(status);
340 fetcher->set_response_code(500); 357 fetcher->set_response_code(500);
341 fetcher->SetResponseString("Internal Server Error"); 358 fetcher->SetResponseString("Internal Server Error");
342 fetcher->delegate()->OnURLFetchComplete(fetcher); 359 fetcher->delegate()->OnURLFetchComplete(fetcher);
343 360 MessageLoop::current()->RunAllPending();
344 EXPECT_FALSE(recognition_ended_); 361 EXPECT_TRUE(recognition_ended_);
345 EXPECT_FALSE(result_received_); 362 EXPECT_FALSE(result_received_);
346 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); 363 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_);
347 } 364 }
348 365
349 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { 366 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) {
350 // Check if things tear down properly if AudioInputController threw an error. 367 // Check if things tear down properly if AudioInputController threw an error.
351 recognizer_->StartRecognition(); 368 recognizer_->StartRecognition();
369 MessageLoop::current()->RunAllPending();
352 TestAudioInputController* controller = 370 TestAudioInputController* controller =
353 audio_input_controller_factory_.controller(); 371 audio_input_controller_factory_.controller();
354 ASSERT_TRUE(controller); 372 ASSERT_TRUE(controller);
355 controller->event_handler()->OnError(controller, 0); 373 controller->event_handler()->OnError(controller, 0);
356 MessageLoop::current()->RunAllPending(); 374 MessageLoop::current()->RunAllPending();
357 EXPECT_FALSE(audio_started_); 375 EXPECT_FALSE(audio_started_);
358 EXPECT_FALSE(audio_ended_); 376 EXPECT_FALSE(audio_ended_);
359 EXPECT_FALSE(recognition_ended_); 377 EXPECT_TRUE(recognition_ended_);
360 EXPECT_FALSE(result_received_); 378 EXPECT_FALSE(result_received_);
361 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); 379 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_);
362 } 380 }
363 381
364 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { 382 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) {
365 // Check if things tear down properly if AudioInputController threw an error 383 // Check if things tear down properly if AudioInputController threw an error
366 // after giving some audio data. 384 // after giving some audio data.
367 recognizer_->StartRecognition(); 385 recognizer_->StartRecognition();
386 MessageLoop::current()->RunAllPending();
368 TestAudioInputController* controller = 387 TestAudioInputController* controller =
369 audio_input_controller_factory_.controller(); 388 audio_input_controller_factory_.controller();
370 ASSERT_TRUE(controller); 389 ASSERT_TRUE(controller);
371 controller->event_handler()->OnData(controller, &audio_packet_[0], 390 controller->event_handler()->OnData(controller, &audio_packet_[0],
372 audio_packet_.size()); 391 audio_packet_.size());
373 controller->event_handler()->OnError(controller, 0); 392 controller->event_handler()->OnError(controller, 0);
374 MessageLoop::current()->RunAllPending(); 393 MessageLoop::current()->RunAllPending();
375 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 394 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
376 EXPECT_TRUE(audio_started_); 395 EXPECT_TRUE(audio_started_);
377 EXPECT_FALSE(audio_ended_); 396 EXPECT_TRUE(audio_ended_);
378 EXPECT_FALSE(recognition_ended_); 397 EXPECT_TRUE(recognition_ended_);
379 EXPECT_FALSE(result_received_); 398 EXPECT_FALSE(result_received_);
380 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); 399 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_);
381 } 400 }
382 401
383 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { 402 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) {
384 // Start recording and give a lot of packets with audio samples set to zero. 403 // Start recording and give a lot of packets with audio samples set to zero.
385 // This should trigger the no-speech detector and issue a callback. 404 // This should trigger the no-speech detector and issue a callback.
386 recognizer_->StartRecognition(); 405 recognizer_->StartRecognition();
406 MessageLoop::current()->RunAllPending();
387 TestAudioInputController* controller = 407 TestAudioInputController* controller =
388 audio_input_controller_factory_.controller(); 408 audio_input_controller_factory_.controller();
389 ASSERT_TRUE(controller); 409 ASSERT_TRUE(controller);
390 controller = audio_input_controller_factory_.controller();
391 ASSERT_TRUE(controller);
392 410
393 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / 411 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
394 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; 412 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs + 1;
395 // The vector is already filled with zero value samples on create. 413 // The vector is already filled with zero value samples on create.
396 for (int i = 0; i < num_packets; ++i) { 414 for (int i = 0; i < num_packets; ++i) {
397 controller->event_handler()->OnData(controller, &audio_packet_[0], 415 controller->event_handler()->OnData(controller, &audio_packet_[0],
398 audio_packet_.size()); 416 audio_packet_.size());
399 } 417 }
400 MessageLoop::current()->RunAllPending(); 418 MessageLoop::current()->RunAllPending();
401 EXPECT_TRUE(audio_started_); 419 EXPECT_TRUE(audio_started_);
402 EXPECT_FALSE(audio_ended_); 420 EXPECT_TRUE(audio_ended_);
403 EXPECT_FALSE(recognition_ended_); 421 EXPECT_TRUE(recognition_ended_);
404 EXPECT_FALSE(result_received_); 422 EXPECT_FALSE(result_received_);
405 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); 423 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_);
406 } 424 }
407 425
408 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { 426 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) {
409 // Start recording and give a lot of packets with audio samples set to zero 427 // Start recording and give a lot of packets with audio samples set to zero
410 // and then some more with reasonably loud audio samples. This should be 428 // and then some more with reasonably loud audio samples. This should be
411 // treated as normal speech input and the no-speech detector should not get 429 // treated as normal speech input and the no-speech detector should not get
412 // triggered. 430 // triggered.
413 recognizer_->StartRecognition(); 431 recognizer_->StartRecognition();
432 MessageLoop::current()->RunAllPending();
414 TestAudioInputController* controller = 433 TestAudioInputController* controller =
415 audio_input_controller_factory_.controller(); 434 audio_input_controller_factory_.controller();
416 ASSERT_TRUE(controller); 435 ASSERT_TRUE(controller);
417 controller = audio_input_controller_factory_.controller(); 436 controller = audio_input_controller_factory_.controller();
418 ASSERT_TRUE(controller); 437 ASSERT_TRUE(controller);
419 438
420 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) / 439 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
421 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; 440 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs;
422 441
423 // The vector is already filled with zero value samples on create. 442 // The vector is already filled with zero value samples on create.
424 for (int i = 0; i < num_packets / 2; ++i) { 443 for (int i = 0; i < num_packets / 2; ++i) {
425 controller->event_handler()->OnData(controller, &audio_packet_[0], 444 controller->event_handler()->OnData(controller, &audio_packet_[0],
426 audio_packet_.size()); 445 audio_packet_.size());
427 } 446 }
428 447
429 FillPacketWithTestWaveform(); 448 FillPacketWithTestWaveform();
430 for (int i = 0; i < num_packets / 2; ++i) { 449 for (int i = 0; i < num_packets / 2; ++i) {
431 controller->event_handler()->OnData(controller, &audio_packet_[0], 450 controller->event_handler()->OnData(controller, &audio_packet_[0],
432 audio_packet_.size()); 451 audio_packet_.size());
433 } 452 }
434 453
435 MessageLoop::current()->RunAllPending(); 454 MessageLoop::current()->RunAllPending();
436 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 455 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
437 EXPECT_TRUE(audio_started_); 456 EXPECT_TRUE(audio_started_);
438 EXPECT_FALSE(audio_ended_); 457 EXPECT_FALSE(audio_ended_);
439 EXPECT_FALSE(recognition_ended_); 458 EXPECT_FALSE(recognition_ended_);
440 recognizer_->AbortRecognition(); 459 recognizer_->AbortRecognition();
460 MessageLoop::current()->RunAllPending();
441 } 461 }
442 462
443 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { 463 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) {
444 // Start recording and give a lot of packets with audio samples set to zero 464 // Start recording and give a lot of packets with audio samples set to zero
445 // and then some more with reasonably loud audio samples. Check that we don't 465 // and then some more with reasonably loud audio samples. Check that we don't
446 // get the callback during estimation phase, then get zero for the silence 466 // get the callback during estimation phase, then get zero for the silence
447 // samples and proper volume for the loud audio. 467 // samples and proper volume for the loud audio.
448 recognizer_->StartRecognition(); 468 recognizer_->StartRecognition();
469 MessageLoop::current()->RunAllPending();
449 TestAudioInputController* controller = 470 TestAudioInputController* controller =
450 audio_input_controller_factory_.controller(); 471 audio_input_controller_factory_.controller();
451 ASSERT_TRUE(controller); 472 ASSERT_TRUE(controller);
452 controller = audio_input_controller_factory_.controller(); 473 controller = audio_input_controller_factory_.controller();
453 ASSERT_TRUE(controller); 474 ASSERT_TRUE(controller);
454 475
455 // Feed some samples to begin with for the endpointer to do noise estimation. 476 // Feed some samples to begin with for the endpointer to do noise estimation.
456 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / 477 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs /
457 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs; 478 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs;
458 FillPacketWithNoise(); 479 FillPacketWithNoise();
(...skipping 14 matching lines...) Expand all
473 controller->event_handler()->OnData(controller, &audio_packet_[0], 494 controller->event_handler()->OnData(controller, &audio_packet_[0],
474 audio_packet_.size()); 495 audio_packet_.size());
475 MessageLoop::current()->RunAllPending(); 496 MessageLoop::current()->RunAllPending();
476 EXPECT_FLOAT_EQ(0.89926866f, volume_); 497 EXPECT_FLOAT_EQ(0.89926866f, volume_);
477 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); 498 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_);
478 499
479 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); 500 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_);
480 EXPECT_FALSE(audio_ended_); 501 EXPECT_FALSE(audio_ended_);
481 EXPECT_FALSE(recognition_ended_); 502 EXPECT_FALSE(recognition_ended_);
482 recognizer_->AbortRecognition(); 503 recognizer_->AbortRecognition();
504 MessageLoop::current()->RunAllPending();
483 } 505 }
484 506
485 } // namespace speech 507 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698