OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "content/shell/renderer/test_runner/MockWebSpeechInputController.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "content/shell/renderer/test_runner/WebTestDelegate.h" | |
9 #include "third_party/WebKit/public/platform/WebCString.h" | |
10 #include "third_party/WebKit/public/platform/WebVector.h" | |
11 #include "third_party/WebKit/public/web/WebSpeechInputListener.h" | |
12 | |
13 #if ENABLE_INPUT_SPEECH | |
14 | |
15 using namespace blink; | |
16 using namespace std; | |
17 | |
18 namespace WebTestRunner { | |
19 | |
20 namespace { | |
21 | |
22 WebSpeechInputResultArray makeRectResult(const WebRect& rect) | |
23 { | |
24 char buffer[100]; | |
25 snprintf(buffer, sizeof(buffer), "%d,%d,%d,%d", rect.x, rect.y, rect.width,
rect.height); | |
26 | |
27 WebSpeechInputResult res; | |
28 res.assign(WebString::fromUTF8(static_cast<const char*>(buffer)), 1.0); | |
29 | |
30 WebSpeechInputResultArray results; | |
31 results.assign(&res, 1); | |
32 return results; | |
33 } | |
34 | |
35 } | |
36 | |
37 MockWebSpeechInputController::MockWebSpeechInputController(WebSpeechInputListene
r* listener) | |
38 : m_listener(listener) | |
39 , m_speechTask(0) | |
40 , m_recording(false) | |
41 , m_requestId(-1) | |
42 , m_dumpRect(false) | |
43 , m_delegate(0) | |
44 { | |
45 } | |
46 | |
47 MockWebSpeechInputController::~MockWebSpeechInputController() | |
48 { | |
49 } | |
50 | |
51 void MockWebSpeechInputController::setDelegate(WebTestDelegate* delegate) | |
52 { | |
53 m_delegate = delegate; | |
54 } | |
55 | |
56 void MockWebSpeechInputController::addMockRecognitionResult(const WebString& res
ult, double confidence, const WebString& language) | |
57 { | |
58 WebSpeechInputResult res; | |
59 res.assign(result, confidence); | |
60 | |
61 if (language.isEmpty()) | |
62 m_resultsForEmptyLanguage.push_back(res); | |
63 else { | |
64 string langString = language.utf8(); | |
65 if (m_recognitionResults.find(langString) == m_recognitionResults.end()) | |
66 m_recognitionResults[langString] = vector<WebSpeechInputResult>(); | |
67 m_recognitionResults[langString].push_back(res); | |
68 } | |
69 } | |
70 | |
71 void MockWebSpeechInputController::setDumpRect(bool dumpRect) | |
72 { | |
73 m_dumpRect = dumpRect; | |
74 } | |
75 | |
76 void MockWebSpeechInputController::clearResults() | |
77 { | |
78 m_resultsForEmptyLanguage.clear(); | |
79 m_recognitionResults.clear(); | |
80 m_dumpRect = false; | |
81 } | |
82 | |
83 bool MockWebSpeechInputController::startRecognition(int requestId, const WebRect
& elementRect, const WebString& language, const WebString& grammar, const WebSec
urityOrigin& origin) | |
84 { | |
85 if (m_speechTask) | |
86 return false; | |
87 | |
88 m_requestId = requestId; | |
89 m_requestRect = elementRect; | |
90 m_recording = true; | |
91 m_language = language.utf8(); | |
92 | |
93 m_speechTask = new SpeechTask(this); | |
94 m_delegate->postTask(m_speechTask); | |
95 | |
96 return true; | |
97 } | |
98 | |
99 void MockWebSpeechInputController::cancelRecognition(int requestId) | |
100 { | |
101 if (m_speechTask) { | |
102 DCHECK_EQ(requestId, m_requestId); | |
103 | |
104 m_speechTask->stop(); | |
105 m_recording = false; | |
106 m_listener->didCompleteRecognition(m_requestId); | |
107 m_requestId = 0; | |
108 } | |
109 } | |
110 | |
111 void MockWebSpeechInputController::stopRecording(int requestId) | |
112 { | |
113 DCHECK_EQ(requestId, m_requestId); | |
114 if (m_speechTask && m_recording) { | |
115 m_speechTask->stop(); | |
116 speechTaskFired(); | |
117 } | |
118 } | |
119 | |
120 void MockWebSpeechInputController::speechTaskFired() | |
121 { | |
122 if (m_recording) { | |
123 m_recording = false; | |
124 m_listener->didCompleteRecording(m_requestId); | |
125 | |
126 m_speechTask = new SpeechTask(this); | |
127 m_delegate->postTask(m_speechTask); | |
128 } else { | |
129 bool noResultsFound = false; | |
130 // We take a copy of the requestId here so that if scripts destroyed the
input element | |
131 // inside one of the callbacks below, we'll still know what this session
's requestId was. | |
132 int requestId = m_requestId; | |
133 m_requestId = 0; | |
134 | |
135 if (m_dumpRect) { | |
136 m_listener->setRecognitionResult(requestId, makeRectResult(m_request
Rect)); | |
137 } else if (m_language.empty()) { | |
138 // Empty language case must be handled separately to avoid problems
with HashMap and empty keys. | |
139 if (!m_resultsForEmptyLanguage.empty()) | |
140 m_listener->setRecognitionResult(requestId, m_resultsForEmptyLan
guage); | |
141 else | |
142 noResultsFound = true; | |
143 } else { | |
144 if (m_recognitionResults.find(m_language) != m_recognitionResults.en
d()) | |
145 m_listener->setRecognitionResult(requestId, m_recognitionResults
[m_language]); | |
146 else | |
147 noResultsFound = true; | |
148 } | |
149 | |
150 if (noResultsFound) { | |
151 // Can't avoid setting a result even if no result was set for the gi
ven language. | |
152 // This would avoid generating the events used to check the results
and the test would timeout. | |
153 string error("error: no result found for language '"); | |
154 error.append(m_language); | |
155 error.append("'"); | |
156 | |
157 WebSpeechInputResult res; | |
158 res.assign(WebString::fromUTF8(error), 1.0); | |
159 | |
160 vector<WebSpeechInputResult> results; | |
161 results.push_back(res); | |
162 | |
163 m_listener->setRecognitionResult(requestId, results); | |
164 } | |
165 } | |
166 } | |
167 | |
168 MockWebSpeechInputController::SpeechTask::SpeechTask(MockWebSpeechInputControlle
r* mock) | |
169 : WebMethodTask<MockWebSpeechInputController>::WebMethodTask(mock) | |
170 { | |
171 } | |
172 | |
173 void MockWebSpeechInputController::SpeechTask::stop() | |
174 { | |
175 m_object->m_speechTask = 0; | |
176 cancel(); | |
177 } | |
178 | |
179 void MockWebSpeechInputController::SpeechTask::runIfValid() | |
180 { | |
181 m_object->m_speechTask = 0; | |
182 m_object->speechTaskFired(); | |
183 } | |
184 | |
185 } | |
186 | |
187 #endif | |
OLD | NEW |