Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: chrome/browser/extensions/speech_input/extension_speech_input_apitest.cc

Issue 8321003: Add API tests to the experimental Speech Input API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 namespace net {
19 class URLRequestContextGetter;
20 }
21
22 // Mock class used to test the extension speech input API.
23 class ExtensionSpeechInputApiTest : public ExtensionApiTest,
24 public ExtensionSpeechInterface {
25 public:
26 ExtensionSpeechInputApiTest();
27 virtual ~ExtensionSpeechInputApiTest();
28
29 void ResetTestSettings();
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 // ExtensionSpeechInputManager::SpeechInterface 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.
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_;
Aaron Boodman 2011/10/21 00:52:47 Please always initialize primitive values. In this
Leandro Graciá Gil 2011/10/21 10:53:58 Done.
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 // State variables are reset at the beginning of each test case.
114 }
115
116 ExtensionSpeechInputApiTest::~ExtensionSpeechInputApiTest() {
117 }
118
119 void ExtensionSpeechInputApiTest::ResetTestSettings() {
120 recording_devices_available_ = true;
Aaron Boodman 2011/10/21 00:52:47 Just do this in the contructor.
Leandro Graciá Gil 2011/10/21 10:53:58 Done.
121 recognizer_is_valid_ = false;
122 next_error_ = speech_input::kErrorNone;
123 next_result_ = speech_input::SpeechInputResult();
124 result_delay_ms_ = 0;
125 }
126
127 void ExtensionSpeechInputApiTest::StartRecording(
128 speech_input::SpeechRecognizerDelegate* delegate,
129 net::URLRequestContextGetter* context_getter,
130 int caller_id,
131 const std::string& language,
132 const std::string& grammar,
133 bool filter_profanities) {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
135 recognizer_is_valid_ = true;
136
137 // Notify that recording started.
138 MessageLoop::current()->PostDelayedTask(FROM_HERE,
139 base::Bind(&ExtensionSpeechInputManager::DidStartReceivingAudio,
140 GetManager(), caller_id), 0);
141
142 // Notify sound start in the input device.
143 MessageLoop::current()->PostDelayedTask(FROM_HERE,
144 base::Bind(&ExtensionSpeechInputManager::DidStartReceivingSpeech,
145 GetManager(), caller_id), 0);
146
147 if (result_delay_ms_ != kDontDispatchCall) {
148 // Dispatch the recognition results.
149 MessageLoop::current()->PostDelayedTask(FROM_HERE,
150 base::Bind(&ExtensionSpeechInputApiTest::ProvideResults, this,
151 caller_id), result_delay_ms_);
152 }
153 }
154
155 void ExtensionSpeechInputApiTest::StopRecording(bool recognition_failed) {
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
157 recognizer_is_valid_ = false;
158 }
159
160 void ExtensionSpeechInputApiTest::ProvideResults(int caller_id) {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
162
163 if (next_error_ != speech_input::kErrorNone) {
164 GetManager()->OnRecognizerError(caller_id, next_error_);
165 return;
166 }
167
168 GetManager()->DidStopReceivingSpeech(caller_id);
169 GetManager()->SetRecognitionResult(caller_id, next_result_);
170 }
171
172 // Every test should leave the manager in the idle state when finished.
173 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, StartStopTest) {
174 AutoManagerHook hook(this);
175 ResetTestSettings();
176
177 SetRecognitionDelay(kDontDispatchCall);
178 ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_;
179 }
180
181 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, NoDevicesAvailable) {
182 AutoManagerHook hook(this);
183 ResetTestSettings();
184
185 SetRecordingDevicesAvailable(false);
186 ASSERT_TRUE(RunExtensionTest("speech_input/start_error")) << message_;
187 }
188
189 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, RecognitionSuccessful) {
190 AutoManagerHook hook(this);
191 ResetTestSettings();
192
193 speech_input::SpeechInputResult result;
194 result.hypotheses.push_back(
195 speech_input::SpeechInputHypothesis(UTF8ToUTF16("this is a test"), 0.99));
196 SetRecognitionResult(result);
197 ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_;
198 }
199
200 IN_PROC_BROWSER_TEST_F(ExtensionSpeechInputApiTest, RecognitionError) {
201 AutoManagerHook hook(this);
202 ResetTestSettings();
203
204 SetRecognitionError(speech_input::kErrorNetwork);
205 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_;
206 }
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698