Debian Codegraph @

android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2012-2015 Reece H. Dunn
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.reecedunn.espeak.test;
18
19 import java.util.HashSet;
20 import java.util.Locale;
21 import java.util.Set;
22
23 import android.annotation.SuppressLint;
24 import android.os.Build;
25 import android.speech.tts.TextToSpeech;
26
27 import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.Matchers.*;
30
31 public class TextToSpeechTest extends TextToSpeechTestCase
32 {
33 private Set<Object> mVoices = null;
34 private Set<String> mAdded = new HashSet<String>();
35 private Set<String> mRemoved = new HashSet<String>();
36
37 @SuppressLint("NewApi")
38 public Set<Object> getVoices()
39 {
40 if (mVoices == null)
41 {
42 Set<android.speech.tts.Voice> voiceData = getEngine().getVoices();
43 assertThat(voiceData, is(notNullValue()));
44
45 mVoices = new HashSet<Object>();
46 for (android.speech.tts.Voice voice : voiceData)
47 {
48 mVoices.add(voice);
49 }
50
51 Set<String> voices = new HashSet<String>();
52 for (Object data : mVoices)
53 {
54 voices.add(((android.speech.tts.Voice)data).getName());
55 }
56
57 Set<String> expected = new HashSet<String>();
58 for (VoiceData.Voice data : VoiceData.voices)
59 {
60 expected.add(data.name);
61 }
62
63 for (String voice : voices)
64 {
65 if (!expected.contains(voice))
66 {
67 mAdded.add(voice);
68 }
69 }
70
71 for (String voice : expected)
72 {
73 if (!voices.contains(voice))
74 {
75 mRemoved.add(voice);
76 }
77 }
78 }
79 return mVoices;
80 }
81
82 public VoiceData.Voice getVoiceData(String name)
83 {
84 for (VoiceData.Voice voice : VoiceData.voices)
85 {
86 if (voice.name.equals(name))
87 {
88 return voice;
89 }
90 }
91 return null;
92 }
93
94 public void testAddedVoices()
95 {
96 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
97 return;
98
99 getVoices(); // Ensure that the voice data has been populated.
100 assertThat(mAdded.toString(), is("[]"));
101 }
102
103 public void testRemovedVoices()
104 {
105 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
106 return;
107
108 getVoices(); // Ensure that the voice data has been populated.
109 assertThat(mRemoved.toString(), is("[]"));
110 }
111
112 @SuppressLint("NewApi")
113 public void testVoices()
114 {
115 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
116 return;
117
118 getVoices(); // Ensure that the voice data has been populated.
119 for (Object item : mVoices)
120 {
121 android.speech.tts.Voice voice = (android.speech.tts.Voice)item;
122 VoiceData.Voice data = getVoiceData(voice.getName());
123
124 assertThat(data, is(notNullValue()));
125 assertThat(voice.getName(), is(data.name));
126 assertThat(voice.getLocale().getLanguage(), is(data.ianaLanguage));
127 assertThat(voice.getLocale().getCountry(), is(data.ianaCountry));
128 assertThat(voice.getLocale().getVariant(), is(data.variant));
129 assertThat(voice.getFeatures(), is(notNullValue()));
130 assertThat(voice.getFeatures().size(), is(0));
131 assertThat(voice.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW));
132 assertThat(voice.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL));
133
134 getEngine().setVoice(voice);
135 assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage));
136 assertThat(getLanguage(getEngine()).getCountry(), is(data.javaCountry));
137 assertThat(getLanguage(getEngine()).getVariant(), is(data.variant));
138
139 android.speech.tts.Voice voice2 = getEngine().getVoice();
140 assertThat(voice2, is(notNullValue()));
141 assertThat(voice2.getName(), is(data.name));
142 assertThat(voice2.getLocale().getLanguage(), is(data.ianaLanguage));
143 assertThat(voice2.getLocale().getCountry(), is(data.ianaCountry));
144 assertThat(voice2.getLocale().getVariant(), is(data.variant));
145 assertThat(voice2.getFeatures(), is(notNullValue()));
146 assertThat(voice2.getFeatures().size(), is(0));
147 assertThat(voice2.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW));
148 assertThat(voice2.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL));
149 }
150 }
151
152 public void testUnsupportedLanguage()
153 {
154 assertThat(getEngine(), is(notNullValue()));
155
156 Locale initialLocale = getLanguage(getEngine());
157 assertThat(getEngine().isLanguageAvailable(new Locale("cel")), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
158 Locale language = getLanguage(getEngine());
159
160 assertThat(getLanguage(getEngine()).getLanguage(), is(initialLocale.getLanguage()));
161 assertThat(getLanguage(getEngine()).getCountry(), is(initialLocale.getCountry()));
162 assertThat(getLanguage(getEngine()).getVariant(), is(initialLocale.getVariant()));
163 }
164
165 public void checkLanguage(VoiceData.Voice data, Locale locale, int status, String language, String country, String variant)
166 {
167 String langTag = locale.toString().replace('_', '-');
168 String context = "";
169 try
170 {
171 context = "isLanguageAvailable";
172 assertThat(getEngine().isLanguageAvailable(locale), isTtsLangCode(status));
173 context = "setLanguage";
174 assertThat(getEngine().setLanguage(locale), isTtsLangCode(status));
175 context = "getLanguage";
176 assertThat(getLanguage(getEngine()).getLanguage(), is(language));
177 assertThat(getLanguage(getEngine()).getCountry(), is(country));
178 assertThat(getLanguage(getEngine()).getVariant(), is(variant));
179 }
180 catch (AssertionError e)
181 {
182 throw new VoiceData.Exception(data, context + "|" + langTag, e);
183 }
184 }
185
186 public void testLanguages()
187 {
188 assertThat(getEngine(), is(notNullValue()));
189
190 for (VoiceData.Voice data : VoiceData.voices)
191 {
192 final Locale iana1 = new Locale(data.ianaLanguage, data.ianaCountry, data.variant);
193 final Locale iana2 = new Locale(data.ianaLanguage, data.ianaCountry, "test");
194 final Locale iana3 = new Locale(data.ianaLanguage, "VU", data.variant);
195
196 final Locale java1 = new Locale(data.javaLanguage, data.javaCountry, data.variant);
197 final Locale java2 = new Locale(data.javaLanguage, data.javaCountry, "test");
198 final Locale java3 = new Locale(data.javaLanguage, "VUT", data.variant);
199
200 checkLanguage(data, iana1, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, data.javaLanguage, data.javaCountry, data.variant);
201 checkLanguage(data, java1, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, data.javaLanguage, data.javaCountry, data.variant);
202
203 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
204 // Android Lollipop sets variant to "" when TextToSpeech.LANG_COUNTRY_AVAILABLE is returned.
205 checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
206 checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
207 } else {
208 if (data.ianaLanguage.equals("vi") && data.ianaCountry.equals("VN")) {
209 checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "hue");
210 checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "hue");
211 } else if (data.ianaLanguage.equals("hy") && data.ianaCountry.equals("AM")) {
212 checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "arevmda");
213 checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "arevmda");
214 } else {
215 checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
216 checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
217 }
218 }
219
220 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
221 // Android Lollipop sets country and variant to "" when TextToSpeech.LANG_AVAILABLE is returned.
222 checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
223 checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
224 } else {
225 switch (data.ianaLanguage) {
226 case "fr":
227 checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "FRA", "");
228 checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "FRA", "");
229 break;
230 case "pt":
231 checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "PRT", "");
232 checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "PRT", "");
233 break;
234 default:
235 checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
236 checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
237 break;
238 }
239 }
240 }
241 }
242 }