OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/command_line.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/extensions/extension_apitest.h" | |
9 #include "chrome/browser/extensions/speech_input/extension_speech_input_api.h" | |
10 #include "chrome/browser/extensions/speech_input/extension_speech_input_manager.
h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/common/chrome_notification_types.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "content/browser/speech/speech_recognizer.h" | |
15 #include "content/common/speech_input_result.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using content::BrowserThread; | |
19 | |
20 namespace net { | |
21 class URLRequestContextGetter; | |
22 } | |
23 | |
24 // Mock class used to test the extension speech input API. | |
25 class ExtensionSpeechInputApiTest : public ExtensionApiTest, | |
26 public ExtensionSpeechInterface { | |
27 public: | |
28 ExtensionSpeechInputApiTest(); | |
29 virtual ~ExtensionSpeechInputApiTest(); | |
30 | |
31 void SetRecordingDevicesAvailable(bool available) { | |
32 recording_devices_available_ = available; | |
33 } | |
34 | |
35 void SetRecognitionError(speech_input::SpeechInputError error) { | |
36 next_error_ = error; | |
37 } | |
38 | |
39 void SetRecognitionResult(const speech_input::SpeechInputResult& result) { | |
40 next_result_ = result; | |
41 } | |
42 | |
43 void SetRecognitionDelay(int result_delay_ms) { | |
44 result_delay_ms_ = result_delay_ms; | |
45 } | |
46 | |
47 // Used as delay when the corresponding call should not be dispatched. | |
48 static const int kDontDispatchCall = -1; | |
49 | |
50 // ExtensionApiTest methods. | |
51 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
52 ExtensionApiTest::SetUpCommandLine(command_line); | |
53 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | |
54 } | |
55 | |
56 // ExtensionSpeechInterface methods. | |
57 virtual bool HasAudioInputDevices() OVERRIDE { | |
58 return recording_devices_available_; | |
59 } | |
60 | |
61 virtual bool IsRecordingInProcess() OVERRIDE { | |
62 // Only the mock recognizer is supposed to be recording during testing. | |
63 return HasValidRecognizer(); | |
64 } | |
65 | |
66 virtual bool HasValidRecognizer() OVERRIDE { | |
67 return recognizer_is_valid_; | |
68 } | |
69 | |
70 virtual void StartRecording( | |
71 speech_input::SpeechRecognizerDelegate* delegate, | |
72 net::URLRequestContextGetter* context_getter, | |
73 int caller_id, | |
74 const std::string& language, | |
75 const std::string& grammar, | |
76 bool filter_profanities) OVERRIDE; | |
77 | |
78 virtual void StopRecording(bool recognition_failed) OVERRIDE; | |
79 | |
80 ExtensionSpeechInputManager* GetManager() { | |
81 return ExtensionSpeechInputManager::GetForProfile(browser()->profile()); | |
82 } | |
83 | |
84 // Auxiliary class used to hook the API manager into the test during its | |
85 // lifetime. Required since browser() is not available during the set up | |
86 // or tear down callbacks, or during the test class construction. | |
87 class AutoManagerHook { | |
88 public: | |
89 explicit AutoManagerHook(ExtensionSpeechInputApiTest* test) | |
90 : test_(test) { | |
91 test_->GetManager()->SetExtensionSpeechInterface(test_); | |
92 } | |
93 | |
94 ~AutoManagerHook() { | |
95 test_->GetManager()->SetExtensionSpeechInterface(NULL); | |
96 } | |
97 | |
98 private: | |
99 ExtensionSpeechInputApiTest* test_; | |
100 }; | |
101 | |
102 private: | |
103 void ProvideResults(int caller_id); | |
104 | |
105 bool recording_devices_available_; | |
106 bool recognizer_is_valid_; | |
107 speech_input::SpeechInputError next_error_; | |
108 speech_input::SpeechInputResult next_result_; | |
109 int result_delay_ms_; | |
110 }; | |
111 | |
112 ExtensionSpeechInputApiTest::ExtensionSpeechInputApiTest() | |
113 : recording_devices_available_(true), | |
114 recognizer_is_valid_(false), | |
115 next_error_(speech_input::kErrorNone), | |
116 result_delay_ms_(0) { | |
117 } | |
118 | |
119 ExtensionSpeechInputApiTest::~ExtensionSpeechInputApiTest() { | |
120 } | |
121 | |
122 void ExtensionSpeechInputApiTest::StartRecording( | |
123 speech_input::SpeechRecognizerDelegate* delegate, | |
124 net::URLRequestContextGetter* context_getter, | |
125 int caller_id, | |
126 const std::string& language, | |
127 const std::string& grammar, | |
128 bool filter_profanities) { | |
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
130 recognizer_is_valid_ = true; | |
131 | |
132 // Notify that recording started. | |
133 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
134 base::Bind(&ExtensionSpeechInputManager::DidStartReceivingAudio, | |
135 GetManager(), caller_id), 0); | |
136 | |
137 // Notify sound start in the input device. | |
138 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
139 base::Bind(&ExtensionSpeechInputManager::DidStartReceivingSpeech, | |
140 GetManager(), caller_id), 0); | |
141 | |
142 if (result_delay_ms_ != kDontDispatchCall) { | |
143 // Dispatch the recognition results. | |
144 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
145 base::Bind(&ExtensionSpeechInputApiTest::ProvideResults, this, | |
146 caller_id), result_delay_ms_); | |
147 } | |
148 } | |
149 | |
150 void ExtensionSpeechInputApiTest::StopRecording(bool recognition_failed) { | |
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
152 recognizer_is_valid_ = false; | |
153 } | |
154 | |
155 void ExtensionSpeechInputApiTest::ProvideResults(int caller_id) { | |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
157 | |
158 if (next_error_ != speech_input::kErrorNone) { | |
159 GetManager()->OnRecognizerError(caller_id, next_error_); | |
160 return; | |
161 } | |
162 | |
163 GetManager()->DidStopReceivingSpeech(caller_id); | |
164 GetManager()->SetRecognitionResult(caller_id, next_result_); | |
165 } | |
166 | |
167 // Every test should leave the manager in the idle state when finished. | |
168 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, StartStopTest) { | |
169 AutoManagerHook hook(this); | |
170 | |
171 SetRecognitionDelay(kDontDispatchCall); | |
172 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_; | |
173 } | |
174 | |
175 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, NoDevicesAvailable) { | |
176 AutoManagerHook hook(this); | |
177 | |
178 SetRecordingDevicesAvailable(false); | |
179 ASSERT_TRUE(RunExtensionTest("speech_input/start_error")) << message_; | |
180 } | |
181 | |
182 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, RecognitionSuccessful) { | |
183 AutoManagerHook hook(this); | |
184 | |
185 speech_input::SpeechInputResult result; | |
186 result.hypotheses.push_back( | |
187 speech_input::SpeechInputHypothesis(UTF8ToUTF16("this is a test"), 0.99)); | |
188 SetRecognitionResult(result); | |
189 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_; | |
190 } | |
191 | |
192 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, RecognitionError) { | |
193 AutoManagerHook hook(this); | |
194 | |
195 SetRecognitionError(speech_input::kErrorNetwork); | |
196 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_; | |
197 } | |
OLD | NEW |