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

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

Issue 9972008: Refactoring of chrome speech recognition architecture (CL1.6) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 17 matching lines...) Expand all
28 class FakeSpeechRecognitionManager; 28 class FakeSpeechRecognitionManager;
29 } 29 }
30 30
31 namespace speech { 31 namespace speech {
32 32
33 const char kTestResult[] = "Pictures of the moon"; 33 const char kTestResult[] = "Pictures of the moon";
34 34
35 class FakeSpeechRecognitionManager : public SpeechRecognitionManagerImpl { 35 class FakeSpeechRecognitionManager : public SpeechRecognitionManagerImpl {
36 public: 36 public:
37 FakeSpeechRecognitionManager() 37 FakeSpeechRecognitionManager()
38 : caller_id_(0), 38 : session_id_(0),
39 delegate_(NULL), 39 delegate_(NULL),
40 did_cancel_all_(false), 40 did_cancel_all_(false),
41 should_send_fake_response_(true), 41 should_send_fake_response_(true),
42 recognition_started_event_(false, false) { 42 recognition_started_event_(false, false) {
43 } 43 }
44 44
45 std::string grammar() { 45 std::string grammar() {
46 return grammar_; 46 return grammar_;
47 } 47 }
48 48
49 bool did_cancel_all() { 49 bool did_cancel_all() {
50 return did_cancel_all_; 50 return did_cancel_all_;
51 } 51 }
52 52
53 void set_should_send_fake_response(bool send) { 53 void set_should_send_fake_response(bool send) {
54 should_send_fake_response_ = send; 54 should_send_fake_response_ = send;
55 } 55 }
56 56
57 bool should_send_fake_response() { 57 bool should_send_fake_response() {
58 return should_send_fake_response_; 58 return should_send_fake_response_;
59 } 59 }
60 60
61 base::WaitableEvent& recognition_started_event() { 61 base::WaitableEvent& recognition_started_event() {
62 return recognition_started_event_; 62 return recognition_started_event_;
63 } 63 }
64 64
65 // SpeechRecognitionManager methods. 65 // SpeechRecognitionManager methods.
66 virtual void StartRecognition( 66 virtual void StartRecognition(
67 InputTagSpeechDispatcherHost* delegate, 67 InputTagSpeechDispatcherHost* delegate,
68 int caller_id, 68 int session_id,
69 int render_process_id, 69 int render_process_id,
70 int render_view_id, 70 int render_view_id,
71 const gfx::Rect& element_rect, 71 const gfx::Rect& element_rect,
72 const std::string& language, 72 const std::string& language,
73 const std::string& grammar, 73 const std::string& grammar,
74 const std::string& origin_url, 74 const std::string& origin_url,
75 net::URLRequestContextGetter* context_getter, 75 net::URLRequestContextGetter* context_getter,
76 content::SpeechRecognitionPreferences* recognition_prefs) OVERRIDE { 76 content::SpeechRecognitionPreferences* recognition_prefs) OVERRIDE {
77 VLOG(1) << "StartRecognition invoked."; 77 VLOG(1) << "StartRecognition invoked.";
78 EXPECT_EQ(0, caller_id_); 78 EXPECT_EQ(0, session_id_);
79 EXPECT_EQ(NULL, delegate_); 79 EXPECT_EQ(NULL, delegate_);
80 caller_id_ = caller_id; 80 session_id_ = session_id;
81 delegate_ = delegate; 81 delegate_ = delegate;
82 grammar_ = grammar; 82 grammar_ = grammar;
83 if (should_send_fake_response_) { 83 if (should_send_fake_response_) {
84 // Give the fake result in a short while. 84 // Give the fake result in a short while.
85 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 85 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
86 &FakeSpeechRecognitionManager::SetFakeRecognitionResult, 86 &FakeSpeechRecognitionManager::SetFakeRecognitionResult,
87 // This class does not need to be refcounted (typically done by 87 // This class does not need to be refcounted (typically done by
88 // PostTask) since it will outlive the test and gets released only 88 // PostTask) since it will outlive the test and gets released only
89 // when the test shuts down. Disabling refcounting here saves a bit 89 // when the test shuts down. Disabling refcounting here saves a bit
90 // of unnecessary code and the factory method can return a plain 90 // of unnecessary code and the factory method can return a plain
91 // pointer below as required by the real code. 91 // pointer below as required by the real code.
92 base::Unretained(this))); 92 base::Unretained(this)));
93 } 93 }
94 recognition_started_event_.Signal(); 94 recognition_started_event_.Signal();
95 } 95 }
96 virtual void CancelRecognition(int caller_id) OVERRIDE { 96 virtual void CancelRecognition(int session_id) OVERRIDE {
97 VLOG(1) << "CancelRecognition invoked."; 97 VLOG(1) << "CancelRecognition invoked.";
98 EXPECT_EQ(caller_id_, caller_id); 98 EXPECT_EQ(session_id_, session_id);
99 caller_id_ = 0; 99 session_id_ = 0;
100 delegate_ = NULL; 100 delegate_ = NULL;
101 } 101 }
102 virtual void StopRecording(int caller_id) OVERRIDE { 102 virtual void StopRecording(int session_id) OVERRIDE {
103 VLOG(1) << "StopRecording invoked."; 103 VLOG(1) << "StopRecording invoked.";
104 EXPECT_EQ(caller_id_, caller_id); 104 EXPECT_EQ(session_id_, session_id);
105 // Nothing to do here since we aren't really recording. 105 // Nothing to do here since we aren't really recording.
106 } 106 }
107 virtual void CancelAllRequestsWithDelegate( 107 virtual void CancelAllRequestsWithDelegate(
108 InputTagSpeechDispatcherHost* delegate) OVERRIDE { 108 InputTagSpeechDispatcherHost* delegate) OVERRIDE {
109 VLOG(1) << "CancelAllRequestsWithDelegate invoked."; 109 VLOG(1) << "CancelAllRequestsWithDelegate invoked.";
110 // delegate_ is set to NULL if a fake result was received (see below), so 110 // delegate_ is set to NULL if a fake result was received (see below), so
111 // check that delegate_ matches the incoming parameter only when there is 111 // check that delegate_ matches the incoming parameter only when there is
112 // no fake result sent. 112 // no fake result sent.
113 EXPECT_TRUE(should_send_fake_response_ || delegate_ == delegate); 113 EXPECT_TRUE(should_send_fake_response_ || delegate_ == delegate);
114 did_cancel_all_ = true; 114 did_cancel_all_ = true;
115 } 115 }
116 116
117 private: 117 private:
118 void SetFakeRecognitionResult() { 118 void SetFakeRecognitionResult() {
119 if (caller_id_) { // Do a check in case we were cancelled.. 119 if (session_id_) { // Do a check in case we were cancelled..
120 VLOG(1) << "Setting fake recognition result."; 120 VLOG(1) << "Setting fake recognition result.";
121 delegate_->DidCompleteRecording(caller_id_); 121 delegate_->DidCompleteRecording(session_id_);
122 content::SpeechRecognitionResult results; 122 content::SpeechRecognitionResult results;
123 results.hypotheses.push_back(content::SpeechRecognitionHypothesis( 123 results.hypotheses.push_back(content::SpeechRecognitionHypothesis(
124 ASCIIToUTF16(kTestResult), 1.0)); 124 ASCIIToUTF16(kTestResult), 1.0));
125 delegate_->SetRecognitionResult(caller_id_, results); 125 delegate_->SetRecognitionResult(session_id_, results);
126 delegate_->DidCompleteRecognition(caller_id_); 126 delegate_->DidCompleteRecognition(session_id_);
127 caller_id_ = 0; 127 session_id_ = 0;
128 delegate_ = NULL; 128 delegate_ = NULL;
129 VLOG(1) << "Finished setting fake recognition result."; 129 VLOG(1) << "Finished setting fake recognition result.";
130 } 130 }
131 } 131 }
132 132
133 int caller_id_; 133 int session_id_;
134 InputTagSpeechDispatcherHost* delegate_; 134 InputTagSpeechDispatcherHost* delegate_;
135 std::string grammar_; 135 std::string grammar_;
136 bool did_cancel_all_; 136 bool did_cancel_all_;
137 bool should_send_fake_response_; 137 bool should_send_fake_response_;
138 base::WaitableEvent recognition_started_event_; 138 base::WaitableEvent recognition_started_event_;
139 }; 139 };
140 140
141 class SpeechRecognitionBrowserTest : public InProcessBrowserTest { 141 class SpeechRecognitionBrowserTest : public InProcessBrowserTest {
142 public: 142 public:
143 // InProcessBrowserTest methods 143 // InProcessBrowserTest methods
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 246
247 // Make the renderer crash. This should trigger 247 // Make the renderer crash. This should trigger
248 // InputTagSpeechDispatcherHost to cancel all pending sessions. 248 // InputTagSpeechDispatcherHost to cancel all pending sessions.
249 GURL test_url("about:crash"); 249 GURL test_url("about:crash");
250 ui_test_utils::NavigateToURL(browser(), test_url); 250 ui_test_utils::NavigateToURL(browser(), test_url);
251 251
252 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all()); 252 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all());
253 } 253 }
254 254
255 } // namespace speech 255 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698