2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "SpeechInputClientMock.h"
34 #if ENABLE(INPUT_SPEECH)
36 #include "SecurityOrigin.h"
37 #include "SpeechInputListener.h"
41 SpeechInputClientMock::SpeechInputClientMock()
43 , m_timer(this, &SpeechInputClientMock::timerFired)
49 void SpeechInputClientMock::setListener(SpeechInputListener* listener)
51 m_listener = listener;
54 bool SpeechInputClientMock::startRecognition(int requestId, const IntRect& elementRect, const AtomicString& language, const String& grammar, SecurityOrigin* origin)
56 if (m_timer.isActive())
58 m_requestId = requestId;
60 m_language = language;
61 m_timer.startOneShot(0);
65 void SpeechInputClientMock::stopRecording(int requestId)
67 ASSERT(requestId == m_requestId);
68 if (m_timer.isActive() && m_recording) {
74 void SpeechInputClientMock::cancelRecognition(int requestId)
76 if (m_timer.isActive()) {
77 ASSERT(requestId == m_requestId);
80 m_listener->didCompleteRecognition(m_requestId);
85 void SpeechInputClientMock::addRecognitionResult(const String& result, double confidence, const AtomicString& language)
87 if (language.isEmpty())
88 m_resultsForEmptyLanguage.append(SpeechInputResult::create(result, confidence));
90 if (!m_recognitionResults.contains(language))
91 m_recognitionResults.set(language, SpeechInputResultArray());
92 m_recognitionResults.find(language)->second.append(SpeechInputResult::create(result, confidence));
96 void SpeechInputClientMock::clearResults()
98 m_resultsForEmptyLanguage.clear();
99 m_recognitionResults.clear();
102 void SpeechInputClientMock::timerFired(WebCore::Timer<SpeechInputClientMock>*)
106 m_listener->didCompleteRecording(m_requestId);
107 m_timer.startOneShot(0);
109 bool noResultsFound = false;
111 // We take a copy of the requestId here so that if scripts destroyed the input element
112 // inside one of the callbacks below, we'll still know what this session's requestId was.
113 int requestId = m_requestId;
116 // Empty language case must be handled separately to avoid problems with HashMap and empty keys.
117 if (m_language.isEmpty()) {
118 if (!m_resultsForEmptyLanguage.isEmpty())
119 m_listener->setRecognitionResult(requestId, m_resultsForEmptyLanguage);
121 noResultsFound = true;
123 if (m_recognitionResults.contains(m_language))
124 m_listener->setRecognitionResult(requestId, m_recognitionResults.get(m_language));
126 noResultsFound = true;
129 if (noResultsFound) {
130 // Can't avoid setting a result even if no result was set for the given language.
131 // This would avoid generating the events used to check the results and the test would timeout.
132 String error("error: no result found for language '");
133 error.append(m_language);
135 SpeechInputResultArray results;
136 results.append(SpeechInputResult::create(error, 1.0));
137 m_listener->setRecognitionResult(requestId, results);
140 m_listener->didCompleteRecognition(requestId);
144 } // namespace WebCore
146 #endif // ENABLE(INPUT_SPEECH)