content/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsThreadUtils.h"
    1.11 +
    1.12 +#include "FakeSpeechRecognitionService.h"
    1.13 +
    1.14 +#include "SpeechRecognition.h"
    1.15 +#include "SpeechRecognitionAlternative.h"
    1.16 +#include "SpeechRecognitionResult.h"
    1.17 +#include "SpeechRecognitionResultList.h"
    1.18 +#include "nsIObserverService.h"
    1.19 +#include "mozilla/Services.h"
    1.20 +
    1.21 +namespace mozilla {
    1.22 +
    1.23 +using namespace dom;
    1.24 +
    1.25 +NS_IMPL_ISUPPORTS(FakeSpeechRecognitionService, nsISpeechRecognitionService, nsIObserver)
    1.26 +
    1.27 +FakeSpeechRecognitionService::FakeSpeechRecognitionService()
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +FakeSpeechRecognitionService::~FakeSpeechRecognitionService()
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +NS_IMETHODIMP
    1.36 +FakeSpeechRecognitionService::Initialize(WeakPtr<SpeechRecognition> aSpeechRecognition)
    1.37 +{
    1.38 +  mRecognition = aSpeechRecognition;
    1.39 +  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
    1.40 +  obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false);
    1.41 +  obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false);
    1.42 +  return NS_OK;
    1.43 +}
    1.44 +
    1.45 +NS_IMETHODIMP
    1.46 +FakeSpeechRecognitionService::ProcessAudioSegment(AudioSegment* aAudioSegment)
    1.47 +{
    1.48 +  return NS_OK;
    1.49 +}
    1.50 +
    1.51 +NS_IMETHODIMP
    1.52 +FakeSpeechRecognitionService::SoundEnd()
    1.53 +{
    1.54 +  return NS_OK;
    1.55 +}
    1.56 +
    1.57 +NS_IMETHODIMP
    1.58 +FakeSpeechRecognitionService::Abort()
    1.59 +{
    1.60 +  return NS_OK;
    1.61 +}
    1.62 +
    1.63 +NS_IMETHODIMP
    1.64 +FakeSpeechRecognitionService::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
    1.65 +{
    1.66 +  MOZ_ASSERT(mRecognition->mTestConfig.mFakeRecognitionService,
    1.67 +             "Got request to fake recognition service event, but "
    1.68 +             TEST_PREFERENCE_FAKE_RECOGNITION_SERVICE " is not set");
    1.69 +
    1.70 +  if (!strcmp(aTopic, SPEECH_RECOGNITION_TEST_END_TOPIC)) {
    1.71 +    nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
    1.72 +    obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC);
    1.73 +    obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC);
    1.74 +
    1.75 +    return NS_OK;
    1.76 +  }
    1.77 +
    1.78 +  const nsDependentString eventName = nsDependentString(aData);
    1.79 +
    1.80 +  if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_ERROR")) {
    1.81 +    mRecognition->DispatchError(SpeechRecognition::EVENT_RECOGNITIONSERVICE_ERROR,
    1.82 +                                SpeechRecognitionErrorCode::Network, // TODO different codes?
    1.83 +                                NS_LITERAL_STRING("RECOGNITIONSERVICE_ERROR test event"));
    1.84 +
    1.85 +  } else if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_FINAL_RESULT")) {
    1.86 +    nsRefPtr<SpeechEvent> event =
    1.87 +      new SpeechEvent(mRecognition,
    1.88 +                      SpeechRecognition::EVENT_RECOGNITIONSERVICE_FINAL_RESULT);
    1.89 +
    1.90 +    event->mRecognitionResultList = BuildMockResultList();
    1.91 +    NS_DispatchToMainThread(event);
    1.92 +  }
    1.93 +
    1.94 +  return NS_OK;
    1.95 +}
    1.96 +
    1.97 +SpeechRecognitionResultList*
    1.98 +FakeSpeechRecognitionService::BuildMockResultList()
    1.99 +{
   1.100 +  SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition);
   1.101 +  SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition);
   1.102 +  SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition);
   1.103 +
   1.104 +  alternative->mTranscript = NS_LITERAL_STRING("Mock final result");
   1.105 +  alternative->mConfidence = 0.0f;
   1.106 +
   1.107 +  result->mItems.AppendElement(alternative);
   1.108 +  resultList->mItems.AppendElement(result);
   1.109 +
   1.110 +  return resultList;
   1.111 +}
   1.112 +
   1.113 +} // namespace mozilla

mercurial