Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsThreadUtils.h"
8 :
9 : #include "FakeSpeechRecognitionService.h"
10 : #include "MediaPrefs.h"
11 :
12 : #include "SpeechRecognition.h"
13 : #include "SpeechRecognitionAlternative.h"
14 : #include "SpeechRecognitionResult.h"
15 : #include "SpeechRecognitionResultList.h"
16 : #include "nsIObserverService.h"
17 : #include "mozilla/Services.h"
18 :
19 : namespace mozilla {
20 :
21 : using namespace dom;
22 :
23 0 : NS_IMPL_ISUPPORTS(FakeSpeechRecognitionService, nsISpeechRecognitionService, nsIObserver)
24 :
25 0 : FakeSpeechRecognitionService::FakeSpeechRecognitionService()
26 : {
27 0 : }
28 :
29 0 : FakeSpeechRecognitionService::~FakeSpeechRecognitionService()
30 : {
31 0 : }
32 :
33 : NS_IMETHODIMP
34 0 : FakeSpeechRecognitionService::Initialize(WeakPtr<SpeechRecognition> aSpeechRecognition)
35 : {
36 0 : mRecognition = aSpeechRecognition;
37 0 : nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
38 0 : obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false);
39 0 : obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false);
40 0 : return NS_OK;
41 : }
42 :
43 : NS_IMETHODIMP
44 0 : FakeSpeechRecognitionService::ProcessAudioSegment(AudioSegment* aAudioSegment, int32_t aSampleRate)
45 : {
46 0 : return NS_OK;
47 : }
48 :
49 : NS_IMETHODIMP
50 0 : FakeSpeechRecognitionService::SoundEnd()
51 : {
52 0 : return NS_OK;
53 : }
54 :
55 : NS_IMETHODIMP
56 0 : FakeSpeechRecognitionService::ValidateAndSetGrammarList(mozilla::dom::SpeechGrammar*, nsISpeechGrammarCompilationCallback*)
57 : {
58 0 : return NS_OK;
59 : }
60 :
61 : NS_IMETHODIMP
62 0 : FakeSpeechRecognitionService::Abort()
63 : {
64 0 : return NS_OK;
65 : }
66 :
67 : NS_IMETHODIMP
68 0 : FakeSpeechRecognitionService::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
69 : {
70 0 : MOZ_ASSERT(MediaPrefs::WebSpeechFakeRecognitionService(),
71 : "Got request to fake recognition service event, but "
72 : TEST_PREFERENCE_FAKE_RECOGNITION_SERVICE " is not set");
73 :
74 0 : if (!strcmp(aTopic, SPEECH_RECOGNITION_TEST_END_TOPIC)) {
75 0 : nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
76 0 : obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC);
77 0 : obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC);
78 :
79 0 : return NS_OK;
80 : }
81 :
82 0 : const nsDependentString eventName = nsDependentString(aData);
83 :
84 0 : if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_ERROR")) {
85 0 : mRecognition->DispatchError(SpeechRecognition::EVENT_RECOGNITIONSERVICE_ERROR,
86 : SpeechRecognitionErrorCode::Network, // TODO different codes?
87 0 : NS_LITERAL_STRING("RECOGNITIONSERVICE_ERROR test event"));
88 :
89 0 : } else if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_FINAL_RESULT")) {
90 : RefPtr<SpeechEvent> event =
91 : new SpeechEvent(mRecognition,
92 0 : SpeechRecognition::EVENT_RECOGNITIONSERVICE_FINAL_RESULT);
93 :
94 0 : event->mRecognitionResultList = BuildMockResultList();
95 0 : NS_DispatchToMainThread(event);
96 : }
97 :
98 0 : return NS_OK;
99 : }
100 :
101 : SpeechRecognitionResultList*
102 0 : FakeSpeechRecognitionService::BuildMockResultList()
103 : {
104 0 : SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition);
105 0 : SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition);
106 0 : if (0 < mRecognition->MaxAlternatives()) {
107 0 : SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition);
108 :
109 0 : alternative->mTranscript = NS_LITERAL_STRING("Mock final result");
110 0 : alternative->mConfidence = 0.0f;
111 :
112 0 : result->mItems.AppendElement(alternative);
113 : }
114 0 : resultList->mItems.AppendElement(result);
115 :
116 0 : return resultList;
117 : }
118 :
119 : } // namespace mozilla
|