-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ShadowTextToSpeech.java
287 lines (251 loc) · 9.1 KB
/
ShadowTextToSpeech.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.Engine;
import android.speech.tts.UtteranceProgressListener;
import android.speech.tts.Voice;
import com.google.common.collect.ImmutableList;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
@Implements(TextToSpeech.class)
public class ShadowTextToSpeech {
private static final Set<Locale> languageAvailabilities = new HashSet<>();
private static final Set<Voice> voices = new HashSet<>();
private static TextToSpeech lastTextToSpeechInstance;
private static int onInitStatus = TextToSpeech.SUCCESS;
@RealObject private TextToSpeech tts;
private Context context;
private TextToSpeech.OnInitListener listener;
private String lastSpokenText;
private boolean shutdown = false;
private boolean stopped = true;
private int queueMode = -1;
private Locale language = null;
private String lastSynthesizeToFileText;
private Voice currentVoice = null;
private final List<String> spokenTextList = new ArrayList<>();
@Implementation
protected void __constructor__(
Context context,
TextToSpeech.OnInitListener listener,
String engine,
String packageName,
boolean useFallback) {
this.context = context;
this.listener = listener;
lastTextToSpeechInstance = tts;
Shadow.invokeConstructor(
TextToSpeech.class,
tts,
ClassParameter.from(Context.class, context),
ClassParameter.from(TextToSpeech.OnInitListener.class, listener),
ClassParameter.from(String.class, engine),
ClassParameter.from(String.class, packageName),
ClassParameter.from(boolean.class, useFallback));
}
@Implementation
protected int initTts() {
// Attempt to model real Android code, where success callbacks occur asynchronously and error
// callbacks occur immediately.
if (listener != null) {
if (onInitStatus == TextToSpeech.SUCCESS) {
new Handler(Looper.getMainLooper()).post(() -> listener.onInit(onInitStatus));
} else {
listener.onInit(onInitStatus);
}
}
return onInitStatus;
}
/**
* Sets the code used by the {@link android.speech.tts.TextToSpeech.OnInitListener} callback
* during initialization. This can test cases where {@link TextToSpeech.ERROR} is used.
*/
public static void setOnInitStatus(int status) {
onInitStatus = status;
}
/**
* Speaks the string using the specified queuing strategy and speech parameters.
*
* @param params The real implementation converts the hashmap into a bundle, but the bundle
* argument is not used in the shadow implementation.
*/
@Implementation
protected int speak(
final String text, final int queueMode, final HashMap<String, String> params) {
if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
return Shadow.directlyOn(tts, TextToSpeech.class).speak(text, queueMode, params);
}
return speak(
text, queueMode, null, params == null ? null : params.get(Engine.KEY_PARAM_UTTERANCE_ID));
}
@Implementation(minSdk = LOLLIPOP)
protected int speak(
final CharSequence text, final int queueMode, final Bundle params, final String utteranceId) {
stopped = false;
lastSpokenText = text.toString();
spokenTextList.add(text.toString());
this.queueMode = queueMode;
if (RuntimeEnvironment.getApiLevel() >= ICE_CREAM_SANDWICH_MR1) {
if (utteranceId != null) {
// The onStart and onDone callbacks are normally delivered asynchronously. Since in
// Robolectric we don't need the wait for TTS package, the asynchronous callbacks are
// simulated by posting it on a handler. The behavior of the callback can be changed for
// each individual test by changing the idling mode of the foreground scheduler.
Handler handler = new Handler(Looper.getMainLooper());
handler.post(
() -> {
UtteranceProgressListener utteranceProgressListener = getUtteranceProgressListener();
if (utteranceProgressListener != null) {
utteranceProgressListener.onStart(utteranceId);
}
// The onDone callback is posted in a separate run-loop from onStart, so that tests
// can pause the scheduler and test the behavior between these two callbacks.
handler.post(
() -> {
UtteranceProgressListener utteranceProgressListener2 =
getUtteranceProgressListener();
if (utteranceProgressListener2 != null) {
utteranceProgressListener2.onDone(utteranceId);
}
});
});
}
}
return TextToSpeech.SUCCESS;
}
@Implementation
protected void shutdown() {
shutdown = true;
}
@Implementation
protected int stop() {
stopped = true;
return TextToSpeech.SUCCESS;
}
@Implementation
protected int isLanguageAvailable(Locale lang) {
for (Locale locale : languageAvailabilities) {
if (locale.getISO3Language().equals(lang.getISO3Language())) {
if (locale.getISO3Country().equals(lang.getISO3Country())) {
if (locale.getVariant().equals(lang.getVariant())) {
return TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE;
}
return TextToSpeech.LANG_COUNTRY_AVAILABLE;
}
return TextToSpeech.LANG_AVAILABLE;
}
}
return TextToSpeech.LANG_NOT_SUPPORTED;
}
@Implementation
protected int setLanguage(Locale locale) {
this.language = locale;
return isLanguageAvailable(locale);
}
/**
* Stores {@code text} and returns {@link TextToSpeech#SUCCESS}.
*
* @see #getLastSynthesizeToFileText()
*/
@Implementation(minSdk = LOLLIPOP)
protected int synthesizeToFile(CharSequence text, Bundle params, File file, String utteranceId) {
this.lastSynthesizeToFileText = text.toString();
return TextToSpeech.SUCCESS;
}
@Implementation(minSdk = LOLLIPOP)
protected int setVoice(Voice voice) {
this.currentVoice = voice;
return TextToSpeech.SUCCESS;
}
@Implementation(minSdk = LOLLIPOP)
protected Set<Voice> getVoices() {
return voices;
}
public UtteranceProgressListener getUtteranceProgressListener() {
return ReflectionHelpers.getField(tts, "mUtteranceProgressListener");
}
public Context getContext() {
return context;
}
public TextToSpeech.OnInitListener getOnInitListener() {
return listener;
}
public String getLastSpokenText() {
return lastSpokenText;
}
public void clearLastSpokenText() {
lastSpokenText = null;
}
public boolean isShutdown() {
return shutdown;
}
/** @return {@code true} if the TTS is stopped. */
public boolean isStopped() {
return stopped;
}
public int getQueueMode() {
return queueMode;
}
/**
* Returns {@link Locale} set using {@link TextToSpeech#setLanguage(Locale)} or null if not set.
*/
public Locale getCurrentLanguage() {
return language;
}
/**
* Returns last text {@link CharSequence} passed to {@link
* TextToSpeech#synthesizeToFile(CharSequence, Bundle, File, String)}.
*/
public String getLastSynthesizeToFileText() {
return lastSynthesizeToFileText;
}
/** Returns list of all the text spoken by {@link #speak}. */
public ImmutableList<String> getSpokenTextList() {
return ImmutableList.copyOf(spokenTextList);
}
/**
* Makes {@link Locale} an available language returned by {@link
* TextToSpeech#isLanguageAvailable(Locale)}. The value returned by {@link
* #isLanguageAvailable(Locale)} will vary depending on language, country, and variant.
*/
public static void addLanguageAvailability(Locale locale) {
languageAvailabilities.add(locale);
}
/** Makes {@link Voice} an available voice returned by {@link TextToSpeech#getVoices()}. */
public static void addVoice(Voice voice) {
voices.add(voice);
}
/** Returns {@link Voice} set using {@link TextToSpeech#setVoice(Voice)}, or null if not set. */
public Voice getCurrentVoice() {
return currentVoice;
}
/** Returns the most recently instantiated {@link TextToSpeech} or null if none exist. */
public static TextToSpeech getLastTextToSpeechInstance() {
return lastTextToSpeechInstance;
}
@Resetter
public static void reset() {
languageAvailabilities.clear();
voices.clear();
lastTextToSpeechInstance = null;
onInitStatus = TextToSpeech.SUCCESS;
}
}