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_recognizer_delegate.h" | 15 #include "content/public/browser/speech_recognition_event_listener.h" |
16 #include "content/public/common/speech_recognition_result.h" | 16 #include "content/public/common/speech_recognition_result.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using content::BrowserThread; | 19 using content::BrowserThread; |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 class URLRequestContextGetter; | 22 class URLRequestContextGetter; |
23 } | 23 } |
24 | 24 |
25 // Mock class used to test the extension speech input API. | 25 // Mock class used to test the extension speech input API. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 virtual bool IsCapturingAudio() OVERRIDE { | 67 virtual bool IsCapturingAudio() OVERRIDE { |
68 // Only the mock recognizer is supposed to be recording during testing. | 68 // Only the mock recognizer is supposed to be recording during testing. |
69 return HasValidRecognizer(); | 69 return HasValidRecognizer(); |
70 } | 70 } |
71 | 71 |
72 virtual bool HasValidRecognizer() OVERRIDE { | 72 virtual bool HasValidRecognizer() OVERRIDE { |
73 return recognizer_is_valid_; | 73 return recognizer_is_valid_; |
74 } | 74 } |
75 | 75 |
76 virtual void StartRecording( | 76 virtual void StartRecording( |
77 content::SpeechRecognizerDelegate* delegate, | 77 content::SpeechRecognitionEventListener* listener, |
78 net::URLRequestContextGetter* context_getter, | 78 net::URLRequestContextGetter* context_getter, |
79 int caller_id, | 79 int caller_id, |
80 const std::string& language, | 80 const std::string& language, |
81 const std::string& grammar, | 81 const std::string& grammar, |
82 bool filter_profanities) OVERRIDE; | 82 bool filter_profanities) OVERRIDE; |
83 | 83 |
84 virtual void StopRecording(bool recognition_failed) OVERRIDE; | 84 virtual void StopRecording(bool recognition_failed) OVERRIDE; |
85 | 85 |
86 SpeechInputExtensionManager* GetManager() { | 86 SpeechInputExtensionManager* GetManager() { |
87 return manager_.get(); | 87 return manager_.get(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 : recording_devices_available_(true), | 121 : recording_devices_available_(true), |
122 recognizer_is_valid_(false), | 122 recognizer_is_valid_(false), |
123 next_error_(content::SPEECH_RECOGNITION_ERROR_NONE), | 123 next_error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
124 result_delay_ms_(0) { | 124 result_delay_ms_(0) { |
125 } | 125 } |
126 | 126 |
127 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { | 127 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { |
128 } | 128 } |
129 | 129 |
130 void SpeechInputExtensionApiTest::StartRecording( | 130 void SpeechInputExtensionApiTest::StartRecording( |
131 content::SpeechRecognizerDelegate* delegate, | 131 content::SpeechRecognitionEventListener* listener, |
132 net::URLRequestContextGetter* context_getter, | 132 net::URLRequestContextGetter* context_getter, |
133 int caller_id, | 133 int caller_id, |
134 const std::string& language, | 134 const std::string& language, |
135 const std::string& grammar, | 135 const std::string& grammar, |
136 bool filter_profanities) { | 136 bool filter_profanities) { |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
138 recognizer_is_valid_ = true; | 138 recognizer_is_valid_ = true; |
139 | 139 |
140 // Notify that recording started. | 140 // Notify that recording started. |
141 MessageLoop::current()->PostDelayedTask( | 141 MessageLoop::current()->PostDelayedTask( |
142 FROM_HERE, | 142 FROM_HERE, |
143 base::Bind(&SpeechInputExtensionManager::DidStartReceivingAudio, | 143 base::Bind(&SpeechInputExtensionManager::OnAudioStart, |
144 GetManager(), | 144 GetManager(), |
145 caller_id), | 145 caller_id), |
146 base::TimeDelta()); | 146 base::TimeDelta()); |
147 | 147 |
148 // Notify sound start in the input device. | 148 // Notify sound start in the input device. |
149 MessageLoop::current()->PostDelayedTask( | 149 MessageLoop::current()->PostDelayedTask( |
150 FROM_HERE, | 150 FROM_HERE, |
151 base::Bind(&SpeechInputExtensionManager::DidStartReceivingSpeech, | 151 base::Bind(&SpeechInputExtensionManager::OnSoundStart, |
152 GetManager(), | 152 GetManager(), |
153 caller_id), | 153 caller_id), |
154 base::TimeDelta()); | 154 base::TimeDelta()); |
155 | 155 |
156 if (result_delay_ms_ != kDontDispatchCall) { | 156 if (result_delay_ms_ != kDontDispatchCall) { |
157 // Dispatch the recognition results. | 157 // Dispatch the recognition results. |
158 MessageLoop::current()->PostDelayedTask( | 158 MessageLoop::current()->PostDelayedTask( |
159 FROM_HERE, | 159 FROM_HERE, |
160 base::Bind(&SpeechInputExtensionApiTest::ProvideResults, | 160 base::Bind(&SpeechInputExtensionApiTest::ProvideResults, |
161 this, | 161 this, |
162 caller_id), | 162 caller_id), |
163 base::TimeDelta::FromMilliseconds(result_delay_ms_)); | 163 base::TimeDelta::FromMilliseconds(result_delay_ms_)); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { | 167 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { |
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
169 recognizer_is_valid_ = false; | 169 recognizer_is_valid_ = false; |
170 } | 170 } |
171 | 171 |
172 void SpeechInputExtensionApiTest::ProvideResults(int caller_id) { | 172 void SpeechInputExtensionApiTest::ProvideResults(int caller_id) { |
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
174 | 174 |
175 if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) { | 175 if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) { |
176 GetManager()->OnRecognizerError(caller_id, next_error_); | 176 GetManager()->OnRecognitionError(caller_id, next_error_); |
177 return; | 177 return; |
178 } | 178 } |
179 | 179 |
180 GetManager()->DidStopReceivingSpeech(caller_id); | 180 GetManager()->OnSoundEnd(caller_id); |
181 GetManager()->SetRecognitionResult(caller_id, next_result_); | 181 GetManager()->OnAudioEnd(caller_id); |
| 182 GetManager()->OnRecognitionResult(caller_id, next_result_); |
182 } | 183 } |
183 | 184 |
184 // Every test should leave the manager in the idle state when finished. | 185 // Every test should leave the manager in the idle state when finished. |
185 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { | 186 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { |
186 AutoManagerHook hook(this); | 187 AutoManagerHook hook(this); |
187 | 188 |
188 SetRecognitionDelay(kDontDispatchCall); | 189 SetRecognitionDelay(kDontDispatchCall); |
189 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; | 190 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; |
190 } | 191 } |
191 | 192 |
(...skipping 14 matching lines...) Expand all Loading... |
206 SetRecognitionResult(result); | 207 SetRecognitionResult(result); |
207 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; | 208 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; |
208 } | 209 } |
209 | 210 |
210 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { | 211 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { |
211 AutoManagerHook hook(this); | 212 AutoManagerHook hook(this); |
212 | 213 |
213 SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK); | 214 SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK); |
214 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; | 215 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; |
215 } | 216 } |
OLD | NEW |