| 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.os.Build; |
| 20 |
import android.speech.tts.TextToSpeech; |
| 21 |
import android.speech.tts.TextToSpeech.OnInitListener; |
| 22 |
import android.test.AndroidTestCase; |
| 23 |
import android.util.Log; |
| 24 |
|
| 25 |
import java.util.Locale; |
| 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 TextToSpeechTestCase extends AndroidTestCase |
| 32 |
{ |
| 33 |
private TextToSpeech mEngine = null; |
| 34 |
private boolean mInitialised = false; |
| 35 |
private int mStatus = TextToSpeech.ERROR; |
| 36 |
|
| 37 |
private OnInitListener mInitCallback = new OnInitListener() |
| 38 |
{ |
| 39 |
@Override |
| 40 |
public void onInit(int status) |
| 41 |
{ |
| 42 |
mStatus = status; |
| 43 |
mInitialised = true; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
@Override |
| 48 |
public void setUp() throws Exception |
| 49 |
{ |
| 50 |
try |
| 51 |
{ |
| 52 |
|
| 53 |
|
| 54 |
mEngine = new TextToSpeech(getContext(), mInitCallback); |
| 55 |
for (int count = 0; !mInitialised && count < (4 * 20); ++count) |
| 56 |
{ |
| 57 |
Thread.sleep(250); |
| 58 |
} |
| 59 |
|
| 60 |
assertThat(mInitialised, is(true)); |
| 61 |
assertThat(mStatus, is(TextToSpeech.SUCCESS)); |
| 62 |
assertThat(mEngine.getDefaultEngine(), is("com.reecedunn.espeak")); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
Locale en = new Locale("en"); |
| 67 |
int available = mEngine.isLanguageAvailable(en); |
| 68 |
for (int count = 0; available != TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE && count < (4 * 20); ++count) |
| 69 |
{ |
| 70 |
Thread.sleep(250); |
| 71 |
available = mEngine.isLanguageAvailable(en); |
| 72 |
Log.d("TextToSpeechTestCase", "setUp: available = " + available); |
| 73 |
} |
| 74 |
|
| 75 |
assertThat(available, isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); |
| 76 |
} |
| 77 |
catch (Exception e) |
| 78 |
{ |
| 79 |
tearDown(); |
| 80 |
throw e; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void tearDown() |
| 86 |
{ |
| 87 |
if (mEngine != null) |
| 88 |
{ |
| 89 |
mEngine.shutdown(); |
| 90 |
mEngine = null; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public TextToSpeech getEngine() |
| 95 |
{ |
| 96 |
return mEngine; |
| 97 |
} |
| 98 |
|
| 99 |
@SuppressWarnings("deprecation") |
| 100 |
public static Locale getLanguage(TextToSpeech engine) { |
| 101 |
if (engine != null) { |
| 102 |
return engine.getLanguage(); |
| 103 |
} |
| 104 |
return null; |
| 105 |
} |
| 106 |
} |