From c9003c50ba37b42dc45ee72faed42331465e4c21 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Wed, 29 Oct 2014 19:50:43 +0000 Subject: [PATCH] TextToSpeech: test and fix onIsValidVoiceName --- .../espeak/test/TextToSpeechTest.java | 47 +++++++++++++++++++ .../src/com/reecedunn/espeak/TtsService.java | 7 ++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java index 4d12e58a..9da9647a 100644 --- a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java @@ -79,6 +79,18 @@ public class TextToSpeechTest extends TextToSpeechTestCase return mVoices; } + public VoiceData.Voice getVoiceData(String name) + { + for (VoiceData.Voice voice : VoiceData.voices) + { + if (voice.name.equals(name)) + { + return voice; + } + } + return null; + } + public void testAddedVoices() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) @@ -97,6 +109,41 @@ public class TextToSpeechTest extends TextToSpeechTestCase assertThat(mRemoved.toString(), is("[]")); } + @SuppressLint("NewApi") + public void testVoices() + { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) + return; + + getVoices(); // Ensure that the voice data has been populated. + for (Object item : mVoices) + { + android.speech.tts.Voice voice = (android.speech.tts.Voice)item; + VoiceData.Voice data = getVoiceData(voice.getName()); + + assertThat(data, is(notNullValue())); + assertThat(voice.getName(), is(data.name)); + assertThat(voice.getLocale().getLanguage(), is(data.ianaLanguage)); + assertThat(voice.getLocale().getCountry(), is(data.ianaCountry)); + assertThat(voice.getLocale().getVariant(), is(data.variant)); + assertThat(voice.getFeatures(), is(nullValue())); + assertThat(voice.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW)); + assertThat(voice.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL)); + + getEngine().setVoice(voice); + + android.speech.tts.Voice voice2 = getEngine().getVoice(); + assertThat(voice2, is(notNullValue())); + assertThat(voice2.getName(), is(data.name)); + assertThat(voice2.getLocale().getLanguage(), is(data.ianaLanguage)); + assertThat(voice2.getLocale().getCountry(), is(data.ianaCountry)); + assertThat(voice2.getLocale().getVariant(), is(data.variant)); + assertThat(voice2.getFeatures(), is(nullValue())); + assertThat(voice2.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW)); + assertThat(voice2.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL)); + } + } + public void testUnsupportedLanguage() { assertThat(getEngine(), is(notNullValue())); diff --git a/android/src/com/reecedunn/espeak/TtsService.java b/android/src/com/reecedunn/espeak/TtsService.java index 9ad1a759..8d11bbd0 100644 --- a/android/src/com/reecedunn/espeak/TtsService.java +++ b/android/src/com/reecedunn/espeak/TtsService.java @@ -217,7 +217,12 @@ public class TtsService extends TextToSpeechService { @Override public int onIsValidVoiceName(String name) { - return super.onIsValidVoiceName(name); + for (Voice voice : mAvailableVoices) { + if (voice.name.equals(name)) { + return TextToSpeech.SUCCESS; + } + } + return TextToSpeech.ERROR; } @Override