diff options
| author | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2014-05-07 16:40:19 +0100 |
|---|---|---|
| committer | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2014-05-09 12:17:25 +0100 |
| commit | 8e65b64e60cfcf07acafecbde5a750bdcfdc2df1 (patch) | |
| tree | fc2c1718204968216d3b7b99c5660730a5a189d2 /tests | |
| parent | a78b65865c92397ecdca6f6cf67058e9ed081ab7 (diff) | |
| download | android_frameworks_support-8e65b64e60cfcf07acafecbde5a750bdcfdc2df1.tar.gz android_frameworks_support-8e65b64e60cfcf07acafecbde5a750bdcfdc2df1.tar.bz2 android_frameworks_support-8e65b64e60cfcf07acafecbde5a750bdcfdc2df1.zip | |
Merge TTS support library into android-support-v4
v4 is the lowest API that exposes TextToSpeech, so it makes sense
to drop the TTS support library into the existing android-support-v4.
I've added a new helper sub-library named android-support-v4-donut (API level 4).
It contains all the public classes and interfaces needed by the new TTS API
support. Thanks to that library, all later helper sub-libraries can directly
use those classes and interfaces - it makes the implementation significantly
simpler.
Change-Id: Idd03a8ad85e256007874e611c464f378e7a29c15
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Android.mk | 2 | ||||
| -rw-r--r-- | tests/java/android/support/v4/speech/tts/TTSImplementationV1Test.java | 441 | ||||
| -rw-r--r-- | tests/java/android/support/v4/speech/tts/TTSImplementationV2Test.java | 213 |
3 files changed, 655 insertions, 1 deletions
diff --git a/tests/Android.mk b/tests/Android.mk index b14ca93253..7bd3b00433 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -5,7 +5,7 @@ LOCAL_MODULE_TAGS := tests LOCAL_SRC_FILES := $(call all-java-files-under, java) -LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 +LOCAL_STATIC_JAVA_LIBRARIES := mockito-target android-support-v4 LOCAL_JAVA_LIBRARIES := android.test.runner LOCAL_PACKAGE_NAME := AndroidSupportTests diff --git a/tests/java/android/support/v4/speech/tts/TTSImplementationV1Test.java b/tests/java/android/support/v4/speech/tts/TTSImplementationV1Test.java new file mode 100644 index 0000000000..f00604241b --- /dev/null +++ b/tests/java/android/support/v4/speech/tts/TTSImplementationV1Test.java @@ -0,0 +1,441 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package android.support.v4.speech.tts; + +import android.speech.tts.TextToSpeech; +import android.speech.tts.UtteranceProgressListener; +import android.support.v4.speech.tts.TextToSpeechClient.ConnectionCallbacks; +import android.support.v4.speech.tts.TextToSpeechClient.EngineStatus; +import android.support.v4.speech.tts.TextToSpeechClient.RequestCallbacks; +import android.support.v4.speech.tts.TextToSpeechClient.UtteranceId; +import android.test.InstrumentationTestCase; + +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Locale; + +/** + * Tests for {@link TTSImplementationV1} class. + */ +public class TTSImplementationV1Test extends InstrumentationTestCase { + @Mock TextToSpeech mOldClientMock; + @Mock RequestCallbacks mRequestCallbacks; + @Mock ConnectionCallbacks mConnectionCallbacks; + + TTSImplementationV1 mImplementationV1; + TextToSpeechClient mClient; + + final String mUtterance = "text"; + final UtteranceId mUtteranceId = new UtteranceId(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext(). + getCacheDir().getPath()); + MockitoAnnotations.initMocks(this); + + mImplementationV1 = new TTSImplementationV1(mOldClientMock); + mClient = new TextToSpeechClient(mImplementationV1, + getInstrumentation().getContext(), "test", false, mRequestCallbacks, + mConnectionCallbacks); + } + + /** Test basic setup success */ + public void testSetupSuccess() { + mClient.connect(); + mImplementationV1.mOnInitListener.onInit(TextToSpeech.SUCCESS); + + Mockito.verify(mConnectionCallbacks).onConnectionSuccess(); + } + + /** Test disconnect */ + public void testDisconnect() { + assertTrue(!mClient.isConnected()); + mClient.connect(); + mImplementationV1.mOnInitListener.onInit(TextToSpeech.SUCCESS); + + assertTrue(mClient.isConnected()); + + mClient.disconnect(); + Mockito.verify(mOldClientMock).shutdown(); + + assertTrue(!mClient.isConnected()); + } + + /** Test basic setup failure */ + public void testSetupFailure() { + mClient.connect(); + mImplementationV1.mOnInitListener.onInit(TextToSpeech.ERROR); + + Mockito.verify(mConnectionCallbacks).onConnectionFailure(); + } + + /** Mock V1 client to support a set of locales + * + * - en-US embedded and network + * - en-GB network + * - en-IN embedded + * - en embedded + * - de embedded + */ + void mockFewVoices() { + // Mock the support for set of locales + Mockito.when(mOldClientMock.isLanguageAvailable(Mockito.any(Locale.class))).thenReturn( + TextToSpeech.LANG_NOT_SUPPORTED); + Mockito.when(mOldClientMock.isLanguageAvailable(Locale.US)).thenReturn( + TextToSpeech.LANG_COUNTRY_AVAILABLE); + Mockito.when(mOldClientMock.isLanguageAvailable(Locale.UK)).thenReturn( + TextToSpeech.LANG_COUNTRY_AVAILABLE); + Mockito.when(mOldClientMock.isLanguageAvailable(new Locale("en", "IN"))).thenReturn( + TextToSpeech.LANG_COUNTRY_AVAILABLE); + Mockito.when(mOldClientMock.isLanguageAvailable(Locale.ENGLISH)).thenReturn( + TextToSpeech.LANG_AVAILABLE); + Mockito.when(mOldClientMock.isLanguageAvailable(Locale.GERMANY)).thenReturn( + TextToSpeech.LANG_AVAILABLE); // "de-DE" not supported, only "de". + Mockito.when(mOldClientMock.isLanguageAvailable(Locale.GERMAN)).thenReturn( + TextToSpeech.LANG_AVAILABLE); + + HashSet<String> featuresEmbedded = new HashSet<String>(); + featuresEmbedded.add(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); + HashSet<String> featuresNetwork = new HashSet<String>(); + featuresNetwork.add(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS); + HashSet<String> featuresBoth = new HashSet<String>(); + featuresBoth.add(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS); + featuresBoth.add(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); + + Mockito.when(mOldClientMock.getFeatures(Locale.US)).thenReturn(featuresBoth); + Mockito.when(mOldClientMock.getFeatures(Locale.UK)).thenReturn(featuresNetwork); + Mockito.when(mOldClientMock.getFeatures(new Locale("en", "IN"))).thenReturn( + featuresEmbedded); + Mockito.when(mOldClientMock.getFeatures(Locale.ENGLISH)).thenReturn(featuresEmbedded); + Mockito.when(mOldClientMock.getFeatures(Locale.GERMAN)).thenReturn(null); + Mockito.when(mOldClientMock.getFeatures(Locale.GERMANY)).thenReturn(null); + } + + /** Connect V2 client and caputure {@link UtteranceProgressListener} instance */ + UtteranceProgressListener connectSuccessfuly() { + // Capture UtteranceProgressListener + final ArgumentCaptor<UtteranceProgressListener> listenerCaptor = + ArgumentCaptor.forClass(UtteranceProgressListener.class); + Mockito.when(mOldClientMock.setOnUtteranceProgressListener(listenerCaptor.capture())) + .thenReturn(TextToSpeech.SUCCESS); + + // Connect and get status + mClient.connect(); + mImplementationV1.mOnInitListener.onInit(TextToSpeech.SUCCESS); + + return listenerCaptor.getValue(); + } + + /** Test generation of a {@link EngineStatus} */ + public void testGetEngineData() { + mockFewVoices(); + + // Connect and get status + mClient.connect(); + mImplementationV1.mOnInitListener.onInit(TextToSpeech.SUCCESS); + + EngineStatus engineStatus = mClient.getEngineStatus(); + assertNotNull(engineStatus); + + // Create hash-map with expected voices + final HashMap<Locale, ArrayList<String>> expectedVoices = + new HashMap<Locale, ArrayList<String>>(); + + ArrayList<String> l = new ArrayList<String>(); + l.add(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS); + l.add(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); + expectedVoices.put(Locale.US, l); + + l = new ArrayList<String>(); + l.add(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS); + expectedVoices.put(Locale.UK, l); + + l = new ArrayList<String>(); + l.add(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); + expectedVoices.put(new Locale("en", "IN"), l); + + l = new ArrayList<String>(); + l.add(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); + expectedVoices.put(Locale.ENGLISH, l); + + l = new ArrayList<String>(); + l.add(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS); + expectedVoices.put(Locale.GERMAN, l); + + assertEquals(6, engineStatus.getVoices().size()); + for (VoiceInfo info : engineStatus.getVoices()) { + ArrayList<String> features = expectedVoices.get(info.getLocale()); + assertNotNull(features); + if (info.getRequiresNetworkConnection()) { + assertTrue(features.remove(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS)); + } else { + assertTrue(info.getName(), + features.remove(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS)); + } + if (features.isEmpty()) { + assertTrue(expectedVoices.remove(info.getLocale()) != null); + } + } + + // Make sure all expected voices were found + assertTrue(expectedVoices.isEmpty()); + } + + /** Test successful {@link TextToSpeechClient#queueSpeak} call */ + public void testQueueSpeakSuccess() { + mockFewVoices(); + UtteranceProgressListener listener = connectSuccessfuly(); + assertNotNull(listener); + + // Mock the speak call + Mockito.when(mOldClientMock.speak(Mockito.anyString(), Mockito.anyInt(), + (HashMap<String, String>) Mockito.any())) + .thenReturn(TextToSpeech.SUCCESS); + + final EngineStatus engineStatus = mClient.getEngineStatus(); + assertNotNull(engineStatus); + final RequestConfig rc = RequestConfigHelper.highestQuality(engineStatus, true, + new RequestConfigHelper.ExactLocaleMatcher(Locale.US)); + + // Call speak + mClient.queueSpeak(mUtterance, mUtteranceId, rc, null); + + // Call back to the listener + listener.onStart(mUtteranceId.toUniqueString()); + listener.onDone(mUtteranceId.toUniqueString()); + + // Verify V1 client calls + Mockito.verify(mOldClientMock).setLanguage(Locale.US); + Mockito.verify(mOldClientMock).setPitch(1.0f); + // We never set speed, so make sure we use value from settings + Mockito.verify(mOldClientMock).setSpeechRate(mImplementationV1.getDefaultSpeechRate()); + + ArgumentCaptor<HashMap> paramsCaptor = + ArgumentCaptor.forClass(HashMap.class); + Mockito.verify(mOldClientMock).speak(Mockito.eq(mUtterance), + Mockito.eq(TextToSpeech.QUEUE_ADD), paramsCaptor.capture()); + + HashMap<String, String> params = paramsCaptor.getValue(); + assertEquals(mUtteranceId.toUniqueString(), params.get( + TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID)); + assertEquals("true", params.get( + TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS)); + + + // Verify callback calls + Mockito.verify(mRequestCallbacks).onSynthesisStart(mUtteranceId); + Mockito.verify(mRequestCallbacks).onSynthesisSuccess(mUtteranceId); + } + + /** Test successful {@link TextToSpeechClient#queueSpeak} call, with synthesis failure */ + public void testQueueSpeakSynthesisFailure() { + mockFewVoices(); + UtteranceProgressListener listener = connectSuccessfuly(); + assertNotNull(listener); + + // Mock the speak call + Mockito.when(mOldClientMock.speak(Mockito.anyString(), Mockito.anyInt(), + (HashMap<String, String>) Mockito.any())) + .thenReturn(TextToSpeech.SUCCESS); + + final EngineStatus engineStatus = mClient.getEngineStatus(); + assertNotNull(engineStatus); + final RequestConfig rcBase = RequestConfigHelper.highestQuality(engineStatus, true, + new RequestConfigHelper.ExactLocaleMatcher(Locale.US)); + + // Set speed and pitch to 0.1f + final RequestConfig rc = RequestConfig.Builder.newBuilder(rcBase) + .setVoiceParam(TextToSpeechClient.Params.SPEECH_SPEED, 0.1f) + .setVoiceParam(TextToSpeechClient.Params.SPEECH_PITCH, 0.1f) + .build(); + + // Call speak + mClient.queueSpeak(mUtterance, mUtteranceId, rc, null); + + // Call back to the listener + listener.onStart(mUtteranceId.toUniqueString()); + listener.onError(mUtteranceId.toUniqueString()); + + // Verify V1 client calls + Mockito.verify(mOldClientMock).setLanguage(Locale.US); + Mockito.verify(mOldClientMock).setPitch(0.1f); + Mockito.verify(mOldClientMock).setSpeechRate(0.1f); + Mockito.verify(mOldClientMock).speak(Mockito.eq(mUtterance), + Mockito.eq(TextToSpeech.QUEUE_ADD), + (HashMap<String, String>) Mockito.any()); + + // Verify callback calls + Mockito.verify(mRequestCallbacks).onSynthesisStart(mUtteranceId); + Mockito.verify(mRequestCallbacks).onSynthesisFailure(mUtteranceId, + TextToSpeechClient.Status.ERROR_UNKNOWN); + } + + /** Test failing {@link TextToSpeechClient#queueSpeak} call */ + public void testQueueSpeakFailure() { + mockFewVoices(); + connectSuccessfuly(); + + // Mock the speak call with error + Mockito.when(mOldClientMock.speak(Mockito.anyString(), Mockito.anyInt(), + (HashMap<String, String>) Mockito.any())) + .thenReturn(TextToSpeech.ERROR); + + final EngineStatus engineStatus = mClient.getEngineStatus(); + assertNotNull(engineStatus); + final RequestConfig rc = RequestConfigHelper.highestQuality(engineStatus, true, + new RequestConfigHelper.ExactLocaleMatcher(Locale.US)); + + // Call speak + mClient.queueSpeak(mUtterance, mUtteranceId, rc, null); + + // Verify V1 client calls + Mockito.verify(mOldClientMock).speak(Mockito.eq(mUtterance), + Mockito.eq(TextToSpeech.QUEUE_ADD), + (HashMap<String, String>) Mockito.any()); + + // Verify callback calls + Mockito.verify(mRequestCallbacks).onSynthesisFailure(mUtteranceId, + TextToSpeechClient.Status.ERROR_UNKNOWN); + } + + /** Test successful {@link TextToSpeechClient#queueSynthesizeToFile} call */ + public void testQueueSynthesizeToFileSucces() { + mockFewVoices(); + UtteranceProgressListener listener = connectSuccessfuly(); + assertNotNull(listener); + + // Mock the speak call + Mockito.when(mOldClientMock.synthesizeToFile(Mockito.anyString(), + (HashMap<String, String>) Mockito.any(), Mockito.anyString())) + .thenReturn(TextToSpeech.SUCCESS); + + final EngineStatus engineStatus = mClient.getEngineStatus(); + assertNotNull(engineStatus); + final RequestConfig rc = RequestConfigHelper.highestQuality(engineStatus, true, + new RequestConfigHelper.ExactLocaleMatcher(Locale.US)); + + // Call speak + File output = new File("test"); + mClient.queueSynthesizeToFile(mUtterance, mUtteranceId, output, rc, null); + + // Call back to the listener + listener.onStart(mUtteranceId.toUniqueString()); + listener.onDone(mUtteranceId.toUniqueString()); + + // Verify V1 client calls + Mockito.verify(mOldClientMock).setLanguage(Locale.US); + Mockito.verify(mOldClientMock).setPitch(1.0f); + // We never set speed, so make sure we use value from settings + Mockito.verify(mOldClientMock).setSpeechRate(mImplementationV1.getDefaultSpeechRate()); + + ArgumentCaptor<HashMap> paramsCaptor = + ArgumentCaptor.forClass(HashMap.class); + Mockito.verify(mOldClientMock).synthesizeToFile(Mockito.eq(mUtterance), + paramsCaptor.capture(), Mockito.eq(output.getAbsolutePath())); + + HashMap<String, String> params = paramsCaptor.getValue(); + assertEquals(mUtteranceId.toUniqueString(), params.get( + TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID)); + assertEquals("true", params.get(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS)); + + // Verify callback calls + Mockito.verify(mRequestCallbacks).onSynthesisStart(mUtteranceId); + Mockito.verify(mRequestCallbacks).onSynthesisSuccess(mUtteranceId); + } + + /** Test successful {@link TextToSpeechClient#queueSynthesizeToFile} call, with synthesis + * failure */ + public void testQueueSynthesizeToFileSynthesisFailure() { + mockFewVoices(); + UtteranceProgressListener listener = connectSuccessfuly(); + assertNotNull(listener); + + // Mock the speak call + Mockito.when(mOldClientMock.synthesizeToFile(Mockito.anyString(), + (HashMap<String, String>) Mockito.any(), Mockito.anyString())) + .thenReturn(TextToSpeech.SUCCESS); + + final EngineStatus engineStatus = mClient.getEngineStatus(); + assertNotNull(engineStatus); + final RequestConfig rcBase = RequestConfigHelper.highestQuality(engineStatus, true, + new RequestConfigHelper.ExactLocaleMatcher(Locale.US)); + + // Set speed and pitch to 0.1f + final RequestConfig rc = RequestConfig.Builder.newBuilder(rcBase) + .setVoiceParam(TextToSpeechClient.Params.SPEECH_SPEED, 0.1f) + .setVoiceParam(TextToSpeechClient.Params.SPEECH_PITCH, 0.1f) + .build(); + + // Call synthesizeToFile + File output = new File("test"); + mClient.queueSynthesizeToFile(mUtterance, mUtteranceId, output, rc, null); + + // Call back to the listener + listener.onStart(mUtteranceId.toUniqueString()); + listener.onError(mUtteranceId.toUniqueString()); + + // Verify V1 client calls + Mockito.verify(mOldClientMock).setLanguage(Locale.US); + Mockito.verify(mOldClientMock).setPitch(0.1f); + Mockito.verify(mOldClientMock).setSpeechRate(0.1f); + Mockito.verify(mOldClientMock).synthesizeToFile(Mockito.eq(mUtterance), + (HashMap<String, String>) Mockito.any(), Mockito.eq(output.getAbsolutePath())); + + // Verify callback calls + Mockito.verify(mRequestCallbacks).onSynthesisStart(mUtteranceId); + Mockito.verify(mRequestCallbacks).onSynthesisFailure(mUtteranceId, + TextToSpeechClient.Status.ERROR_UNKNOWN); + } + + /** Test failing {@link TextToSpeechClient#queueSynthesizeToFile} call */ + public void testQueueSynthesizeToFileFailure() { + mockFewVoices(); + connectSuccessfuly(); + + // Mock the synthesizeToFile call with error + Mockito.when(mOldClientMock.synthesizeToFile(Mockito.anyString(), + (HashMap<String, String>) Mockito.any(), Mockito.anyString())) + .thenReturn(TextToSpeech.ERROR); + + final EngineStatus engineStatus = mClient.getEngineStatus(); + assertNotNull(engineStatus); + final RequestConfig rc = RequestConfigHelper.highestQuality(engineStatus, true, + new RequestConfigHelper.ExactLocaleMatcher(Locale.US)); + + // Call speak + File output = new File("test"); + mClient.queueSynthesizeToFile(mUtterance, mUtteranceId, output, rc, null); + + // Verify V1 client calls + Mockito.verify(mOldClientMock).synthesizeToFile(Mockito.eq(mUtterance), + (HashMap<String, String>) Mockito.any(), Mockito.eq(output.getAbsolutePath())); + + // Verify callback calls + Mockito.verify(mRequestCallbacks).onSynthesisFailure(mUtteranceId, + TextToSpeechClient.Status.ERROR_UNKNOWN); + } + +} diff --git a/tests/java/android/support/v4/speech/tts/TTSImplementationV2Test.java b/tests/java/android/support/v4/speech/tts/TTSImplementationV2Test.java new file mode 100644 index 0000000000..26e8dfd7cb --- /dev/null +++ b/tests/java/android/support/v4/speech/tts/TTSImplementationV2Test.java @@ -0,0 +1,213 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package android.support.v4.speech.tts; + +import android.content.Context; +import android.os.Bundle; +import android.speech.tts.TextToSpeech; +import android.support.v4.speech.tts.TextToSpeechClient.ConnectionCallbacks; +import android.support.v4.speech.tts.TextToSpeechClient.RequestCallbacks; +import android.support.v4.speech.tts.TextToSpeechClient.UtteranceId; +import android.test.InstrumentationTestCase; + +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.HashMap; +import java.util.Locale; + +/** + * Tests for {@link TTSImplementationV2} class. + */ +public class TTSImplementationV2Test extends InstrumentationTestCase { + @Mock android.speech.tts.TextToSpeechClient mRealClientMock; + @Mock RequestCallbacks mRequestCallbacksMock; + @Mock ConnectionCallbacks mConnectionCallbacksMock; + + @Mock TTSImplementationV2.TextToSpeechClientConstructor mClientConstructorMock; + TTSImplementationV2 mImplementationV2; + TextToSpeechClient mClient; + + final String mUtterance = "text"; + final UtteranceId mUtteranceId = new UtteranceId(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext(). + getCacheDir().getPath()); + MockitoAnnotations.initMocks(this); + + Mockito.when(mClientConstructorMock.newClient( + (Context) Mockito.any(), + Mockito.anyString(), Mockito.anyBoolean(), + (android.speech.tts.TextToSpeechClient.RequestCallbacks)Mockito.any(), + (android.speech.tts.TextToSpeechClient.ConnectionCallbacks)Mockito.any())) + .thenReturn(mRealClientMock); + + mImplementationV2 = new TTSImplementationV2(mClientConstructorMock); + mClient = new TextToSpeechClient(mImplementationV2, + getInstrumentation().getContext(), "test", false, mRequestCallbacksMock, + mConnectionCallbacksMock); + } + + private class Callbacks { + android.speech.tts.TextToSpeechClient.RequestCallbacks requestCallback; + android.speech.tts.TextToSpeechClient.ConnectionCallbacks connectionCallbacks; + } + + public Callbacks connect() { + ArgumentCaptor<android.speech.tts.TextToSpeechClient.RequestCallbacks> + requestCallbackCaptor = + ArgumentCaptor.forClass( + android.speech.tts.TextToSpeechClient.RequestCallbacks.class); + + ArgumentCaptor<android.speech.tts.TextToSpeechClient.ConnectionCallbacks> + connectionCallbackCaptor = + ArgumentCaptor.forClass( + android.speech.tts.TextToSpeechClient.ConnectionCallbacks.class); + + Mockito.verify(mClientConstructorMock).newClient( + Mockito.eq(getInstrumentation().getContext()), + Mockito.eq("test"), Mockito.eq(false), + requestCallbackCaptor.capture(), + connectionCallbackCaptor.capture()); + + mClient.connect(); + + Callbacks callbacks = new Callbacks(); + callbacks.requestCallback = requestCallbackCaptor.getValue(); + callbacks.connectionCallbacks = connectionCallbackCaptor.getValue(); + assertNotNull(callbacks.requestCallback); + assertNotNull(callbacks.connectionCallbacks); + return callbacks; + } + + public void testSetupSuccess() { + Callbacks callbacks = connect(); + callbacks.connectionCallbacks.onConnectionSuccess(); + Mockito.verify(mConnectionCallbacksMock).onConnectionSuccess(); + } + + public void testSetupFailure() { + Callbacks callbacks = connect(); + callbacks.connectionCallbacks.onConnectionFailure(); + Mockito.verify(mConnectionCallbacksMock).onConnectionFailure(); + } + + private static void assertEquals(Bundle expeced, Bundle value) { + if (expeced.size() != value.size()) { + fail("Received bundle has different number of mappings: " + value.size() + + ", expected: " + expeced.size()); + } + for (String key : value.keySet()) { + if (!expeced.containsKey(key)) { + fail("received bundle is missing key: " + key); + } + Object e = expeced.get(key); + Object v = value.get(key); + if (!e.equals(v)) { + fail("received bundle has wrong value for key " + key + " value: " + v + + ", expected: " + e); + } + } + } + + public android.speech.tts.VoiceInfo createFrameworkVoiceInfo() { + Bundle params = new Bundle(); + params.putInt("foo1", 1); + params.putInt("foo2", 1); + Bundle features = new Bundle(); + features.putInt("bar", 2); + return (new android.speech.tts.VoiceInfo.Builder()) + .setName("name") + .setQuality(123) + .setQuality(321) + .setRequiresNetworkConnection(false) + .setLocale(Locale.GERMANY) + .setParamsWithDefaults(params).setAdditionalFeatures(features).build(); + } + + public void testVoiceInfoConvert() { + android.speech.tts.VoiceInfo frameworkVoiceInfo = createFrameworkVoiceInfo(); + + VoiceInfo supportVoiceInfo = + TTSImplementationV2.convert(frameworkVoiceInfo); + + assertEquals(frameworkVoiceInfo.getName(), supportVoiceInfo.getName()); + assertEquals(frameworkVoiceInfo.getLatency(), supportVoiceInfo.getLatency()); + assertEquals(frameworkVoiceInfo.getQuality(), supportVoiceInfo.getQuality()); + assertEquals(frameworkVoiceInfo.getLocale(), supportVoiceInfo.getLocale()); + assertEquals(frameworkVoiceInfo.getRequiresNetworkConnection(), + supportVoiceInfo.getRequiresNetworkConnection()); + assertEquals(frameworkVoiceInfo.getAdditionalFeatures(), + supportVoiceInfo.getAdditionalFeatures()); + assertEquals(frameworkVoiceInfo.getParamsWithDefaults(), + supportVoiceInfo.getParamsWithDefaults()); + + android.speech.tts.VoiceInfo frameworkVoiceInfo2 = + TTSImplementationV2.convert(supportVoiceInfo); + + assertEquals(frameworkVoiceInfo, frameworkVoiceInfo2); + } + + public void testRequestInfoConvert() { + android.speech.tts.VoiceInfo frameworkVoiceInfo = createFrameworkVoiceInfo(); + VoiceInfo supportVoiceInfo = TTSImplementationV2.convert(frameworkVoiceInfo); + + RequestConfig.Builder builder = RequestConfig.Builder.newBuilder(); + builder.setVoice(supportVoiceInfo); + builder.setAudioParamVolume(0.5f); + builder.setVoiceParam("foo1", 5); + RequestConfig supportRequestConfig = builder.build(); + + android.speech.tts.RequestConfig frameworkRequestConfig = + TTSImplementationV2.convert(supportRequestConfig); + + assertEquals(frameworkRequestConfig.getVoice(), frameworkVoiceInfo); + assertEquals(frameworkRequestConfig.getAudioParams(), + supportRequestConfig.getAudioParams()); + assertEquals(frameworkRequestConfig.getVoiceParams(), + supportRequestConfig.getVoiceParams()); + } + + public void testRequestCallbacksConvert() { + android.speech.tts.TextToSpeechClient.RequestCallbacks frameworkRequestCallbacks = + TTSImplementationV2.convert(mRequestCallbacksMock); + + frameworkRequestCallbacks.onSynthesisFailure(TTSImplementationV2.convert(mUtteranceId), 29); + Mockito.verify(mRequestCallbacksMock).onSynthesisFailure(mUtteranceId, 29); + + frameworkRequestCallbacks.onSynthesisSuccess(TTSImplementationV2.convert(mUtteranceId)); + Mockito.verify(mRequestCallbacksMock).onSynthesisSuccess(mUtteranceId); + + frameworkRequestCallbacks.onSynthesisStop(TTSImplementationV2.convert(mUtteranceId)); + Mockito.verify(mRequestCallbacksMock).onSynthesisStop(mUtteranceId); + + frameworkRequestCallbacks.onSynthesisStart(TTSImplementationV2.convert(mUtteranceId)); + Mockito.verify(mRequestCallbacksMock).onSynthesisStart(mUtteranceId); + + frameworkRequestCallbacks.onSynthesisFallback(TTSImplementationV2.convert(mUtteranceId)); + Mockito.verify(mRequestCallbacksMock).onSynthesisFallback(mUtteranceId); + + frameworkRequestCallbacks.onSynthesisProgress(TTSImplementationV2.convert(mUtteranceId), + 1, 2); + Mockito.verify(mRequestCallbacksMock).onSynthesisProgress(mUtteranceId, 1, 2); + } +} |
