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 "content/shell/renderer/test_runner/WebTestDelegate.h"
8 #include "third_party/WebKit/public/platform/WebCString.h"
9 #include "third_party/WebKit/public/platform/WebVector.h"
10 #include "third_party/WebKit/public/web/WebSpeechInputListener.h"
12 #if ENABLE_INPUT_SPEECH
14 using namespace blink;
17 namespace WebTestRunner {
21 WebSpeechInputResultArray makeRectResult(const WebRect& rect)
24 snprintf(buffer, sizeof(buffer), "%d,%d,%d,%d", rect.x, rect.y, rect.width, rect.height);
26 WebSpeechInputResult res;
27 res.assign(WebString::fromUTF8(static_cast<const char*>(buffer)), 1.0);
29 WebSpeechInputResultArray results;
30 results.assign(&res, 1);
36 MockWebSpeechInputController::MockWebSpeechInputController(WebSpeechInputListener* listener)
37 : m_listener(listener)
46 MockWebSpeechInputController::~MockWebSpeechInputController()
50 void MockWebSpeechInputController::setDelegate(WebTestDelegate* delegate)
52 m_delegate = delegate;
55 void MockWebSpeechInputController::addMockRecognitionResult(const WebString& result, double confidence, const WebString& language)
57 WebSpeechInputResult res;
58 res.assign(result, confidence);
60 if (language.isEmpty())
61 m_resultsForEmptyLanguage.push_back(res);
63 string langString = language.utf8();
64 if (m_recognitionResults.find(langString) == m_recognitionResults.end())
65 m_recognitionResults[langString] = vector<WebSpeechInputResult>();
66 m_recognitionResults[langString].push_back(res);
70 void MockWebSpeechInputController::setDumpRect(bool dumpRect)
72 m_dumpRect = dumpRect;
75 void MockWebSpeechInputController::clearResults()
77 m_resultsForEmptyLanguage.clear();
78 m_recognitionResults.clear();
82 bool MockWebSpeechInputController::startRecognition(int requestId, const WebRect& elementRect, const WebString& language, const WebString& grammar, const WebSecurityOrigin& origin)
87 m_requestId = requestId;
88 m_requestRect = elementRect;
90 m_language = language.utf8();
92 m_speechTask = new SpeechTask(this);
93 m_delegate->postTask(m_speechTask);
98 void MockWebSpeechInputController::cancelRecognition(int requestId)
101 BLINK_ASSERT(requestId == m_requestId);
103 m_speechTask->stop();
105 m_listener->didCompleteRecognition(m_requestId);
110 void MockWebSpeechInputController::stopRecording(int requestId)
112 BLINK_ASSERT(requestId == m_requestId);
113 if (m_speechTask && m_recording) {
114 m_speechTask->stop();
119 void MockWebSpeechInputController::speechTaskFired()
123 m_listener->didCompleteRecording(m_requestId);
125 m_speechTask = new SpeechTask(this);
126 m_delegate->postTask(m_speechTask);
128 bool noResultsFound = false;
129 // We take a copy of the requestId here so that if scripts destroyed the input element
130 // inside one of the callbacks below, we'll still know what this session's requestId was.
131 int requestId = m_requestId;
135 m_listener->setRecognitionResult(requestId, makeRectResult(m_requestRect));
136 } else if (m_language.empty()) {
137 // Empty language case must be handled separately to avoid problems with HashMap and empty keys.
138 if (!m_resultsForEmptyLanguage.empty())
139 m_listener->setRecognitionResult(requestId, m_resultsForEmptyLanguage);
141 noResultsFound = true;
143 if (m_recognitionResults.find(m_language) != m_recognitionResults.end())
144 m_listener->setRecognitionResult(requestId, m_recognitionResults[m_language]);
146 noResultsFound = true;
149 if (noResultsFound) {
150 // Can't avoid setting a result even if no result was set for the given language.
151 // This would avoid generating the events used to check the results and the test would timeout.
152 string error("error: no result found for language '");
153 error.append(m_language);
156 WebSpeechInputResult res;
157 res.assign(WebString::fromUTF8(error), 1.0);
159 vector<WebSpeechInputResult> results;
160 results.push_back(res);
162 m_listener->setRecognitionResult(requestId, results);
167 MockWebSpeechInputController::SpeechTask::SpeechTask(MockWebSpeechInputController* mock)
168 : WebMethodTask<MockWebSpeechInputController>::WebMethodTask(mock)
172 void MockWebSpeechInputController::SpeechTask::stop()
174 m_object->m_speechTask = 0;
178 void MockWebSpeechInputController::SpeechTask::runIfValid()
180 m_object->m_speechTask = 0;
181 m_object->speechTaskFired();