FileDocCategorySizeDatePackage
TextToSpeechTests.javaAPI DocAndroid 5.1 API10340Thu Mar 12 22:22:44 GMT 2015com.android.speech.tts

TextToSpeechTests

public 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
Constructors Summary
Methods Summary
public static voidawaitCountDown(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 voidblockingCallSpeak(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 voidblockingInitAndVerify(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$CountDownBehaviourdoCountDown(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 voidsetUp()


    
         
        IDelegate passThrough = LittleMock.mock(IDelegate.class);
        MockableTextToSpeechService.setMocker(passThrough);

        blockingInitAndVerify(MOCK_ENGINE, TextToSpeech.SUCCESS);
        assertEquals(MOCK_ENGINE, mTts.getCurrentEngine());
    
public voidtearDown()

        if (mTts != null) {
            mTts.shutdown();
        }
    
public voidtestDefaultLanguage_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 voidtestEngineInitialized()

        // 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 voidtestIsLanguageAvailable()

        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 voidtestSetLanguage_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 voidtestSetLanguage_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 voidtestSetLanguage_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());