| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/speech/speech_input_extension_api.h" | 10 #include "chrome/browser/speech/speech_input_extension_api.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return HasValidRecognizer(); | 70 return HasValidRecognizer(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 virtual bool HasValidRecognizer() OVERRIDE { | 73 virtual bool HasValidRecognizer() OVERRIDE { |
| 74 return recognizer_is_valid_; | 74 return recognizer_is_valid_; |
| 75 } | 75 } |
| 76 | 76 |
| 77 virtual void StartRecording( | 77 virtual void StartRecording( |
| 78 content::SpeechRecognitionEventListener* listener, | 78 content::SpeechRecognitionEventListener* listener, |
| 79 net::URLRequestContextGetter* context_getter, | 79 net::URLRequestContextGetter* context_getter, |
| 80 int caller_id, | 80 int session_id, |
| 81 const std::string& language, | 81 const std::string& language, |
| 82 const std::string& grammar, | 82 const std::string& grammar, |
| 83 bool filter_profanities) OVERRIDE; | 83 bool filter_profanities) OVERRIDE; |
| 84 | 84 |
| 85 virtual void StopRecording(bool recognition_failed) OVERRIDE; | 85 virtual void StopRecording(bool recognition_failed) OVERRIDE; |
| 86 | 86 |
| 87 SpeechInputExtensionManager* GetManager() { | 87 SpeechInputExtensionManager* GetManager() { |
| 88 return manager_.get(); | 88 return manager_.get(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Auxiliary class used to hook the API manager into the test during its | 91 // Auxiliary class used to hook the API manager into the test during its |
| 92 // lifetime. Required since browser() is not available during the set up | 92 // lifetime. Required since browser() is not available during the set up |
| 93 // or tear down callbacks, or during the test class construction. | 93 // or tear down callbacks, or during the test class construction. |
| 94 class AutoManagerHook { | 94 class AutoManagerHook { |
| 95 public: | 95 public: |
| 96 explicit AutoManagerHook(SpeechInputExtensionApiTest* test) | 96 explicit AutoManagerHook(SpeechInputExtensionApiTest* test) |
| 97 : test_(test) { | 97 : test_(test) { |
| 98 test_->GetManager()->SetSpeechInputExtensionInterface(test_); | 98 test_->GetManager()->SetSpeechInputExtensionInterface(test_); |
| 99 } | 99 } |
| 100 | 100 |
| 101 ~AutoManagerHook() { | 101 ~AutoManagerHook() { |
| 102 test_->GetManager()->SetSpeechInputExtensionInterface(NULL); | 102 test_->GetManager()->SetSpeechInputExtensionInterface(NULL); |
| 103 } | 103 } |
| 104 | 104 |
| 105 private: | 105 private: |
| 106 SpeechInputExtensionApiTest* test_; | 106 SpeechInputExtensionApiTest* test_; |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 void ProvideResults(int caller_id); | 110 void ProvideResults(int session_id); |
| 111 | 111 |
| 112 bool recording_devices_available_; | 112 bool recording_devices_available_; |
| 113 bool recognizer_is_valid_; | 113 bool recognizer_is_valid_; |
| 114 content::SpeechRecognitionErrorCode next_error_; | 114 content::SpeechRecognitionErrorCode next_error_; |
| 115 content::SpeechRecognitionResult next_result_; | 115 content::SpeechRecognitionResult next_result_; |
| 116 int result_delay_ms_; | 116 int result_delay_ms_; |
| 117 | 117 |
| 118 scoped_refptr<SpeechInputExtensionManager> manager_; | 118 scoped_refptr<SpeechInputExtensionManager> manager_; |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest() | 121 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest() |
| 122 : recording_devices_available_(true), | 122 : recording_devices_available_(true), |
| 123 recognizer_is_valid_(false), | 123 recognizer_is_valid_(false), |
| 124 next_error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 124 next_error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
| 125 result_delay_ms_(0) { | 125 result_delay_ms_(0) { |
| 126 } | 126 } |
| 127 | 127 |
| 128 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { | 128 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { |
| 129 } | 129 } |
| 130 | 130 |
| 131 void SpeechInputExtensionApiTest::StartRecording( | 131 void SpeechInputExtensionApiTest::StartRecording( |
| 132 content::SpeechRecognitionEventListener* listener, | 132 content::SpeechRecognitionEventListener* listener, |
| 133 net::URLRequestContextGetter* context_getter, | 133 net::URLRequestContextGetter* context_getter, |
| 134 int caller_id, | 134 int session_id, |
| 135 const std::string& language, | 135 const std::string& language, |
| 136 const std::string& grammar, | 136 const std::string& grammar, |
| 137 bool filter_profanities) { | 137 bool filter_profanities) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 139 recognizer_is_valid_ = true; | 139 recognizer_is_valid_ = true; |
| 140 | 140 |
| 141 // Notify that recording started. | 141 // Notify that recording started. |
| 142 MessageLoop::current()->PostDelayedTask( | 142 MessageLoop::current()->PostDelayedTask( |
| 143 FROM_HERE, | 143 FROM_HERE, |
| 144 base::Bind(&SpeechInputExtensionManager::OnAudioStart, | 144 base::Bind(&SpeechInputExtensionManager::OnAudioStart, |
| 145 GetManager(), | 145 GetManager(), |
| 146 caller_id), | 146 session_id), |
| 147 base::TimeDelta()); | 147 base::TimeDelta()); |
| 148 | 148 |
| 149 // Notify sound start in the input device. | 149 // Notify sound start in the input device. |
| 150 MessageLoop::current()->PostDelayedTask( | 150 MessageLoop::current()->PostDelayedTask( |
| 151 FROM_HERE, | 151 FROM_HERE, |
| 152 base::Bind(&SpeechInputExtensionManager::OnSoundStart, | 152 base::Bind(&SpeechInputExtensionManager::OnSoundStart, |
| 153 GetManager(), | 153 GetManager(), |
| 154 caller_id), | 154 session_id), |
| 155 base::TimeDelta()); | 155 base::TimeDelta()); |
| 156 | 156 |
| 157 if (result_delay_ms_ != kDontDispatchCall) { | 157 if (result_delay_ms_ != kDontDispatchCall) { |
| 158 // Dispatch the recognition results. | 158 // Dispatch the recognition results. |
| 159 MessageLoop::current()->PostDelayedTask( | 159 MessageLoop::current()->PostDelayedTask( |
| 160 FROM_HERE, | 160 FROM_HERE, |
| 161 base::Bind(&SpeechInputExtensionApiTest::ProvideResults, | 161 base::Bind(&SpeechInputExtensionApiTest::ProvideResults, |
| 162 this, | 162 this, |
| 163 caller_id), | 163 session_id), |
| 164 base::TimeDelta::FromMilliseconds(result_delay_ms_)); | 164 base::TimeDelta::FromMilliseconds(result_delay_ms_)); |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { | 168 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 170 recognizer_is_valid_ = false; | 170 recognizer_is_valid_ = false; |
| 171 } | 171 } |
| 172 | 172 |
| 173 void SpeechInputExtensionApiTest::ProvideResults(int caller_id) { | 173 void SpeechInputExtensionApiTest::ProvideResults(int session_id) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 175 | 175 |
| 176 if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) { | 176 if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) { |
| 177 GetManager()->OnRecognitionError(caller_id, next_error_); | 177 GetManager()->OnRecognitionError(session_id, next_error_); |
| 178 return; | 178 return; |
| 179 } | 179 } |
| 180 | 180 |
| 181 GetManager()->OnSoundEnd(caller_id); | 181 GetManager()->OnSoundEnd(session_id); |
| 182 GetManager()->OnAudioEnd(caller_id); | 182 GetManager()->OnAudioEnd(session_id); |
| 183 GetManager()->OnRecognitionResult(caller_id, next_result_); | 183 GetManager()->OnRecognitionResult(session_id, next_result_); |
| 184 } | 184 } |
| 185 | 185 |
| 186 // Every test should leave the manager in the idle state when finished. | 186 // Every test should leave the manager in the idle state when finished. |
| 187 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { | 187 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { |
| 188 AutoManagerHook hook(this); | 188 AutoManagerHook hook(this); |
| 189 | 189 |
| 190 SetRecognitionDelay(kDontDispatchCall); | 190 SetRecognitionDelay(kDontDispatchCall); |
| 191 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; | 191 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; |
| 192 } | 192 } |
| 193 | 193 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 208 SetRecognitionResult(result); | 208 SetRecognitionResult(result); |
| 209 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; | 209 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; |
| 210 } | 210 } |
| 211 | 211 |
| 212 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { | 212 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { |
| 213 AutoManagerHook hook(this); | 213 AutoManagerHook hook(this); |
| 214 | 214 |
| 215 SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK); | 215 SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK); |
| 216 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; | 216 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; |
| 217 } | 217 } |
| OLD | NEW |