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

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

Issue 8137005: Applying changes to the existing speech input code to support the extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review and unit test fixes. Created 9 years, 2 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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.h" 7 #include "content/browser/browser_thread.h"
8 #include "content/browser/speech/speech_recognizer.h" 8 #include "content/browser/speech/speech_recognizer.h"
9 #include "content/test/test_url_fetcher_factory.h" 9 #include "content/test/test_url_fetcher_factory.h"
10 #include "media/audio/test_audio_input_controller_factory.h" 10 #include "media/audio/test_audio_input_controller_factory.h"
(...skipping 13 matching lines...) Expand all
24 SpeechRecognizerTest() 24 SpeechRecognizerTest()
25 : io_thread_(BrowserThread::IO, &message_loop_), 25 : io_thread_(BrowserThread::IO, &message_loop_),
26 ALLOW_THIS_IN_INITIALIZER_LIST( 26 ALLOW_THIS_IN_INITIALIZER_LIST(
27 recognizer_(new SpeechRecognizer(this, 1, std::string(), 27 recognizer_(new SpeechRecognizer(this, 1, std::string(),
28 std::string(), NULL, false, 28 std::string(), NULL, false,
29 std::string(), std::string()))), 29 std::string(), std::string()))),
30 recording_complete_(false), 30 recording_complete_(false),
31 recognition_complete_(false), 31 recognition_complete_(false),
32 result_received_(false), 32 result_received_(false),
33 audio_received_(false), 33 audio_received_(false),
34 error_(SpeechRecognizer::RECOGNIZER_NO_ERROR), 34 error_(kErrorNone),
35 volume_(-1.0f) { 35 volume_(-1.0f) {
36 int audio_packet_length_bytes = 36 int audio_packet_length_bytes =
37 (SpeechRecognizer::kAudioSampleRate * 37 (SpeechRecognizer::kAudioSampleRate *
38 SpeechRecognizer::kAudioPacketIntervalMs * 38 SpeechRecognizer::kAudioPacketIntervalMs *
39 ChannelLayoutToChannelCount(SpeechRecognizer::kChannelLayout) * 39 ChannelLayoutToChannelCount(SpeechRecognizer::kChannelLayout) *
40 SpeechRecognizer::kNumBitsPerAudioSample) / (8 * 1000); 40 SpeechRecognizer::kNumBitsPerAudioSample) / (8 * 1000);
41 audio_packet_.resize(audio_packet_length_bytes); 41 audio_packet_.resize(audio_packet_length_bytes);
42 } 42 }
43 43
44 // SpeechRecognizer::Delegate methods. 44 // SpeechRecognizer::Delegate methods.
45 virtual void SetRecognitionResult(int caller_id, 45 virtual void SetRecognitionResult(int caller_id,
46 bool error, 46 bool error,
47 const SpeechInputResultArray& result) { 47 const SpeechInputResult& result) {
Satish 2011/10/06 09:09:06 use OVERRIDE for all these virtuals
Leandro GraciĆ” Gil 2011/10/06 18:26:25 Done.
48 result_received_ = true; 48 result_received_ = true;
49 } 49 }
50 50
51 virtual void DidCompleteRecording(int caller_id) { 51 virtual void DidCompleteRecording(int caller_id) {
52 recording_complete_ = true; 52 recording_complete_ = true;
53 } 53 }
54 54
55 virtual void DidCompleteRecognition(int caller_id) { 55 virtual void DidCompleteRecognition(int caller_id) {
56 recognition_complete_ = true; 56 recognition_complete_ = true;
57 } 57 }
58 58
59 virtual void DidCompleteEnvironmentEstimation(int caller_id) { 59 virtual void DidCompleteEnvironmentEstimation(int caller_id) {
60 } 60 }
61 61
62 virtual void DidStartReceivingAudio(int caller_id) { 62 virtual void DidStartReceivingAudio(int caller_id) {
63 audio_received_ = true; 63 audio_received_ = true;
64 } 64 }
65 65
66 virtual void DidStartReceivingSpeech(int caller_id) {
67 }
68
69 virtual void DidStopReceivingSpeech(int caller_id) {
70 }
71
66 virtual void OnRecognizerError(int caller_id, 72 virtual void OnRecognizerError(int caller_id,
67 SpeechRecognizer::ErrorCode error) { 73 SpeechInputError error) {
68 error_ = error; 74 error_ = error;
69 } 75 }
70 76
71 virtual void SetInputVolume(int caller_id, float volume, float noise_volume) { 77 virtual void SetInputVolume(int caller_id, float volume, float noise_volume) {
72 volume_ = volume; 78 volume_ = volume;
73 noise_volume_ = noise_volume; 79 noise_volume_ = noise_volume;
74 } 80 }
75 81
76 // testing::Test methods. 82 // testing::Test methods.
77 virtual void SetUp() { 83 virtual void SetUp() {
(...skipping 21 matching lines...) Expand all
99 } 105 }
100 106
101 protected: 107 protected:
102 MessageLoopForIO message_loop_; 108 MessageLoopForIO message_loop_;
103 BrowserThread io_thread_; 109 BrowserThread io_thread_;
104 scoped_refptr<SpeechRecognizer> recognizer_; 110 scoped_refptr<SpeechRecognizer> recognizer_;
105 bool recording_complete_; 111 bool recording_complete_;
106 bool recognition_complete_; 112 bool recognition_complete_;
107 bool result_received_; 113 bool result_received_;
108 bool audio_received_; 114 bool audio_received_;
109 SpeechRecognizer::ErrorCode error_; 115 SpeechInputError error_;
110 TestURLFetcherFactory url_fetcher_factory_; 116 TestURLFetcherFactory url_fetcher_factory_;
111 TestAudioInputControllerFactory audio_input_controller_factory_; 117 TestAudioInputControllerFactory audio_input_controller_factory_;
112 std::vector<uint8> audio_packet_; 118 std::vector<uint8> audio_packet_;
113 float volume_; 119 float volume_;
114 float noise_volume_; 120 float noise_volume_;
115 }; 121 };
116 122
117 TEST_F(SpeechRecognizerTest, StopNoData) { 123 TEST_F(SpeechRecognizerTest, StopNoData) {
118 // Check for callbacks when stopping record before any audio gets recorded. 124 // Check for callbacks when stopping record before any audio gets recorded.
119 EXPECT_TRUE(recognizer_->StartRecording()); 125 EXPECT_TRUE(recognizer_->StartRecording());
120 recognizer_->CancelRecognition(); 126 recognizer_->CancelRecognition();
121 EXPECT_FALSE(recording_complete_); 127 EXPECT_FALSE(recording_complete_);
122 EXPECT_FALSE(recognition_complete_); 128 EXPECT_FALSE(recognition_complete_);
123 EXPECT_FALSE(result_received_); 129 EXPECT_FALSE(result_received_);
124 EXPECT_FALSE(audio_received_); 130 EXPECT_FALSE(audio_received_);
125 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 131 EXPECT_EQ(kErrorNone, error_);
126 } 132 }
127 133
128 TEST_F(SpeechRecognizerTest, CancelNoData) { 134 TEST_F(SpeechRecognizerTest, CancelNoData) {
129 // Check for callbacks when canceling recognition before any audio gets 135 // Check for callbacks when canceling recognition before any audio gets
130 // recorded. 136 // recorded.
131 EXPECT_TRUE(recognizer_->StartRecording()); 137 EXPECT_TRUE(recognizer_->StartRecording());
132 recognizer_->StopRecording(); 138 recognizer_->StopRecording();
133 EXPECT_TRUE(recording_complete_); 139 EXPECT_TRUE(recording_complete_);
134 EXPECT_TRUE(recognition_complete_); 140 EXPECT_TRUE(recognition_complete_);
135 EXPECT_FALSE(result_received_); 141 EXPECT_FALSE(result_received_);
136 EXPECT_FALSE(audio_received_); 142 EXPECT_FALSE(audio_received_);
137 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 143 EXPECT_EQ(kErrorNone, error_);
138 } 144 }
139 145
140 TEST_F(SpeechRecognizerTest, StopWithData) { 146 TEST_F(SpeechRecognizerTest, StopWithData) {
141 // Start recording, give some data and then stop. This should wait for the 147 // Start recording, give some data and then stop. This should wait for the
142 // network callback to arrive before completion. 148 // network callback to arrive before completion.
143 EXPECT_TRUE(recognizer_->StartRecording()); 149 EXPECT_TRUE(recognizer_->StartRecording());
144 TestAudioInputController* controller = 150 TestAudioInputController* controller =
145 audio_input_controller_factory_.controller(); 151 audio_input_controller_factory_.controller();
146 ASSERT_TRUE(controller); 152 ASSERT_TRUE(controller);
147 153
148 // Try sending 5 chunks of mock audio data and verify that each of them 154 // Try sending 5 chunks of mock audio data and verify that each of them
149 // resulted immediately in a packet sent out via the network. This verifies 155 // resulted immediately in a packet sent out via the network. This verifies
150 // that we are streaming out encoded data as chunks without waiting for the 156 // that we are streaming out encoded data as chunks without waiting for the
151 // full recording to complete. 157 // full recording to complete.
152 const size_t kNumChunks = 5; 158 const size_t kNumChunks = 5;
153 for (size_t i = 0; i < kNumChunks; ++i) { 159 for (size_t i = 0; i < kNumChunks; ++i) {
154 controller->event_handler()->OnData(controller, &audio_packet_[0], 160 controller->event_handler()->OnData(controller, &audio_packet_[0],
155 audio_packet_.size()); 161 audio_packet_.size());
156 MessageLoop::current()->RunAllPending(); 162 MessageLoop::current()->RunAllPending();
157 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 163 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
158 ASSERT_TRUE(fetcher); 164 ASSERT_TRUE(fetcher);
159 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); 165 EXPECT_EQ(i + 1, fetcher->upload_chunks().size());
160 } 166 }
161 167
162 recognizer_->StopRecording(); 168 recognizer_->StopRecording();
163 EXPECT_TRUE(audio_received_); 169 EXPECT_TRUE(audio_received_);
164 EXPECT_TRUE(recording_complete_); 170 EXPECT_TRUE(recording_complete_);
165 EXPECT_FALSE(recognition_complete_); 171 EXPECT_FALSE(recognition_complete_);
166 EXPECT_FALSE(result_received_); 172 EXPECT_FALSE(result_received_);
167 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 173 EXPECT_EQ(kErrorNone, error_);
168 174
169 // Issue the network callback to complete the process. 175 // Issue the network callback to complete the process.
170 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 176 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
171 ASSERT_TRUE(fetcher); 177 ASSERT_TRUE(fetcher);
172 178
173 fetcher->set_url(fetcher->original_url()); 179 fetcher->set_url(fetcher->original_url());
174 net::URLRequestStatus status; 180 net::URLRequestStatus status;
175 status.set_status(net::URLRequestStatus::SUCCESS); 181 status.set_status(net::URLRequestStatus::SUCCESS);
176 fetcher->set_status(status); 182 fetcher->set_status(status);
177 fetcher->set_response_code(200); 183 fetcher->set_response_code(200);
178 fetcher->SetResponseString("{\"hypotheses\":[{\"utterance\":\"123\"}]}"); 184 fetcher->SetResponseString(
185 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}");
179 fetcher->delegate()->OnURLFetchComplete(fetcher); 186 fetcher->delegate()->OnURLFetchComplete(fetcher);
180 187
181 EXPECT_TRUE(recognition_complete_); 188 EXPECT_TRUE(recognition_complete_);
182 EXPECT_TRUE(result_received_); 189 EXPECT_TRUE(result_received_);
183 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 190 EXPECT_EQ(kErrorNone, error_);
184 } 191 }
185 192
186 TEST_F(SpeechRecognizerTest, CancelWithData) { 193 TEST_F(SpeechRecognizerTest, CancelWithData) {
187 // Start recording, give some data and then cancel. This should create 194 // Start recording, give some data and then cancel. This should create
188 // a network request but give no callbacks. 195 // a network request but give no callbacks.
189 EXPECT_TRUE(recognizer_->StartRecording()); 196 EXPECT_TRUE(recognizer_->StartRecording());
190 TestAudioInputController* controller = 197 TestAudioInputController* controller =
191 audio_input_controller_factory_.controller(); 198 audio_input_controller_factory_.controller();
192 ASSERT_TRUE(controller); 199 ASSERT_TRUE(controller);
193 controller->event_handler()->OnData(controller, &audio_packet_[0], 200 controller->event_handler()->OnData(controller, &audio_packet_[0],
194 audio_packet_.size()); 201 audio_packet_.size());
195 MessageLoop::current()->RunAllPending(); 202 MessageLoop::current()->RunAllPending();
196 recognizer_->CancelRecognition(); 203 recognizer_->CancelRecognition();
197 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 204 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
198 EXPECT_TRUE(audio_received_); 205 EXPECT_TRUE(audio_received_);
199 EXPECT_FALSE(recording_complete_); 206 EXPECT_FALSE(recording_complete_);
200 EXPECT_FALSE(recognition_complete_); 207 EXPECT_FALSE(recognition_complete_);
201 EXPECT_FALSE(result_received_); 208 EXPECT_FALSE(result_received_);
202 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 209 EXPECT_EQ(kErrorNone, error_);
203 } 210 }
204 211
205 TEST_F(SpeechRecognizerTest, ConnectionError) { 212 TEST_F(SpeechRecognizerTest, ConnectionError) {
206 // Start recording, give some data and then stop. Issue the network callback 213 // Start recording, give some data and then stop. Issue the network callback
207 // with a connection error and verify that the recognizer bubbles the error up 214 // with a connection error and verify that the recognizer bubbles the error up
208 EXPECT_TRUE(recognizer_->StartRecording()); 215 EXPECT_TRUE(recognizer_->StartRecording());
209 TestAudioInputController* controller = 216 TestAudioInputController* controller =
210 audio_input_controller_factory_.controller(); 217 audio_input_controller_factory_.controller();
211 ASSERT_TRUE(controller); 218 ASSERT_TRUE(controller);
212 controller->event_handler()->OnData(controller, &audio_packet_[0], 219 controller->event_handler()->OnData(controller, &audio_packet_[0],
213 audio_packet_.size()); 220 audio_packet_.size());
214 MessageLoop::current()->RunAllPending(); 221 MessageLoop::current()->RunAllPending();
215 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 222 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
216 ASSERT_TRUE(fetcher); 223 ASSERT_TRUE(fetcher);
217 224
218 recognizer_->StopRecording(); 225 recognizer_->StopRecording();
219 EXPECT_TRUE(audio_received_); 226 EXPECT_TRUE(audio_received_);
220 EXPECT_TRUE(recording_complete_); 227 EXPECT_TRUE(recording_complete_);
221 EXPECT_FALSE(recognition_complete_); 228 EXPECT_FALSE(recognition_complete_);
222 EXPECT_FALSE(result_received_); 229 EXPECT_FALSE(result_received_);
223 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 230 EXPECT_EQ(kErrorNone, error_);
224 231
225 // Issue the network callback to complete the process. 232 // Issue the network callback to complete the process.
226 fetcher->set_url(fetcher->original_url()); 233 fetcher->set_url(fetcher->original_url());
227 net::URLRequestStatus status; 234 net::URLRequestStatus status;
228 status.set_status(net::URLRequestStatus::FAILED); 235 status.set_status(net::URLRequestStatus::FAILED);
229 status.set_error(net::ERR_CONNECTION_REFUSED); 236 status.set_error(net::ERR_CONNECTION_REFUSED);
230 fetcher->set_status(status); 237 fetcher->set_status(status);
231 fetcher->set_response_code(0); 238 fetcher->set_response_code(0);
232 fetcher->SetResponseString(""); 239 fetcher->SetResponseString("");
233 fetcher->delegate()->OnURLFetchComplete(fetcher); 240 fetcher->delegate()->OnURLFetchComplete(fetcher);
234 241
235 EXPECT_FALSE(recognition_complete_); 242 EXPECT_FALSE(recognition_complete_);
236 EXPECT_FALSE(result_received_); 243 EXPECT_FALSE(result_received_);
237 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_NETWORK, error_); 244 EXPECT_EQ(kErrorNetwork, error_);
238 } 245 }
239 246
240 TEST_F(SpeechRecognizerTest, ServerError) { 247 TEST_F(SpeechRecognizerTest, ServerError) {
241 // Start recording, give some data and then stop. Issue the network callback 248 // Start recording, give some data and then stop. Issue the network callback
242 // with a 500 error and verify that the recognizer bubbles the error up 249 // with a 500 error and verify that the recognizer bubbles the error up
243 EXPECT_TRUE(recognizer_->StartRecording()); 250 EXPECT_TRUE(recognizer_->StartRecording());
244 TestAudioInputController* controller = 251 TestAudioInputController* controller =
245 audio_input_controller_factory_.controller(); 252 audio_input_controller_factory_.controller();
246 ASSERT_TRUE(controller); 253 ASSERT_TRUE(controller);
247 controller->event_handler()->OnData(controller, &audio_packet_[0], 254 controller->event_handler()->OnData(controller, &audio_packet_[0],
248 audio_packet_.size()); 255 audio_packet_.size());
249 MessageLoop::current()->RunAllPending(); 256 MessageLoop::current()->RunAllPending();
250 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 257 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
251 ASSERT_TRUE(fetcher); 258 ASSERT_TRUE(fetcher);
252 259
253 recognizer_->StopRecording(); 260 recognizer_->StopRecording();
254 EXPECT_TRUE(audio_received_); 261 EXPECT_TRUE(audio_received_);
255 EXPECT_TRUE(recording_complete_); 262 EXPECT_TRUE(recording_complete_);
256 EXPECT_FALSE(recognition_complete_); 263 EXPECT_FALSE(recognition_complete_);
257 EXPECT_FALSE(result_received_); 264 EXPECT_FALSE(result_received_);
258 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 265 EXPECT_EQ(kErrorNone, error_);
259 266
260 // Issue the network callback to complete the process. 267 // Issue the network callback to complete the process.
261 fetcher->set_url(fetcher->original_url()); 268 fetcher->set_url(fetcher->original_url());
262 net::URLRequestStatus status; 269 net::URLRequestStatus status;
263 status.set_status(net::URLRequestStatus::SUCCESS); 270 status.set_status(net::URLRequestStatus::SUCCESS);
264 fetcher->set_status(status); 271 fetcher->set_status(status);
265 fetcher->set_response_code(500); 272 fetcher->set_response_code(500);
266 fetcher->SetResponseString("Internal Server Error"); 273 fetcher->SetResponseString("Internal Server Error");
267 fetcher->delegate()->OnURLFetchComplete(fetcher); 274 fetcher->delegate()->OnURLFetchComplete(fetcher);
268 275
269 EXPECT_FALSE(recognition_complete_); 276 EXPECT_FALSE(recognition_complete_);
270 EXPECT_FALSE(result_received_); 277 EXPECT_FALSE(result_received_);
271 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_NETWORK, error_); 278 EXPECT_EQ(kErrorNetwork, error_);
272 } 279 }
273 280
274 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { 281 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) {
275 // Check if things tear down properly if AudioInputController threw an error. 282 // Check if things tear down properly if AudioInputController threw an error.
276 EXPECT_TRUE(recognizer_->StartRecording()); 283 EXPECT_TRUE(recognizer_->StartRecording());
277 TestAudioInputController* controller = 284 TestAudioInputController* controller =
278 audio_input_controller_factory_.controller(); 285 audio_input_controller_factory_.controller();
279 ASSERT_TRUE(controller); 286 ASSERT_TRUE(controller);
280 controller->event_handler()->OnError(controller, 0); 287 controller->event_handler()->OnError(controller, 0);
281 MessageLoop::current()->RunAllPending(); 288 MessageLoop::current()->RunAllPending();
282 EXPECT_FALSE(audio_received_); 289 EXPECT_FALSE(audio_received_);
283 EXPECT_FALSE(recording_complete_); 290 EXPECT_FALSE(recording_complete_);
284 EXPECT_FALSE(recognition_complete_); 291 EXPECT_FALSE(recognition_complete_);
285 EXPECT_FALSE(result_received_); 292 EXPECT_FALSE(result_received_);
286 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_); 293 EXPECT_EQ(kErrorAudio, error_);
287 } 294 }
288 295
289 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { 296 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) {
290 // Check if things tear down properly if AudioInputController threw an error 297 // Check if things tear down properly if AudioInputController threw an error
291 // after giving some audio data. 298 // after giving some audio data.
292 EXPECT_TRUE(recognizer_->StartRecording()); 299 EXPECT_TRUE(recognizer_->StartRecording());
293 TestAudioInputController* controller = 300 TestAudioInputController* controller =
294 audio_input_controller_factory_.controller(); 301 audio_input_controller_factory_.controller();
295 ASSERT_TRUE(controller); 302 ASSERT_TRUE(controller);
296 controller->event_handler()->OnData(controller, &audio_packet_[0], 303 controller->event_handler()->OnData(controller, &audio_packet_[0],
297 audio_packet_.size()); 304 audio_packet_.size());
298 controller->event_handler()->OnError(controller, 0); 305 controller->event_handler()->OnError(controller, 0);
299 MessageLoop::current()->RunAllPending(); 306 MessageLoop::current()->RunAllPending();
300 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); 307 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
301 EXPECT_TRUE(audio_received_); 308 EXPECT_TRUE(audio_received_);
302 EXPECT_FALSE(recording_complete_); 309 EXPECT_FALSE(recording_complete_);
303 EXPECT_FALSE(recognition_complete_); 310 EXPECT_FALSE(recognition_complete_);
304 EXPECT_FALSE(result_received_); 311 EXPECT_FALSE(result_received_);
305 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_); 312 EXPECT_EQ(kErrorAudio, error_);
306 } 313 }
307 314
308 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { 315 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) {
309 // Start recording and give a lot of packets with audio samples set to zero. 316 // Start recording and give a lot of packets with audio samples set to zero.
310 // This should trigger the no-speech detector and issue a callback. 317 // This should trigger the no-speech detector and issue a callback.
311 EXPECT_TRUE(recognizer_->StartRecording()); 318 EXPECT_TRUE(recognizer_->StartRecording());
312 TestAudioInputController* controller = 319 TestAudioInputController* controller =
313 audio_input_controller_factory_.controller(); 320 audio_input_controller_factory_.controller();
314 ASSERT_TRUE(controller); 321 ASSERT_TRUE(controller);
315 controller = audio_input_controller_factory_.controller(); 322 controller = audio_input_controller_factory_.controller();
316 ASSERT_TRUE(controller); 323 ASSERT_TRUE(controller);
317 324
318 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutSec * 1000) / 325 int num_packets = (SpeechRecognizer::kNoSpeechTimeoutSec * 1000) /
319 SpeechRecognizer::kAudioPacketIntervalMs; 326 SpeechRecognizer::kAudioPacketIntervalMs;
320 // The vector is already filled with zero value samples on create. 327 // The vector is already filled with zero value samples on create.
321 for (int i = 0; i < num_packets; ++i) { 328 for (int i = 0; i < num_packets; ++i) {
322 controller->event_handler()->OnData(controller, &audio_packet_[0], 329 controller->event_handler()->OnData(controller, &audio_packet_[0],
323 audio_packet_.size()); 330 audio_packet_.size());
324 } 331 }
325 MessageLoop::current()->RunAllPending(); 332 MessageLoop::current()->RunAllPending();
326 EXPECT_TRUE(audio_received_); 333 EXPECT_TRUE(audio_received_);
327 EXPECT_FALSE(recording_complete_); 334 EXPECT_FALSE(recording_complete_);
328 EXPECT_FALSE(recognition_complete_); 335 EXPECT_FALSE(recognition_complete_);
329 EXPECT_FALSE(result_received_); 336 EXPECT_FALSE(result_received_);
330 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_NO_SPEECH, error_); 337 EXPECT_EQ(kErrorNoSpeech, error_);
331 } 338 }
332 339
333 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { 340 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) {
334 // Start recording and give a lot of packets with audio samples set to zero 341 // Start recording and give a lot of packets with audio samples set to zero
335 // and then some more with reasonably loud audio samples. This should be 342 // and then some more with reasonably loud audio samples. This should be
336 // treated as normal speech input and the no-speech detector should not get 343 // treated as normal speech input and the no-speech detector should not get
337 // triggered. 344 // triggered.
338 EXPECT_TRUE(recognizer_->StartRecording()); 345 EXPECT_TRUE(recognizer_->StartRecording());
339 TestAudioInputController* controller = 346 TestAudioInputController* controller =
340 audio_input_controller_factory_.controller(); 347 audio_input_controller_factory_.controller();
(...skipping 10 matching lines...) Expand all
351 audio_packet_.size()); 358 audio_packet_.size());
352 } 359 }
353 360
354 FillPacketWithTestWaveform(); 361 FillPacketWithTestWaveform();
355 for (int i = 0; i < num_packets / 2; ++i) { 362 for (int i = 0; i < num_packets / 2; ++i) {
356 controller->event_handler()->OnData(controller, &audio_packet_[0], 363 controller->event_handler()->OnData(controller, &audio_packet_[0],
357 audio_packet_.size()); 364 audio_packet_.size());
358 } 365 }
359 366
360 MessageLoop::current()->RunAllPending(); 367 MessageLoop::current()->RunAllPending();
361 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 368 EXPECT_EQ(kErrorNone, error_);
362 EXPECT_TRUE(audio_received_); 369 EXPECT_TRUE(audio_received_);
363 EXPECT_FALSE(recording_complete_); 370 EXPECT_FALSE(recording_complete_);
364 EXPECT_FALSE(recognition_complete_); 371 EXPECT_FALSE(recognition_complete_);
365 recognizer_->CancelRecognition(); 372 recognizer_->CancelRecognition();
366 } 373 }
367 374
368 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { 375 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) {
369 // Start recording and give a lot of packets with audio samples set to zero 376 // Start recording and give a lot of packets with audio samples set to zero
370 // and then some more with reasonably loud audio samples. Check that we don't 377 // and then some more with reasonably loud audio samples. Check that we don't
371 // get the callback during estimation phase, then get zero for the silence 378 // get the callback during estimation phase, then get zero for the silence
(...skipping 22 matching lines...) Expand all
394 MessageLoop::current()->RunAllPending(); 401 MessageLoop::current()->RunAllPending();
395 EXPECT_FLOAT_EQ(0.74939233f, volume_); 402 EXPECT_FLOAT_EQ(0.74939233f, volume_);
396 403
397 FillPacketWithTestWaveform(); 404 FillPacketWithTestWaveform();
398 controller->event_handler()->OnData(controller, &audio_packet_[0], 405 controller->event_handler()->OnData(controller, &audio_packet_[0],
399 audio_packet_.size()); 406 audio_packet_.size());
400 MessageLoop::current()->RunAllPending(); 407 MessageLoop::current()->RunAllPending();
401 EXPECT_FLOAT_EQ(0.89926866f, volume_); 408 EXPECT_FLOAT_EQ(0.89926866f, volume_);
402 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); 409 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_);
403 410
404 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); 411 EXPECT_EQ(kErrorNone, error_);
405 EXPECT_FALSE(recording_complete_); 412 EXPECT_FALSE(recording_complete_);
406 EXPECT_FALSE(recognition_complete_); 413 EXPECT_FALSE(recognition_complete_);
407 recognizer_->CancelRecognition(); 414 recognizer_->CancelRecognition();
408 } 415 }
409 416
410 } // namespace speech_input 417 } // namespace speech_input
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698