TextToSpeechTestspublic class TextToSpeechTests extends android.test.InstrumentationTestCase
Fields Summary |
---|
private static final String | MOCK_ENGINE | private static final String | MOCK_PACKAGE | private android.speech.tts.TextToSpeech | mTts |
Methods Summary |
---|
public static void | awaitCountDown(java.util.concurrent.CountDownLatch latch, long timeout, java.util.concurrent.TimeUnit unit)
Assert.assertTrue("Waited too long for method call", latch.await(timeout, unit));
| private void | blockingCallSpeak(java.lang.String speech, com.android.speech.tts.MockableTextToSpeechService.IDelegate mock)
final CountDownLatch latch = new CountDownLatch(1);
doCountDown(latch).when(mock).onSynthesizeText(LittleMock.<SynthesisRequest>anyObject(),
LittleMock.<SynthesisCallback>anyObject());
mTts.speak(speech, TextToSpeech.QUEUE_ADD, null);
awaitCountDown(latch, 5, TimeUnit.SECONDS);
| private void | blockingInitAndVerify(java.lang.String engine, int errorCode)
TextToSpeech.OnInitListener listener = LittleMock.mock(
TextToSpeech.OnInitListener.class);
final CountDownLatch latch = new CountDownLatch(1);
doCountDown(latch).when(listener).onInit(errorCode);
mTts = new TextToSpeech(getInstrumentation().getTargetContext(),
listener, engine, MOCK_PACKAGE, false /* use fallback package */);
awaitCountDown(latch, 5, TimeUnit.SECONDS);
| public static com.android.speech.tts.TextToSpeechTests$CountDownBehaviour | doCountDown(java.util.concurrent.CountDownLatch latch)
return new CountDownBehaviour() {
@Override
public <T> T when(T mock) {
return LittleMock.doAnswer(new Callable<Void>() {
@Override
public Void call() throws Exception {
latch.countDown();
return null;
}
}).when(mock);
}
@Override
public Behaviour andReturn(final Object result) {
return new Behaviour() {
@Override
public <T> T when(T mock) {
return LittleMock.doAnswer(new Callable<Object>() {
@Override
public Object call() throws Exception {
latch.countDown();
return result;
}
}).when(mock);
}
};
}
};
| public void | setUp()
IDelegate passThrough = LittleMock.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(passThrough);
blockingInitAndVerify(MOCK_ENGINE, TextToSpeech.SUCCESS);
assertEquals(MOCK_ENGINE, mTts.getCurrentEngine());
| public void | tearDown()
if (mTts != null) {
mTts.shutdown();
}
| public void | testDefaultLanguage_setsVoiceName()
IDelegate delegate = LittleMock.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate);
// ---------------------------------------------------------
// Test that default language also sets the default voice
// name
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
LittleMock.anyString(), LittleMock.anyString(), LittleMock.anyString());
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onLoadLanguage(
LittleMock.anyString(), LittleMock.anyString(), LittleMock.anyString());
blockingCallSpeak("foo bar", delegate);
ArgumentCaptor<SynthesisRequest> req = LittleMock.createCaptor();
LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req.capture(),
LittleMock.<SynthesisCallback>anyObject());
Locale defaultLocale = Locale.getDefault();
assertEquals(defaultLocale.getISO3Language(), req.getValue().getLanguage());
assertEquals(defaultLocale.getISO3Country(), req.getValue().getCountry());
assertEquals("", req.getValue().getVariant());
assertEquals(defaultLocale.toLanguageTag(), req.getValue().getVoiceName());
| public void | testEngineInitialized()
// Fail on an engine that doesn't exist.
blockingInitAndVerify("__DOES_NOT_EXIST__", TextToSpeech.ERROR);
// Also, the "current engine" must be null
assertNull(mTts.getCurrentEngine());
| public void | testIsLanguageAvailable()
IDelegate delegate = LittleMock.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate);
// Test1: Simple end to end test.
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(
delegate).onIsLanguageAvailable("eng", "USA", "");
assertEquals(TextToSpeech.LANG_COUNTRY_AVAILABLE, mTts.isLanguageAvailable(Locale.US));
LittleMock.verify(delegate, LittleMock.times(1)).onIsLanguageAvailable(
"eng", "USA", "");
| public void | testSetLanguage_availableLanguage()
IDelegate delegate = LittleMock.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate);
// ---------------------------------------------------------
// Test 2 : Tests that when the language is successfully set
// like above (returns LANG_COUNTRY_AVAILABLE). That the
// request language changes from that point on.
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
"eng", "USA", "variant");
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onIsLanguageAvailable(
"eng", "USA", "");
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_AVAILABLE).when(delegate).onLoadLanguage(
"eng", "USA", "");
mTts.setLanguage(new Locale("eng", "USA", "variant"));
blockingCallSpeak("foo bar", delegate);
ArgumentCaptor<SynthesisRequest> req = LittleMock.createCaptor();
LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req.capture(),
LittleMock.<SynthesisCallback>anyObject());
assertEquals("eng", req.getValue().getLanguage());
assertEquals("USA", req.getValue().getCountry());
assertEquals("", req.getValue().getVariant());
assertEquals("en-US", req.getValue().getVoiceName());
| public void | testSetLanguage_delegation()
IDelegate delegate = LittleMock.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate);
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE).when(delegate).onIsLanguageAvailable(
"eng", "USA", "variant");
LittleMock.doReturn(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE).when(delegate).onLoadLanguage(
"eng", "USA", "variant");
// Test 1 :Tests that calls to onLoadLanguage( ) are delegated through to the
// service without any caching or intermediate steps.
assertEquals(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, mTts.setLanguage(new Locale("eng", "USA", "variant")));
LittleMock.verify(delegate, LittleMock.anyTimes()).onIsLanguageAvailable(
"eng", "USA", "variant");
LittleMock.verify(delegate, LittleMock.times(1)).onLoadLanguage(
"eng", "USA", "variant");
| public void | testSetLanguage_unavailableLanguage()
IDelegate delegate = LittleMock.mock(IDelegate.class);
MockableTextToSpeechService.setMocker(delegate);
// ---------------------------------------------------------
// TEST 3 : Tests that the language that is set does not change when the
// engine reports it could not load the specified language.
LittleMock.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when(
delegate).onIsLanguageAvailable("fra", "FRA", "");
LittleMock.doReturn(TextToSpeech.LANG_NOT_SUPPORTED).when(
delegate).onLoadLanguage("fra", "FRA", "");
mTts.setLanguage(Locale.FRANCE);
blockingCallSpeak("le fou barre", delegate);
ArgumentCaptor<SynthesisRequest> req2 = LittleMock.createCaptor();
LittleMock.verify(delegate, LittleMock.times(1)).onSynthesizeText(req2.capture(),
LittleMock.<SynthesisCallback>anyObject());
// The params are basically unchanged.
assertEquals("eng", req2.getValue().getLanguage());
assertEquals("USA", req2.getValue().getCountry());
assertEquals("", req2.getValue().getVariant());
assertEquals("en-US", req2.getValue().getVoiceName());
|
|