OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "content/browser/browser_thread_impl.h" | 7 #include "content/browser/browser_thread_impl.h" |
8 #include "content/browser/speech/speech_recognizer_impl.h" | 8 #include "content/browser/speech/speech_recognizer_impl.h" |
9 #include "content/public/browser/speech_recognizer_delegate.h" | 9 #include "content/public/browser/speech_recognition_event_listener.h" |
10 #include "content/test/test_url_fetcher_factory.h" | 10 #include "content/test/test_url_fetcher_factory.h" |
11 #include "media/audio/audio_manager.h" | 11 #include "media/audio/audio_manager.h" |
12 #include "media/audio/fake_audio_input_stream.h" | 12 #include "media/audio/fake_audio_input_stream.h" |
13 #include "media/audio/fake_audio_output_stream.h" | 13 #include "media/audio/fake_audio_output_stream.h" |
14 #include "media/audio/test_audio_input_controller_factory.h" | 14 #include "media/audio/test_audio_input_controller_factory.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using content::BrowserThread; | 19 using content::BrowserThread; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 virtual void Init() OVERRIDE {}; | 59 virtual void Init() OVERRIDE {}; |
60 private: | 60 private: |
61 scoped_ptr<base::Thread> audio_thread_; | 61 scoped_ptr<base::Thread> audio_thread_; |
62 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); | 62 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); |
63 }; | 63 }; |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 | 66 |
67 namespace speech { | 67 namespace speech { |
68 | 68 |
69 class SpeechRecognizerTest : public content::SpeechRecognizerDelegate, | 69 class SpeechRecognizerImplTest : public content::SpeechRecognitionEventListener, |
70 public testing::Test { | 70 public testing::Test { |
71 public: | 71 public: |
72 SpeechRecognizerTest() | 72 SpeechRecognizerImplTest() |
73 : io_thread_(BrowserThread::IO, &message_loop_), | 73 : io_thread_(BrowserThread::IO, &message_loop_), |
74 audio_manager_(new MockAudioManager()), | 74 audio_manager_(new MockAudioManager()), |
75 recording_complete_(false), | 75 audio_closed_(false), |
76 recognition_complete_(false), | 76 recognition_ended_(false), |
77 result_received_(false), | 77 result_received_(false), |
78 audio_received_(false), | 78 audio_started_(false), |
79 error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 79 error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
80 volume_(-1.0f) { | 80 volume_(-1.0f) { |
81 recognizer_ = new SpeechRecognizerImpl( | 81 recognizer_ = new SpeechRecognizerImpl( |
82 this, 1, std::string(), std::string(), NULL, false, std::string(), | 82 this, 1, std::string(), std::string(), NULL, false, std::string(), |
83 std::string()); | 83 std::string()); |
84 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); | 84 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); |
85 int audio_packet_length_bytes = | 85 int audio_packet_length_bytes = |
86 (SpeechRecognizerImpl::kAudioSampleRate * | 86 (SpeechRecognizerImpl::kAudioSampleRate * |
87 SpeechRecognizerImpl::kAudioPacketIntervalMs * | 87 SpeechRecognizerImpl::kAudioPacketIntervalMs * |
88 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * | 88 ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) * |
89 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); | 89 SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000); |
90 audio_packet_.resize(audio_packet_length_bytes); | 90 audio_packet_.resize(audio_packet_length_bytes); |
91 } | 91 } |
92 | 92 |
93 // Overridden from content::SpeechRecognizerDelegate: | 93 // Overridden from content::SpeechRecognitionEventListener: |
94 virtual void SetRecognitionResult( | 94 virtual void OnAudioStart(int session_id) OVERRIDE { |
95 int caller_id, | 95 audio_started_ = true; |
96 const content::SpeechRecognitionResult& result) OVERRIDE { | 96 } |
97 | |
98 virtual void OnAudioEnd(int session_id) OVERRIDE { | |
99 audio_closed_ = true; | |
Satish
2012/03/13 15:30:15
use audio_ended_ to match audio_started_ above?
Primiano Tucci (use gerrit)
2012/03/14 09:40:35
Right.
| |
100 } | |
101 | |
102 virtual void OnRecognitionResult( | |
103 int session_id, const content::SpeechRecognitionResult& result) OVERRIDE { | |
97 result_received_ = true; | 104 result_received_ = true; |
98 } | 105 } |
99 | 106 |
100 virtual void DidCompleteRecording(int caller_id) OVERRIDE { | 107 virtual void OnRecognitionError( |
101 recording_complete_ = true; | 108 int session_id, |
102 } | 109 const content::SpeechRecognitionErrorCode& error) OVERRIDE { |
103 | |
104 virtual void DidCompleteRecognition(int caller_id) OVERRIDE { | |
105 recognition_complete_ = true; | |
106 } | |
107 | |
108 virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE { | |
109 } | |
110 | |
111 virtual void DidStartReceivingAudio(int caller_id) OVERRIDE { | |
112 audio_received_ = true; | |
113 } | |
114 | |
115 virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE { | |
116 } | |
117 | |
118 virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE { | |
119 } | |
120 | |
121 virtual void OnRecognizerError( | |
122 int caller_id, content::SpeechRecognitionErrorCode error) OVERRIDE { | |
123 error_ = error; | 110 error_ = error; |
124 } | 111 } |
125 | 112 |
126 virtual void SetInputVolume(int caller_id, float volume, | 113 virtual void OnAudioLevelsChanged(int session_id, float volume, |
127 float noise_volume) OVERRIDE { | 114 float noise_volume) OVERRIDE { |
128 volume_ = volume; | 115 volume_ = volume; |
129 noise_volume_ = noise_volume; | 116 noise_volume_ = noise_volume; |
130 } | 117 } |
131 | 118 |
119 virtual void OnRecognitionEnd(int session_id, bool success) OVERRIDE { | |
120 recognition_ended_ = true; | |
121 } | |
122 | |
123 virtual void OnRecognitionStart(int session_id) OVERRIDE {} | |
124 virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE {} | |
125 virtual void OnSoundStart(int session_id) OVERRIDE {} | |
126 virtual void OnSoundEnd(int session_id) OVERRIDE {} | |
127 | |
Satish
2012/03/13 15:30:15
remove extra newline
Primiano Tucci (use gerrit)
2012/03/14 09:40:35
Done.
| |
128 | |
132 // testing::Test methods. | 129 // testing::Test methods. |
133 virtual void SetUp() OVERRIDE { | 130 virtual void SetUp() OVERRIDE { |
134 AudioInputController::set_factory_for_testing( | 131 AudioInputController::set_factory_for_testing( |
135 &audio_input_controller_factory_); | 132 &audio_input_controller_factory_); |
136 } | 133 } |
137 | 134 |
138 virtual void TearDown() OVERRIDE { | 135 virtual void TearDown() OVERRIDE { |
139 AudioInputController::set_factory_for_testing(NULL); | 136 AudioInputController::set_factory_for_testing(NULL); |
140 } | 137 } |
141 | 138 |
(...skipping 10 matching lines...) Expand all Loading... | |
152 value += factor; | 149 value += factor; |
153 audio_packet_[i] = value % 100; | 150 audio_packet_[i] = value % 100; |
154 } | 151 } |
155 } | 152 } |
156 | 153 |
157 protected: | 154 protected: |
158 MessageLoopForIO message_loop_; | 155 MessageLoopForIO message_loop_; |
159 BrowserThreadImpl io_thread_; | 156 BrowserThreadImpl io_thread_; |
160 scoped_refptr<SpeechRecognizerImpl> recognizer_; | 157 scoped_refptr<SpeechRecognizerImpl> recognizer_; |
161 scoped_ptr<AudioManager> audio_manager_; | 158 scoped_ptr<AudioManager> audio_manager_; |
162 bool recording_complete_; | 159 bool audio_closed_; |
163 bool recognition_complete_; | 160 bool recognition_ended_; |
164 bool result_received_; | 161 bool result_received_; |
165 bool audio_received_; | 162 bool audio_started_; |
166 content::SpeechRecognitionErrorCode error_; | 163 content::SpeechRecognitionErrorCode error_; |
167 TestURLFetcherFactory url_fetcher_factory_; | 164 TestURLFetcherFactory url_fetcher_factory_; |
168 TestAudioInputControllerFactory audio_input_controller_factory_; | 165 TestAudioInputControllerFactory audio_input_controller_factory_; |
169 std::vector<uint8> audio_packet_; | 166 std::vector<uint8> audio_packet_; |
170 float volume_; | 167 float volume_; |
171 float noise_volume_; | 168 float noise_volume_; |
172 }; | 169 }; |
173 | 170 |
174 TEST_F(SpeechRecognizerTest, StopNoData) { | 171 TEST_F(SpeechRecognizerImplTest, StopNoData) { |
175 // Check for callbacks when stopping record before any audio gets recorded. | 172 // Check for callbacks when stopping record before any audio gets recorded. |
176 EXPECT_TRUE(recognizer_->StartRecording()); | 173 EXPECT_TRUE(recognizer_->StartRecognition()); |
177 recognizer_->CancelRecognition(); | 174 recognizer_->AbortRecognition(); |
178 EXPECT_FALSE(recording_complete_); | 175 EXPECT_FALSE(audio_closed_); |
179 EXPECT_FALSE(recognition_complete_); | 176 EXPECT_FALSE(recognition_ended_); |
180 EXPECT_FALSE(result_received_); | 177 EXPECT_FALSE(result_received_); |
181 EXPECT_FALSE(audio_received_); | 178 EXPECT_FALSE(audio_started_); |
182 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 179 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
183 } | 180 } |
184 | 181 |
185 TEST_F(SpeechRecognizerTest, CancelNoData) { | 182 TEST_F(SpeechRecognizerImplTest, CancelNoData) { |
186 // Check for callbacks when canceling recognition before any audio gets | 183 // Check for callbacks when canceling recognition before any audio gets |
187 // recorded. | 184 // recorded. |
188 EXPECT_TRUE(recognizer_->StartRecording()); | 185 EXPECT_TRUE(recognizer_->StartRecognition()); |
189 recognizer_->StopRecording(); | 186 recognizer_->StopAudioCapture(); |
190 EXPECT_TRUE(recording_complete_); | 187 EXPECT_TRUE(audio_closed_); |
191 EXPECT_TRUE(recognition_complete_); | 188 EXPECT_TRUE(recognition_ended_); |
192 EXPECT_FALSE(result_received_); | 189 EXPECT_FALSE(result_received_); |
193 EXPECT_FALSE(audio_received_); | 190 EXPECT_FALSE(audio_started_); |
194 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 191 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
195 } | 192 } |
196 | 193 |
197 TEST_F(SpeechRecognizerTest, StopWithData) { | 194 TEST_F(SpeechRecognizerImplTest, StopWithData) { |
198 // Start recording, give some data and then stop. This should wait for the | 195 // Start recording, give some data and then stop. This should wait for the |
199 // network callback to arrive before completion. | 196 // network callback to arrive before completion. |
200 EXPECT_TRUE(recognizer_->StartRecording()); | 197 EXPECT_TRUE(recognizer_->StartRecognition()); |
201 TestAudioInputController* controller = | 198 TestAudioInputController* controller = |
202 audio_input_controller_factory_.controller(); | 199 audio_input_controller_factory_.controller(); |
203 ASSERT_TRUE(controller); | 200 ASSERT_TRUE(controller); |
204 | 201 |
205 // Try sending 5 chunks of mock audio data and verify that each of them | 202 // Try sending 5 chunks of mock audio data and verify that each of them |
206 // resulted immediately in a packet sent out via the network. This verifies | 203 // resulted immediately in a packet sent out via the network. This verifies |
207 // that we are streaming out encoded data as chunks without waiting for the | 204 // that we are streaming out encoded data as chunks without waiting for the |
208 // full recording to complete. | 205 // full recording to complete. |
209 const size_t kNumChunks = 5; | 206 const size_t kNumChunks = 5; |
210 for (size_t i = 0; i < kNumChunks; ++i) { | 207 for (size_t i = 0; i < kNumChunks; ++i) { |
211 controller->event_handler()->OnData(controller, &audio_packet_[0], | 208 controller->event_handler()->OnData(controller, &audio_packet_[0], |
212 audio_packet_.size()); | 209 audio_packet_.size()); |
213 MessageLoop::current()->RunAllPending(); | 210 MessageLoop::current()->RunAllPending(); |
214 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 211 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
215 ASSERT_TRUE(fetcher); | 212 ASSERT_TRUE(fetcher); |
216 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); | 213 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); |
217 } | 214 } |
218 | 215 |
219 recognizer_->StopRecording(); | 216 recognizer_->StopAudioCapture(); |
220 EXPECT_TRUE(audio_received_); | 217 EXPECT_TRUE(audio_started_); |
221 EXPECT_TRUE(recording_complete_); | 218 EXPECT_TRUE(audio_closed_); |
222 EXPECT_FALSE(recognition_complete_); | 219 EXPECT_FALSE(recognition_ended_); |
223 EXPECT_FALSE(result_received_); | 220 EXPECT_FALSE(result_received_); |
224 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 221 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
225 | 222 |
226 // Issue the network callback to complete the process. | 223 // Issue the network callback to complete the process. |
227 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 224 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
228 ASSERT_TRUE(fetcher); | 225 ASSERT_TRUE(fetcher); |
229 | 226 |
230 fetcher->set_url(fetcher->GetOriginalURL()); | 227 fetcher->set_url(fetcher->GetOriginalURL()); |
231 net::URLRequestStatus status; | 228 net::URLRequestStatus status; |
232 status.set_status(net::URLRequestStatus::SUCCESS); | 229 status.set_status(net::URLRequestStatus::SUCCESS); |
233 fetcher->set_status(status); | 230 fetcher->set_status(status); |
234 fetcher->set_response_code(200); | 231 fetcher->set_response_code(200); |
235 fetcher->SetResponseString( | 232 fetcher->SetResponseString( |
236 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); | 233 "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}"); |
237 fetcher->delegate()->OnURLFetchComplete(fetcher); | 234 fetcher->delegate()->OnURLFetchComplete(fetcher); |
238 | 235 |
239 EXPECT_TRUE(recognition_complete_); | 236 EXPECT_TRUE(recognition_ended_); |
240 EXPECT_TRUE(result_received_); | 237 EXPECT_TRUE(result_received_); |
241 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 238 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
242 } | 239 } |
243 | 240 |
244 TEST_F(SpeechRecognizerTest, CancelWithData) { | 241 TEST_F(SpeechRecognizerImplTest, CancelWithData) { |
245 // Start recording, give some data and then cancel. This should create | 242 // Start recording, give some data and then cancel. This should create |
246 // a network request but give no callbacks. | 243 // a network request but give no callbacks. |
247 EXPECT_TRUE(recognizer_->StartRecording()); | 244 EXPECT_TRUE(recognizer_->StartRecognition()); |
248 TestAudioInputController* controller = | 245 TestAudioInputController* controller = |
249 audio_input_controller_factory_.controller(); | 246 audio_input_controller_factory_.controller(); |
250 ASSERT_TRUE(controller); | 247 ASSERT_TRUE(controller); |
251 controller->event_handler()->OnData(controller, &audio_packet_[0], | 248 controller->event_handler()->OnData(controller, &audio_packet_[0], |
252 audio_packet_.size()); | 249 audio_packet_.size()); |
253 MessageLoop::current()->RunAllPending(); | 250 MessageLoop::current()->RunAllPending(); |
254 recognizer_->CancelRecognition(); | 251 recognizer_->AbortRecognition(); |
255 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 252 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
256 EXPECT_TRUE(audio_received_); | 253 EXPECT_TRUE(audio_started_); |
257 EXPECT_FALSE(recording_complete_); | 254 EXPECT_FALSE(audio_closed_); |
258 EXPECT_FALSE(recognition_complete_); | 255 EXPECT_FALSE(recognition_ended_); |
259 EXPECT_FALSE(result_received_); | 256 EXPECT_FALSE(result_received_); |
260 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 257 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
261 } | 258 } |
262 | 259 |
263 TEST_F(SpeechRecognizerTest, ConnectionError) { | 260 TEST_F(SpeechRecognizerImplTest, ConnectionError) { |
264 // Start recording, give some data and then stop. Issue the network callback | 261 // Start recording, give some data and then stop. Issue the network callback |
265 // with a connection error and verify that the recognizer bubbles the error up | 262 // with a connection error and verify that the recognizer bubbles the error up |
266 EXPECT_TRUE(recognizer_->StartRecording()); | 263 EXPECT_TRUE(recognizer_->StartRecognition()); |
267 TestAudioInputController* controller = | 264 TestAudioInputController* controller = |
268 audio_input_controller_factory_.controller(); | 265 audio_input_controller_factory_.controller(); |
269 ASSERT_TRUE(controller); | 266 ASSERT_TRUE(controller); |
270 controller->event_handler()->OnData(controller, &audio_packet_[0], | 267 controller->event_handler()->OnData(controller, &audio_packet_[0], |
271 audio_packet_.size()); | 268 audio_packet_.size()); |
272 MessageLoop::current()->RunAllPending(); | 269 MessageLoop::current()->RunAllPending(); |
273 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 270 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
274 ASSERT_TRUE(fetcher); | 271 ASSERT_TRUE(fetcher); |
275 | 272 |
276 recognizer_->StopRecording(); | 273 recognizer_->StopAudioCapture(); |
277 EXPECT_TRUE(audio_received_); | 274 EXPECT_TRUE(audio_started_); |
278 EXPECT_TRUE(recording_complete_); | 275 EXPECT_TRUE(audio_closed_); |
279 EXPECT_FALSE(recognition_complete_); | 276 EXPECT_FALSE(recognition_ended_); |
280 EXPECT_FALSE(result_received_); | 277 EXPECT_FALSE(result_received_); |
281 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 278 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
282 | 279 |
283 // Issue the network callback to complete the process. | 280 // Issue the network callback to complete the process. |
284 fetcher->set_url(fetcher->GetOriginalURL()); | 281 fetcher->set_url(fetcher->GetOriginalURL()); |
285 net::URLRequestStatus status; | 282 net::URLRequestStatus status; |
286 status.set_status(net::URLRequestStatus::FAILED); | 283 status.set_status(net::URLRequestStatus::FAILED); |
287 status.set_error(net::ERR_CONNECTION_REFUSED); | 284 status.set_error(net::ERR_CONNECTION_REFUSED); |
288 fetcher->set_status(status); | 285 fetcher->set_status(status); |
289 fetcher->set_response_code(0); | 286 fetcher->set_response_code(0); |
290 fetcher->SetResponseString(""); | 287 fetcher->SetResponseString(""); |
291 fetcher->delegate()->OnURLFetchComplete(fetcher); | 288 fetcher->delegate()->OnURLFetchComplete(fetcher); |
292 | 289 |
293 EXPECT_FALSE(recognition_complete_); | 290 EXPECT_FALSE(recognition_ended_); |
294 EXPECT_FALSE(result_received_); | 291 EXPECT_FALSE(result_received_); |
295 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 292 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
296 } | 293 } |
297 | 294 |
298 TEST_F(SpeechRecognizerTest, ServerError) { | 295 TEST_F(SpeechRecognizerImplTest, ServerError) { |
299 // Start recording, give some data and then stop. Issue the network callback | 296 // Start recording, give some data and then stop. Issue the network callback |
300 // with a 500 error and verify that the recognizer bubbles the error up | 297 // with a 500 error and verify that the recognizer bubbles the error up |
301 EXPECT_TRUE(recognizer_->StartRecording()); | 298 EXPECT_TRUE(recognizer_->StartRecognition()); |
302 TestAudioInputController* controller = | 299 TestAudioInputController* controller = |
303 audio_input_controller_factory_.controller(); | 300 audio_input_controller_factory_.controller(); |
304 ASSERT_TRUE(controller); | 301 ASSERT_TRUE(controller); |
305 controller->event_handler()->OnData(controller, &audio_packet_[0], | 302 controller->event_handler()->OnData(controller, &audio_packet_[0], |
306 audio_packet_.size()); | 303 audio_packet_.size()); |
307 MessageLoop::current()->RunAllPending(); | 304 MessageLoop::current()->RunAllPending(); |
308 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 305 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
309 ASSERT_TRUE(fetcher); | 306 ASSERT_TRUE(fetcher); |
310 | 307 |
311 recognizer_->StopRecording(); | 308 recognizer_->StopAudioCapture(); |
312 EXPECT_TRUE(audio_received_); | 309 EXPECT_TRUE(audio_started_); |
313 EXPECT_TRUE(recording_complete_); | 310 EXPECT_TRUE(audio_closed_); |
314 EXPECT_FALSE(recognition_complete_); | 311 EXPECT_FALSE(recognition_ended_); |
315 EXPECT_FALSE(result_received_); | 312 EXPECT_FALSE(result_received_); |
316 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 313 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
317 | 314 |
318 // Issue the network callback to complete the process. | 315 // Issue the network callback to complete the process. |
319 fetcher->set_url(fetcher->GetOriginalURL()); | 316 fetcher->set_url(fetcher->GetOriginalURL()); |
320 net::URLRequestStatus status; | 317 net::URLRequestStatus status; |
321 status.set_status(net::URLRequestStatus::SUCCESS); | 318 status.set_status(net::URLRequestStatus::SUCCESS); |
322 fetcher->set_status(status); | 319 fetcher->set_status(status); |
323 fetcher->set_response_code(500); | 320 fetcher->set_response_code(500); |
324 fetcher->SetResponseString("Internal Server Error"); | 321 fetcher->SetResponseString("Internal Server Error"); |
325 fetcher->delegate()->OnURLFetchComplete(fetcher); | 322 fetcher->delegate()->OnURLFetchComplete(fetcher); |
326 | 323 |
327 EXPECT_FALSE(recognition_complete_); | 324 EXPECT_FALSE(recognition_ended_); |
328 EXPECT_FALSE(result_received_); | 325 EXPECT_FALSE(result_received_); |
329 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); | 326 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NETWORK, error_); |
330 } | 327 } |
331 | 328 |
332 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { | 329 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) { |
333 // Check if things tear down properly if AudioInputController threw an error. | 330 // Check if things tear down properly if AudioInputController threw an error. |
334 EXPECT_TRUE(recognizer_->StartRecording()); | 331 EXPECT_TRUE(recognizer_->StartRecognition()); |
335 TestAudioInputController* controller = | 332 TestAudioInputController* controller = |
336 audio_input_controller_factory_.controller(); | 333 audio_input_controller_factory_.controller(); |
337 ASSERT_TRUE(controller); | 334 ASSERT_TRUE(controller); |
338 controller->event_handler()->OnError(controller, 0); | 335 controller->event_handler()->OnError(controller, 0); |
339 MessageLoop::current()->RunAllPending(); | 336 MessageLoop::current()->RunAllPending(); |
340 EXPECT_FALSE(audio_received_); | 337 EXPECT_FALSE(audio_started_); |
341 EXPECT_FALSE(recording_complete_); | 338 EXPECT_FALSE(audio_closed_); |
342 EXPECT_FALSE(recognition_complete_); | 339 EXPECT_FALSE(recognition_ended_); |
343 EXPECT_FALSE(result_received_); | 340 EXPECT_FALSE(result_received_); |
344 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 341 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
345 } | 342 } |
346 | 343 |
347 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) { | 344 TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) { |
348 // Check if things tear down properly if AudioInputController threw an error | 345 // Check if things tear down properly if AudioInputController threw an error |
349 // after giving some audio data. | 346 // after giving some audio data. |
350 EXPECT_TRUE(recognizer_->StartRecording()); | 347 EXPECT_TRUE(recognizer_->StartRecognition()); |
351 TestAudioInputController* controller = | 348 TestAudioInputController* controller = |
352 audio_input_controller_factory_.controller(); | 349 audio_input_controller_factory_.controller(); |
353 ASSERT_TRUE(controller); | 350 ASSERT_TRUE(controller); |
354 controller->event_handler()->OnData(controller, &audio_packet_[0], | 351 controller->event_handler()->OnData(controller, &audio_packet_[0], |
355 audio_packet_.size()); | 352 audio_packet_.size()); |
356 controller->event_handler()->OnError(controller, 0); | 353 controller->event_handler()->OnError(controller, 0); |
357 MessageLoop::current()->RunAllPending(); | 354 MessageLoop::current()->RunAllPending(); |
358 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); | 355 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
359 EXPECT_TRUE(audio_received_); | 356 EXPECT_TRUE(audio_started_); |
360 EXPECT_FALSE(recording_complete_); | 357 EXPECT_FALSE(audio_closed_); |
361 EXPECT_FALSE(recognition_complete_); | 358 EXPECT_FALSE(recognition_ended_); |
362 EXPECT_FALSE(result_received_); | 359 EXPECT_FALSE(result_received_); |
363 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); | 360 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_AUDIO, error_); |
364 } | 361 } |
365 | 362 |
366 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { | 363 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) { |
367 // Start recording and give a lot of packets with audio samples set to zero. | 364 // Start recording and give a lot of packets with audio samples set to zero. |
368 // This should trigger the no-speech detector and issue a callback. | 365 // This should trigger the no-speech detector and issue a callback. |
369 EXPECT_TRUE(recognizer_->StartRecording()); | 366 EXPECT_TRUE(recognizer_->StartRecognition()); |
370 TestAudioInputController* controller = | 367 TestAudioInputController* controller = |
371 audio_input_controller_factory_.controller(); | 368 audio_input_controller_factory_.controller(); |
372 ASSERT_TRUE(controller); | 369 ASSERT_TRUE(controller); |
373 controller = audio_input_controller_factory_.controller(); | 370 controller = audio_input_controller_factory_.controller(); |
374 ASSERT_TRUE(controller); | 371 ASSERT_TRUE(controller); |
375 | 372 |
376 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / | 373 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / |
377 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 374 SpeechRecognizerImpl::kAudioPacketIntervalMs; |
378 // The vector is already filled with zero value samples on create. | 375 // The vector is already filled with zero value samples on create. |
379 for (int i = 0; i < num_packets; ++i) { | 376 for (int i = 0; i < num_packets; ++i) { |
380 controller->event_handler()->OnData(controller, &audio_packet_[0], | 377 controller->event_handler()->OnData(controller, &audio_packet_[0], |
381 audio_packet_.size()); | 378 audio_packet_.size()); |
382 } | 379 } |
383 MessageLoop::current()->RunAllPending(); | 380 MessageLoop::current()->RunAllPending(); |
384 EXPECT_TRUE(audio_received_); | 381 EXPECT_TRUE(audio_started_); |
385 EXPECT_FALSE(recording_complete_); | 382 EXPECT_FALSE(audio_closed_); |
386 EXPECT_FALSE(recognition_complete_); | 383 EXPECT_FALSE(recognition_ended_); |
387 EXPECT_FALSE(result_received_); | 384 EXPECT_FALSE(result_received_); |
388 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); | 385 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_); |
389 } | 386 } |
390 | 387 |
391 TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) { | 388 TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) { |
392 // Start recording and give a lot of packets with audio samples set to zero | 389 // Start recording and give a lot of packets with audio samples set to zero |
393 // and then some more with reasonably loud audio samples. This should be | 390 // and then some more with reasonably loud audio samples. This should be |
394 // treated as normal speech input and the no-speech detector should not get | 391 // treated as normal speech input and the no-speech detector should not get |
395 // triggered. | 392 // triggered. |
396 EXPECT_TRUE(recognizer_->StartRecording()); | 393 EXPECT_TRUE(recognizer_->StartRecognition()); |
397 TestAudioInputController* controller = | 394 TestAudioInputController* controller = |
398 audio_input_controller_factory_.controller(); | 395 audio_input_controller_factory_.controller(); |
399 ASSERT_TRUE(controller); | 396 ASSERT_TRUE(controller); |
400 controller = audio_input_controller_factory_.controller(); | 397 controller = audio_input_controller_factory_.controller(); |
401 ASSERT_TRUE(controller); | 398 ASSERT_TRUE(controller); |
402 | 399 |
403 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / | 400 int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutSec * 1000) / |
404 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 401 SpeechRecognizerImpl::kAudioPacketIntervalMs; |
405 | 402 |
406 // The vector is already filled with zero value samples on create. | 403 // The vector is already filled with zero value samples on create. |
407 for (int i = 0; i < num_packets / 2; ++i) { | 404 for (int i = 0; i < num_packets / 2; ++i) { |
408 controller->event_handler()->OnData(controller, &audio_packet_[0], | 405 controller->event_handler()->OnData(controller, &audio_packet_[0], |
409 audio_packet_.size()); | 406 audio_packet_.size()); |
410 } | 407 } |
411 | 408 |
412 FillPacketWithTestWaveform(); | 409 FillPacketWithTestWaveform(); |
413 for (int i = 0; i < num_packets / 2; ++i) { | 410 for (int i = 0; i < num_packets / 2; ++i) { |
414 controller->event_handler()->OnData(controller, &audio_packet_[0], | 411 controller->event_handler()->OnData(controller, &audio_packet_[0], |
415 audio_packet_.size()); | 412 audio_packet_.size()); |
416 } | 413 } |
417 | 414 |
418 MessageLoop::current()->RunAllPending(); | 415 MessageLoop::current()->RunAllPending(); |
419 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 416 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
420 EXPECT_TRUE(audio_received_); | 417 EXPECT_TRUE(audio_started_); |
421 EXPECT_FALSE(recording_complete_); | 418 EXPECT_FALSE(audio_closed_); |
422 EXPECT_FALSE(recognition_complete_); | 419 EXPECT_FALSE(recognition_ended_); |
423 recognizer_->CancelRecognition(); | 420 recognizer_->AbortRecognition(); |
424 } | 421 } |
425 | 422 |
426 TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) { | 423 TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) { |
427 // Start recording and give a lot of packets with audio samples set to zero | 424 // Start recording and give a lot of packets with audio samples set to zero |
428 // and then some more with reasonably loud audio samples. Check that we don't | 425 // and then some more with reasonably loud audio samples. Check that we don't |
429 // get the callback during estimation phase, then get zero for the silence | 426 // get the callback during estimation phase, then get zero for the silence |
430 // samples and proper volume for the loud audio. | 427 // samples and proper volume for the loud audio. |
431 EXPECT_TRUE(recognizer_->StartRecording()); | 428 EXPECT_TRUE(recognizer_->StartRecognition()); |
432 TestAudioInputController* controller = | 429 TestAudioInputController* controller = |
433 audio_input_controller_factory_.controller(); | 430 audio_input_controller_factory_.controller(); |
434 ASSERT_TRUE(controller); | 431 ASSERT_TRUE(controller); |
435 controller = audio_input_controller_factory_.controller(); | 432 controller = audio_input_controller_factory_.controller(); |
436 ASSERT_TRUE(controller); | 433 ASSERT_TRUE(controller); |
437 | 434 |
438 // Feed some samples to begin with for the endpointer to do noise estimation. | 435 // Feed some samples to begin with for the endpointer to do noise estimation. |
439 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / | 436 int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs / |
440 SpeechRecognizerImpl::kAudioPacketIntervalMs; | 437 SpeechRecognizerImpl::kAudioPacketIntervalMs; |
441 FillPacketWithNoise(); | 438 FillPacketWithNoise(); |
(...skipping 11 matching lines...) Expand all Loading... | |
453 EXPECT_FLOAT_EQ(0.74939233f, volume_); | 450 EXPECT_FLOAT_EQ(0.74939233f, volume_); |
454 | 451 |
455 FillPacketWithTestWaveform(); | 452 FillPacketWithTestWaveform(); |
456 controller->event_handler()->OnData(controller, &audio_packet_[0], | 453 controller->event_handler()->OnData(controller, &audio_packet_[0], |
457 audio_packet_.size()); | 454 audio_packet_.size()); |
458 MessageLoop::current()->RunAllPending(); | 455 MessageLoop::current()->RunAllPending(); |
459 EXPECT_FLOAT_EQ(0.89926866f, volume_); | 456 EXPECT_FLOAT_EQ(0.89926866f, volume_); |
460 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); | 457 EXPECT_FLOAT_EQ(0.75071919f, noise_volume_); |
461 | 458 |
462 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 459 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
463 EXPECT_FALSE(recording_complete_); | 460 EXPECT_FALSE(audio_closed_); |
464 EXPECT_FALSE(recognition_complete_); | 461 EXPECT_FALSE(recognition_ended_); |
465 recognizer_->CancelRecognition(); | 462 recognizer_->AbortRecognition(); |
466 } | 463 } |
467 | 464 |
468 } // namespace speech | 465 } // namespace speech |
OLD | NEW |