/
TextToSpeechTest.java
242 lines (211 loc) · 10.3 KB
/
TextToSpeechTest.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
/*
* Copyright (C) 2012-2015 Reece H. Dunn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reecedunn.espeak.test;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import android.annotation.SuppressLint;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class TextToSpeechTest extends TextToSpeechTestCase
{
private Set<Object> mVoices = null;
private Set<String> mAdded = new HashSet<String>();
private Set<String> mRemoved = new HashSet<String>();
@SuppressLint("NewApi")
public Set<Object> getVoices()
{
if (mVoices == null)
{
Set<android.speech.tts.Voice> voiceData = getEngine().getVoices();
assertThat(voiceData, is(notNullValue()));
mVoices = new HashSet<Object>();
for (android.speech.tts.Voice voice : voiceData)
{
mVoices.add(voice);
}
Set<String> voices = new HashSet<String>();
for (Object data : mVoices)
{
voices.add(((android.speech.tts.Voice)data).getName());
}
Set<String> expected = new HashSet<String>();
for (VoiceData.Voice data : VoiceData.voices)
{
expected.add(data.name);
}
for (String voice : voices)
{
if (!expected.contains(voice))
{
mAdded.add(voice);
}
}
for (String voice : expected)
{
if (!voices.contains(voice))
{
mRemoved.add(voice);
}
}
}
return mVoices;
}
public VoiceData.Voice getVoiceData(String name)
{
for (VoiceData.Voice voice : VoiceData.voices)
{
if (voice.name.equals(name))
{
return voice;
}
}
return null;
}
public void testAddedVoices()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
return;
getVoices(); // Ensure that the voice data has been populated.
assertThat(mAdded.toString(), is("[]"));
}
public void testRemovedVoices()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
return;
getVoices(); // Ensure that the voice data has been populated.
assertThat(mRemoved.toString(), is("[]"));
}
@SuppressLint("NewApi")
public void testVoices()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
return;
getVoices(); // Ensure that the voice data has been populated.
for (Object item : mVoices)
{
android.speech.tts.Voice voice = (android.speech.tts.Voice)item;
VoiceData.Voice data = getVoiceData(voice.getName());
assertThat(data, is(notNullValue()));
assertThat(voice.getName(), is(data.name));
assertThat(voice.getLocale().getLanguage(), is(data.ianaLanguage));
assertThat(voice.getLocale().getCountry(), is(data.ianaCountry));
assertThat(voice.getLocale().getVariant(), is(data.variant));
assertThat(voice.getFeatures(), is(notNullValue()));
assertThat(voice.getFeatures().size(), is(0));
assertThat(voice.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW));
assertThat(voice.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL));
getEngine().setVoice(voice);
assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage));
assertThat(getLanguage(getEngine()).getCountry(), is(data.javaCountry));
assertThat(getLanguage(getEngine()).getVariant(), is(data.variant));
android.speech.tts.Voice voice2 = getEngine().getVoice();
assertThat(voice2, is(notNullValue()));
assertThat(voice2.getName(), is(data.name));
assertThat(voice2.getLocale().getLanguage(), is(data.ianaLanguage));
assertThat(voice2.getLocale().getCountry(), is(data.ianaCountry));
assertThat(voice2.getLocale().getVariant(), is(data.variant));
assertThat(voice2.getFeatures(), is(notNullValue()));
assertThat(voice2.getFeatures().size(), is(0));
assertThat(voice2.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW));
assertThat(voice2.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL));
}
}
public void testUnsupportedLanguage()
{
assertThat(getEngine(), is(notNullValue()));
Locale initialLocale = getLanguage(getEngine());
assertThat(getEngine().isLanguageAvailable(new Locale("cel")), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
Locale language = getLanguage(getEngine());
assertThat(getLanguage(getEngine()).getLanguage(), is(initialLocale.getLanguage()));
assertThat(getLanguage(getEngine()).getCountry(), is(initialLocale.getCountry()));
assertThat(getLanguage(getEngine()).getVariant(), is(initialLocale.getVariant()));
}
public void checkLanguage(VoiceData.Voice data, Locale locale, int status, String language, String country, String variant)
{
String langTag = locale.toString().replace('_', '-');
String context = "";
try
{
context = "isLanguageAvailable";
assertThat(getEngine().isLanguageAvailable(locale), isTtsLangCode(status));
context = "setLanguage";
assertThat(getEngine().setLanguage(locale), isTtsLangCode(status));
context = "getLanguage";
assertThat(getLanguage(getEngine()).getLanguage(), is(language));
assertThat(getLanguage(getEngine()).getCountry(), is(country));
assertThat(getLanguage(getEngine()).getVariant(), is(variant));
}
catch (AssertionError e)
{
throw new VoiceData.Exception(data, context + "|" + langTag, e);
}
}
public void testLanguages()
{
assertThat(getEngine(), is(notNullValue()));
for (VoiceData.Voice data : VoiceData.voices)
{
final Locale iana1 = new Locale(data.ianaLanguage, data.ianaCountry, data.variant);
final Locale iana2 = new Locale(data.ianaLanguage, data.ianaCountry, "test");
final Locale iana3 = new Locale(data.ianaLanguage, "VU", data.variant);
final Locale java1 = new Locale(data.javaLanguage, data.javaCountry, data.variant);
final Locale java2 = new Locale(data.javaLanguage, data.javaCountry, "test");
final Locale java3 = new Locale(data.javaLanguage, "VUT", data.variant);
checkLanguage(data, iana1, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, data.javaLanguage, data.javaCountry, data.variant);
checkLanguage(data, java1, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, data.javaLanguage, data.javaCountry, data.variant);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
// Android Lollipop sets variant to "" when TextToSpeech.LANG_COUNTRY_AVAILABLE is returned.
checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
} else {
if (data.ianaLanguage.equals("vi") && data.ianaCountry.equals("VN")) {
checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "hue");
checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "hue");
} else if (data.ianaLanguage.equals("hy") && data.ianaCountry.equals("AM")) {
checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "arevmda");
checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "arevmda");
} else {
checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
}
}
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
// Android Lollipop sets country and variant to "" when TextToSpeech.LANG_AVAILABLE is returned.
checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
} else {
switch (data.ianaLanguage) {
case "fr":
checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "FRA", "");
checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "FRA", "");
break;
case "pt":
checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "PRT", "");
checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "PRT", "");
break;
default:
checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
break;
}
}
}
}
}