| 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/MockWebSpeechRecognizer.h" |
| 31 |
| 32 #include "content/public/test/layout_tests/WebTestDelegate.h" |
| 33 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h" |
| 34 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h" |
| 35 |
| 36 using namespace blink; |
| 37 using namespace std; |
| 38 |
| 39 namespace WebTestRunner { |
| 40 |
| 41 namespace { |
| 42 |
| 43 // Task class for calling a client function that does not take any parameters. |
| 44 typedef void (WebSpeechRecognizerClient::*ClientFunctionPointer)(const WebSpeech
RecognitionHandle&); |
| 45 class ClientCallTask : public MockWebSpeechRecognizer::Task { |
| 46 public: |
| 47 ClientCallTask(MockWebSpeechRecognizer* mock, ClientFunctionPointer function
) |
| 48 : MockWebSpeechRecognizer::Task(mock) |
| 49 , m_function(function) |
| 50 { |
| 51 } |
| 52 |
| 53 virtual void run() OVERRIDE { (m_recognizer->client()->*m_function)(m_recogn
izer->handle()); } |
| 54 |
| 55 private: |
| 56 ClientFunctionPointer m_function; |
| 57 }; |
| 58 |
| 59 // Task for delivering a result event. |
| 60 class ResultTask : public MockWebSpeechRecognizer::Task { |
| 61 public: |
| 62 ResultTask(MockWebSpeechRecognizer* mock, const WebString transcript, float
confidence) |
| 63 : MockWebSpeechRecognizer::Task(mock) |
| 64 , m_transcript(transcript) |
| 65 , m_confidence(confidence) |
| 66 { |
| 67 } |
| 68 |
| 69 virtual void run() OVERRIDE |
| 70 { |
| 71 WebVector<WebString> transcripts(static_cast<size_t>(1)); |
| 72 WebVector<float> confidences(static_cast<size_t>(1)); |
| 73 transcripts[0] = m_transcript; |
| 74 confidences[0] = m_confidence; |
| 75 WebVector<WebSpeechRecognitionResult> finalResults(static_cast<size_t>(1
)); |
| 76 WebVector<WebSpeechRecognitionResult> interimResults; |
| 77 finalResults[0].assign(transcripts, confidences, true); |
| 78 |
| 79 m_recognizer->client()->didReceiveResults(m_recognizer->handle(), finalR
esults, interimResults); |
| 80 } |
| 81 |
| 82 private: |
| 83 WebString m_transcript; |
| 84 float m_confidence; |
| 85 }; |
| 86 |
| 87 // Task for delivering a nomatch event. |
| 88 class NoMatchTask : public MockWebSpeechRecognizer::Task { |
| 89 public: |
| 90 NoMatchTask(MockWebSpeechRecognizer* mock) : MockWebSpeechRecognizer::Task(m
ock) { } |
| 91 virtual void run() OVERRIDE { m_recognizer->client()->didReceiveNoMatch(m_re
cognizer->handle(), WebSpeechRecognitionResult()); } |
| 92 }; |
| 93 |
| 94 // Task for delivering an error event. |
| 95 class ErrorTask : public MockWebSpeechRecognizer::Task { |
| 96 public: |
| 97 ErrorTask(MockWebSpeechRecognizer* mock, WebSpeechRecognizerClient::ErrorCod
e code, const WebString& message) |
| 98 : MockWebSpeechRecognizer::Task(mock) |
| 99 , m_code(code) |
| 100 , m_message(message) |
| 101 { |
| 102 } |
| 103 |
| 104 virtual void run() OVERRIDE { m_recognizer->client()->didReceiveError(m_reco
gnizer->handle(), m_message, m_code); } |
| 105 |
| 106 private: |
| 107 WebSpeechRecognizerClient::ErrorCode m_code; |
| 108 WebString m_message; |
| 109 }; |
| 110 |
| 111 } // namespace |
| 112 |
| 113 MockWebSpeechRecognizer::MockWebSpeechRecognizer() |
| 114 : m_wasAborted(false) |
| 115 , m_taskQueueRunning(false) |
| 116 , m_delegate(0) |
| 117 { |
| 118 } |
| 119 |
| 120 MockWebSpeechRecognizer::~MockWebSpeechRecognizer() |
| 121 { |
| 122 clearTaskQueue(); |
| 123 } |
| 124 |
| 125 void MockWebSpeechRecognizer::setDelegate(WebTestDelegate* delegate) |
| 126 { |
| 127 m_delegate = delegate; |
| 128 } |
| 129 |
| 130 void MockWebSpeechRecognizer::start(const WebSpeechRecognitionHandle& handle, co
nst WebSpeechRecognitionParams& params, WebSpeechRecognizerClient* client) |
| 131 { |
| 132 m_wasAborted = false; |
| 133 m_handle = handle; |
| 134 m_client = client; |
| 135 |
| 136 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idStart)); |
| 137 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idStartAudio)); |
| 138 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idStartSound)); |
| 139 |
| 140 if (!m_mockTranscripts.empty()) { |
| 141 BLINK_ASSERT(m_mockTranscripts.size() == m_mockConfidences.size()); |
| 142 |
| 143 for (size_t i = 0; i < m_mockTranscripts.size(); ++i) |
| 144 m_taskQueue.push_back(new ResultTask(this, m_mockTranscripts[i], m_m
ockConfidences[i])); |
| 145 |
| 146 m_mockTranscripts.clear(); |
| 147 m_mockConfidences.clear(); |
| 148 } else |
| 149 m_taskQueue.push_back(new NoMatchTask(this)); |
| 150 |
| 151 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idEndSound)); |
| 152 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idEndAudio)); |
| 153 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idEnd)); |
| 154 |
| 155 startTaskQueue(); |
| 156 } |
| 157 |
| 158 void MockWebSpeechRecognizer::stop(const WebSpeechRecognitionHandle& handle, Web
SpeechRecognizerClient* client) |
| 159 { |
| 160 m_handle = handle; |
| 161 m_client = client; |
| 162 |
| 163 // FIXME: Implement. |
| 164 BLINK_ASSERT_NOT_REACHED(); |
| 165 } |
| 166 |
| 167 void MockWebSpeechRecognizer::abort(const WebSpeechRecognitionHandle& handle, We
bSpeechRecognizerClient* client) |
| 168 { |
| 169 m_handle = handle; |
| 170 m_client = client; |
| 171 |
| 172 clearTaskQueue(); |
| 173 m_wasAborted = true; |
| 174 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idEnd)); |
| 175 startTaskQueue(); |
| 176 } |
| 177 |
| 178 void MockWebSpeechRecognizer::addMockResult(const WebString& transcript, float c
onfidence) |
| 179 { |
| 180 m_mockTranscripts.push_back(transcript); |
| 181 m_mockConfidences.push_back(confidence); |
| 182 } |
| 183 |
| 184 void MockWebSpeechRecognizer::setError(const WebString& error, const WebString&
message) |
| 185 { |
| 186 WebSpeechRecognizerClient::ErrorCode code; |
| 187 if (error == "OtherError") |
| 188 code = WebSpeechRecognizerClient::OtherError; |
| 189 else if (error == "NoSpeechError") |
| 190 code = WebSpeechRecognizerClient::NoSpeechError; |
| 191 else if (error == "AbortedError") |
| 192 code = WebSpeechRecognizerClient::AbortedError; |
| 193 else if (error == "AudioCaptureError") |
| 194 code = WebSpeechRecognizerClient::AudioCaptureError; |
| 195 else if (error == "NetworkError") |
| 196 code = WebSpeechRecognizerClient::NetworkError; |
| 197 else if (error == "NotAllowedError") |
| 198 code = WebSpeechRecognizerClient::NotAllowedError; |
| 199 else if (error == "ServiceNotAllowedError") |
| 200 code = WebSpeechRecognizerClient::ServiceNotAllowedError; |
| 201 else if (error == "BadGrammarError") |
| 202 code = WebSpeechRecognizerClient::BadGrammarError; |
| 203 else if (error == "LanguageNotSupportedError") |
| 204 code = WebSpeechRecognizerClient::LanguageNotSupportedError; |
| 205 else |
| 206 return; |
| 207 |
| 208 clearTaskQueue(); |
| 209 m_taskQueue.push_back(new ErrorTask(this, code, message)); |
| 210 m_taskQueue.push_back(new ClientCallTask(this, &WebSpeechRecognizerClient::d
idEnd)); |
| 211 startTaskQueue(); |
| 212 } |
| 213 |
| 214 void MockWebSpeechRecognizer::startTaskQueue() |
| 215 { |
| 216 if (m_taskQueueRunning) |
| 217 return; |
| 218 m_delegate->postTask(new StepTask(this)); |
| 219 m_taskQueueRunning = true; |
| 220 } |
| 221 |
| 222 void MockWebSpeechRecognizer::clearTaskQueue() |
| 223 { |
| 224 while (!m_taskQueue.empty()) { |
| 225 delete m_taskQueue.front(); |
| 226 m_taskQueue.pop_front(); |
| 227 } |
| 228 m_taskQueueRunning = false; |
| 229 } |
| 230 |
| 231 void MockWebSpeechRecognizer::StepTask::runIfValid() |
| 232 { |
| 233 if (m_object->m_taskQueue.empty()) { |
| 234 m_object->m_taskQueueRunning = false; |
| 235 return; |
| 236 } |
| 237 |
| 238 Task* task = m_object->m_taskQueue.front(); |
| 239 m_object->m_taskQueue.pop_front(); |
| 240 task->run(); |
| 241 delete task; |
| 242 |
| 243 if (m_object->m_taskQueue.empty()) { |
| 244 m_object->m_taskQueueRunning = false; |
| 245 return; |
| 246 } |
| 247 |
| 248 m_object->m_delegate->postTask(new StepTask(m_object)); |
| 249 } |
| 250 |
| 251 } |
| OLD | NEW |