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

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