Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "mozilla/StaticPrefs.h"
18
19
namespace mozilla {
20
21
using namespace dom;
22
23
NS_IMPL_ISUPPORTS(FakeSpeechRecognitionService, nsISpeechRecognitionService, nsIObserver)
24
25
FakeSpeechRecognitionService::FakeSpeechRecognitionService()
26
0
{
27
0
}
28
29
FakeSpeechRecognitionService::~FakeSpeechRecognitionService()
30
0
{
31
0
}
32
33
NS_IMETHODIMP
34
FakeSpeechRecognitionService::Initialize(WeakPtr<SpeechRecognition> aSpeechRecognition)
35
0
{
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
0
}
42
43
NS_IMETHODIMP
44
FakeSpeechRecognitionService::ProcessAudioSegment(AudioSegment* aAudioSegment, int32_t aSampleRate)
45
0
{
46
0
  return NS_OK;
47
0
}
48
49
NS_IMETHODIMP
50
FakeSpeechRecognitionService::SoundEnd()
51
0
{
52
0
  return NS_OK;
53
0
}
54
55
NS_IMETHODIMP
56
FakeSpeechRecognitionService::ValidateAndSetGrammarList(mozilla::dom::SpeechGrammar*, nsISpeechGrammarCompilationCallback*)
57
0
{
58
0
  return NS_OK;
59
0
}
60
61
NS_IMETHODIMP
62
FakeSpeechRecognitionService::Abort()
63
0
{
64
0
  return NS_OK;
65
0
}
66
67
NS_IMETHODIMP
68
FakeSpeechRecognitionService::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
69
0
{
70
0
  MOZ_ASSERT(StaticPrefs::MediaWebspeechTextFakeRecognitionService(),
71
0
             "Got request to fake recognition service event, but "
72
0
             "media.webspeech.test.fake_recognition_service is not set");
73
0
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
0
79
0
    return NS_OK;
80
0
  }
81
0
82
0
  const nsDependentString eventName = nsDependentString(aData);
83
0
84
0
  if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_ERROR")) {
85
0
    mRecognition->DispatchError(SpeechRecognition::EVENT_RECOGNITIONSERVICE_ERROR,
86
0
                                SpeechRecognitionErrorCode::Network, // TODO different codes?
87
0
                                NS_LITERAL_STRING("RECOGNITIONSERVICE_ERROR test event"));
88
0
89
0
  } else if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_FINAL_RESULT")) {
90
0
    RefPtr<SpeechEvent> event =
91
0
      new SpeechEvent(mRecognition,
92
0
                      SpeechRecognition::EVENT_RECOGNITIONSERVICE_FINAL_RESULT);
93
0
94
0
    event->mRecognitionResultList = BuildMockResultList();
95
0
    NS_DispatchToMainThread(event);
96
0
  }
97
0
98
0
  return NS_OK;
99
0
}
100
101
SpeechRecognitionResultList*
102
FakeSpeechRecognitionService::BuildMockResultList()
103
0
{
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
0
109
0
    alternative->mTranscript = NS_LITERAL_STRING("Mock final result");
110
0
    alternative->mConfidence = 0.0f;
111
0
112
0
    result->mItems.AppendElement(alternative);
113
0
  }
114
0
  resultList->mItems.AppendElement(result);
115
0
116
0
  return resultList;
117
0
}
118
119
} // namespace mozilla