OLD | NEW |
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/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
9 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
13 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
14 #include "content/browser/renderer_host/render_view_host_impl.h" | 15 #include "content/browser/renderer_host/render_view_host_impl.h" |
15 #include "content/browser/speech/input_tag_speech_dispatcher_host.h" | 16 #include "content/browser/speech/input_tag_speech_dispatcher_host.h" |
16 #include "content/browser/speech/speech_recognition_manager_impl.h" | 17 #include "content/browser/speech/speech_recognition_manager_impl.h" |
17 #include "content/browser/web_contents/web_contents_impl.h" | 18 #include "content/browser/web_contents/web_contents_impl.h" |
18 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/speech_recognition_session_config.h" |
| 21 #include "content/public/browser/speech_recognition_session_context.h" |
19 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
20 #include "content/public/common/speech_recognition_error.h" | 23 #include "content/public/common/speech_recognition_error.h" |
21 #include "content/public/common/speech_recognition_result.h" | 24 #include "content/public/common/speech_recognition_result.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
23 | 26 |
| 27 using content::SpeechRecognitionEventListener; |
| 28 using content::SpeechRecognitionSessionContext; |
24 using content::NavigationController; | 29 using content::NavigationController; |
25 using content::WebContents; | 30 using content::WebContents; |
26 | 31 |
27 namespace speech { | 32 namespace speech { |
28 class FakeSpeechRecognitionManager; | 33 class FakeSpeechRecognitionManager; |
29 } | 34 } |
30 | 35 |
31 namespace speech { | 36 namespace speech { |
32 | 37 |
33 const char kTestResult[] = "Pictures of the moon"; | 38 const char kTestResult[] = "Pictures of the moon"; |
34 | 39 |
35 class FakeSpeechRecognitionManager : public SpeechRecognitionManagerImpl { | 40 class FakeSpeechRecognitionManager : public SpeechRecognitionManagerImpl { |
36 public: | 41 public: |
37 FakeSpeechRecognitionManager() | 42 FakeSpeechRecognitionManager() |
38 : session_id_(0), | 43 : session_id_(0), |
39 delegate_(NULL), | 44 listener_(NULL), |
40 did_cancel_all_(false), | 45 did_cancel_all_(false), |
41 should_send_fake_response_(true), | 46 should_send_fake_response_(true), |
42 recognition_started_event_(false, false) { | 47 recognition_started_event_(false, false) { |
43 } | 48 } |
44 | 49 |
45 std::string grammar() { | 50 std::string grammar() { |
46 return grammar_; | 51 return grammar_; |
47 } | 52 } |
48 | 53 |
49 bool did_cancel_all() { | 54 bool did_cancel_all() { |
50 return did_cancel_all_; | 55 return did_cancel_all_; |
51 } | 56 } |
52 | 57 |
53 void set_should_send_fake_response(bool send) { | 58 void set_should_send_fake_response(bool send) { |
54 should_send_fake_response_ = send; | 59 should_send_fake_response_ = send; |
55 } | 60 } |
56 | 61 |
57 bool should_send_fake_response() { | 62 bool should_send_fake_response() { |
58 return should_send_fake_response_; | 63 return should_send_fake_response_; |
59 } | 64 } |
60 | 65 |
61 base::WaitableEvent& recognition_started_event() { | 66 base::WaitableEvent& recognition_started_event() { |
62 return recognition_started_event_; | 67 return recognition_started_event_; |
63 } | 68 } |
64 | 69 |
65 // SpeechRecognitionManager methods. | 70 // SpeechRecognitionManager methods. |
66 virtual void StartRecognition( | 71 virtual int CreateSession( |
67 InputTagSpeechDispatcherHost* delegate, | 72 const content::SpeechRecognitionSessionConfig& config, |
68 int session_id, | 73 SpeechRecognitionEventListener* event_listener) OVERRIDE { |
69 int render_process_id, | 74 VLOG(1) << "FAKE CreateSession invoked."; |
70 int render_view_id, | |
71 const gfx::Rect& element_rect, | |
72 const std::string& language, | |
73 const std::string& grammar, | |
74 const std::string& origin_url, | |
75 net::URLRequestContextGetter* context_getter, | |
76 content::SpeechRecognitionPreferences* recognition_prefs) OVERRIDE { | |
77 VLOG(1) << "StartRecognition invoked."; | |
78 EXPECT_EQ(0, session_id_); | 75 EXPECT_EQ(0, session_id_); |
79 EXPECT_EQ(NULL, delegate_); | 76 EXPECT_EQ(NULL, listener_); |
80 session_id_ = session_id; | 77 listener_ = event_listener; |
81 delegate_ = delegate; | 78 grammar_ = config.grammar; |
82 grammar_ = grammar; | 79 session_ctx_ = config.initial_context; |
| 80 session_id_ = 1; |
| 81 return session_id_; |
| 82 } |
| 83 |
| 84 virtual void StartSession(int session_id) OVERRIDE { |
| 85 VLOG(1) << "FAKE StartSession invoked."; |
| 86 EXPECT_EQ(session_id, session_id_); |
| 87 EXPECT_TRUE(listener_ != NULL); |
| 88 |
83 if (should_send_fake_response_) { | 89 if (should_send_fake_response_) { |
84 // Give the fake result in a short while. | 90 // Give the fake result in a short while. |
85 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 91 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
86 &FakeSpeechRecognitionManager::SetFakeRecognitionResult, | 92 &FakeSpeechRecognitionManager::SetFakeRecognitionResult, |
87 // This class does not need to be refcounted (typically done by | 93 // This class does not need to be refcounted (typically done by |
88 // PostTask) since it will outlive the test and gets released only | 94 // PostTask) since it will outlive the test and gets released only |
89 // when the test shuts down. Disabling refcounting here saves a bit | 95 // when the test shuts down. Disabling refcounting here saves a bit |
90 // of unnecessary code and the factory method can return a plain | 96 // of unnecessary code and the factory method can return a plain |
91 // pointer below as required by the real code. | 97 // pointer below as required by the real code. |
92 base::Unretained(this))); | 98 base::Unretained(this))); |
93 } | 99 } |
94 recognition_started_event_.Signal(); | 100 recognition_started_event_.Signal(); |
95 } | 101 } |
96 virtual void CancelRecognition(int session_id) OVERRIDE { | 102 |
97 VLOG(1) << "CancelRecognition invoked."; | 103 virtual void AbortSession(int session_id) OVERRIDE { |
| 104 VLOG(1) << "FAKE AbortSession invoked."; |
98 EXPECT_EQ(session_id_, session_id); | 105 EXPECT_EQ(session_id_, session_id); |
99 session_id_ = 0; | 106 session_id_ = 0; |
100 delegate_ = NULL; | 107 listener_ = NULL; |
101 } | 108 } |
102 virtual void StopRecording(int session_id) OVERRIDE { | 109 |
| 110 virtual void StopAudioCaptureForSession(int session_id) OVERRIDE { |
103 VLOG(1) << "StopRecording invoked."; | 111 VLOG(1) << "StopRecording invoked."; |
104 EXPECT_EQ(session_id_, session_id); | 112 EXPECT_EQ(session_id_, session_id); |
105 // Nothing to do here since we aren't really recording. | 113 // Nothing to do here since we aren't really recording. |
106 } | 114 } |
107 virtual void CancelAllRequestsWithDelegate( | 115 |
108 InputTagSpeechDispatcherHost* delegate) OVERRIDE { | 116 virtual void AbortAllSessionsForListener( |
| 117 content::SpeechRecognitionEventListener* listener) OVERRIDE { |
109 VLOG(1) << "CancelAllRequestsWithDelegate invoked."; | 118 VLOG(1) << "CancelAllRequestsWithDelegate invoked."; |
110 // delegate_ is set to NULL if a fake result was received (see below), so | 119 // listener_ 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 | 120 // check that listener_ matches the incoming parameter only when there is |
112 // no fake result sent. | 121 // no fake result sent. |
113 EXPECT_TRUE(should_send_fake_response_ || delegate_ == delegate); | 122 EXPECT_TRUE(should_send_fake_response_ || listener_ == listener); |
114 did_cancel_all_ = true; | 123 did_cancel_all_ = true; |
115 } | 124 } |
116 | 125 |
| 126 virtual void SendSessionToBackground(int session_id) OVERRIDE {} |
| 127 virtual bool HasAudioInputDevices() OVERRIDE { return true; } |
| 128 virtual bool IsCapturingAudio() OVERRIDE { return true; } |
| 129 virtual string16 GetAudioInputDeviceModel() OVERRIDE { return string16(); } |
| 130 virtual void ShowAudioInputSettings() OVERRIDE {} |
| 131 |
| 132 virtual int LookupSessionByContext( |
| 133 base::Callback<bool( |
| 134 const content::SpeechRecognitionSessionContext&)> matcher) |
| 135 const OVERRIDE { |
| 136 bool matched = matcher.Run(session_ctx_); |
| 137 return matched ? session_id_ : 0; |
| 138 } |
| 139 |
| 140 virtual content::SpeechRecognitionSessionContext GetSessionContext( |
| 141 int session_id) const OVERRIDE { |
| 142 EXPECT_EQ(session_id, session_id_); |
| 143 return session_ctx_; |
| 144 } |
| 145 |
117 private: | 146 private: |
118 void SetFakeRecognitionResult() { | 147 void SetFakeRecognitionResult() { |
119 if (session_id_) { // Do a check in case we were cancelled.. | 148 if (session_id_) { // Do a check in case we were cancelled.. |
120 VLOG(1) << "Setting fake recognition result."; | 149 VLOG(1) << "Setting fake recognition result."; |
121 delegate_->DidCompleteRecording(session_id_); | 150 listener_->OnAudioEnd(session_id_); |
122 content::SpeechRecognitionResult results; | 151 content::SpeechRecognitionResult results; |
123 results.hypotheses.push_back(content::SpeechRecognitionHypothesis( | 152 results.hypotheses.push_back(content::SpeechRecognitionHypothesis( |
124 ASCIIToUTF16(kTestResult), 1.0)); | 153 ASCIIToUTF16(kTestResult), 1.0)); |
125 delegate_->SetRecognitionResult(session_id_, results); | 154 listener_->OnRecognitionResult(session_id_, results); |
126 delegate_->DidCompleteRecognition(session_id_); | 155 listener_->OnRecognitionEnd(session_id_); |
127 session_id_ = 0; | 156 session_id_ = 0; |
128 delegate_ = NULL; | 157 listener_ = NULL; |
129 VLOG(1) << "Finished setting fake recognition result."; | 158 VLOG(1) << "Finished setting fake recognition result."; |
130 } | 159 } |
131 } | 160 } |
132 | 161 |
133 int session_id_; | 162 int session_id_; |
134 InputTagSpeechDispatcherHost* delegate_; | 163 SpeechRecognitionEventListener* listener_; |
| 164 SpeechRecognitionSessionContext session_ctx_; |
135 std::string grammar_; | 165 std::string grammar_; |
136 bool did_cancel_all_; | 166 bool did_cancel_all_; |
137 bool should_send_fake_response_; | 167 bool should_send_fake_response_; |
138 base::WaitableEvent recognition_started_event_; | 168 base::WaitableEvent recognition_started_event_; |
139 }; | 169 }; |
140 | 170 |
141 class SpeechRecognitionBrowserTest : public InProcessBrowserTest { | 171 class SpeechRecognitionBrowserTest : public InProcessBrowserTest { |
142 public: | 172 public: |
143 // InProcessBrowserTest methods | 173 // InProcessBrowserTest methods |
144 virtual void SetUpCommandLine(CommandLine* command_line) { | 174 virtual void SetUpCommandLine(CommandLine* command_line) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 276 |
247 // Make the renderer crash. This should trigger | 277 // Make the renderer crash. This should trigger |
248 // InputTagSpeechDispatcherHost to cancel all pending sessions. | 278 // InputTagSpeechDispatcherHost to cancel all pending sessions. |
249 GURL test_url("about:crash"); | 279 GURL test_url("about:crash"); |
250 ui_test_utils::NavigateToURL(browser(), test_url); | 280 ui_test_utils::NavigateToURL(browser(), test_url); |
251 | 281 |
252 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all()); | 282 EXPECT_TRUE(fake_speech_recognition_manager_.did_cancel_all()); |
253 } | 283 } |
254 | 284 |
255 } // namespace speech | 285 } // namespace speech |
OLD | NEW |