| 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 /* |
| 6 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 7 * |
| 8 * Redistribution and use in source and binary forms, with or without |
| 9 * modification, are permitted provided that the following conditions |
| 10 * are met: |
| 11 * |
| 12 * 1. Redistributions of source code must retain the above copyright |
| 13 * notice, this list of conditions and the following disclaimer. |
| 14 * 2. Redistributions in binary form must reproduce the above copyright |
| 15 * notice, this list of conditions and the following disclaimer in the |
| 16 * documentation and/or other materials provided with the distribution. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 */ |
| 29 |
| 30 #include "content/test/layout_tests/runner/MockWebSpeechInputController.h" |
| 31 |
| 32 #include "content/public/test/layout_tests/WebTestDelegate.h" |
| 33 #include "third_party/WebKit/public/platform/WebCString.h" |
| 34 #include "third_party/WebKit/public/platform/WebVector.h" |
| 35 #include "third_party/WebKit/public/web/WebSpeechInputListener.h" |
| 36 |
| 37 #if ENABLE_INPUT_SPEECH |
| 38 |
| 39 using namespace blink; |
| 40 using namespace std; |
| 41 |
| 42 namespace WebTestRunner { |
| 43 |
| 44 namespace { |
| 45 |
| 46 WebSpeechInputResultArray makeRectResult(const WebRect& rect) |
| 47 { |
| 48 char buffer[100]; |
| 49 snprintf(buffer, sizeof(buffer), "%d,%d,%d,%d", rect.x, rect.y, rect.width,
rect.height); |
| 50 |
| 51 WebSpeechInputResult res; |
| 52 res.assign(WebString::fromUTF8(static_cast<const char*>(buffer)), 1.0); |
| 53 |
| 54 WebSpeechInputResultArray results; |
| 55 results.assign(&res, 1); |
| 56 return results; |
| 57 } |
| 58 |
| 59 } |
| 60 |
| 61 MockWebSpeechInputController::MockWebSpeechInputController(WebSpeechInputListene
r* listener) |
| 62 : m_listener(listener) |
| 63 , m_speechTask(0) |
| 64 , m_recording(false) |
| 65 , m_requestId(-1) |
| 66 , m_dumpRect(false) |
| 67 , m_delegate(0) |
| 68 { |
| 69 } |
| 70 |
| 71 MockWebSpeechInputController::~MockWebSpeechInputController() |
| 72 { |
| 73 } |
| 74 |
| 75 void MockWebSpeechInputController::setDelegate(WebTestDelegate* delegate) |
| 76 { |
| 77 m_delegate = delegate; |
| 78 } |
| 79 |
| 80 void MockWebSpeechInputController::addMockRecognitionResult(const WebString& res
ult, double confidence, const WebString& language) |
| 81 { |
| 82 WebSpeechInputResult res; |
| 83 res.assign(result, confidence); |
| 84 |
| 85 if (language.isEmpty()) |
| 86 m_resultsForEmptyLanguage.push_back(res); |
| 87 else { |
| 88 string langString = language.utf8(); |
| 89 if (m_recognitionResults.find(langString) == m_recognitionResults.end()) |
| 90 m_recognitionResults[langString] = vector<WebSpeechInputResult>(); |
| 91 m_recognitionResults[langString].push_back(res); |
| 92 } |
| 93 } |
| 94 |
| 95 void MockWebSpeechInputController::setDumpRect(bool dumpRect) |
| 96 { |
| 97 m_dumpRect = dumpRect; |
| 98 } |
| 99 |
| 100 void MockWebSpeechInputController::clearResults() |
| 101 { |
| 102 m_resultsForEmptyLanguage.clear(); |
| 103 m_recognitionResults.clear(); |
| 104 m_dumpRect = false; |
| 105 } |
| 106 |
| 107 bool MockWebSpeechInputController::startRecognition(int requestId, const WebRect
& elementRect, const WebString& language, const WebString& grammar, const WebSec
urityOrigin& origin) |
| 108 { |
| 109 if (m_speechTask) |
| 110 return false; |
| 111 |
| 112 m_requestId = requestId; |
| 113 m_requestRect = elementRect; |
| 114 m_recording = true; |
| 115 m_language = language.utf8(); |
| 116 |
| 117 m_speechTask = new SpeechTask(this); |
| 118 m_delegate->postTask(m_speechTask); |
| 119 |
| 120 return true; |
| 121 } |
| 122 |
| 123 void MockWebSpeechInputController::cancelRecognition(int requestId) |
| 124 { |
| 125 if (m_speechTask) { |
| 126 BLINK_ASSERT(requestId == m_requestId); |
| 127 |
| 128 m_speechTask->stop(); |
| 129 m_recording = false; |
| 130 m_listener->didCompleteRecognition(m_requestId); |
| 131 m_requestId = 0; |
| 132 } |
| 133 } |
| 134 |
| 135 void MockWebSpeechInputController::stopRecording(int requestId) |
| 136 { |
| 137 BLINK_ASSERT(requestId == m_requestId); |
| 138 if (m_speechTask && m_recording) { |
| 139 m_speechTask->stop(); |
| 140 speechTaskFired(); |
| 141 } |
| 142 } |
| 143 |
| 144 void MockWebSpeechInputController::speechTaskFired() |
| 145 { |
| 146 if (m_recording) { |
| 147 m_recording = false; |
| 148 m_listener->didCompleteRecording(m_requestId); |
| 149 |
| 150 m_speechTask = new SpeechTask(this); |
| 151 m_delegate->postTask(m_speechTask); |
| 152 } else { |
| 153 bool noResultsFound = false; |
| 154 // We take a copy of the requestId here so that if scripts destroyed the
input element |
| 155 // inside one of the callbacks below, we'll still know what this session
's requestId was. |
| 156 int requestId = m_requestId; |
| 157 m_requestId = 0; |
| 158 |
| 159 if (m_dumpRect) { |
| 160 m_listener->setRecognitionResult(requestId, makeRectResult(m_request
Rect)); |
| 161 } else if (m_language.empty()) { |
| 162 // Empty language case must be handled separately to avoid problems
with HashMap and empty keys. |
| 163 if (!m_resultsForEmptyLanguage.empty()) |
| 164 m_listener->setRecognitionResult(requestId, m_resultsForEmptyLan
guage); |
| 165 else |
| 166 noResultsFound = true; |
| 167 } else { |
| 168 if (m_recognitionResults.find(m_language) != m_recognitionResults.en
d()) |
| 169 m_listener->setRecognitionResult(requestId, m_recognitionResults
[m_language]); |
| 170 else |
| 171 noResultsFound = true; |
| 172 } |
| 173 |
| 174 if (noResultsFound) { |
| 175 // Can't avoid setting a result even if no result was set for the gi
ven language. |
| 176 // This would avoid generating the events used to check the results
and the test would timeout. |
| 177 string error("error: no result found for language '"); |
| 178 error.append(m_language); |
| 179 error.append("'"); |
| 180 |
| 181 WebSpeechInputResult res; |
| 182 res.assign(WebString::fromUTF8(error), 1.0); |
| 183 |
| 184 vector<WebSpeechInputResult> results; |
| 185 results.push_back(res); |
| 186 |
| 187 m_listener->setRecognitionResult(requestId, results); |
| 188 } |
| 189 } |
| 190 } |
| 191 |
| 192 MockWebSpeechInputController::SpeechTask::SpeechTask(MockWebSpeechInputControlle
r* mock) |
| 193 : WebMethodTask<MockWebSpeechInputController>::WebMethodTask(mock) |
| 194 { |
| 195 } |
| 196 |
| 197 void MockWebSpeechInputController::SpeechTask::stop() |
| 198 { |
| 199 m_object->m_speechTask = 0; |
| 200 cancel(); |
| 201 } |
| 202 |
| 203 void MockWebSpeechInputController::SpeechTask::runIfValid() |
| 204 { |
| 205 m_object->m_speechTask = 0; |
| 206 m_object->speechTaskFired(); |
| 207 } |
| 208 |
| 209 } |
| 210 |
| 211 #endif |
| OLD | NEW |