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