michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsThreadUtils.h" michael@0: michael@0: #include "FakeSpeechRecognitionService.h" michael@0: michael@0: #include "SpeechRecognition.h" michael@0: #include "SpeechRecognitionAlternative.h" michael@0: #include "SpeechRecognitionResult.h" michael@0: #include "SpeechRecognitionResultList.h" michael@0: #include "nsIObserverService.h" michael@0: #include "mozilla/Services.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: using namespace dom; michael@0: michael@0: NS_IMPL_ISUPPORTS(FakeSpeechRecognitionService, nsISpeechRecognitionService, nsIObserver) michael@0: michael@0: FakeSpeechRecognitionService::FakeSpeechRecognitionService() michael@0: { michael@0: } michael@0: michael@0: FakeSpeechRecognitionService::~FakeSpeechRecognitionService() michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FakeSpeechRecognitionService::Initialize(WeakPtr aSpeechRecognition) michael@0: { michael@0: mRecognition = aSpeechRecognition; michael@0: nsCOMPtr obs = services::GetObserverService(); michael@0: obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false); michael@0: obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FakeSpeechRecognitionService::ProcessAudioSegment(AudioSegment* aAudioSegment) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FakeSpeechRecognitionService::SoundEnd() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FakeSpeechRecognitionService::Abort() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FakeSpeechRecognitionService::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData) michael@0: { michael@0: MOZ_ASSERT(mRecognition->mTestConfig.mFakeRecognitionService, michael@0: "Got request to fake recognition service event, but " michael@0: TEST_PREFERENCE_FAKE_RECOGNITION_SERVICE " is not set"); michael@0: michael@0: if (!strcmp(aTopic, SPEECH_RECOGNITION_TEST_END_TOPIC)) { michael@0: nsCOMPtr obs = services::GetObserverService(); michael@0: obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC); michael@0: obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: const nsDependentString eventName = nsDependentString(aData); michael@0: michael@0: if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_ERROR")) { michael@0: mRecognition->DispatchError(SpeechRecognition::EVENT_RECOGNITIONSERVICE_ERROR, michael@0: SpeechRecognitionErrorCode::Network, // TODO different codes? michael@0: NS_LITERAL_STRING("RECOGNITIONSERVICE_ERROR test event")); michael@0: michael@0: } else if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_FINAL_RESULT")) { michael@0: nsRefPtr event = michael@0: new SpeechEvent(mRecognition, michael@0: SpeechRecognition::EVENT_RECOGNITIONSERVICE_FINAL_RESULT); michael@0: michael@0: event->mRecognitionResultList = BuildMockResultList(); michael@0: NS_DispatchToMainThread(event); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: SpeechRecognitionResultList* michael@0: FakeSpeechRecognitionService::BuildMockResultList() michael@0: { michael@0: SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition); michael@0: SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition); michael@0: SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition); michael@0: michael@0: alternative->mTranscript = NS_LITERAL_STRING("Mock final result"); michael@0: alternative->mConfidence = 0.0f; michael@0: michael@0: result->mItems.AppendElement(alternative); michael@0: resultList->mItems.AppendElement(result); michael@0: michael@0: return resultList; michael@0: } michael@0: michael@0: } // namespace mozilla