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 "base/rand_util.h" | |
7 #include "chrome/common/net/test_url_fetcher_factory.h" | 8 #include "chrome/common/net/test_url_fetcher_factory.h" |
8 #include "content/browser/browser_thread.h" | 9 #include "content/browser/browser_thread.h" |
9 #include "content/browser/speech/speech_recognizer.h" | 10 #include "content/browser/speech/speech_recognizer.h" |
10 #include "media/audio/test_audio_input_controller_factory.h" | 11 #include "media/audio/test_audio_input_controller_factory.h" |
11 #include "net/url_request/url_request_status.h" | 12 #include "net/url_request/url_request_status.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 using media::AudioInputController; | 15 using media::AudioInputController; |
15 using media::TestAudioInputController; | 16 using media::TestAudioInputController; |
16 using media::TestAudioInputControllerFactory; | 17 using media::TestAudioInputControllerFactory; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 } | 56 } |
56 | 57 |
57 virtual void DidCompleteEnvironmentEstimation(int caller_id) { | 58 virtual void DidCompleteEnvironmentEstimation(int caller_id) { |
58 } | 59 } |
59 | 60 |
60 virtual void OnRecognizerError(int caller_id, | 61 virtual void OnRecognizerError(int caller_id, |
61 SpeechRecognizer::ErrorCode error) { | 62 SpeechRecognizer::ErrorCode error) { |
62 error_ = error; | 63 error_ = error; |
63 } | 64 } |
64 | 65 |
65 virtual void SetInputVolume(int caller_id, float volume) { | 66 virtual void SetInputVolume(int caller_id, float volume, float noise_volume) { |
66 volume_ = volume; | 67 volume_ = volume; |
68 noise_volume_ = noise_volume; | |
67 } | 69 } |
68 | 70 |
69 // testing::Test methods. | 71 // testing::Test methods. |
70 virtual void SetUp() { | 72 virtual void SetUp() { |
71 URLFetcher::set_factory(&url_fetcher_factory_); | 73 URLFetcher::set_factory(&url_fetcher_factory_); |
72 AudioInputController::set_factory(&audio_input_controller_factory_); | 74 AudioInputController::set_factory(&audio_input_controller_factory_); |
73 } | 75 } |
74 | 76 |
75 virtual void TearDown() { | 77 virtual void TearDown() { |
76 URLFetcher::set_factory(NULL); | 78 URLFetcher::set_factory(NULL); |
77 AudioInputController::set_factory(NULL); | 79 AudioInputController::set_factory(NULL); |
78 } | 80 } |
79 | 81 |
80 void FillPacketWithTestWaveform() { | 82 void FillPacketWithTestWaveform() { |
81 // Fill the input with a simple pattern, a 125Hz sawtooth waveform. | 83 // Fill the input with a simple pattern, a 125Hz sawtooth waveform. |
82 for (size_t i = 0; i < audio_packet_.size(); ++i) | 84 for (size_t i = 0; i < audio_packet_.size(); ++i) |
83 audio_packet_[i] = static_cast<uint8>(i); | 85 audio_packet_[i] = static_cast<uint8>(i); |
84 } | 86 } |
85 | 87 |
88 void FillPacketWithNoise() { | |
89 for (size_t i = 0; i < audio_packet_.size(); ++i) | |
90 audio_packet_[i] = static_cast<uint8>(base::RandInt(0, 50)); | |
Satish
2011/03/01 16:44:17
Please ignore this part, I should be hardcoding th
bulach
2011/03/01 17:39:04
yay for deterministic tests! :)
| |
91 } | |
92 | |
86 protected: | 93 protected: |
87 MessageLoopForIO message_loop_; | 94 MessageLoopForIO message_loop_; |
88 BrowserThread io_thread_; | 95 BrowserThread io_thread_; |
89 scoped_refptr<SpeechRecognizer> recognizer_; | 96 scoped_refptr<SpeechRecognizer> recognizer_; |
90 bool recording_complete_; | 97 bool recording_complete_; |
91 bool recognition_complete_; | 98 bool recognition_complete_; |
92 bool result_received_; | 99 bool result_received_; |
93 SpeechRecognizer::ErrorCode error_; | 100 SpeechRecognizer::ErrorCode error_; |
94 TestURLFetcherFactory url_fetcher_factory_; | 101 TestURLFetcherFactory url_fetcher_factory_; |
95 TestAudioInputControllerFactory audio_input_controller_factory_; | 102 TestAudioInputControllerFactory audio_input_controller_factory_; |
96 std::vector<uint8> audio_packet_; | 103 std::vector<uint8> audio_packet_; |
97 float volume_; | 104 float volume_; |
105 float noise_volume_; | |
98 }; | 106 }; |
99 | 107 |
100 TEST_F(SpeechRecognizerTest, StopNoData) { | 108 TEST_F(SpeechRecognizerTest, StopNoData) { |
101 // Check for callbacks when stopping record before any audio gets recorded. | 109 // Check for callbacks when stopping record before any audio gets recorded. |
102 EXPECT_TRUE(recognizer_->StartRecording()); | 110 EXPECT_TRUE(recognizer_->StartRecording()); |
103 recognizer_->CancelRecognition(); | 111 recognizer_->CancelRecognition(); |
104 EXPECT_FALSE(recording_complete_); | 112 EXPECT_FALSE(recording_complete_); |
105 EXPECT_FALSE(recognition_complete_); | 113 EXPECT_FALSE(recognition_complete_); |
106 EXPECT_FALSE(result_received_); | 114 EXPECT_FALSE(result_received_); |
107 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 115 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 EXPECT_TRUE(recognizer_->StartRecording()); | 273 EXPECT_TRUE(recognizer_->StartRecording()); |
266 TestAudioInputController* controller = | 274 TestAudioInputController* controller = |
267 audio_input_controller_factory_.controller(); | 275 audio_input_controller_factory_.controller(); |
268 ASSERT_TRUE(controller); | 276 ASSERT_TRUE(controller); |
269 controller = audio_input_controller_factory_.controller(); | 277 controller = audio_input_controller_factory_.controller(); |
270 ASSERT_TRUE(controller); | 278 ASSERT_TRUE(controller); |
271 | 279 |
272 // Feed some samples to begin with for the endpointer to do noise estimation. | 280 // Feed some samples to begin with for the endpointer to do noise estimation. |
273 int num_packets = SpeechRecognizer::kEndpointerEstimationTimeMs / | 281 int num_packets = SpeechRecognizer::kEndpointerEstimationTimeMs / |
274 SpeechRecognizer::kAudioPacketIntervalMs; | 282 SpeechRecognizer::kAudioPacketIntervalMs; |
283 FillPacketWithNoise(); | |
275 for (int i = 0; i < num_packets; ++i) { | 284 for (int i = 0; i < num_packets; ++i) { |
276 controller->event_handler()->OnData(controller, &audio_packet_[0], | 285 controller->event_handler()->OnData(controller, &audio_packet_[0], |
277 audio_packet_.size()); | 286 audio_packet_.size()); |
278 } | 287 } |
279 MessageLoop::current()->RunAllPending(); | 288 MessageLoop::current()->RunAllPending(); |
280 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. | 289 EXPECT_EQ(-1.0f, volume_); // No audio volume set yet. |
281 | 290 |
282 // The vector is already filled with zero value samples on create. | 291 // The vector is already filled with zero value samples on create. |
283 controller->event_handler()->OnData(controller, &audio_packet_[0], | 292 controller->event_handler()->OnData(controller, &audio_packet_[0], |
284 audio_packet_.size()); | 293 audio_packet_.size()); |
285 MessageLoop::current()->RunAllPending(); | 294 MessageLoop::current()->RunAllPending(); |
286 EXPECT_EQ(0, volume_); | 295 EXPECT_EQ(0, volume_); |
287 | 296 |
288 FillPacketWithTestWaveform(); | 297 FillPacketWithTestWaveform(); |
289 controller->event_handler()->OnData(controller, &audio_packet_[0], | 298 controller->event_handler()->OnData(controller, &audio_packet_[0], |
290 audio_packet_.size()); | 299 audio_packet_.size()); |
291 MessageLoop::current()->RunAllPending(); | 300 MessageLoop::current()->RunAllPending(); |
292 EXPECT_FLOAT_EQ(0.9f, volume_); | 301 EXPECT_FLOAT_EQ(0.81907868f, volume_); |
302 EXPECT_FLOAT_EQ(0.36962864f, noise_volume_); | |
hans
2011/03/01 17:25:25
maybe make these magic numbers into constant varia
| |
293 | 303 |
294 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); | 304 EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_); |
295 EXPECT_FALSE(recording_complete_); | 305 EXPECT_FALSE(recording_complete_); |
296 EXPECT_FALSE(recognition_complete_); | 306 EXPECT_FALSE(recognition_complete_); |
297 recognizer_->CancelRecognition(); | 307 recognizer_->CancelRecognition(); |
298 } | 308 } |
299 | 309 |
300 } // namespace speech_input | 310 } // namespace speech_input |
OLD | NEW |