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

Side by Side Diff: chrome/browser/speech/speech_input_extension_apitest.cc

Issue 8508051: Revert 109285 - Seems to have broken chrome_frame_net_tests on the win_rel trybot, stuffing up th... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 1 month 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
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/speech/speech_input_extension_api.h"
10 #include "chrome/browser/speech/speech_input_extension_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 SpeechInputExtensionApiTest : public ExtensionApiTest,
26 public SpeechInputExtensionInterface {
27 public:
28 SpeechInputExtensionApiTest();
29 virtual ~SpeechInputExtensionApiTest();
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 // SpeechInputExtensionInterface 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 SpeechInputExtensionManager* GetManager() {
81 return SpeechInputExtensionManager::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(SpeechInputExtensionApiTest* test)
90 : test_(test) {
91 test_->GetManager()->SetSpeechInputExtensionInterface(test_);
92 }
93
94 ~AutoManagerHook() {
95 test_->GetManager()->SetSpeechInputExtensionInterface(NULL);
96 }
97
98 private:
99 SpeechInputExtensionApiTest* 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 SpeechInputExtensionApiTest::SpeechInputExtensionApiTest()
113 : recording_devices_available_(true),
114 recognizer_is_valid_(false),
115 next_error_(speech_input::kErrorNone),
116 result_delay_ms_(0) {
117 }
118
119 SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() {
120 }
121
122 void SpeechInputExtensionApiTest::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(&SpeechInputExtensionManager::DidStartReceivingAudio,
135 GetManager(), caller_id), 0);
136
137 // Notify sound start in the input device.
138 MessageLoop::current()->PostDelayedTask(FROM_HERE,
139 base::Bind(&SpeechInputExtensionManager::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(&SpeechInputExtensionApiTest::ProvideResults, this,
146 caller_id), result_delay_ms_);
147 }
148 }
149
150 void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
152 recognizer_is_valid_ = false;
153 }
154
155 void SpeechInputExtensionApiTest::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(SpeechInputExtensionApiTest, 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(SpeechInputExtensionApiTest, 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(SpeechInputExtensionApiTest, 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(SpeechInputExtensionApiTest, RecognitionError) {
193 AutoManagerHook hook(this);
194
195 SetRecognitionError(speech_input::kErrorNetwork);
196 ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_;
197 }
OLDNEW
« no previous file with comments | « chrome/browser/speech/speech_input_extension_api.cc ('k') | chrome/browser/speech/speech_input_extension_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698