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

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

Powered by Google App Engine
This is Rietveld 408576698