| OLD | NEW |
| 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 "chrome/common/net/test_url_fetcher_factory.h" | 7 #include "chrome/common/net/test_url_fetcher_factory.h" |
| 8 #include "content/browser/browser_thread.h" | 8 #include "content/browser/browser_thread.h" |
| 9 #include "content/browser/speech/speech_recognizer.h" | 9 #include "content/browser/speech/speech_recognizer.h" |
| 10 #include "media/audio/test_audio_input_controller_factory.h" | 10 #include "media/audio/test_audio_input_controller_factory.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 131 |
| 132 TEST_F(SpeechRecognizerTest, StopWithData) { | 132 TEST_F(SpeechRecognizerTest, StopWithData) { |
| 133 // Start recording, give some data and then stop. This should wait for the | 133 // Start recording, give some data and then stop. This should wait for the |
| 134 // network callback to arrive before completion. | 134 // network callback to arrive before completion. |
| 135 EXPECT_TRUE(recognizer_->StartRecording()); | 135 EXPECT_TRUE(recognizer_->StartRecording()); |
| 136 TestAudioInputController* controller = | 136 TestAudioInputController* controller = |
| 137 audio_input_controller_factory_.controller(); | 137 audio_input_controller_factory_.controller(); |
| 138 ASSERT_TRUE(controller); | 138 ASSERT_TRUE(controller); |
| 139 controller = audio_input_controller_factory_.controller(); | 139 controller = audio_input_controller_factory_.controller(); |
| 140 ASSERT_TRUE(controller); | 140 ASSERT_TRUE(controller); |
| 141 controller->event_handler()->OnData(controller, &audio_packet_[0], | 141 |
| 142 audio_packet_.size()); | 142 // Try sending 5 chunks of mock audio data and verify that each of them |
| 143 MessageLoop::current()->RunAllPending(); | 143 // resulted immediately in a packet sent out via the network. This verifies |
| 144 // that we are streaming out encoded data as chunks without waiting for the |
| 145 // full recording to complete. |
| 146 const size_t kNumChunks = 5; |
| 147 for (size_t i = 0; i < kNumChunks; ++i) { |
| 148 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 149 audio_packet_.size()); |
| 150 MessageLoop::current()->RunAllPending(); |
| 151 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 152 ASSERT_TRUE(fetcher); |
| 153 EXPECT_EQ(i + 1, fetcher->upload_chunks().size()); |
| 154 } |
| 155 |
| 144 recognizer_->StopRecording(); | 156 recognizer_->StopRecording(); |
| 145 EXPECT_TRUE(recording_complete_); | 157 EXPECT_TRUE(recording_complete_); |
| 146 EXPECT_FALSE(recognition_complete_); | 158 EXPECT_FALSE(recognition_complete_); |
| 147 EXPECT_FALSE(result_received_); | 159 EXPECT_FALSE(result_received_); |
| 148 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 160 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
| 149 | 161 |
| 150 // Issue the network callback to complete the process. | 162 // Issue the network callback to complete the process. |
| 151 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 163 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 152 ASSERT_TRUE(fetcher); | 164 ASSERT_TRUE(fetcher); |
| 153 net::URLRequestStatus status; | 165 net::URLRequestStatus status; |
| 154 status.set_status(net::URLRequestStatus::SUCCESS); | 166 status.set_status(net::URLRequestStatus::SUCCESS); |
| 155 fetcher->delegate()->OnURLFetchComplete( | 167 fetcher->delegate()->OnURLFetchComplete( |
| 156 fetcher, fetcher->original_url(), status, 200, ResponseCookies(), | 168 fetcher, fetcher->original_url(), status, 200, ResponseCookies(), |
| 157 "{\"hypotheses\":[{\"utterance\":\"123\"}]}"); | 169 "{\"hypotheses\":[{\"utterance\":\"123\"}]}"); |
| 158 EXPECT_TRUE(recognition_complete_); | 170 EXPECT_TRUE(recognition_complete_); |
| 159 EXPECT_TRUE(result_received_); | 171 EXPECT_TRUE(result_received_); |
| 160 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 172 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
| 161 } | 173 } |
| 162 | 174 |
| 163 TEST_F(SpeechRecognizerTest, CancelWithData) { | 175 TEST_F(SpeechRecognizerTest, CancelWithData) { |
| 164 // Start recording, give some data and then cancel. This should not create | 176 // Start recording, give some data and then cancel. This should create |
| 165 // a network request and finish immediately. | 177 // a network request but give no callbacks. |
| 166 EXPECT_TRUE(recognizer_->StartRecording()); | 178 EXPECT_TRUE(recognizer_->StartRecording()); |
| 167 TestAudioInputController* controller = | 179 TestAudioInputController* controller = |
| 168 audio_input_controller_factory_.controller(); | 180 audio_input_controller_factory_.controller(); |
| 169 ASSERT_TRUE(controller); | 181 ASSERT_TRUE(controller); |
| 170 controller->event_handler()->OnData(controller, &audio_packet_[0], | 182 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 171 audio_packet_.size()); | 183 audio_packet_.size()); |
| 172 MessageLoop::current()->RunAllPending(); | 184 MessageLoop::current()->RunAllPending(); |
| 173 recognizer_->CancelRecognition(); | 185 recognizer_->CancelRecognition(); |
| 174 EXPECT_EQ(NULL, url_fetcher_factory_.GetFetcherByID(0)); | 186 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 175 EXPECT_FALSE(recording_complete_); | 187 EXPECT_FALSE(recording_complete_); |
| 176 EXPECT_FALSE(recognition_complete_); | 188 EXPECT_FALSE(recognition_complete_); |
| 177 EXPECT_FALSE(result_received_); | 189 EXPECT_FALSE(result_received_); |
| 178 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 190 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
| 179 } | 191 } |
| 180 | 192 |
| 181 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { | 193 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) { |
| 182 // Check if things tear down properly if AudioInputController threw an error. | 194 // Check if things tear down properly if AudioInputController threw an error. |
| 183 EXPECT_TRUE(recognizer_->StartRecording()); | 195 EXPECT_TRUE(recognizer_->StartRecording()); |
| 184 TestAudioInputController* controller = | 196 TestAudioInputController* controller = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 196 // Check if things tear down properly if AudioInputController threw an error | 208 // Check if things tear down properly if AudioInputController threw an error |
| 197 // after giving some audio data. | 209 // after giving some audio data. |
| 198 EXPECT_TRUE(recognizer_->StartRecording()); | 210 EXPECT_TRUE(recognizer_->StartRecording()); |
| 199 TestAudioInputController* controller = | 211 TestAudioInputController* controller = |
| 200 audio_input_controller_factory_.controller(); | 212 audio_input_controller_factory_.controller(); |
| 201 ASSERT_TRUE(controller); | 213 ASSERT_TRUE(controller); |
| 202 controller->event_handler()->OnData(controller, &audio_packet_[0], | 214 controller->event_handler()->OnData(controller, &audio_packet_[0], |
| 203 audio_packet_.size()); | 215 audio_packet_.size()); |
| 204 controller->event_handler()->OnError(controller, 0); | 216 controller->event_handler()->OnError(controller, 0); |
| 205 MessageLoop::current()->RunAllPending(); | 217 MessageLoop::current()->RunAllPending(); |
| 206 EXPECT_EQ(NULL, url_fetcher_factory_.GetFetcherByID(0)); | 218 ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0)); |
| 207 EXPECT_FALSE(recording_complete_); | 219 EXPECT_FALSE(recording_complete_); |
| 208 EXPECT_FALSE(recognition_complete_); | 220 EXPECT_FALSE(recognition_complete_); |
| 209 EXPECT_FALSE(result_received_); | 221 EXPECT_FALSE(result_received_); |
| 210 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_); | 222 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_); |
| 211 } | 223 } |
| 212 | 224 |
| 213 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { | 225 TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) { |
| 214 // Start recording and give a lot of packets with audio samples set to zero. | 226 // Start recording and give a lot of packets with audio samples set to zero. |
| 215 // This should trigger the no-speech detector and issue a callback. | 227 // This should trigger the no-speech detector and issue a callback. |
| 216 EXPECT_TRUE(recognizer_->StartRecording()); | 228 EXPECT_TRUE(recognizer_->StartRecording()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 EXPECT_FLOAT_EQ(0.81907868f, volume_); | 316 EXPECT_FLOAT_EQ(0.81907868f, volume_); |
| 305 EXPECT_FLOAT_EQ(0.52143687f, noise_volume_); | 317 EXPECT_FLOAT_EQ(0.52143687f, noise_volume_); |
| 306 | 318 |
| 307 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 319 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
| 308 EXPECT_FALSE(recording_complete_); | 320 EXPECT_FALSE(recording_complete_); |
| 309 EXPECT_FALSE(recognition_complete_); | 321 EXPECT_FALSE(recognition_complete_); |
| 310 recognizer_->CancelRecognition(); | 322 recognizer_->CancelRecognition(); |
| 311 } | 323 } |
| 312 | 324 |
| 313 } // namespace speech_input | 325 } // namespace speech_input |
| OLD | NEW |