| 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/google_one_shot_remote_engine.h" | 8 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 9 #include "content/browser/speech/speech_recognizer.h" | 9 #include "content/browser/speech/speech_recognizer.h" |
| 10 #include "content/public/browser/speech_recognition_event_listener.h" | 10 #include "content/public/browser/speech_recognition_event_listener.h" |
| 11 #include "media/audio/audio_manager.h" | 11 #include "media/audio/mock_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/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
| 17 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 using base::MessageLoopProxy; | 20 using base::MessageLoopProxy; |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 using content::BrowserThreadImpl; | 22 using content::BrowserThreadImpl; |
| 23 using media::AudioInputController; | 23 using media::AudioInputController; |
| 24 using media::AudioInputStream; | 24 using media::AudioInputStream; |
| 25 using media::AudioManager; | 25 using media::AudioManager; |
| 26 using media::AudioOutputStream; | 26 using media::AudioOutputStream; |
| 27 using media::AudioParameters; | 27 using media::AudioParameters; |
| 28 using media::TestAudioInputController; | 28 using media::TestAudioInputController; |
| 29 using media::TestAudioInputControllerFactory; | 29 using media::TestAudioInputControllerFactory; |
| 30 | 30 |
| 31 namespace { | |
| 32 | |
| 33 class MockAudioManager : public media::AudioManagerBase { | |
| 34 public: | |
| 35 MockAudioManager() { | |
| 36 audio_thread_.reset(new base::Thread("MockAudioThread")); | |
| 37 CHECK(audio_thread_->Start()); | |
| 38 } | |
| 39 virtual bool HasAudioOutputDevices() OVERRIDE { return true; } | |
| 40 virtual bool HasAudioInputDevices() OVERRIDE { return true; } | |
| 41 virtual string16 GetAudioInputDeviceModel() OVERRIDE { return string16(); } | |
| 42 virtual bool CanShowAudioInputSettings() OVERRIDE { return false; } | |
| 43 virtual void ShowAudioInputSettings() OVERRIDE {} | |
| 44 virtual void GetAudioInputDeviceNames( | |
| 45 media::AudioDeviceNames* device_names) OVERRIDE {} | |
| 46 virtual AudioOutputStream* MakeAudioOutputStream( | |
| 47 const AudioParameters& params) OVERRIDE { | |
| 48 return media::FakeAudioOutputStream::MakeFakeStream(this, params); | |
| 49 } | |
| 50 virtual AudioOutputStream* MakeAudioOutputStreamProxy( | |
| 51 const AudioParameters& params) OVERRIDE { | |
| 52 NOTREACHED(); | |
| 53 return NULL; | |
| 54 } | |
| 55 virtual AudioInputStream* MakeAudioInputStream( | |
| 56 const AudioParameters& params, const std::string& device_id) OVERRIDE { | |
| 57 return media::FakeAudioInputStream::MakeFakeStream(this, params); | |
| 58 } | |
| 59 virtual AudioOutputStream* MakeLinearOutputStream( | |
| 60 const AudioParameters& params) OVERRIDE { | |
| 61 NOTREACHED(); | |
| 62 return NULL; | |
| 63 } | |
| 64 virtual AudioOutputStream* MakeLowLatencyOutputStream( | |
| 65 const AudioParameters& params) OVERRIDE { | |
| 66 NOTREACHED(); | |
| 67 return NULL; | |
| 68 } | |
| 69 virtual AudioInputStream* MakeLinearInputStream( | |
| 70 const AudioParameters& params, const std::string& device_id) OVERRIDE { | |
| 71 NOTREACHED(); | |
| 72 return NULL; | |
| 73 } | |
| 74 virtual AudioInputStream* MakeLowLatencyInputStream( | |
| 75 const AudioParameters& params, const std::string& device_id) OVERRIDE { | |
| 76 NOTREACHED(); | |
| 77 return NULL; | |
| 78 } | |
| 79 virtual void MuteAll() OVERRIDE {} | |
| 80 virtual void UnMuteAll() OVERRIDE {} | |
| 81 virtual bool IsRecordingInProcess() OVERRIDE { return false; } | |
| 82 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE { | |
| 83 return audio_thread_->message_loop_proxy(); | |
| 84 } | |
| 85 virtual void Init() OVERRIDE {}; | |
| 86 private: | |
| 87 scoped_ptr<base::Thread> audio_thread_; | |
| 88 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); | |
| 89 }; | |
| 90 } // namespace | |
| 91 | |
| 92 | |
| 93 namespace speech { | 31 namespace speech { |
| 94 | 32 |
| 95 class SpeechRecognizerTest : public content::SpeechRecognitionEventListener, | 33 class SpeechRecognizerTest : public content::SpeechRecognitionEventListener, |
| 96 public testing::Test { | 34 public testing::Test { |
| 97 public: | 35 public: |
| 98 SpeechRecognizerTest() | 36 SpeechRecognizerTest() |
| 99 : io_thread_(BrowserThread::IO, &message_loop_), | 37 : io_thread_(BrowserThread::IO, &message_loop_), |
| 100 audio_manager_(new MockAudioManager()), | |
| 101 recognition_started_(false), | 38 recognition_started_(false), |
| 102 recognition_ended_(false), | 39 recognition_ended_(false), |
| 103 result_received_(false), | 40 result_received_(false), |
| 104 audio_started_(false), | 41 audio_started_(false), |
| 105 audio_ended_(false), | 42 audio_ended_(false), |
| 106 sound_started_(false), | 43 sound_started_(false), |
| 107 sound_ended_(false), | 44 sound_ended_(false), |
| 108 error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 45 error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
| 109 volume_(-1.0f) { | 46 volume_(-1.0f) { |
| 110 // SpeechRecognizer takes ownership of sr_engine. | 47 // SpeechRecognizer takes ownership of sr_engine. |
| 111 SpeechRecognitionEngine* sr_engine = | 48 SpeechRecognitionEngine* sr_engine = |
| 112 new GoogleOneShotRemoteEngine(NULL /* URLRequestContextGetter */); | 49 new GoogleOneShotRemoteEngine(NULL /* URLRequestContextGetter */); |
| 113 SpeechRecognitionEngineConfig config; | 50 SpeechRecognitionEngineConfig config; |
| 114 config.audio_num_bits_per_sample = SpeechRecognizer::kNumBitsPerAudioSample; | 51 config.audio_num_bits_per_sample = SpeechRecognizer::kNumBitsPerAudioSample; |
| 115 config.audio_sample_rate = SpeechRecognizer::kAudioSampleRate; | 52 config.audio_sample_rate = SpeechRecognizer::kAudioSampleRate; |
| 116 config.filter_profanities = false; | 53 config.filter_profanities = false; |
| 117 sr_engine->SetConfig(config); | 54 sr_engine->SetConfig(config); |
| 118 | 55 |
| 119 const int kTestingSessionId = 1; | 56 const int kTestingSessionId = 1; |
| 120 const bool kOneShotMode = true; | 57 const bool kOneShotMode = true; |
| 121 recognizer_ = new SpeechRecognizer( | 58 recognizer_ = new SpeechRecognizer( |
| 122 this, kTestingSessionId, kOneShotMode, sr_engine); | 59 this, kTestingSessionId, kOneShotMode, sr_engine); |
| 123 recognizer_->SetAudioManagerForTesting(audio_manager_.get()); | 60 audio_manager_.reset(new media::MockAudioManager( |
| 61 MessageLoop::current()->message_loop_proxy())); |
| 62 recognizer_->SetAudioManagerForTests(audio_manager_.get()); |
| 124 | 63 |
| 125 int audio_packet_length_bytes = | 64 int audio_packet_length_bytes = |
| 126 (SpeechRecognizer::kAudioSampleRate * | 65 (SpeechRecognizer::kAudioSampleRate * |
| 127 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * | 66 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs * |
| 128 ChannelLayoutToChannelCount(SpeechRecognizer::kChannelLayout) * | 67 ChannelLayoutToChannelCount(SpeechRecognizer::kChannelLayout) * |
| 129 SpeechRecognizer::kNumBitsPerAudioSample) / (8 * 1000); | 68 SpeechRecognizer::kNumBitsPerAudioSample) / (8 * 1000); |
| 130 audio_packet_.resize(audio_packet_length_bytes); | 69 audio_packet_.resize(audio_packet_length_bytes); |
| 131 } | 70 } |
| 132 | 71 |
| 133 void CheckEventsConsistency() { | 72 void CheckEventsConsistency() { |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 | 490 |
| 552 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); | 491 EXPECT_EQ(content::SPEECH_RECOGNITION_ERROR_NONE, error_); |
| 553 EXPECT_FALSE(audio_ended_); | 492 EXPECT_FALSE(audio_ended_); |
| 554 EXPECT_FALSE(recognition_ended_); | 493 EXPECT_FALSE(recognition_ended_); |
| 555 recognizer_->AbortRecognition(); | 494 recognizer_->AbortRecognition(); |
| 556 MessageLoop::current()->RunAllPending(); | 495 MessageLoop::current()->RunAllPending(); |
| 557 CheckFinalEventsConsistency(); | 496 CheckFinalEventsConsistency(); |
| 558 } | 497 } |
| 559 | 498 |
| 560 } // namespace speech | 499 } // namespace speech |
| OLD | NEW |