Skip to content

Commit

Permalink
Android: code reformat and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykMis committed Apr 28, 2023
1 parent db7b209 commit 86cded9
Show file tree
Hide file tree
Showing 91 changed files with 3,917 additions and 4,077 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package com.reecedunn.espeak.test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import android.app.Activity;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
Expand All @@ -28,16 +32,15 @@
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class CheckVoiceDataTest extends ActivityUnitTestCase<CheckVoiceData>
{
public class CheckVoiceDataTest extends ActivityUnitTestCase<CheckVoiceData> {
Field mResultCode;
Field mResultData;

public void setUp() throws Exception
{
public CheckVoiceDataTest() {
super(CheckVoiceData.class);
}

public void setUp() throws Exception {
super.setUp();

mResultCode = Activity.class.getDeclaredField("mResultCode");
Expand All @@ -47,33 +50,23 @@ public void setUp() throws Exception
mResultData.setAccessible(true);
}

public int getResultCode() throws IllegalAccessException
{
return (Integer)mResultCode.get(getActivity());
public int getResultCode() throws IllegalAccessException {
return (Integer) mResultCode.get(getActivity());
}

public Intent getResultData() throws IllegalAccessException
{
return (Intent)mResultData.get(getActivity());
}

public CheckVoiceDataTest()
{
super(CheckVoiceData.class);
public Intent getResultData() throws IllegalAccessException {
return (Intent) mResultData.get(getActivity());
}

public Set<String> getExpectedVoices()
{
public Set<String> getExpectedVoices() {
Set<String> expected = new HashSet<String>();
for (VoiceData.Voice voice : VoiceData.voices)
{
for (VoiceData.Voice voice : VoiceData.voices) {
expected.add(voice.locale);
}
return expected;
}

public void testUnavailableVoices() throws IllegalAccessException
{
public void testUnavailableVoices() throws IllegalAccessException {
Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivity(intent, null, null);

Expand All @@ -89,8 +82,7 @@ public void testUnavailableVoices() throws IllegalAccessException
assertThat(unavailable.toString(), is("[]"));
}

public void testAvailableVoicesAdded() throws IllegalAccessException
{
public void testAvailableVoicesAdded() throws IllegalAccessException {
Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivity(intent, null, null);

Expand All @@ -107,19 +99,16 @@ public void testAvailableVoicesAdded() throws IllegalAccessException
Set<String> expected = getExpectedVoices();

Set<String> added = new HashSet<String>();
for (String voice : available)
{
if (!expected.contains(voice))
{
for (String voice : available) {
if (!expected.contains(voice)) {
added.add(voice);
}
}

assertThat(added.toString(), is("[]"));
}

public void testAvailableVoicesRemoved() throws IllegalAccessException
{
public void testAvailableVoicesRemoved() throws IllegalAccessException {
Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivity(intent, null, null);

Expand All @@ -136,10 +125,8 @@ public void testAvailableVoicesRemoved() throws IllegalAccessException
Set<String> expected = getExpectedVoices();

Set<String> removed = new HashSet<String>();
for (String voice : expected)
{
if (!available.contains(voice))
{
for (String voice : expected) {
if (!available.contains(voice)) {
removed.add(voice);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@

package com.reecedunn.espeak.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import com.reecedunn.espeak.SpeechSynthesis;
import com.reecedunn.espeak.Voice;
import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

import android.media.AudioFormat;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.AnyOf.anyOf;
import com.reecedunn.espeak.SpeechSynthesis;
import com.reecedunn.espeak.Voice;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class SpeechSynthesisTest extends TextToSpeechTestCase
{
public class SpeechSynthesisTest extends TextToSpeechTestCase {
public static final Locale af = new Locale("af"); // Afrikaans
public static final Locale afr = new Locale("afr"); // Afrikaans

Expand Down Expand Up @@ -92,27 +92,21 @@ public class SpeechSynthesisTest extends TextToSpeechTestCase
public static final Locale eng_GBR_scotland = new Locale("en", "GBR", "scotland"); // English (Scottish,Great Britain)
public static final Locale eng_GBR_north = new Locale("en", "GBR", "north"); // English (North,Great Britain)

private SpeechSynthesis.SynthReadyCallback mCallback = new SpeechSynthesis.SynthReadyCallback()
{
private final SpeechSynthesis.SynthReadyCallback mCallback = new SpeechSynthesis.SynthReadyCallback() {
@Override
public void onSynthDataReady(byte[] audioData)
{
public void onSynthDataReady(byte[] audioData) {
}

@Override
public void onSynthDataComplete()
{
public void onSynthDataComplete() {
}
};

private final Set<String> mAdded = new HashSet<String>();
private final Set<String> mRemoved = new HashSet<String>();
private Map<String, Voice> mVoices = null;
private Set<String> mAdded = new HashSet<String>();
private Set<String> mRemoved = new HashSet<String>();

public Map<String, Voice> getVoices()
{
if (mVoices == null)
{
public Map<String, Voice> getVoices() {
if (mVoices == null) {
final SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
mVoices = new HashMap<String, Voice>();
for (Voice voice : synth.getAvailableVoices()) {
Expand All @@ -122,90 +116,72 @@ public Map<String, Voice> getVoices()
assertThat(mVoices, is(notNullValue()));

Set<String> voices = new HashSet<String>();
for (Voice data : mVoices.values())
{
for (Voice data : mVoices.values()) {
voices.add(data.name);
}

Set<String> expected = new HashSet<String>();
for (VoiceData.Voice data : VoiceData.voices)
{
for (VoiceData.Voice data : VoiceData.voices) {
expected.add(data.name);
}

for (String voice : voices)
{
if (!expected.contains(voice))
{
for (String voice : voices) {
if (!expected.contains(voice)) {
mAdded.add(voice);
}
}

for (String voice : expected)
{
if (!voices.contains(voice))
{
for (String voice : expected) {
if (!voices.contains(voice)) {
mRemoved.add(voice);
}
}
}
return mVoices;
}

public Voice getVoice(String name)
{
public Voice getVoice(String name) {
return getVoices().get(name);
}

public void testConstruction()
{
public void testConstruction() {
final SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
assertThat(synth.getSampleRate(), is(22050));
assertThat(synth.getChannelCount(), is(1));
assertThat(synth.getAudioFormat(), is(AudioFormat.ENCODING_PCM_16BIT));
}

public void testJavaToIanaLanguageCode()
{
for (VoiceData.Voice data : VoiceData.voices)
{
public void testJavaToIanaLanguageCode() {
for (VoiceData.Voice data : VoiceData.voices) {
assertThat(SpeechSynthesis.getIanaLanguageCode(data.javaLanguage), is(data.ianaLanguage));
}
}

public void testJavaToIanaCountryCode()
{
for (VoiceData.Voice data : VoiceData.voices)
{
public void testJavaToIanaCountryCode() {
for (VoiceData.Voice data : VoiceData.voices) {
assertThat(SpeechSynthesis.getIanaCountryCode(data.javaCountry), is(data.ianaCountry));
}
}

public void testAddedVoices()
{
public void testAddedVoices() {
getVoices(); // Ensure that the voice data has been populated.
assertThat(mAdded.toString(), is("[]"));
}

public void testRemovedVoices()
{
public void testRemovedVoices() {
getVoices(); // Ensure that the voice data has been populated.
assertThat(mRemoved.toString(), is("[]"));
}

public void testVoiceData()
{
for (VoiceData.Voice data : VoiceData.voices)
{
if (mRemoved.contains(data.name))
{
public void testVoiceData() {
for (VoiceData.Voice data : VoiceData.voices) {
if (mRemoved.contains(data.name)) {
Log.i("SpeechSynthesisTest", "Skipping the missing voice '" + data.name + "'");
continue;
}

String context = "[voice]";
try
{
try {
final Voice voice = getVoice(data.name);
assertThat(voice, is(notNullValue()));

Expand All @@ -229,16 +205,13 @@ public void testVoiceData()
assertThat(voice.locale.getVariant(), is(data.variant));
context = "[toString]";
assertThat(voice.toString(), is(data.locale));
}
catch (AssertionError e)
{
} catch (AssertionError e) {
throw new VoiceData.Exception(data, context, e);
}
}
}

public void testMatchVoiceWithLanguage()
{
public void testMatchVoiceWithLanguage() {
final Voice voice = getVoice("de"); // language="de" country="" variant=""
assertThat(voice, is(notNullValue()));

Expand All @@ -258,8 +231,7 @@ public void testMatchVoiceWithLanguage()
assertThat(voice.match(deu_CHE_1901), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
}

public void testMatchVoiceWithLanguageAndCountry()
{
public void testMatchVoiceWithLanguageAndCountry() {
final Voice voice = getVoice("fr-be"); // language="fr" country="BE" variant=""
assertThat(voice, is(notNullValue()));

Expand All @@ -283,8 +255,7 @@ public void testMatchVoiceWithLanguageAndCountry()
assertThat(voice.match(fra_BEL_1694acad), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
}

public void testMatchVoiceWithLanguageCountryAndVariant()
{
public void testMatchVoiceWithLanguageCountryAndVariant() {
final Voice voice = getVoice("en-gb-scotland"); // language="en" country="GB" variant="scotland"
assertThat(voice, is(notNullValue()));

Expand All @@ -308,37 +279,30 @@ public void testMatchVoiceWithLanguageCountryAndVariant()
assertThat(voice.match(eng_GBR_north), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
}

public void testGetSampleText()
{
public void testGetSampleText() {
final String[] currentLocales = getContext().getResources().getAssets().getLocales();
for (VoiceData.Voice data : VoiceData.voices)
{
if (mRemoved.contains(data.name))
{
for (VoiceData.Voice data : VoiceData.voices) {
if (mRemoved.contains(data.name)) {
Log.i("SpeechSynthesisTest", "Skipping the missing voice '" + data.name + "'");
continue;
}

String context = null;
try
{
try {
final Locale ianaLocale = new Locale(data.ianaLanguage, data.ianaCountry, data.variant);
context = "[iana:sample-text]";
assertThat(SpeechSynthesis.getSampleText(getContext(), ianaLocale), isIn(data.sampleText));
context = "[iana:resource-locale]";
assertThat(getContext().getResources().getAssets().getLocales(), is(currentLocales));

if (!data.javaLanguage.equals(""))
{
if (!data.javaLanguage.equals("")) {
final Locale javaLocale = new Locale(data.javaLanguage, data.javaCountry, data.variant);
context = "[java:sample-text]";
assertThat(SpeechSynthesis.getSampleText(getContext(), javaLocale), isIn(data.sampleText));
context = "[java:resource-locale]";
assertThat(getContext().getResources().getAssets().getLocales(), is(currentLocales));
}
}
catch (AssertionError e)
{
} catch (AssertionError e) {
throw new VoiceData.Exception(data, context, e);
}
}
Expand Down
Loading

0 comments on commit 86cded9

Please sign in to comment.