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_recognizer_delegate.h" |
16 #include "content/public/common/speech_input_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. |
26 class SpeechInputExtensionApiTest : public ExtensionApiTest, | 26 class SpeechInputExtensionApiTest : public ExtensionApiTest, |
27 public SpeechInputExtensionInterface { | 27 public SpeechInputExtensionInterface { |
28 public: | 28 public: |
29 SpeechInputExtensionApiTest(); | 29 SpeechInputExtensionApiTest(); |
30 virtual ~SpeechInputExtensionApiTest(); | 30 virtual ~SpeechInputExtensionApiTest(); |
31 | 31 |
32 void SetRecordingDevicesAvailable(bool available) { | 32 void SetRecordingDevicesAvailable(bool available) { |
33 recording_devices_available_ = available; | 33 recording_devices_available_ = available; |
34 } | 34 } |
35 | 35 |
36 void SetRecognitionError(content::SpeechInputError error) { | 36 void SetRecognitionError(content::SpeechRecognitionErrorCode error) { |
37 next_error_ = error; | 37 next_error_ = error; |
38 } | 38 } |
39 | 39 |
40 void SetRecognitionResult(const content::SpeechInputResult& result) { | 40 void SetRecognitionResult(const content::SpeechRecognitionResult& result) { |
41 next_result_ = result; | 41 next_result_ = result; |
42 } | 42 } |
43 | 43 |
44 void SetRecognitionDelay(int result_delay_ms) { | 44 void SetRecognitionDelay(int result_delay_ms) { |
45 result_delay_ms_ = result_delay_ms; | 45 result_delay_ms_ = result_delay_ms; |
46 } | 46 } |
47 | 47 |
48 // Used as delay when the corresponding call should not be dispatched. | 48 // Used as delay when the corresponding call should not be dispatched. |
49 static const int kDontDispatchCall = -1; | 49 static const int kDontDispatchCall = -1; |
50 | 50 |
51 // ExtensionApiTest methods. | 51 // ExtensionApiTest methods. |
52 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 52 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
53 ExtensionApiTest::SetUpCommandLine(command_line); | 53 ExtensionApiTest::SetUpCommandLine(command_line); |
54 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 54 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
55 } | 55 } |
56 | 56 |
57 // SpeechInputExtensionInterface methods. | 57 // SpeechInputExtensionInterface methods. |
58 virtual bool HasAudioInputDevices() OVERRIDE { | 58 virtual bool HasAudioInputDevices() OVERRIDE { |
59 return recording_devices_available_; | 59 return recording_devices_available_; |
60 } | 60 } |
61 | 61 |
62 virtual bool IsRecordingInProcess() OVERRIDE { | 62 virtual bool IsCapturingAudio() OVERRIDE { |
63 // Only the mock recognizer is supposed to be recording during testing. | 63 // Only the mock recognizer is supposed to be recording during testing. |
64 return HasValidRecognizer(); | 64 return HasValidRecognizer(); |
65 } | 65 } |
66 | 66 |
67 virtual bool HasValidRecognizer() OVERRIDE { | 67 virtual bool HasValidRecognizer() OVERRIDE { |
68 return recognizer_is_valid_; | 68 return recognizer_is_valid_; |
69 } | 69 } |
70 | 70 |
71 virtual void StartRecording( | 71 virtual void StartRecording( |
72 content::SpeechRecognizerDelegate* delegate, | 72 content::SpeechRecognizerDelegate* delegate, |
(...skipping 25 matching lines...) Expand all Loading... |
98 | 98 |
99 private: | 99 private: |
100 SpeechInputExtensionApiTest* test_; | 100 SpeechInputExtensionApiTest* test_; |
101 }; | 101 }; |
102 | 102 |
103 private: | 103 private: |
104 void ProvideResults(int caller_id); | 104 void ProvideResults(int caller_id); |
105 | 105 |
106 bool recording_devices_available_; | 106 bool recording_devices_available_; |
107 bool recognizer_is_valid_; | 107 bool recognizer_is_valid_; |
108 content::SpeechInputError next_error_; | 108 content::SpeechRecognitionErrorCode next_error_; |
109 content::SpeechInputResult next_result_; | 109 content::SpeechRecognitionResult next_result_; |
110 int result_delay_ms_; | 110 int result_delay_ms_; |
111 }; | 111 }; |
112 | 112 |
113 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest() | 113 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest() |
114 : recording_devices_available_(true), | 114 : recording_devices_available_(true), |
115 recognizer_is_valid_(false), | 115 recognizer_is_valid_(false), |
116 next_error_(content::SPEECH_INPUT_ERROR_NONE), | 116 next_error_(content::SPEECH_RECOGNITION_ERROR_NONE), |
117 result_delay_ms_(0) { | 117 result_delay_ms_(0) { |
118 } | 118 } |
119 | 119 |
120 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { | 120 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() { |
121 } | 121 } |
122 | 122 |
123 void SpeechInputExtensionApiTest::StartRecording( | 123 void SpeechInputExtensionApiTest::StartRecording( |
124 content::SpeechRecognizerDelegate* delegate, | 124 content::SpeechRecognizerDelegate* delegate, |
125 net::URLRequestContextGetter* context_getter, | 125 net::URLRequestContextGetter* context_getter, |
126 int caller_id, | 126 int caller_id, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 } | 158 } |
159 | 159 |
160 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { | 160 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) { |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
162 recognizer_is_valid_ = false; | 162 recognizer_is_valid_ = false; |
163 } | 163 } |
164 | 164 |
165 void SpeechInputExtensionApiTest::ProvideResults(int caller_id) { | 165 void SpeechInputExtensionApiTest::ProvideResults(int caller_id) { |
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
167 | 167 |
168 if (next_error_ != content::SPEECH_INPUT_ERROR_NONE) { | 168 if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) { |
169 GetManager()->OnRecognizerError(caller_id, next_error_); | 169 GetManager()->OnRecognizerError(caller_id, next_error_); |
170 return; | 170 return; |
171 } | 171 } |
172 | 172 |
173 GetManager()->DidStopReceivingSpeech(caller_id); | 173 GetManager()->DidStopReceivingSpeech(caller_id); |
174 GetManager()->SetRecognitionResult(caller_id, next_result_); | 174 GetManager()->SetRecognitionResult(caller_id, next_result_); |
175 } | 175 } |
176 | 176 |
177 // Every test should leave the manager in the idle state when finished. | 177 // Every test should leave the manager in the idle state when finished. |
178 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { | 178 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) { |
179 AutoManagerHook hook(this); | 179 AutoManagerHook hook(this); |
180 | 180 |
181 SetRecognitionDelay(kDontDispatchCall); | 181 SetRecognitionDelay(kDontDispatchCall); |
182 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; | 182 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; |
183 } | 183 } |
184 | 184 |
185 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, NoDevicesAvailable) { | 185 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, NoDevicesAvailable) { |
186 AutoManagerHook hook(this); | 186 AutoManagerHook hook(this); |
187 | 187 |
188 SetRecordingDevicesAvailable(false); | 188 SetRecordingDevicesAvailable(false); |
189 ASSERT_TRUE(RunExtensionTest("speech_input/start_error")) << message_; | 189 ASSERT_TRUE(RunExtensionTest("speech_input/start_error")) << message_; |
190 } | 190 } |
191 | 191 |
192 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionSuccessful) { | 192 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionSuccessful) { |
193 AutoManagerHook hook(this); | 193 AutoManagerHook hook(this); |
194 | 194 |
195 content::SpeechInputResult result; | 195 content::SpeechRecognitionResult result; |
196 result.hypotheses.push_back( | 196 result.hypotheses.push_back( |
197 content::SpeechInputHypothesis(UTF8ToUTF16("this is a test"), 0.99)); | 197 content::SpeechRecognitionHypothesis( |
| 198 UTF8ToUTF16("this is a test"), 0.99)); |
198 SetRecognitionResult(result); | 199 SetRecognitionResult(result); |
199 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; | 200 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; |
200 } | 201 } |
201 | 202 |
202 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { | 203 IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) { |
203 AutoManagerHook hook(this); | 204 AutoManagerHook hook(this); |
204 | 205 |
205 SetRecognitionError(content::SPEECH_INPUT_ERROR_NETWORK); | 206 SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK); |
206 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; | 207 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; |
207 } | 208 } |
OLD | NEW |