| 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" |
| 11 #include "chrome/browser/speech/speech_input_extension_manager.h" | 11 #include "chrome/browser/speech/speech_input_extension_manager.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "content/public/browser/speech_recognition_event_listener.h" | 15 #include "content/public/browser/speech_recognition_event_listener.h" |
| 16 #include "content/public/common/speech_recognition_error.h" | 16 #include "content/public/common/speech_recognition_error.h" |
| 17 #include "content/public/common/speech_recognition_result.h" | 17 #include "content/public/common/speech_recognition_result.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 class URLRequestContextGetter; | 23 class URLRequestContextGetter; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace { |
| 27 const int kSessionIDForTests = 0; |
| 28 } |
| 29 |
| 26 // Mock class used to test the extension speech input API. | 30 // Mock class used to test the extension speech input API. |
| 27 class SpeechInputExtensionApiTest : public ExtensionApiTest, | 31 class SpeechInputExtensionApiTest : public ExtensionApiTest, |
| 28 public SpeechInputExtensionInterface { | 32 public SpeechInputExtensionInterface { |
| 29 public: | 33 public: |
| 30 SpeechInputExtensionApiTest(); | 34 SpeechInputExtensionApiTest(); |
| 31 virtual ~SpeechInputExtensionApiTest(); | 35 virtual ~SpeechInputExtensionApiTest(); |
| 32 | 36 |
| 33 void SetRecordingDevicesAvailable(bool available) { | 37 void SetRecordingDevicesAvailable(bool available) { |
| 34 recording_devices_available_ = available; | 38 recording_devices_available_ = available; |
| 35 } | 39 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return HasValidRecognizer(); | 74 return HasValidRecognizer(); |
| 71 } | 75 } |
| 72 | 76 |
| 73 virtual bool HasValidRecognizer() OVERRIDE { | 77 virtual bool HasValidRecognizer() OVERRIDE { |
| 74 return recognizer_is_valid_; | 78 return recognizer_is_valid_; |
| 75 } | 79 } |
| 76 | 80 |
| 77 virtual void StartRecording( | 81 virtual void StartRecording( |
| 78 content::SpeechRecognitionEventListener* listener, | 82 content::SpeechRecognitionEventListener* listener, |
| 79 net::URLRequestContextGetter* context_getter, | 83 net::URLRequestContextGetter* context_getter, |
| 80 int session_id, | 84 const string16& extension_name, |
| 81 const std::string& language, | 85 const std::string& language, |
| 82 const std::string& grammar, | 86 const std::string& grammar, |
| 83 bool filter_profanities) OVERRIDE; | 87 bool filter_profanities, |
| 88 bool show_notification) OVERRIDE; |
| 84 | 89 |
| 85 virtual void StopRecording(bool recognition_failed) OVERRIDE; | 90 virtual void StopRecording(bool recognition_failed) OVERRIDE; |
| 86 | 91 |
| 87 SpeechInputExtensionManager* GetManager() { | 92 SpeechInputExtensionManager* GetManager() { |
| 88 return manager_.get(); | 93 return manager_.get(); |
| 89 } | 94 } |
| 90 | 95 |
| 91 // Auxiliary class used to hook the API manager into the test during its | 96 // 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 | 97 // lifetime. Required since browser() is not available during the set up |
| 93 // or tear down callbacks, or during the test class construction. | 98 // or tear down callbacks, or during the test class construction. |
| 94 class AutoManagerHook { | 99 class AutoManagerHook { |
| 95 public: | 100 public: |
| 96 explicit AutoManagerHook(SpeechInputExtensionApiTest* test) | 101 explicit AutoManagerHook(SpeechInputExtensionApiTest* test) |
| 97 : test_(test) { | 102 : test_(test) { |
| 98 test_->GetManager()->SetSpeechInputExtensionInterface(test_); | 103 test_->GetManager()->SetSpeechInputExtensionInterface(test_); |
| 99 } | 104 } |
| 100 | 105 |
| 101 ~AutoManagerHook() { | 106 ~AutoManagerHook() { |
| 102 test_->GetManager()->SetSpeechInputExtensionInterface(NULL); | 107 test_->GetManager()->SetSpeechInputExtensionInterface(NULL); |
| 103 } | 108 } |
| 104 | 109 |
| 105 private: | 110 private: |
| 106 SpeechInputExtensionApiTest* test_; | 111 SpeechInputExtensionApiTest* test_; |
| 107 }; | 112 }; |
| 108 | 113 |
| 109 private: | 114 private: |
| 110 void ProvideResults(int session_id); | 115 void ProvideResults(); |
| 111 | 116 |
| 112 bool recording_devices_available_; | 117 bool recording_devices_available_; |
| 113 bool recognizer_is_valid_; | 118 bool recognizer_is_valid_; |
| 114 content::SpeechRecognitionErrorCode next_error_; | 119 content::SpeechRecognitionErrorCode next_error_; |
| 115 content::SpeechRecognitionResult next_result_; | 120 content::SpeechRecognitionResult next_result_; |
| 116 int result_delay_ms_; | 121 int result_delay_ms_; |
| 117 | 122 |
| 118 scoped_refptr<SpeechInputExtensionManager> manager_; | 123 scoped_refptr<SpeechInputExtensionManager> manager_; |
| 119 }; | 124 }; |
| 120 | 125 |
| 121 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest() | 126 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest() |
| 122 : recording_devices_available_(true), | 127 : recording_devices_available_(true), |
| 123 recognizer_is_valid_(false), | 128 recognizer_is_valid_(false), |
| 124 next_error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 129 next_error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
| 125 result_delay_ms_(0) { | 130 result_delay_ms_(0) { |
| 126 } | 131 } |
| 127 | 132 |
| 128 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { | 133 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { |
| 129 } | 134 } |
| 130 | 135 |
| 131 void SpeechInputExtensionApiTest::StartRecording( | 136 void SpeechInputExtensionApiTest::StartRecording( |
| 132 content::SpeechRecognitionEventListener* listener, | 137 content::SpeechRecognitionEventListener* listener, |
| 133 net::URLRequestContextGetter* context_getter, | 138 net::URLRequestContextGetter* context_getter, |
| 134 int session_id, | 139 const string16& extension_name, |
| 135 const std::string& language, | 140 const std::string& language, |
| 136 const std::string& grammar, | 141 const std::string& grammar, |
| 137 bool filter_profanities) { | 142 bool filter_profanities, |
| 143 bool show_notification) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 139 recognizer_is_valid_ = true; | 145 recognizer_is_valid_ = true; |
| 140 | 146 |
| 141 // Notify that recording started. | 147 // Notify that recording started. |
| 142 MessageLoop::current()->PostDelayedTask( | 148 MessageLoop::current()->PostDelayedTask( |
| 143 FROM_HERE, | 149 FROM_HERE, |
| 144 base::Bind(&SpeechInputExtensionManager::OnAudioStart, | 150 base::Bind(&SpeechInputExtensionManager::OnAudioStart, |
| 145 GetManager(), | 151 GetManager(), |
| 146 session_id), | 152 kSessionIDForTests), |
| 147 base::TimeDelta()); | 153 base::TimeDelta()); |
| 148 | 154 |
| 149 // Notify sound start in the input device. | 155 // Notify sound start in the input device. |
| 150 MessageLoop::current()->PostDelayedTask( | 156 MessageLoop::current()->PostDelayedTask( |
| 151 FROM_HERE, | 157 FROM_HERE, |
| 152 base::Bind(&SpeechInputExtensionManager::OnSoundStart, | 158 base::Bind(&SpeechInputExtensionManager::OnSoundStart, |
| 153 GetManager(), | 159 GetManager(), |
| 154 session_id), | 160 kSessionIDForTests), |
| 155 base::TimeDelta()); | 161 base::TimeDelta()); |
| 156 | 162 |
| 157 if (result_delay_ms_ != kDontDispatchCall) { | 163 if (result_delay_ms_ != kDontDispatchCall) { |
| 158 // Dispatch the recognition results. | 164 // Dispatch the recognition results. |
| 159 MessageLoop::current()->PostDelayedTask( | 165 MessageLoop::current()->PostDelayedTask( |
| 160 FROM_HERE, | 166 FROM_HERE, |
| 161 base::Bind(&SpeechInputExtensionApiTest::ProvideResults, | 167 base::Bind(&SpeechInputExtensionApiTest::ProvideResults, this), |
| 162 this, | |
| 163 session_id), | |
| 164 base::TimeDelta::FromMilliseconds(result_delay_ms_)); | 168 base::TimeDelta::FromMilliseconds(result_delay_ms_)); |
| 165 } | 169 } |
| 166 } | 170 } |
| 167 | 171 |
| 168 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { | 172 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 170 recognizer_is_valid_ = false; | 174 recognizer_is_valid_ = false; |
| 171 } | 175 } |
| 172 | 176 |
| 173 void SpeechInputExtensionApiTest::ProvideResults(int session_id) { | 177 void SpeechInputExtensionApiTest::ProvideResults() { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 175 | 179 |
| 176 if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) { | 180 if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) { |
| 177 GetManager()->OnRecognitionError( | 181 GetManager()->OnRecognitionError( |
| 178 session_id, content::SpeechRecognitionError(next_error_)); | 182 kSessionIDForTests, content::SpeechRecognitionError(next_error_)); |
| 179 return; | 183 return; |
| 180 } | 184 } |
| 181 | 185 |
| 182 GetManager()->OnSoundEnd(session_id); | 186 GetManager()->OnSoundEnd(kSessionIDForTests); |
| 183 GetManager()->OnAudioEnd(session_id); | 187 GetManager()->OnAudioEnd(kSessionIDForTests); |
| 184 GetManager()->OnRecognitionResult(session_id, next_result_); | 188 GetManager()->OnRecognitionResult(kSessionIDForTests, next_result_); |
| 189 GetManager()->OnRecognitionEnd(kSessionIDForTests); |
| 185 } | 190 } |
| 186 | 191 |
| 187 // Every test should leave the manager in the idle state when finished. | 192 // Every test should leave the manager in the idle state when finished. |
| 188 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { | 193 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { |
| 189 AutoManagerHook hook(this); | 194 AutoManagerHook hook(this); |
| 190 | 195 |
| 191 SetRecognitionDelay(kDontDispatchCall); | 196 SetRecognitionDelay(kDontDispatchCall); |
| 192 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; | 197 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; |
| 193 } | 198 } |
| 194 | 199 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 209 SetRecognitionResult(result); | 214 SetRecognitionResult(result); |
| 210 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; | 215 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; |
| 211 } | 216 } |
| 212 | 217 |
| 213 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { | 218 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { |
| 214 AutoManagerHook hook(this); | 219 AutoManagerHook hook(this); |
| 215 | 220 |
| 216 SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK); | 221 SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK); |
| 217 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; | 222 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; |
| 218 } | 223 } |
| OLD | NEW |