1/* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31#include "config.h" 32#include "SpeechInputClientMock.h" 33 34#if ENABLE(INPUT_SPEECH) 35 36#include "SecurityOrigin.h" 37#include "SpeechInputListener.h" 38 39namespace WebCore { 40 41SpeechInputClientMock::SpeechInputClientMock() 42 : m_recording(false) 43 , m_timer(this, &SpeechInputClientMock::timerFired) 44 , m_listener(0) 45 , m_requestId(0) 46{ 47} 48 49void SpeechInputClientMock::setListener(SpeechInputListener* listener) 50{ 51 m_listener = listener; 52} 53 54bool SpeechInputClientMock::startRecognition(int requestId, const IntRect& elementRect, const AtomicString& language, const String& grammar, SecurityOrigin* origin) 55{ 56 if (m_timer.isActive()) 57 return false; 58 m_requestId = requestId; 59 m_recording = true; 60 m_language = language; 61 m_timer.startOneShot(0); 62 return true; 63} 64 65void SpeechInputClientMock::stopRecording(int requestId) 66{ 67 ASSERT(requestId == m_requestId); 68 if (m_timer.isActive() && m_recording) { 69 m_timer.stop(); 70 timerFired(&m_timer); 71 } 72} 73 74void SpeechInputClientMock::cancelRecognition(int requestId) 75{ 76 if (m_timer.isActive()) { 77 ASSERT(requestId == m_requestId); 78 m_timer.stop(); 79 m_recording = false; 80 m_listener->didCompleteRecognition(m_requestId); 81 m_requestId = 0; 82 } 83} 84 85void SpeechInputClientMock::addRecognitionResult(const String& result, double confidence, const AtomicString& language) 86{ 87 if (language.isEmpty()) 88 m_resultsForEmptyLanguage.append(SpeechInputResult::create(result, confidence)); 89 else { 90 if (!m_recognitionResults.contains(language)) 91 m_recognitionResults.set(language, SpeechInputResultArray()); 92 m_recognitionResults.find(language)->second.append(SpeechInputResult::create(result, confidence)); 93 } 94} 95 96void SpeechInputClientMock::clearResults() 97{ 98 m_resultsForEmptyLanguage.clear(); 99 m_recognitionResults.clear(); 100} 101 102void SpeechInputClientMock::timerFired(WebCore::Timer<SpeechInputClientMock>*) 103{ 104 if (m_recording) { 105 m_recording = false; 106 m_listener->didCompleteRecording(m_requestId); 107 m_timer.startOneShot(0); 108 } else { 109 bool noResultsFound = false; 110 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; 114 m_requestId = 0; 115 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); 120 else 121 noResultsFound = true; 122 } else { 123 if (m_recognitionResults.contains(m_language)) 124 m_listener->setRecognitionResult(requestId, m_recognitionResults.get(m_language)); 125 else 126 noResultsFound = true; 127 } 128 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); 134 error.append("'"); 135 SpeechInputResultArray results; 136 results.append(SpeechInputResult::create(error, 1.0)); 137 m_listener->setRecognitionResult(requestId, results); 138 } 139 140 m_listener->didCompleteRecognition(requestId); 141 } 142} 143 144} // namespace WebCore 145 146#endif // ENABLE(INPUT_SPEECH) 147