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

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 FakeSpeechRecognitionManager. 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 18 matching lines...) Expand all
69 70
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 grammar_ = config.grammar; 78 grammar_ = config.grammar;
78 session_ctx_ = config.initial_context; 79 session_ctx_ = config.initial_context;
80 session_config_ = config;
79 session_id_ = 1; 81 session_id_ = 1;
80 return session_id_; 82 return session_id_;
81 } 83 }
82 84
83 virtual void StartSession(int session_id) OVERRIDE { 85 virtual void StartSession(int session_id) OVERRIDE {
84 VLOG(1) << "FAKE StartSession invoked."; 86 VLOG(1) << "FAKE StartSession invoked.";
85 EXPECT_EQ(session_id, session_id_); 87 EXPECT_EQ(session_id, session_id_);
86 EXPECT_TRUE(listener_ != NULL); 88 EXPECT_TRUE(listener_ != NULL);
87 89
88 if (should_send_fake_response_) { 90 if (should_send_fake_response_) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 virtual void ShowAudioInputSettings() OVERRIDE {} 130 virtual void ShowAudioInputSettings() OVERRIDE {}
129 131
130 virtual int LookupSessionByContext( 132 virtual int LookupSessionByContext(
131 base::Callback<bool( 133 base::Callback<bool(
132 const content::SpeechRecognitionSessionContext&)> matcher) 134 const content::SpeechRecognitionSessionContext&)> matcher)
133 const OVERRIDE { 135 const OVERRIDE {
134 bool matched = matcher.Run(session_ctx_); 136 bool matched = matcher.Run(session_ctx_);
135 return matched ? session_id_ : 0; 137 return matched ? session_id_ : 0;
136 } 138 }
137 139
140 virtual const SpeechRecognitionSessionConfig& GetSessionConfig(
141 int session_id) const OVERRIDE {
142 EXPECT_EQ(session_id, session_id_);
143 return session_config_;
144 }
145
138 virtual content::SpeechRecognitionSessionContext GetSessionContext( 146 virtual content::SpeechRecognitionSessionContext GetSessionContext(
139 int session_id) const OVERRIDE { 147 int session_id) const OVERRIDE {
140 EXPECT_EQ(session_id, session_id_); 148 EXPECT_EQ(session_id, session_id_);
141 return session_ctx_; 149 return session_ctx_;
142 } 150 }
143 151
144 private: 152 private:
145 void SetFakeRecognitionResult() { 153 void SetFakeRecognitionResult() {
146 if (session_id_) { // Do a check in case we were cancelled.. 154 if (session_id_) { // Do a check in case we were cancelled..
147 VLOG(1) << "Setting fake recognition result."; 155 VLOG(1) << "Setting fake recognition result.";
148 listener_->OnAudioEnd(session_id_); 156 listener_->OnAudioEnd(session_id_);
149 content::SpeechRecognitionResult results; 157 content::SpeechRecognitionResult results;
150 results.hypotheses.push_back(content::SpeechRecognitionHypothesis( 158 results.hypotheses.push_back(content::SpeechRecognitionHypothesis(
151 ASCIIToUTF16(kTestResult), 1.0)); 159 ASCIIToUTF16(kTestResult), 1.0));
152 listener_->OnRecognitionResult(session_id_, results); 160 listener_->OnRecognitionResult(session_id_, results);
153 listener_->OnRecognitionEnd(session_id_); 161 listener_->OnRecognitionEnd(session_id_);
154 session_id_ = 0; 162 session_id_ = 0;
155 listener_ = NULL; 163 listener_ = NULL;
156 VLOG(1) << "Finished setting fake recognition result."; 164 VLOG(1) << "Finished setting fake recognition result.";
157 } 165 }
158 } 166 }
159 167
160 int session_id_; 168 int session_id_;
161 SpeechRecognitionEventListener* listener_; 169 SpeechRecognitionEventListener* listener_;
170 SpeechRecognitionSessionConfig session_config_;
162 SpeechRecognitionSessionContext session_ctx_; 171 SpeechRecognitionSessionContext session_ctx_;
163 std::string grammar_; 172 std::string grammar_;
164 bool did_cancel_all_; 173 bool did_cancel_all_;
165 bool should_send_fake_response_; 174 bool should_send_fake_response_;
166 base::WaitableEvent recognition_started_event_; 175 base::WaitableEvent recognition_started_event_;
167 }; 176 };
168 177
169 class SpeechRecognitionBrowserTest : public InProcessBrowserTest { 178 class SpeechRecognitionBrowserTest : public InProcessBrowserTest {
170 public: 179 public:
171 // InProcessBrowserTest methods 180 // InProcessBrowserTest methods
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 EXPECT_EQ("pass", browser()->GetSelectedWebContents()->GetURL().ref()); 227 EXPECT_EQ("pass", browser()->GetSelectedWebContents()->GetURL().ref());
219 } 228 }
220 229
221 // InProcessBrowserTest methods. 230 // InProcessBrowserTest methods.
222 virtual void SetUpInProcessBrowserTestFixture() { 231 virtual void SetUpInProcessBrowserTestFixture() {
223 fake_speech_recognition_manager_.set_should_send_fake_response(true); 232 fake_speech_recognition_manager_.set_should_send_fake_response(true);
224 speech_recognition_manager_ = &fake_speech_recognition_manager_; 233 speech_recognition_manager_ = &fake_speech_recognition_manager_;
225 234
226 // Inject the fake manager factory so that the test result is returned to 235 // Inject the fake manager factory so that the test result is returned to
227 // the web page. 236 // the web page.
228 InputTagSpeechDispatcherHost::set_manager(speech_recognition_manager_); 237 InputTagSpeechDispatcherHost::set_manager_for_tests(
238 speech_recognition_manager_);
229 } 239 }
230 240
231 virtual void TearDownInProcessBrowserTestFixture() { 241 virtual void TearDownInProcessBrowserTestFixture() {
232 speech_recognition_manager_ = NULL; 242 speech_recognition_manager_ = NULL;
233 } 243 }
234 244
235 FakeSpeechRecognitionManager fake_speech_recognition_manager_; 245 FakeSpeechRecognitionManager fake_speech_recognition_manager_;
236 246
237 // This is used by the static |fakeManager|, and it is a pointer rather than a 247 // This is used by the static |fakeManager|, and it is a pointer rather than a
238 // direct instance per the style guide. 248 // direct instance per the style guide.
239 static SpeechRecognitionManagerImpl* speech_recognition_manager_; 249 static content::SpeechRecognitionManager* speech_recognition_manager_;
240 }; 250 };
241 251
242 SpeechRecognitionManagerImpl* 252 content::SpeechRecognitionManager*
243 SpeechRecognitionBrowserTest::speech_recognition_manager_ = NULL; 253 SpeechRecognitionBrowserTest::speech_recognition_manager_ = NULL;
244 254
245 // TODO(satish): Once this flakiness has been fixed, add a second test here to 255 // TODO(satish): Once this flakiness has been fixed, add a second test here to
246 // check for sending many clicks in succession to the speech button and verify 256 // check for sending many clicks in succession to the speech button and verify
247 // that it doesn't cause any crash but works as expected. This should act as the 257 // that it doesn't cause any crash but works as expected. This should act as the
248 // test for http://crbug.com/59173 258 // test for http://crbug.com/59173
249 // 259 //
250 // TODO(satish): Similar to above, once this flakiness has been fixed add 260 // TODO(satish): Similar to above, once this flakiness has been fixed add
251 // another test here to check that when speech recognition is in progress and 261 // another test here to check that when speech recognition is in progress and
252 // a renderer crashes, we get a call to 262 // a renderer crashes, we get a call to
(...skipping 21 matching lines...) Expand all
274 284
275 // Make the renderer crash. This should trigger 285 // Make the renderer crash. This should trigger
276 // InputTagSpeechDispatcherHost to cancel all pending sessions. 286 // InputTagSpeechDispatcherHost to cancel all pending sessions.
277 GURL test_url("about:crash"); 287 GURL test_url("about:crash");
278 ui_test_utils::NavigateToURL(browser(), test_url); 288 ui_test_utils::NavigateToURL(browser(), test_url);
279 289
280 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all()); 290 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all());
281 } 291 }
282 292
283 } // namespace speech 293 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698