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.
5 #include "content/shell/renderer/test_runner/MockWebSpeechInputController.h"
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"
13 #if ENABLE_INPUT_SPEECH
15 using namespace blink
;
18 namespace WebTestRunner
{
22 WebSpeechInputResultArray
makeRectResult(const WebRect
& rect
)
25 snprintf(buffer
, sizeof(buffer
), "%d,%d,%d,%d", rect
.x
, rect
.y
, rect
.width
, rect
.height
);
27 WebSpeechInputResult res
;
28 res
.assign(WebString::fromUTF8(static_cast<const char*>(buffer
)), 1.0);
30 WebSpeechInputResultArray results
;
31 results
.assign(&res
, 1);
37 MockWebSpeechInputController::MockWebSpeechInputController(WebSpeechInputListener
* listener
)
38 : m_listener(listener
)
47 MockWebSpeechInputController::~MockWebSpeechInputController()
51 void MockWebSpeechInputController::setDelegate(WebTestDelegate
* delegate
)
53 m_delegate
= delegate
;
56 void MockWebSpeechInputController::addMockRecognitionResult(const WebString
& result
, double confidence
, const WebString
& language
)
58 WebSpeechInputResult res
;
59 res
.assign(result
, confidence
);
61 if (language
.isEmpty())
62 m_resultsForEmptyLanguage
.push_back(res
);
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
);
71 void MockWebSpeechInputController::setDumpRect(bool dumpRect
)
73 m_dumpRect
= dumpRect
;
76 void MockWebSpeechInputController::clearResults()
78 m_resultsForEmptyLanguage
.clear();
79 m_recognitionResults
.clear();
83 bool MockWebSpeechInputController::startRecognition(int requestId
, const WebRect
& elementRect
, const WebString
& language
, const WebString
& grammar
, const WebSecurityOrigin
& origin
)
88 m_requestId
= requestId
;
89 m_requestRect
= elementRect
;
91 m_language
= language
.utf8();
93 m_speechTask
= new SpeechTask(this);
94 m_delegate
->postTask(m_speechTask
);
99 void MockWebSpeechInputController::cancelRecognition(int requestId
)
102 DCHECK_EQ(requestId
, m_requestId
);
104 m_speechTask
->stop();
106 m_listener
->didCompleteRecognition(m_requestId
);
111 void MockWebSpeechInputController::stopRecording(int requestId
)
113 DCHECK_EQ(requestId
, m_requestId
);
114 if (m_speechTask
&& m_recording
) {
115 m_speechTask
->stop();
120 void MockWebSpeechInputController::speechTaskFired()
124 m_listener
->didCompleteRecording(m_requestId
);
126 m_speechTask
= new SpeechTask(this);
127 m_delegate
->postTask(m_speechTask
);
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
;
136 m_listener
->setRecognitionResult(requestId
, makeRectResult(m_requestRect
));
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_resultsForEmptyLanguage
);
142 noResultsFound
= true;
144 if (m_recognitionResults
.find(m_language
) != m_recognitionResults
.end())
145 m_listener
->setRecognitionResult(requestId
, m_recognitionResults
[m_language
]);
147 noResultsFound
= true;
150 if (noResultsFound
) {
151 // Can't avoid setting a result even if no result was set for the given 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
);
157 WebSpeechInputResult res
;
158 res
.assign(WebString::fromUTF8(error
), 1.0);
160 vector
<WebSpeechInputResult
> results
;
161 results
.push_back(res
);
163 m_listener
->setRecognitionResult(requestId
, results
);
168 MockWebSpeechInputController::SpeechTask::SpeechTask(MockWebSpeechInputController
* mock
)
169 : WebMethodTask
<MockWebSpeechInputController
>::WebMethodTask(mock
)
173 void MockWebSpeechInputController::SpeechTask::stop()
175 m_object
->m_speechTask
= 0;
179 void MockWebSpeechInputController::SpeechTask::runIfValid()
181 m_object
->m_speechTask
= 0;
182 m_object
->speechTaskFired();