[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / content / shell / renderer / test_runner / MockWebSpeechInputController.cpp
blob07526b0af8497089543191ea0d7ea9abdb8f7775
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;
16 using namespace std;
18 namespace WebTestRunner {
20 namespace {
22 WebSpeechInputResultArray makeRectResult(const WebRect& rect)
24 char buffer[100];
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);
32 return results;
37 MockWebSpeechInputController::MockWebSpeechInputController(WebSpeechInputListener* 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)
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);
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);
71 void MockWebSpeechInputController::setDumpRect(bool dumpRect)
73 m_dumpRect = dumpRect;
76 void MockWebSpeechInputController::clearResults()
78 m_resultsForEmptyLanguage.clear();
79 m_recognitionResults.clear();
80 m_dumpRect = false;
83 bool MockWebSpeechInputController::startRecognition(int requestId, const WebRect& elementRect, const WebString& language, const WebString& grammar, const WebSecurityOrigin& origin)
85 if (m_speechTask)
86 return false;
88 m_requestId = requestId;
89 m_requestRect = elementRect;
90 m_recording = true;
91 m_language = language.utf8();
93 m_speechTask = new SpeechTask(this);
94 m_delegate->postTask(m_speechTask);
96 return true;
99 void MockWebSpeechInputController::cancelRecognition(int requestId)
101 if (m_speechTask) {
102 DCHECK_EQ(requestId, m_requestId);
104 m_speechTask->stop();
105 m_recording = false;
106 m_listener->didCompleteRecognition(m_requestId);
107 m_requestId = 0;
111 void MockWebSpeechInputController::stopRecording(int requestId)
113 DCHECK_EQ(requestId, m_requestId);
114 if (m_speechTask && m_recording) {
115 m_speechTask->stop();
116 speechTaskFired();
120 void MockWebSpeechInputController::speechTaskFired()
122 if (m_recording) {
123 m_recording = false;
124 m_listener->didCompleteRecording(m_requestId);
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;
135 if (m_dumpRect) {
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);
141 else
142 noResultsFound = true;
143 } else {
144 if (m_recognitionResults.find(m_language) != m_recognitionResults.end())
145 m_listener->setRecognitionResult(requestId, m_recognitionResults[m_language]);
146 else
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);
155 error.append("'");
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;
176 cancel();
179 void MockWebSpeechInputController::SpeechTask::runIfValid()
181 m_object->m_speechTask = 0;
182 m_object->speechTaskFired();
187 #endif