| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.reecedunn.espeak.test; |
| 18 |
|
| 19 |
import android.annotation.SuppressLint; |
| 20 |
import android.content.Context; |
| 21 |
import android.os.Build; |
| 22 |
import android.speech.tts.TextToSpeech; |
| 23 |
import android.test.AndroidTestCase; |
| 24 |
|
| 25 |
import com.reecedunn.espeak.TtsService; |
| 26 |
import com.reecedunn.espeak.Voice; |
| 27 |
|
| 28 |
import java.util.Locale; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode; |
| 32 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 33 |
import static org.hamcrest.Matchers.*; |
| 34 |
|
| 35 |
public class TextToSpeechServiceTest extends AndroidTestCase |
| 36 |
{ |
| 37 |
public class TtsServiceTest extends TtsService |
| 38 |
{ |
| 39 |
public TtsServiceTest(Context context) |
| 40 |
{ |
| 41 |
attachBaseContext(context); |
| 42 |
} |
| 43 |
|
| 44 |
public String[] onGetLanguage() { |
| 45 |
return super.onGetLanguage(); |
| 46 |
} |
| 47 |
|
| 48 |
public int onIsLanguageAvailable(String language, String country, String variant) { |
| 49 |
return super.onIsLanguageAvailable(language, country, variant); |
| 50 |
} |
| 51 |
|
| 52 |
public int onLoadLanguage(String language, String country, String variant) { |
| 53 |
return super.onLoadLanguage(language, country, variant); |
| 54 |
} |
| 55 |
|
| 56 |
public Set<String> onGetFeaturesForLanguage(String language, String country, String variant) { |
| 57 |
return super.onGetFeaturesForLanguage(language, country, variant); |
| 58 |
} |
| 59 |
|
| 60 |
public Voice getActiveVoice() { |
| 61 |
return mMatchingVoice; |
| 62 |
} |
| 63 |
|
| 64 |
@SuppressLint("NewApi") |
| 65 |
private android.speech.tts.Voice getVoice(String name) { |
| 66 |
for (android.speech.tts.Voice voice : onGetVoices()) { |
| 67 |
if (voice.getName().equals(name)) { |
| 68 |
return voice; |
| 69 |
} |
| 70 |
} |
| 71 |
return null; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
private TtsServiceTest mService = null; |
| 76 |
|
| 77 |
@Override |
| 78 |
public void setUp() throws Exception |
| 79 |
{ |
| 80 |
mService = new TtsServiceTest(getContext()); |
| 81 |
mService.onCreate(); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void tearDown() |
| 86 |
{ |
| 87 |
if (mService != null) |
| 88 |
{ |
| 89 |
mService.onDestroy(); |
| 90 |
mService = null; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private void checkLanguage(String[] locale, String language, String country, String variant) { |
| 95 |
assertThat(locale.length, is(3)); |
| 96 |
assertThat(locale[0], is(language)); |
| 97 |
assertThat(locale[1], is(country)); |
| 98 |
assertThat(locale[2], is(variant)); |
| 99 |
} |
| 100 |
|
| 101 |
public void testOnLoadLanguage() { |
| 102 |
assertThat(mService.onLoadLanguage("eng", "", ""), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 103 |
checkLanguage(mService.onGetLanguage(), "eng", "GBR", ""); |
| 104 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 105 |
assertThat(mService.getActiveVoice().name, is("en-gb")); |
| 106 |
|
| 107 |
assertThat(mService.onLoadLanguage("eng", "USA", ""), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 108 |
checkLanguage(mService.onGetLanguage(), "eng", "USA", ""); |
| 109 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 110 |
assertThat(mService.getActiveVoice().name, is("en-us")); |
| 111 |
|
| 112 |
assertThat(mService.onLoadLanguage("eng", "GBR", "scotland"), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 113 |
checkLanguage(mService.onGetLanguage(), "eng", "GBR", "scotland"); |
| 114 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 115 |
assertThat(mService.getActiveVoice().name, is("en-gb-scotland")); |
| 116 |
|
| 117 |
assertThat(mService.onLoadLanguage("eng", "USA", "rp"), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE)); |
| 118 |
checkLanguage(mService.onGetLanguage(), "eng", "USA", ""); |
| 119 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 120 |
assertThat(mService.getActiveVoice().name, is("en-us")); |
| 121 |
|
| 122 |
assertThat(mService.onLoadLanguage("eng", "", "scotland"), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 123 |
checkLanguage(mService.onGetLanguage(), "eng", "GBR", ""); |
| 124 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 125 |
assertThat(mService.getActiveVoice().name, is("en-gb")); |
| 126 |
|
| 127 |
assertThat(mService.onLoadLanguage("eng", "FRA", "rp"), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 128 |
checkLanguage(mService.onGetLanguage(), "eng", "GBR", ""); |
| 129 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 130 |
assertThat(mService.getActiveVoice().name, is("en-gb")); |
| 131 |
|
| 132 |
assertThat(mService.onLoadLanguage("eng", "FRA", ""), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 133 |
checkLanguage(mService.onGetLanguage(), "eng", "GBR", ""); |
| 134 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 135 |
assertThat(mService.getActiveVoice().name, is("en-gb")); |
| 136 |
|
| 137 |
assertThat(mService.onLoadLanguage("ine", "", ""), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED)); |
| 138 |
checkLanguage(mService.onGetLanguage(), "eng", "GBR", ""); |
| 139 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 140 |
assertThat(mService.getActiveVoice().name, is("en-gb")); |
| 141 |
} |
| 142 |
|
| 143 |
public void testOnIsLanguageAvailable() { |
| 144 |
assertThat(mService.onLoadLanguage("vie", "VNM", "central"), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 145 |
|
| 146 |
assertThat(mService.onIsLanguageAvailable("eng", "", ""), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 147 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 148 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 149 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 150 |
|
| 151 |
assertThat(mService.onIsLanguageAvailable("eng", "USA", ""), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 152 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 153 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 154 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 155 |
|
| 156 |
assertThat(mService.onIsLanguageAvailable("eng", "GBR", "scotland"), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 157 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 158 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 159 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 160 |
|
| 161 |
assertThat(mService.onIsLanguageAvailable("eng", "USA", "rp"), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE)); |
| 162 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 163 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 164 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 165 |
|
| 166 |
assertThat(mService.onIsLanguageAvailable("eng", "", "scotland"), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 167 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 168 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 169 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 170 |
|
| 171 |
assertThat(mService.onIsLanguageAvailable("eng", "FRA", "rp"), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 172 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 173 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 174 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 175 |
|
| 176 |
assertThat(mService.onIsLanguageAvailable("eng", "FRA", ""), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 177 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 178 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 179 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 180 |
|
| 181 |
assertThat(mService.onIsLanguageAvailable("ine", "", ""), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED)); |
| 182 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 183 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 184 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 185 |
} |
| 186 |
|
| 187 |
public void testOnGetDefaultVoiceNameFor() { |
| 188 |
assertThat(mService.onLoadLanguage("vie", "VNM", "central"), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 189 |
|
| 190 |
assertThat(mService.onGetDefaultVoiceNameFor("eng", "", ""), is("en-gb")); |
| 191 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 192 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 193 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 194 |
|
| 195 |
assertThat(mService.onGetDefaultVoiceNameFor("eng", "USA", ""), is("en-us")); |
| 196 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 197 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 198 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 199 |
|
| 200 |
assertThat(mService.onGetDefaultVoiceNameFor("eng", "GBR", "scotland"), is("en-gb-scotland")); |
| 201 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 202 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 203 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 204 |
|
| 205 |
assertThat(mService.onGetDefaultVoiceNameFor("eng", "USA", "rp"), is("en-us")); |
| 206 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 207 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 208 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 209 |
|
| 210 |
assertThat(mService.onGetDefaultVoiceNameFor("eng", "", "scotland"), is("en-gb")); |
| 211 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 212 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 213 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 214 |
|
| 215 |
assertThat(mService.onGetDefaultVoiceNameFor("eng", "FRA", "rp"), is("en-gb")); |
| 216 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 217 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 218 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 219 |
|
| 220 |
assertThat(mService.onGetDefaultVoiceNameFor("eng", "FRA", ""), is("en-gb")); |
| 221 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 222 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 223 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 224 |
|
| 225 |
assertThat(mService.onGetDefaultVoiceNameFor("ine", "", ""), is(nullValue())); |
| 226 |
checkLanguage(mService.onGetLanguage(), "vie", "VNM", "central"); |
| 227 |
assertThat(mService.getActiveVoice(), is(notNullValue())); |
| 228 |
assertThat(mService.getActiveVoice().name, is("vi-vn-x-central")); |
| 229 |
} |
| 230 |
|
| 231 |
public void testLanguages() { |
| 232 |
for (VoiceData.Voice data : VoiceData.voices) |
| 233 |
{ |
| 234 |
assertThat(mService.onIsLanguageAvailable(data.javaLanguage, data.javaCountry, data.variant), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 235 |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { |
| 236 |
assertThat(mService.onLoadLanguage(data.javaLanguage, data.javaCountry, data.variant), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); |
| 237 |
checkLanguage(mService.onGetLanguage(), data.javaLanguage, data.javaCountry, data.variant); |
| 238 |
} else { |
| 239 |
assertThat(mService.onGetDefaultVoiceNameFor(data.javaLanguage, data.javaCountry, data.variant), is(data.name)); |
| 240 |
assertThat(mService.onLoadVoice(data.name), is(TextToSpeech.SUCCESS)); |
| 241 |
|
| 242 |
android.speech.tts.Voice voice = mService.getVoice(data.name); |
| 243 |
assertThat(voice, is(notNullValue())); |
| 244 |
|
| 245 |
Locale locale = voice.getLocale(); |
| 246 |
assertThat(locale, is(notNullValue())); |
| 247 |
assertThat(locale.getISO3Language(), is(data.javaLanguage)); |
| 248 |
assertThat(locale.getISO3Country(), is(data.javaCountry)); |
| 249 |
assertThat(locale.getVariant(), is(data.variant)); |
| 250 |
|
| 251 |
Set<String> features = mService.onGetFeaturesForLanguage(data.javaLanguage, data.javaCountry, data.variant); |
| 252 |
assertThat(features, is(notNullValue())); |
| 253 |
assertThat(features.size(), is(0)); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |