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

Side by Side Diff: content/browser/speech/speech_recognition_browsertest.cc

Issue 10399025: Moved instantiation of SpeechRecognitionManager inside browser_main_loop, instead of Singleton. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed build issues. Created 8 years, 7 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
OLDNEW
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/file_path.h" 7 #include "base/file_path.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/synchronization/waitable_event.h" 10 #include "base/synchronization/waitable_event.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/test/base/in_process_browser_test.h" 13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h" 14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/browser/renderer_host/render_view_host_impl.h" 15 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/speech/input_tag_speech_dispatcher_host.h" 16 #include "content/browser/speech/input_tag_speech_dispatcher_host.h"
17 #include "content/browser/speech/speech_recognition_manager_impl.h"
18 #include "content/browser/web_contents/web_contents_impl.h" 17 #include "content/browser/web_contents/web_contents_impl.h"
19 #include "content/public/browser/notification_types.h" 18 #include "content/public/browser/notification_types.h"
19 #include "content/public/browser/speech_recognition_manager.h"
20 #include "content/public/browser/speech_recognition_session_config.h" 20 #include "content/public/browser/speech_recognition_session_config.h"
21 #include "content/public/browser/speech_recognition_session_context.h" 21 #include "content/public/browser/speech_recognition_session_context.h"
22 #include "content/public/common/content_switches.h" 22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/speech_recognition_error.h" 23 #include "content/public/common/speech_recognition_error.h"
24 #include "content/public/common/speech_recognition_result.h" 24 #include "content/public/common/speech_recognition_result.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
26 26
27 using content::SpeechRecognitionEventListener; 27 using content::SpeechRecognitionEventListener;
28 using content::SpeechRecognitionSessionConfig;
28 using content::SpeechRecognitionSessionContext; 29 using content::SpeechRecognitionSessionContext;
29 using content::NavigationController; 30 using content::NavigationController;
30 using content::WebContents; 31 using content::WebContents;
31 32
32 namespace speech { 33 namespace speech {
33 class FakeSpeechRecognitionManager; 34 class FakeSpeechRecognitionManager;
34 } 35 }
35 36
36 namespace speech { 37 namespace speech {
37 38
38 const char kTestResult[] = "Pictures of the moon"; 39 const char kTestResult[] = "Pictures of the moon";
39 40
40 class FakeSpeechRecognitionManager : public SpeechRecognitionManagerImpl { 41 class FakeSpeechRecognitionManager : public content::SpeechRecognitionManager {
41 public: 42 public:
42 FakeSpeechRecognitionManager() 43 FakeSpeechRecognitionManager()
43 : session_id_(0), 44 : session_id_(0),
44 listener_(NULL), 45 listener_(NULL),
45 did_cancel_all_(false), 46 did_cancel_all_(false),
46 should_send_fake_response_(true), 47 should_send_fake_response_(true),
47 recognition_started_event_(false, false) { 48 recognition_started_event_(false, false) {
48 } 49 }
49 50
50 std::string grammar() { 51 std::string grammar() {
(...skipping 19 matching lines...) Expand all
70 // SpeechRecognitionManager methods. 71 // SpeechRecognitionManager methods.
71 virtual int CreateSession( 72 virtual int CreateSession(
72 const content::SpeechRecognitionSessionConfig& config) OVERRIDE { 73 const content::SpeechRecognitionSessionConfig& config) OVERRIDE {
73 VLOG(1) << "FAKE CreateSession invoked."; 74 VLOG(1) << "FAKE CreateSession invoked.";
74 EXPECT_EQ(0, session_id_); 75 EXPECT_EQ(0, session_id_);
75 EXPECT_EQ(NULL, listener_); 76 EXPECT_EQ(NULL, listener_);
76 listener_ = config.event_listener; 77 listener_ = config.event_listener;
77 if (config.grammars.size() > 0) 78 if (config.grammars.size() > 0)
78 grammar_ = config.grammars[0].url; 79 grammar_ = config.grammars[0].url;
79 session_ctx_ = config.initial_context; 80 session_ctx_ = config.initial_context;
81 session_config_ = config;
80 session_id_ = 1; 82 session_id_ = 1;
81 return session_id_; 83 return session_id_;
82 } 84 }
83 85
84 virtual void StartSession(int session_id) OVERRIDE { 86 virtual void StartSession(int session_id) OVERRIDE {
85 VLOG(1) << "FAKE StartSession invoked."; 87 VLOG(1) << "FAKE StartSession invoked.";
86 EXPECT_EQ(session_id, session_id_); 88 EXPECT_EQ(session_id, session_id_);
87 EXPECT_TRUE(listener_ != NULL); 89 EXPECT_TRUE(listener_ != NULL);
88 90
89 if (should_send_fake_response_) { 91 if (should_send_fake_response_) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 virtual void ShowAudioInputSettings() OVERRIDE {} 131 virtual void ShowAudioInputSettings() OVERRIDE {}
130 132
131 virtual int LookupSessionByContext( 133 virtual int LookupSessionByContext(
132 base::Callback<bool( 134 base::Callback<bool(
133 const content::SpeechRecognitionSessionContext&)> matcher) 135 const content::SpeechRecognitionSessionContext&)> matcher)
134 const OVERRIDE { 136 const OVERRIDE {
135 bool matched = matcher.Run(session_ctx_); 137 bool matched = matcher.Run(session_ctx_);
136 return matched ? session_id_ : 0; 138 return matched ? session_id_ : 0;
137 } 139 }
138 140
141 virtual const SpeechRecognitionSessionConfig& GetSessionConfig(
142 int session_id) const OVERRIDE {
143 EXPECT_EQ(session_id, session_id_);
144 return session_config_;
145 }
146
139 virtual content::SpeechRecognitionSessionContext GetSessionContext( 147 virtual content::SpeechRecognitionSessionContext GetSessionContext(
140 int session_id) const OVERRIDE { 148 int session_id) const OVERRIDE {
141 EXPECT_EQ(session_id, session_id_); 149 EXPECT_EQ(session_id, session_id_);
142 return session_ctx_; 150 return session_ctx_;
143 } 151 }
144 152
145 private: 153 private:
146 void SetFakeRecognitionResult() { 154 void SetFakeRecognitionResult() {
147 if (session_id_) { // Do a check in case we were cancelled.. 155 if (session_id_) { // Do a check in case we were cancelled..
148 VLOG(1) << "Setting fake recognition result."; 156 VLOG(1) << "Setting fake recognition result.";
149 listener_->OnAudioEnd(session_id_); 157 listener_->OnAudioEnd(session_id_);
150 content::SpeechRecognitionResult results; 158 content::SpeechRecognitionResult results;
151 results.hypotheses.push_back(content::SpeechRecognitionHypothesis( 159 results.hypotheses.push_back(content::SpeechRecognitionHypothesis(
152 ASCIIToUTF16(kTestResult), 1.0)); 160 ASCIIToUTF16(kTestResult), 1.0));
153 listener_->OnRecognitionResult(session_id_, results); 161 listener_->OnRecognitionResult(session_id_, results);
154 listener_->OnRecognitionEnd(session_id_); 162 listener_->OnRecognitionEnd(session_id_);
155 session_id_ = 0; 163 session_id_ = 0;
156 listener_ = NULL; 164 listener_ = NULL;
157 VLOG(1) << "Finished setting fake recognition result."; 165 VLOG(1) << "Finished setting fake recognition result.";
158 } 166 }
159 } 167 }
160 168
161 int session_id_; 169 int session_id_;
162 SpeechRecognitionEventListener* listener_; 170 SpeechRecognitionEventListener* listener_;
171 SpeechRecognitionSessionConfig session_config_;
163 SpeechRecognitionSessionContext session_ctx_; 172 SpeechRecognitionSessionContext session_ctx_;
164 std::string grammar_; 173 std::string grammar_;
165 bool did_cancel_all_; 174 bool did_cancel_all_;
166 bool should_send_fake_response_; 175 bool should_send_fake_response_;
167 base::WaitableEvent recognition_started_event_; 176 base::WaitableEvent recognition_started_event_;
168 }; 177 };
169 178
170 class SpeechRecognitionBrowserTest : public InProcessBrowserTest { 179 class SpeechRecognitionBrowserTest : public InProcessBrowserTest {
171 public: 180 public:
172 // InProcessBrowserTest methods 181 // InProcessBrowserTest methods
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 EXPECT_EQ("pass", browser()->GetSelectedWebContents()->GetURL().ref()); 228 EXPECT_EQ("pass", browser()->GetSelectedWebContents()->GetURL().ref());
220 } 229 }
221 230
222 // InProcessBrowserTest methods. 231 // InProcessBrowserTest methods.
223 virtual void SetUpInProcessBrowserTestFixture() { 232 virtual void SetUpInProcessBrowserTestFixture() {
224 fake_speech_recognition_manager_.set_should_send_fake_response(true); 233 fake_speech_recognition_manager_.set_should_send_fake_response(true);
225 speech_recognition_manager_ = &fake_speech_recognition_manager_; 234 speech_recognition_manager_ = &fake_speech_recognition_manager_;
226 235
227 // Inject the fake manager factory so that the test result is returned to 236 // Inject the fake manager factory so that the test result is returned to
228 // the web page. 237 // the web page.
229 InputTagSpeechDispatcherHost::set_manager(speech_recognition_manager_); 238 InputTagSpeechDispatcherHost::SetManagerForTests(
239 speech_recognition_manager_);
230 } 240 }
231 241
232 virtual void TearDownInProcessBrowserTestFixture() { 242 virtual void TearDownInProcessBrowserTestFixture() {
233 speech_recognition_manager_ = NULL; 243 speech_recognition_manager_ = NULL;
234 } 244 }
235 245
236 FakeSpeechRecognitionManager fake_speech_recognition_manager_; 246 FakeSpeechRecognitionManager fake_speech_recognition_manager_;
237 247
238 // This is used by the static |fakeManager|, and it is a pointer rather than a 248 // This is used by the static |fakeManager|, and it is a pointer rather than a
239 // direct instance per the style guide. 249 // direct instance per the style guide.
240 static SpeechRecognitionManagerImpl* speech_recognition_manager_; 250 static content::SpeechRecognitionManager* speech_recognition_manager_;
241 }; 251 };
242 252
243 SpeechRecognitionManagerImpl* 253 content::SpeechRecognitionManager*
244 SpeechRecognitionBrowserTest::speech_recognition_manager_ = NULL; 254 SpeechRecognitionBrowserTest::speech_recognition_manager_ = NULL;
245 255
246 // TODO(satish): Once this flakiness has been fixed, add a second test here to 256 // TODO(satish): Once this flakiness has been fixed, add a second test here to
247 // check for sending many clicks in succession to the speech button and verify 257 // check for sending many clicks in succession to the speech button and verify
248 // that it doesn't cause any crash but works as expected. This should act as the 258 // that it doesn't cause any crash but works as expected. This should act as the
249 // test for http://crbug.com/59173 259 // test for http://crbug.com/59173
250 // 260 //
251 // TODO(satish): Similar to above, once this flakiness has been fixed add 261 // TODO(satish): Similar to above, once this flakiness has been fixed add
252 // another test here to check that when speech recognition is in progress and 262 // another test here to check that when speech recognition is in progress and
253 // a renderer crashes, we get a call to 263 // a renderer crashes, we get a call to
(...skipping 21 matching lines...) Expand all
275 285
276 // Make the renderer crash. This should trigger 286 // Make the renderer crash. This should trigger
277 // InputTagSpeechDispatcherHost to cancel all pending sessions. 287 // InputTagSpeechDispatcherHost to cancel all pending sessions.
278 GURL test_url("about:crash"); 288 GURL test_url("about:crash");
279 ui_test_utils::NavigateToURL(browser(), test_url); 289 ui_test_utils::NavigateToURL(browser(), test_url);
280 290
281 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all()); 291 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all());
282 } 292 }
283 293
284 } // namespace speech 294 } // namespace speech
OLDNEW
« no previous file with comments | « content/browser/speech/input_tag_speech_dispatcher_host.cc ('k') | content/browser/speech/speech_recognition_manager_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698