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

Unified Diff: chrome/browser/speech/speech_recognizer_unittest.cc

Issue 3341020: Speech input: Do environment estimation and detect the no-speech case. (Closed)
Patch Set: . Created 10 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/speech/speech_recognizer_unittest.cc
diff --git a/chrome/browser/speech/speech_recognizer_unittest.cc b/chrome/browser/speech/speech_recognizer_unittest.cc
index f4407a69d59a84bc7cc96dce190dff08960e3af0..9e2f4c76bce573814f8d1eb95353a9d0b2582538 100644
--- a/chrome/browser/speech/speech_recognizer_unittest.cc
+++ b/chrome/browser/speech/speech_recognizer_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/scoped_ptr.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/speech/speech_recognizer.h"
#include "chrome/common/net/test_url_fetcher_factory.h"
@@ -29,7 +30,13 @@ class SpeechRecognizerTest : public SpeechRecognizerDelegate,
recording_complete_(false),
recognition_complete_(false),
result_received_(false),
- error_(false) {
+ error_(SpeechRecognizer::RECOGNIZER_NO_ERROR) {
+ audio_packet_length_bytes_ =
+ (SpeechRecognizer::kAudioSampleRate *
+ SpeechRecognizer::kAudioPacketIntervalMs *
+ SpeechRecognizer::kNumAudioChannels *
+ SpeechRecognizer::kNumBitsPerAudioSample) / (8 * 1000);
+ audio_packet_.reset(new uint8[audio_packet_length_bytes_]);
}
void StartTest() {
@@ -51,8 +58,12 @@ class SpeechRecognizerTest : public SpeechRecognizerDelegate,
recognition_complete_ = true;
}
- virtual void OnRecognizerError(int caller_id) {
- error_ = true;
+ virtual void DidCompleteEnvironmentEstimation(int caller_id) {
+ }
+
+ virtual void OnRecognizerError(int caller_id,
+ SpeechRecognizer::ErrorCode error) {
+ error_ = error;
}
// testing::Test methods.
@@ -73,9 +84,11 @@ class SpeechRecognizerTest : public SpeechRecognizerDelegate,
bool recording_complete_;
bool recognition_complete_;
bool result_received_;
- bool error_;
+ SpeechRecognizer::ErrorCode error_;
TestURLFetcherFactory url_fetcher_factory_;
TestAudioInputControllerFactory audio_input_controller_factory_;
+ scoped_ptr<uint8> audio_packet_;
+ int audio_packet_length_bytes_;
joth 2010/09/09 10:51:37 would a vector<uint8> work better than these 2 mem
};
TEST_F(SpeechRecognizerTest, StopNoData) {
@@ -85,7 +98,7 @@ TEST_F(SpeechRecognizerTest, StopNoData) {
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_FALSE(error_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
}
TEST_F(SpeechRecognizerTest, CancelNoData) {
@@ -96,12 +109,10 @@ TEST_F(SpeechRecognizerTest, CancelNoData) {
EXPECT_TRUE(recording_complete_);
EXPECT_TRUE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_FALSE(error_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
}
TEST_F(SpeechRecognizerTest, StopWithData) {
- uint8 data[kAudioPacketLengthBytes] = { 0 };
-
// Start recording, give some data and then stop. This should wait for the
// network callback to arrive before completion.
EXPECT_TRUE(recognizer_->StartRecording());
@@ -110,43 +121,44 @@ TEST_F(SpeechRecognizerTest, StopWithData) {
ASSERT_TRUE(controller);
controller = audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
- controller->event_handler()->OnData(controller, data, sizeof(data));
+ controller->event_handler()->OnData(controller, audio_packet_.get(),
+ audio_packet_length_bytes_);
MessageLoop::current()->RunAllPending();
recognizer_->StopRecording();
EXPECT_TRUE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_FALSE(error_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
// Issue the network callback to complete the process.
TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
URLRequestStatus status;
status.set_status(URLRequestStatus::SUCCESS);
- fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(),
- status, 200, ResponseCookies(), "");
+ fetcher->delegate()->OnURLFetchComplete(
+ fetcher, fetcher->original_url(), status, 200, ResponseCookies(),
+ "{\"hypotheses\":[{\"utterance\":\"123\"}]}");
EXPECT_TRUE(recognition_complete_);
EXPECT_TRUE(result_received_);
- EXPECT_FALSE(error_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
}
TEST_F(SpeechRecognizerTest, CancelWithData) {
- uint8 data[kAudioPacketLengthBytes] = { 0 };
-
// Start recording, give some data and then cancel. This should not create
// a network request and finish immediately.
EXPECT_TRUE(recognizer_->StartRecording());
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
- controller->event_handler()->OnData(controller, data, sizeof(data));
+ controller->event_handler()->OnData(controller, audio_packet_.get(),
+ audio_packet_length_bytes_);
MessageLoop::current()->RunAllPending();
recognizer_->CancelRecognition();
EXPECT_EQ(NULL, url_fetcher_factory_.GetFetcherByID(0));
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_FALSE(error_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
}
TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) {
@@ -160,26 +172,85 @@ TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) {
EXPECT_TRUE(recording_complete_);
EXPECT_TRUE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_TRUE(error_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_);
}
TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) {
- uint8 data[kAudioPacketLengthBytes] = { 0 };
-
// Check if things tear down properly if AudioInputController threw an error
// after giving some audio data.
EXPECT_TRUE(recognizer_->StartRecording());
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
- controller->event_handler()->OnData(controller, data, sizeof(data));
+ controller->event_handler()->OnData(controller, audio_packet_.get(),
+ audio_packet_length_bytes_);
controller->event_handler()->OnError(controller, 0);
MessageLoop::current()->RunAllPending();
EXPECT_EQ(NULL, url_fetcher_factory_.GetFetcherByID(0));
EXPECT_TRUE(recording_complete_);
EXPECT_TRUE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_TRUE(error_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_);
+}
+
+TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) {
+ // Start recording and give a lot of packets with audio samples set to zero.
+ // This should trigger the no-speech detector and issue a callback.
+ EXPECT_TRUE(recognizer_->StartRecording());
+ TestAudioInputController* controller =
+ audio_input_controller_factory_.controller();
+ ASSERT_TRUE(controller);
+ controller = audio_input_controller_factory_.controller();
+ ASSERT_TRUE(controller);
+
+ int num_packets = (SpeechRecognizer::kNoSpeechTimeoutSec * 1000) /
+ SpeechRecognizer::kAudioPacketIntervalMs;
+ for (int i = 0; i < audio_packet_length_bytes_; ++i)
+ audio_packet_.get()[i] = 0;
joth 2010/09/09 10:51:37 with vector, audio_packet_.resize(desired_length,
+ for (int i = 0; i < num_packets; ++i) {
+ controller->event_handler()->OnData(controller, audio_packet_.get(),
+ audio_packet_length_bytes_);
+ }
+ MessageLoop::current()->RunAllPending();
+ EXPECT_TRUE(recording_complete_);
+ EXPECT_TRUE(recognition_complete_);
+ EXPECT_FALSE(result_received_);
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_NO_SPEECH, error_);
+}
+
+TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) {
+ // Start recording and give a lot of packets with audio samples set to zero
+ // and then some more with reasonably loud audio samples. This should be
+ // treated as normal speech input and the no-speech detector should not get
+ // triggered.
+ EXPECT_TRUE(recognizer_->StartRecording());
+ TestAudioInputController* controller =
+ audio_input_controller_factory_.controller();
+ ASSERT_TRUE(controller);
+ controller = audio_input_controller_factory_.controller();
+ ASSERT_TRUE(controller);
+
+ int num_packets = (SpeechRecognizer::kNoSpeechTimeoutSec * 1000) /
+ SpeechRecognizer::kAudioPacketIntervalMs;
+
+ for (int i = 0; i < audio_packet_length_bytes_; ++i)
+ audio_packet_.get()[i] = 0;
joth 2010/09/09 10:51:37 ditto
+ for (int i = 0; i < num_packets / 2; ++i) {
+ controller->event_handler()->OnData(controller, audio_packet_.get(),
+ audio_packet_length_bytes_);
+ }
+ for (int i = 0; i < audio_packet_length_bytes_; ++i)
+ audio_packet_.get()[i] = static_cast<uint8>(i);
joth 2010/09/09 10:51:37 would it be more representative to put a sine wave
Satish 2010/09/09 11:30:19 I thought of that earlier but a sine wave isn't ex
+ for (int i = 0; i < num_packets / 2; ++i) {
+ controller->event_handler()->OnData(controller, audio_packet_.get(),
+ audio_packet_length_bytes_);
+ }
+
+ MessageLoop::current()->RunAllPending();
+ EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_FALSE(recording_complete_);
+ EXPECT_FALSE(recognition_complete_);
+ recognizer_->CancelRecognition();
}
} // namespace speech_input
« chrome/browser/speech/speech_input_manager.cc ('K') | « chrome/browser/speech/speech_recognizer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698