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 "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" |
| 11 |
| 12 #if ENABLE_INPUT_SPEECH |
| 13 |
| 14 using namespace blink; |
| 15 using namespace std; |
| 16 |
| 17 namespace WebTestRunner { |
| 18 |
| 19 namespace { |
| 20 |
| 21 WebSpeechInputResultArray makeRectResult(const WebRect& rect) |
| 22 { |
| 23 char buffer[100]; |
| 24 snprintf(buffer, sizeof(buffer), "%d,%d,%d,%d", rect.x, rect.y, rect.width,
rect.height); |
| 25 |
| 26 WebSpeechInputResult res; |
| 27 res.assign(WebString::fromUTF8(static_cast<const char*>(buffer)), 1.0); |
| 28 |
| 29 WebSpeechInputResultArray results; |
| 30 results.assign(&res, 1); |
| 31 return results; |
| 32 } |
| 33 |
| 34 } |
| 35 |
| 36 MockWebSpeechInputController::MockWebSpeechInputController(WebSpeechInputListene
r* listener) |
| 37 : m_listener(listener) |
| 38 , m_speechTask(0) |
| 39 , m_recording(false) |
| 40 , m_requestId(-1) |
| 41 , m_dumpRect(false) |
| 42 , m_delegate(0) |
| 43 { |
| 44 } |
| 45 |
| 46 MockWebSpeechInputController::~MockWebSpeechInputController() |
| 47 { |
| 48 } |
| 49 |
| 50 void MockWebSpeechInputController::setDelegate(WebTestDelegate* delegate) |
| 51 { |
| 52 m_delegate = delegate; |
| 53 } |
| 54 |
| 55 void MockWebSpeechInputController::addMockRecognitionResult(const WebString& res
ult, double confidence, const WebString& language) |
| 56 { |
| 57 WebSpeechInputResult res; |
| 58 res.assign(result, confidence); |
| 59 |
| 60 if (language.isEmpty()) |
| 61 m_resultsForEmptyLanguage.push_back(res); |
| 62 else { |
| 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); |
| 67 } |
| 68 } |
| 69 |
| 70 void MockWebSpeechInputController::setDumpRect(bool dumpRect) |
| 71 { |
| 72 m_dumpRect = dumpRect; |
| 73 } |
| 74 |
| 75 void MockWebSpeechInputController::clearResults() |
| 76 { |
| 77 m_resultsForEmptyLanguage.clear(); |
| 78 m_recognitionResults.clear(); |
| 79 m_dumpRect = false; |
| 80 } |
| 81 |
| 82 bool MockWebSpeechInputController::startRecognition(int requestId, const WebRect
& elementRect, const WebString& language, const WebString& grammar, const WebSec
urityOrigin& origin) |
| 83 { |
| 84 if (m_speechTask) |
| 85 return false; |
| 86 |
| 87 m_requestId = requestId; |
| 88 m_requestRect = elementRect; |
| 89 m_recording = true; |
| 90 m_language = language.utf8(); |
| 91 |
| 92 m_speechTask = new SpeechTask(this); |
| 93 m_delegate->postTask(m_speechTask); |
| 94 |
| 95 return true; |
| 96 } |
| 97 |
| 98 void MockWebSpeechInputController::cancelRecognition(int requestId) |
| 99 { |
| 100 if (m_speechTask) { |
| 101 BLINK_ASSERT(requestId == m_requestId); |
| 102 |
| 103 m_speechTask->stop(); |
| 104 m_recording = false; |
| 105 m_listener->didCompleteRecognition(m_requestId); |
| 106 m_requestId = 0; |
| 107 } |
| 108 } |
| 109 |
| 110 void MockWebSpeechInputController::stopRecording(int requestId) |
| 111 { |
| 112 BLINK_ASSERT(requestId == m_requestId); |
| 113 if (m_speechTask && m_recording) { |
| 114 m_speechTask->stop(); |
| 115 speechTaskFired(); |
| 116 } |
| 117 } |
| 118 |
| 119 void MockWebSpeechInputController::speechTaskFired() |
| 120 { |
| 121 if (m_recording) { |
| 122 m_recording = false; |
| 123 m_listener->didCompleteRecording(m_requestId); |
| 124 |
| 125 m_speechTask = new SpeechTask(this); |
| 126 m_delegate->postTask(m_speechTask); |
| 127 } else { |
| 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; |
| 132 m_requestId = 0; |
| 133 |
| 134 if (m_dumpRect) { |
| 135 m_listener->setRecognitionResult(requestId, makeRectResult(m_request
Rect)); |
| 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_resultsForEmptyLan
guage); |
| 140 else |
| 141 noResultsFound = true; |
| 142 } else { |
| 143 if (m_recognitionResults.find(m_language) != m_recognitionResults.en
d()) |
| 144 m_listener->setRecognitionResult(requestId, m_recognitionResults
[m_language]); |
| 145 else |
| 146 noResultsFound = true; |
| 147 } |
| 148 |
| 149 if (noResultsFound) { |
| 150 // Can't avoid setting a result even if no result was set for the gi
ven 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); |
| 154 error.append("'"); |
| 155 |
| 156 WebSpeechInputResult res; |
| 157 res.assign(WebString::fromUTF8(error), 1.0); |
| 158 |
| 159 vector<WebSpeechInputResult> results; |
| 160 results.push_back(res); |
| 161 |
| 162 m_listener->setRecognitionResult(requestId, results); |
| 163 } |
| 164 } |
| 165 } |
| 166 |
| 167 MockWebSpeechInputController::SpeechTask::SpeechTask(MockWebSpeechInputControlle
r* mock) |
| 168 : WebMethodTask<MockWebSpeechInputController>::WebMethodTask(mock) |
| 169 { |
| 170 } |
| 171 |
| 172 void MockWebSpeechInputController::SpeechTask::stop() |
| 173 { |
| 174 m_object->m_speechTask = 0; |
| 175 cancel(); |
| 176 } |
| 177 |
| 178 void MockWebSpeechInputController::SpeechTask::runIfValid() |
| 179 { |
| 180 m_object->m_speechTask = 0; |
| 181 m_object->speechTaskFired(); |
| 182 } |
| 183 |
| 184 } |
| 185 |
| 186 #endif |
OLD | NEW |