This source file includes following definitions.
- volume_
- CheckEventsConsistency
- CheckFinalEventsConsistency
- OnAudioStart
- OnAudioEnd
- OnRecognitionResults
- OnRecognitionError
- OnAudioLevelsChange
- OnRecognitionEnd
- OnRecognitionStart
- OnEnvironmentEstimationComplete
- OnSoundStart
- OnSoundEnd
- SetUp
- TearDown
- FillPacketWithTestWaveform
- FillPacketWithNoise
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <vector>
#include "content/browser/browser_thread_impl.h"
#include "content/browser/speech/google_one_shot_remote_engine.h"
#include "content/browser/speech/speech_recognizer_impl.h"
#include "content/public/browser/speech_recognition_event_listener.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/fake_audio_input_stream.h"
#include "media/audio/fake_audio_output_stream.h"
#include "media/audio/mock_audio_manager.h"
#include "media/audio/test_audio_input_controller_factory.h"
#include "net/base/net_errors.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::MessageLoopProxy;
using media::AudioInputController;
using media::AudioInputStream;
using media::AudioManager;
using media::AudioOutputStream;
using media::AudioParameters;
using media::TestAudioInputController;
using media::TestAudioInputControllerFactory;
namespace content {
class SpeechRecognizerImplTest : public SpeechRecognitionEventListener,
public testing::Test {
public:
SpeechRecognizerImplTest()
: io_thread_(BrowserThread::IO, &message_loop_),
recognition_started_(false),
recognition_ended_(false),
result_received_(false),
audio_started_(false),
audio_ended_(false),
sound_started_(false),
sound_ended_(false),
error_(SPEECH_RECOGNITION_ERROR_NONE),
volume_(-1.0f) {
SpeechRecognitionEngine* sr_engine =
new GoogleOneShotRemoteEngine(NULL );
SpeechRecognitionEngineConfig config;
config.audio_num_bits_per_sample =
SpeechRecognizerImpl::kNumBitsPerAudioSample;
config.audio_sample_rate = SpeechRecognizerImpl::kAudioSampleRate;
config.filter_profanities = false;
sr_engine->SetConfig(config);
const int kTestingSessionId = 1;
recognizer_ = new SpeechRecognizerImpl(
this, kTestingSessionId, false, false, sr_engine);
audio_manager_.reset(new media::MockAudioManager(
base::MessageLoop::current()->message_loop_proxy().get()));
recognizer_->SetAudioManagerForTesting(audio_manager_.get());
int audio_packet_length_bytes =
(SpeechRecognizerImpl::kAudioSampleRate *
GoogleOneShotRemoteEngine::kAudioPacketIntervalMs *
ChannelLayoutToChannelCount(SpeechRecognizerImpl::kChannelLayout) *
SpeechRecognizerImpl::kNumBitsPerAudioSample) / (8 * 1000);
audio_packet_.resize(audio_packet_length_bytes);
}
void CheckEventsConsistency() {
EXPECT_TRUE(!recognition_ended_ || recognition_started_);
EXPECT_TRUE(!audio_ended_ || audio_started_);
EXPECT_TRUE(!sound_ended_ || sound_started_);
EXPECT_TRUE(!audio_started_ || recognition_started_);
EXPECT_TRUE(!sound_started_ || audio_started_);
EXPECT_TRUE(!audio_ended_ || (sound_ended_ || !sound_started_));
EXPECT_TRUE(!recognition_ended_ || (audio_ended_ || !audio_started_));
}
void CheckFinalEventsConsistency() {
EXPECT_FALSE(recognition_started_ ^ recognition_ended_);
EXPECT_FALSE(audio_started_ ^ audio_ended_);
EXPECT_FALSE(sound_started_ ^ sound_ended_);
}
virtual void OnAudioStart(int session_id) OVERRIDE {
audio_started_ = true;
CheckEventsConsistency();
}
virtual void OnAudioEnd(int session_id) OVERRIDE {
audio_ended_ = true;
CheckEventsConsistency();
}
virtual void OnRecognitionResults(
int session_id, const SpeechRecognitionResults& results) OVERRIDE {
result_received_ = true;
}
virtual void OnRecognitionError(
int session_id, const SpeechRecognitionError& error) OVERRIDE {
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(recognition_ended_);
error_ = error.code;
}
virtual void OnAudioLevelsChange(int session_id, float volume,
float noise_volume) OVERRIDE {
volume_ = volume;
noise_volume_ = noise_volume;
}
virtual void OnRecognitionEnd(int session_id) OVERRIDE {
recognition_ended_ = true;
CheckEventsConsistency();
}
virtual void OnRecognitionStart(int session_id) OVERRIDE {
recognition_started_ = true;
CheckEventsConsistency();
}
virtual void OnEnvironmentEstimationComplete(int session_id) OVERRIDE {}
virtual void OnSoundStart(int session_id) OVERRIDE {
sound_started_ = true;
CheckEventsConsistency();
}
virtual void OnSoundEnd(int session_id) OVERRIDE {
sound_ended_ = true;
CheckEventsConsistency();
}
virtual void SetUp() OVERRIDE {
AudioInputController::set_factory_for_testing(
&audio_input_controller_factory_);
}
virtual void TearDown() OVERRIDE {
AudioInputController::set_factory_for_testing(NULL);
}
void FillPacketWithTestWaveform() {
for (size_t i = 0; i < audio_packet_.size(); ++i)
audio_packet_[i] = static_cast<uint8>(i);
}
void FillPacketWithNoise() {
int value = 0;
int factor = 175;
for (size_t i = 0; i < audio_packet_.size(); ++i) {
value += factor;
audio_packet_[i] = value % 100;
}
}
protected:
base::MessageLoopForIO message_loop_;
BrowserThreadImpl io_thread_;
scoped_refptr<SpeechRecognizerImpl> recognizer_;
scoped_ptr<AudioManager> audio_manager_;
bool recognition_started_;
bool recognition_ended_;
bool result_received_;
bool audio_started_;
bool audio_ended_;
bool sound_started_;
bool sound_ended_;
SpeechRecognitionErrorCode error_;
net::TestURLFetcherFactory url_fetcher_factory_;
TestAudioInputControllerFactory audio_input_controller_factory_;
std::vector<uint8> audio_packet_;
float volume_;
float noise_volume_;
};
TEST_F(SpeechRecognizerImplTest, StopNoData) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
recognizer_->StopAudioCapture();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, CancelNoData) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
recognizer_->AbortRecognition();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, StopWithData) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
const size_t kNumChunks = 5;
for (size_t i = 0; i < kNumChunks; ++i) {
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
base::MessageLoop::current()->RunUntilIdle();
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(i + 1, fetcher->upload_chunks().size());
}
recognizer_->StopAudioCapture();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(audio_started_);
EXPECT_TRUE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->set_url(fetcher->GetOriginalURL());
net::URLRequestStatus status;
status.set_status(net::URLRequestStatus::SUCCESS);
fetcher->set_status(status);
fetcher->set_response_code(200);
fetcher->SetResponseString(
"{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}");
fetcher->delegate()->OnURLFetchComplete(fetcher);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(recognition_ended_);
EXPECT_TRUE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, CancelWithData) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
base::MessageLoop::current()->RunUntilIdle();
recognizer_->AbortRecognition();
base::MessageLoop::current()->RunUntilIdle();
ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
EXPECT_TRUE(recognition_started_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_ABORTED, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, ConnectionError) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
base::MessageLoop::current()->RunUntilIdle();
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
recognizer_->StopAudioCapture();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(audio_started_);
EXPECT_TRUE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
fetcher->set_url(fetcher->GetOriginalURL());
net::URLRequestStatus status;
status.set_status(net::URLRequestStatus::FAILED);
status.set_error(net::ERR_CONNECTION_REFUSED);
fetcher->set_status(status);
fetcher->set_response_code(0);
fetcher->SetResponseString(std::string());
fetcher->delegate()->OnURLFetchComplete(fetcher);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, ServerError) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
base::MessageLoop::current()->RunUntilIdle();
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
recognizer_->StopAudioCapture();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(audio_started_);
EXPECT_TRUE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
fetcher->set_url(fetcher->GetOriginalURL());
net::URLRequestStatus status;
status.set_status(net::URLRequestStatus::SUCCESS);
fetcher->set_status(status);
fetcher->set_response_code(500);
fetcher->SetResponseString("Internal Server Error");
fetcher->delegate()->OnURLFetchComplete(fetcher);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(recognition_ended_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NETWORK, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, AudioControllerErrorNoData) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
controller->event_handler()->OnError(controller,
AudioInputController::UNKNOWN_ERROR);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_FALSE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, AudioControllerErrorWithData) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
controller->event_handler()->OnError(controller,
AudioInputController::UNKNOWN_ERROR);
base::MessageLoop::current()->RunUntilIdle();
ASSERT_TRUE(url_fetcher_factory_.GetFetcherByID(0));
EXPECT_TRUE(recognition_started_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_AUDIO, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackIssued) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
GoogleOneShotRemoteEngine::kAudioPacketIntervalMs + 1;
for (int i = 0; i < num_packets; ++i) {
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
}
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(recognition_started_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(result_received_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NO_SPEECH, error_);
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, NoSpeechCallbackNotIssued) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
controller = audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
int num_packets = (SpeechRecognizerImpl::kNoSpeechTimeoutMs) /
GoogleOneShotRemoteEngine::kAudioPacketIntervalMs;
for (int i = 0; i < num_packets / 2; ++i) {
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
}
FillPacketWithTestWaveform();
for (int i = 0; i < num_packets / 2; ++i) {
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
}
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
EXPECT_TRUE(audio_started_);
EXPECT_FALSE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
recognizer_->AbortRecognition();
base::MessageLoop::current()->RunUntilIdle();
CheckFinalEventsConsistency();
}
TEST_F(SpeechRecognizerImplTest, SetInputVolumeCallback) {
recognizer_->StartRecognition(media::AudioManagerBase::kDefaultDeviceId);
base::MessageLoop::current()->RunUntilIdle();
TestAudioInputController* controller =
audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
controller = audio_input_controller_factory_.controller();
ASSERT_TRUE(controller);
int num_packets = SpeechRecognizerImpl::kEndpointerEstimationTimeMs /
GoogleOneShotRemoteEngine::kAudioPacketIntervalMs;
FillPacketWithNoise();
for (int i = 0; i < num_packets; ++i) {
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
}
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(-1.0f, volume_);
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
base::MessageLoop::current()->RunUntilIdle();
EXPECT_FLOAT_EQ(0.74939233f, volume_);
FillPacketWithTestWaveform();
controller->event_handler()->OnData(controller, &audio_packet_[0],
audio_packet_.size());
base::MessageLoop::current()->RunUntilIdle();
EXPECT_NEAR(0.89926866f, volume_, 0.00001f);
EXPECT_FLOAT_EQ(0.75071919f, noise_volume_);
EXPECT_EQ(SPEECH_RECOGNITION_ERROR_NONE, error_);
EXPECT_FALSE(audio_ended_);
EXPECT_FALSE(recognition_ended_);
recognizer_->AbortRecognition();
base::MessageLoop::current()->RunUntilIdle();
CheckFinalEventsConsistency();
}
}