|
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 |
|
11 #include "SpeechRecognition.h" |
|
12 #include "SpeechRecognitionAlternative.h" |
|
13 #include "SpeechRecognitionResult.h" |
|
14 #include "SpeechRecognitionResultList.h" |
|
15 #include "nsIObserverService.h" |
|
16 #include "mozilla/Services.h" |
|
17 |
|
18 namespace mozilla { |
|
19 |
|
20 using namespace dom; |
|
21 |
|
22 NS_IMPL_ISUPPORTS(FakeSpeechRecognitionService, nsISpeechRecognitionService, nsIObserver) |
|
23 |
|
24 FakeSpeechRecognitionService::FakeSpeechRecognitionService() |
|
25 { |
|
26 } |
|
27 |
|
28 FakeSpeechRecognitionService::~FakeSpeechRecognitionService() |
|
29 { |
|
30 } |
|
31 |
|
32 NS_IMETHODIMP |
|
33 FakeSpeechRecognitionService::Initialize(WeakPtr<SpeechRecognition> aSpeechRecognition) |
|
34 { |
|
35 mRecognition = aSpeechRecognition; |
|
36 nsCOMPtr<nsIObserverService> obs = services::GetObserverService(); |
|
37 obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false); |
|
38 obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false); |
|
39 return NS_OK; |
|
40 } |
|
41 |
|
42 NS_IMETHODIMP |
|
43 FakeSpeechRecognitionService::ProcessAudioSegment(AudioSegment* aAudioSegment) |
|
44 { |
|
45 return NS_OK; |
|
46 } |
|
47 |
|
48 NS_IMETHODIMP |
|
49 FakeSpeechRecognitionService::SoundEnd() |
|
50 { |
|
51 return NS_OK; |
|
52 } |
|
53 |
|
54 NS_IMETHODIMP |
|
55 FakeSpeechRecognitionService::Abort() |
|
56 { |
|
57 return NS_OK; |
|
58 } |
|
59 |
|
60 NS_IMETHODIMP |
|
61 FakeSpeechRecognitionService::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData) |
|
62 { |
|
63 MOZ_ASSERT(mRecognition->mTestConfig.mFakeRecognitionService, |
|
64 "Got request to fake recognition service event, but " |
|
65 TEST_PREFERENCE_FAKE_RECOGNITION_SERVICE " is not set"); |
|
66 |
|
67 if (!strcmp(aTopic, SPEECH_RECOGNITION_TEST_END_TOPIC)) { |
|
68 nsCOMPtr<nsIObserverService> obs = services::GetObserverService(); |
|
69 obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC); |
|
70 obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC); |
|
71 |
|
72 return NS_OK; |
|
73 } |
|
74 |
|
75 const nsDependentString eventName = nsDependentString(aData); |
|
76 |
|
77 if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_ERROR")) { |
|
78 mRecognition->DispatchError(SpeechRecognition::EVENT_RECOGNITIONSERVICE_ERROR, |
|
79 SpeechRecognitionErrorCode::Network, // TODO different codes? |
|
80 NS_LITERAL_STRING("RECOGNITIONSERVICE_ERROR test event")); |
|
81 |
|
82 } else if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_FINAL_RESULT")) { |
|
83 nsRefPtr<SpeechEvent> event = |
|
84 new SpeechEvent(mRecognition, |
|
85 SpeechRecognition::EVENT_RECOGNITIONSERVICE_FINAL_RESULT); |
|
86 |
|
87 event->mRecognitionResultList = BuildMockResultList(); |
|
88 NS_DispatchToMainThread(event); |
|
89 } |
|
90 |
|
91 return NS_OK; |
|
92 } |
|
93 |
|
94 SpeechRecognitionResultList* |
|
95 FakeSpeechRecognitionService::BuildMockResultList() |
|
96 { |
|
97 SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition); |
|
98 SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition); |
|
99 SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition); |
|
100 |
|
101 alternative->mTranscript = NS_LITERAL_STRING("Mock final result"); |
|
102 alternative->mConfidence = 0.0f; |
|
103 |
|
104 result->mItems.AppendElement(alternative); |
|
105 resultList->mItems.AppendElement(result); |
|
106 |
|
107 return resultList; |
|
108 } |
|
109 |
|
110 } // namespace mozilla |