diff options
Diffstat (limited to 'tests/TtsTests')
7 files changed, 1323 insertions, 0 deletions
diff --git a/tests/TtsTests/src/com/android/speech/tts/AbstractTtsSemioticClassTest.java b/tests/TtsTests/src/com/android/speech/tts/AbstractTtsSemioticClassTest.java new file mode 100644 index 0000000..31484f4 --- /dev/null +++ b/tests/TtsTests/src/com/android/speech/tts/AbstractTtsSemioticClassTest.java @@ -0,0 +1,189 @@ +/* + * 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 com.android.speech.tts; + +import android.test.InstrumentationTestCase; + +import android.speech.tts.Markup; +import android.speech.tts.Utterance; +import android.speech.tts.Utterance.AbstractTtsSemioticClass; + +public class AbstractTtsSemioticClassTest extends InstrumentationTestCase { + + public static class TtsMock extends AbstractTtsSemioticClass<TtsMock> { + public TtsMock() { + super(); + } + + public TtsMock(Markup markup) { + super(); + } + + public void setType(String type) { + mMarkup.setType(type); + } + } + + public void testFluentAPI() { + new TtsMock() + .setPlainText("a plaintext") // from AbstractTts + .setGender(Utterance.GENDER_MALE) // from AbstractTtsSemioticClass + .setType("test"); // from TtsMock + } + + public void testDefaultConstructor() { + new TtsMock(); + } + + public void testMarkupConstructor() { + Markup markup = new Markup(); + new TtsMock(markup); + } + + public void testGetType() { + TtsMock t = new TtsMock(); + t.setType("type1"); + assertEquals("type1", t.getType()); + t.setType(null); + assertEquals(null, t.getType()); + t.setType("type2"); + assertEquals("type2", t.getType()); + } + + + public void testDefaultGender() { + assertEquals(Utterance.GENDER_UNKNOWN, new TtsMock().getGender()); + } + + public void testSetGender() { + assertEquals(Utterance.GENDER_MALE, + new TtsMock().setGender(Utterance.GENDER_MALE).getGender()); + } + + public void testSetGenderNegative() { + try { + new TtsMock().setGender(-1); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testSetGenderOutOfBounds() { + try { + new TtsMock().setGender(4); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testDefaultAnimacy() { + assertEquals(Utterance.ANIMACY_UNKNOWN, new TtsMock().getAnimacy()); + } + + public void testSetAnimacy() { + assertEquals(Utterance.ANIMACY_ANIMATE, + new TtsMock().setAnimacy(Utterance.ANIMACY_ANIMATE).getAnimacy()); + } + + public void testSetAnimacyNegative() { + try { + new TtsMock().setAnimacy(-1); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testSetAnimacyOutOfBounds() { + try { + new TtsMock().setAnimacy(4); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testDefaultMultiplicity() { + assertEquals(Utterance.MULTIPLICITY_UNKNOWN, new TtsMock().getMultiplicity()); + } + + public void testSetMultiplicity() { + assertEquals(Utterance.MULTIPLICITY_DUAL, + new TtsMock().setMultiplicity(Utterance.MULTIPLICITY_DUAL).getMultiplicity()); + } + + public void testSetMultiplicityNegative() { + try { + new TtsMock().setMultiplicity(-1); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testSetMultiplicityOutOfBounds() { + try { + new TtsMock().setMultiplicity(4); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testDefaultCase() { + assertEquals(Utterance.CASE_UNKNOWN, new TtsMock().getCase()); + } + + public void testSetCase() { + assertEquals(Utterance.CASE_VOCATIVE, + new TtsMock().setCase(Utterance.CASE_VOCATIVE).getCase()); + } + + public void testSetCaseNegative() { + try { + new TtsMock().setCase(-1); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testSetCaseOutOfBounds() { + try { + new TtsMock().setCase(9); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testToString() { + TtsMock t = new TtsMock() + .setAnimacy(Utterance.ANIMACY_INANIMATE) + .setCase(Utterance.CASE_INSTRUMENTAL) + .setGender(Utterance.GENDER_FEMALE) + .setMultiplicity(Utterance.MULTIPLICITY_PLURAL); + String str = + "animacy: \"2\" " + + "case: \"8\" " + + "gender: \"3\" " + + "multiplicity: \"3\""; + assertEquals(str, t.toString()); + } + + public void testToStringSetToUnkown() { + TtsMock t = new TtsMock() + .setAnimacy(Utterance.ANIMACY_INANIMATE) + .setCase(Utterance.CASE_INSTRUMENTAL) + .setGender(Utterance.GENDER_FEMALE) + .setMultiplicity(Utterance.MULTIPLICITY_PLURAL) + // set back to unknown + .setAnimacy(Utterance.ANIMACY_UNKNOWN) + .setCase(Utterance.CASE_UNKNOWN) + .setGender(Utterance.GENDER_UNKNOWN) + .setMultiplicity(Utterance.MULTIPLICITY_UNKNOWN); + String str = ""; + assertEquals(str, t.toString()); + } + +} diff --git a/tests/TtsTests/src/com/android/speech/tts/AbstractTtsTest.java b/tests/TtsTests/src/com/android/speech/tts/AbstractTtsTest.java new file mode 100644 index 0000000..281c97f --- /dev/null +++ b/tests/TtsTests/src/com/android/speech/tts/AbstractTtsTest.java @@ -0,0 +1,106 @@ +/* + * 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 com.android.speech.tts; + +import android.test.InstrumentationTestCase; + +import android.speech.tts.Markup; +import android.speech.tts.Utterance.AbstractTts; + +public class AbstractTtsTest extends InstrumentationTestCase { + + public static class TtsMock extends AbstractTts<TtsMock> { + public TtsMock() { + super(); + } + + public TtsMock(Markup markup) { + super(); + } + + public void setType(String type) { + mMarkup.setType(type); + } + + @Override + public TtsMock setParameter(String key, String value) { + return super.setParameter(key, value); + } + + @Override + public TtsMock removeParameter(String key) { + return super.removeParameter(key); + } + } + + public void testDefaultConstructor() { + new TtsMock(); + } + + public void testMarkupConstructor() { + Markup markup = new Markup(); + new TtsMock(markup); + } + + public void testGetType() { + TtsMock t = new TtsMock(); + t.setType("type1"); + assertEquals("type1", t.getType()); + t.setType(null); + assertEquals(null, t.getType()); + t.setType("type2"); + assertEquals("type2", t.getType()); + } + + public void testGeneratePlainText() { + assertNull(new TtsMock().generatePlainText()); + } + + public void testToString() { + TtsMock t = new TtsMock(); + t.setType("a_type"); + t.setPlainText("a plaintext"); + t.setParameter("key1", "value1"); + t.setParameter("aaa", "value2"); + String str = + "type: \"a_type\" " + + "plain_text: \"a plaintext\" " + + "aaa: \"value2\" " + + "key1: \"value1\""; + assertEquals(str, t.toString()); + } + + public void testRemoveParameter() { + TtsMock t = new TtsMock(); + t.setParameter("key1", "value 1"); + t.setParameter("aaa", "value a"); + t.removeParameter("key1"); + String str = + "aaa: \"value a\""; + assertEquals(str, t.toString()); + } + + public void testRemoveParameterBySettingNull() { + TtsMock t = new TtsMock(); + t.setParameter("key1", "value 1"); + t.setParameter("aaa", "value a"); + t.setParameter("aaa", null); + String str = + "key1: \"value 1\""; + assertEquals(str, t.toString()); + } +} diff --git a/tests/TtsTests/src/com/android/speech/tts/MarkupTest.java b/tests/TtsTests/src/com/android/speech/tts/MarkupTest.java new file mode 100644 index 0000000..7ef93ce --- /dev/null +++ b/tests/TtsTests/src/com/android/speech/tts/MarkupTest.java @@ -0,0 +1,510 @@ +/* + * 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 com.android.speech.tts; + +import junit.framework.Assert; +import android.os.Parcel; +import android.test.InstrumentationTestCase; + +import android.speech.tts.Markup; + +public class MarkupTest extends InstrumentationTestCase { + + public void testEmptyMarkup() { + Markup markup = new Markup(); + assertNull(markup.getType()); + assertNull(markup.getPlainText()); + assertEquals(0, markup.parametersSize()); + assertEquals(0, markup.nestedMarkupSize()); + } + + public void testGetSetType() { + Markup markup = new Markup(); + markup.setType("one"); + assertEquals("one", markup.getType()); + markup.setType(null); + assertNull(markup.getType()); + markup.setType("two"); + assertEquals("two", markup.getType()); + } + + public void testGetSetPlainText() { + Markup markup = new Markup(); + markup.setPlainText("one"); + assertEquals("one", markup.getPlainText()); + markup.setPlainText(null); + assertNull(markup.getPlainText()); + markup.setPlainText("two"); + assertEquals("two", markup.getPlainText()); + } + + public void testParametersSize1() { + Markup markup = new Markup(); + markup.addNestedMarkup(new Markup()); + assertEquals(1, markup.nestedMarkupSize()); + } + + public void testParametersSize2() { + Markup markup = new Markup(); + markup.addNestedMarkup(new Markup()); + markup.addNestedMarkup(new Markup()); + assertEquals(2, markup.nestedMarkupSize()); + } + + public void testRemoveParameter() { + Markup m = new Markup("type"); + m.setParameter("key1", "value1"); + m.setParameter("key2", "value2"); + m.setParameter("key3", "value3"); + assertEquals(3, m.parametersSize()); + m.removeParameter("key1"); + assertEquals(2, m.parametersSize()); + m.removeParameter("key3"); + assertEquals(1, m.parametersSize()); + assertNull(m.getParameter("key1")); + assertEquals("value2", m.getParameter("key2")); + assertNull(m.getParameter("key3")); + } + + public void testEmptyEqual() { + Markup m1 = new Markup(); + Markup m2 = new Markup(); + assertTrue(m1.equals(m2)); + } + + public void testFilledEqual() { + Markup m1 = new Markup(); + m1.setType("type"); + m1.setPlainText("plain text"); + m1.setParameter("key1", "value1"); + m1.addNestedMarkup(new Markup()); + Markup m2 = new Markup(); + m2.setType("type"); + m2.setPlainText("plain text"); + m2.setParameter("key1", "value1"); + m2.addNestedMarkup(new Markup()); + assertTrue(m1.equals(m2)); + } + + public void testDifferentTypeEqual() { + Markup m1 = new Markup(); + m1.setType("type1"); + Markup m2 = new Markup(); + m2.setType("type2"); + assertFalse(m1.equals(m2)); + } + + public void testDifferentPlainTextEqual() { + Markup m1 = new Markup(); + m1.setPlainText("plainText1"); + Markup m2 = new Markup(); + m2.setPlainText("plainText2"); + assertFalse(m1.equals(m2)); + } + + public void testDifferentParamEqual() { + Markup m1 = new Markup(); + m1.setParameter("test", "value1"); + Markup m2 = new Markup(); + m2.setParameter("test", "value2"); + assertFalse(m1.equals(m2)); + } + + public void testDifferentParameterKeyEqual() { + Markup m1 = new Markup(); + m1.setParameter("test1", "value"); + Markup m2 = new Markup(); + m2.setParameter("test2", "value"); + assertFalse(m1.equals(m2)); + } + + public void testDifferentParameterValueEqual() { + Markup m1 = new Markup(); + m1.setParameter("test", "value1"); + Markup m2 = new Markup(); + m2.setParameter("test", "value2"); + assertFalse(m1.equals(m2)); + } + + public void testDifferentNestedMarkupEqual() { + Markup m1 = new Markup(); + Markup nested = new Markup(); + nested.setParameter("key", "value"); + m1.addNestedMarkup(nested); + Markup m2 = new Markup(); + m2.addNestedMarkup(new Markup()); + assertFalse(m1.equals(m2)); + } + + public void testEmptyToFromString() { + Markup m1 = new Markup(); + String str = m1.toString(); + assertEquals("", str); + + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testTypeToFromString() { + Markup m1 = new Markup("atype"); + String str = m1.toString(); + assertEquals("type: \"atype\"", str); + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testPlainTextToFromString() { + Markup m1 = new Markup(); + m1.setPlainText("some_plainText"); + String str = m1.toString(); + assertEquals("plain_text: \"some_plainText\"", str); + + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testParameterToFromString() { + Markup m1 = new Markup("cardinal"); + m1.setParameter("integer", "-22"); + String str = m1.toString(); + assertEquals("type: \"cardinal\" integer: \"-22\"", str); + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + // Parameters should be ordered alphabettically, so the output is stable. + public void testParameterOrderToFromString() { + Markup m1 = new Markup("cardinal"); + m1.setParameter("ccc", "-"); + m1.setParameter("aaa", "-"); + m1.setParameter("aa", "-"); + m1.setParameter("bbb", "-"); + String str = m1.toString(); + assertEquals( + "type: \"cardinal\" " + + "aa: \"-\" " + + "aaa: \"-\" " + + "bbb: \"-\" " + + "ccc: \"-\"", + str); + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testEmptyNestedToFromString() { + Markup m1 = new Markup("atype"); + m1.addNestedMarkup(new Markup()); + String str = m1.toString(); + assertEquals("type: \"atype\" markup {}", str); + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testNestedWithTypeToFromString() { + Markup m1 = new Markup("atype"); + m1.addNestedMarkup(new Markup("nested_type")); + String str = m1.toString(); + assertEquals( + "type: \"atype\" " + + "markup { type: \"nested_type\" }", + str); + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testRemoveNestedMarkup() { + Markup m = new Markup("atype"); + Markup m1 = new Markup("nested_type1"); + Markup m2 = new Markup("nested_type2"); + Markup m3 = new Markup("nested_type3"); + m.addNestedMarkup(m1); + m.addNestedMarkup(m2); + m.addNestedMarkup(m3); + m.removeNestedMarkup(m1); + m.removeNestedMarkup(m3); + String str = m.toString(); + assertEquals( + "type: \"atype\" " + + "markup { type: \"nested_type2\" }", + str); + Markup mFromString = Markup.markupFromString(str); + assertEquals(m, mFromString); + } + + public void testLotsofNestingToFromString() { + Markup m1 = new Markup("top") + .addNestedMarkup(new Markup("top_child1") + .addNestedMarkup(new Markup("top_child1_child1")) + .addNestedMarkup(new Markup("top_child1_child2"))) + .addNestedMarkup(new Markup("top_child2") + .addNestedMarkup(new Markup("top_child2_child2")) + .addNestedMarkup(new Markup("top_child2_child2"))); + + String str = m1.toString(); + assertEquals( + "type: \"top\" " + + "markup { " + + "type: \"top_child1\" " + + "markup { type: \"top_child1_child1\" } " + + "markup { type: \"top_child1_child2\" } " + + "} " + + "markup { " + + "type: \"top_child2\" " + + "markup { type: \"top_child2_child2\" } " + + "markup { type: \"top_child2_child2\" } " + + "}", + str); + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testFilledToFromString() { + Markup m1 = new Markup("measure"); + m1.setPlainText("fifty-five amps"); + m1.setParameter("unit", "meter"); + m1.addNestedMarkup(new Markup("cardinal").setParameter("integer", "55")); + String str = m1.toString(); + assertEquals( + "type: \"measure\" " + + "plain_text: \"fifty-five amps\" " + + "unit: \"meter\" " + + "markup { type: \"cardinal\" integer: \"55\" }", + str); + + Markup m2 = Markup.markupFromString(str); + assertEquals(m1, m2); + } + + public void testErrorFromString() { + String str = "type: \"atype\" markup {mistake}"; + try { + Markup.markupFromString(str); + Assert.fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testEscapeQuotes() { + Markup m1 = new Markup("text") + .setParameter("something_unknown", "\"this\" is \"a sentence \" with quotes\""); + String str = m1.toString(); + assertEquals( + "type: \"text\" " + + "something_unknown: \"\\\"this\\\" is \\\"a sentence \\\" with quotes\\\"\"", + str); + + Markup m2 = Markup.markupFromString(str); + assertEquals(m1.toString(), m2.toString()); + assertEquals(m1, m2); + } + + public void testEscapeSlashes1() { + Markup m1 = new Markup("text") + .setParameter("something_unknown", "\\ \\\\ \t \n \""); + String str = m1.toString(); + assertEquals( + "type: \"text\" " + + "something_unknown: \"\\\\ \\\\\\\\ \t \n \\\"\"", + str); + + Markup m2 = Markup.markupFromString(str); + assertEquals(m1.toString(), m2.toString()); + assertEquals(m1, m2); + } + + public void testEscapeSlashes2() { + Markup m1 = new Markup("text") + .setParameter("something_unknown", "\\\"\\\"\\\\\"\"\\\\\\\"\"\""); + String str = m1.toString(); + assertEquals( + "type: \"text\" " + + "something_unknown: \"\\\\\\\"\\\\\\\"\\\\\\\\\\\"\\\"\\\\\\\\\\\\\\\"\\\"\\\"\"", + str); + + Markup m2 = Markup.markupFromString(str); + assertEquals(m1.toString(), m2.toString()); + assertEquals(m1, m2); + } + + public void testBadInput1() { + String str = "type: \"text\" text: \"\\\""; + try { + Markup.markupFromString(str); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testBadInput2() { + String str = "type: \"text\" text: \"\\a\""; + try { + Markup.markupFromString(str); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testValidParameterKey() { + Markup m = new Markup(); + m.setParameter("ke9__yk_88ey_za7_", "test"); + } + + public void testInValidParameterKeyEmpty() { + Markup m = new Markup(); + try { + m.setParameter("", "test"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testInValidParameterKeyDollar() { + Markup m = new Markup(); + try { + m.setParameter("ke9y$k88ey7", "test"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testInValidParameterKeySpace() { + Markup m = new Markup(); + try { + m.setParameter("ke9yk88ey7 ", "test"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testValidType() { + new Markup("_this_is_1_valid_type_222"); + } + + public void testInValidTypeAmpersand() { + try { + new Markup("abcde1234&"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testInValidTypeSpace() { + try { + new Markup(" "); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testSimpleParcelable() { + Markup markup = new Markup(); + + Parcel parcel = Parcel.obtain(); + markup.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + Markup fromParcel = (Markup) Markup.CREATOR.createFromParcel(parcel); + + assertFalse(markup == fromParcel); + assertEquals(markup, fromParcel); + } + + public void testTypeParcelable() { + Markup markup = new Markup("text"); + + Parcel parcel = Parcel.obtain(); + markup.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + Markup fromParcel = (Markup) Markup.CREATOR.createFromParcel(parcel); + + assertFalse(markup == fromParcel); + assertEquals(markup, fromParcel); + } + + public void testPlainTextsParcelable() { + Markup markup = new Markup(); + markup.setPlainText("plainText"); + + Parcel parcel = Parcel.obtain(); + markup.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + Markup fromParcel = (Markup) Markup.CREATOR.createFromParcel(parcel); + + assertFalse(markup == fromParcel); + assertEquals(markup, fromParcel); + } + + public void testParametersParcelable() { + Markup markup = new Markup(); + markup.setParameter("key1", "value1"); + markup.setParameter("key2", "value2"); + markup.setParameter("key3", "value3"); + + Parcel parcel = Parcel.obtain(); + markup.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + Markup fromParcel = (Markup) Markup.CREATOR.createFromParcel(parcel); + + assertFalse(markup == fromParcel); + assertEquals(markup, fromParcel); + } + + public void testNestedParcelable() { + Markup markup = new Markup(); + markup.addNestedMarkup(new Markup("first")); + markup.addNestedMarkup(new Markup("second")); + markup.addNestedMarkup(new Markup("third")); + + Parcel parcel = Parcel.obtain(); + markup.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + Markup fromParcel = (Markup) Markup.CREATOR.createFromParcel(parcel); + + assertFalse(markup == fromParcel); + assertEquals(markup, fromParcel); + } + + public void testAllFieldsParcelable() { + Markup markup = new Markup("text"); + markup.setPlainText("plain text"); + markup.setParameter("key1", "value1"); + markup.setParameter("key2", "value2"); + markup.setParameter("key3", "value3"); + markup.addNestedMarkup(new Markup("first")); + markup.addNestedMarkup(new Markup("second")); + markup.addNestedMarkup(new Markup("third")); + + Parcel parcel = Parcel.obtain(); + markup.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + Markup fromParcel = (Markup) Markup.CREATOR.createFromParcel(parcel); + + assertFalse(markup == fromParcel); + assertEquals(markup, fromParcel); + } + + public void testKeyCannotBeType() { + try { + new Markup().setParameter("type", "vale"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testKeyCannotBePlainText() { + try { + new Markup().setParameter("plain_text", "value"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } +} diff --git a/tests/TtsTests/src/com/android/speech/tts/TtsCardinalTest.java b/tests/TtsTests/src/com/android/speech/tts/TtsCardinalTest.java new file mode 100644 index 0000000..c34f4ac --- /dev/null +++ b/tests/TtsTests/src/com/android/speech/tts/TtsCardinalTest.java @@ -0,0 +1,119 @@ +/* + * 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 com.android.speech.tts; + +import junit.framework.Assert; +import android.test.InstrumentationTestCase; +import android.test.MoreAsserts; + +import android.speech.tts.Markup; +import android.speech.tts.Utterance; +import android.speech.tts.Utterance.TtsCardinal; +import android.speech.tts.Utterance.TtsText; + +public class TtsCardinalTest extends InstrumentationTestCase { + + public void testConstruct() { + assertNotNull(new TtsCardinal(0)); + } + + public void testFluentAPI() { + new TtsCardinal() + .setPlainText("a plaintext") // from AbstractTts + .setGender(Utterance.GENDER_MALE) // from AbstractTtsSemioticClass + .setInteger("-10001"); // from TtsText + } + + public void testZero() { + assertEquals("0", new TtsCardinal(0).getInteger()); + } + + public void testThirtyOne() { + assertEquals("31", new TtsCardinal(31).getInteger()); + } + + public void testMarkupZero() { + TtsCardinal c = new TtsCardinal(0); + Markup m = c.getMarkup(); + assertEquals("0", m.getParameter("integer")); + } + + public void testMarkupThirtyOne() { + TtsCardinal c = new TtsCardinal(31); + Markup m = c.getMarkup(); + assertEquals("31", m.getParameter("integer")); + } + + public void testMarkupThirtyOneString() { + TtsCardinal c = new TtsCardinal("31"); + Markup m = c.getMarkup(); + assertEquals("31", m.getParameter("integer")); + } + + public void testMarkupNegativeThirtyOne() { + TtsCardinal c = new TtsCardinal(-31); + Markup m = c.getMarkup(); + assertEquals("-31", m.getParameter("integer")); + } + + public void testMarkupMinusZero() { + TtsCardinal c = new TtsCardinal("-0"); + Markup m = c.getMarkup(); + assertEquals("-0", m.getParameter("integer")); + } + + public void testMarkupNegativeThirtyOneString() { + TtsCardinal c = new TtsCardinal("-31"); + Markup m = c.getMarkup(); + assertEquals("-31", m.getParameter("integer")); + } + + public void testOnlyLetters() { + try { + new TtsCardinal("abc"); + Assert.fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testOnlyMinus() { + try { + new TtsCardinal("-"); + Assert.fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testNegativeLetters() { + try { + new TtsCardinal("-abc"); + Assert.fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testLetterNumberMix() { + try { + new TtsCardinal("-0a1b2c"); + Assert.fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void letterNumberMix2() { + try { + new TtsCardinal("-a0b1c2"); + Assert.fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } +} diff --git a/tests/TtsTests/src/com/android/speech/tts/TtsEnginesTests.java b/tests/TtsTests/src/com/android/speech/tts/TtsEnginesTests.java new file mode 100644 index 0000000..3fbc44b --- /dev/null +++ b/tests/TtsTests/src/com/android/speech/tts/TtsEnginesTests.java @@ -0,0 +1,77 @@ +package com.android.speech.tts; + +import android.speech.tts.TtsEngines; +import android.test.InstrumentationTestCase; + +import java.util.Locale; + +public class TtsEnginesTests extends InstrumentationTestCase { + private TtsEngines mTtsHelper; + + @Override + public void setUp() { + mTtsHelper = new TtsEngines(getInstrumentation().getContext()); + } + + public void testParseLocaleString() { + assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("eng-usa")); + assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("eng-USA")); + assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("en-US")); + assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("en_us")); + assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("eng_US")); + assertEquals(new Locale("en", "US", "foobar"), + mTtsHelper.parseLocaleString("eng_US-foobar")); + assertEquals(new Locale("en", "", "foobar"), mTtsHelper.parseLocaleString("eng__foobar")); + assertNull(mTtsHelper.parseLocaleString("cc_xx_barbar")); + assertNull(mTtsHelper.parseLocaleString("cc--barbar")); + + assertEquals(new Locale("en"), mTtsHelper.parseLocaleString("eng")); + assertEquals(new Locale("en","US","var"), mTtsHelper.parseLocaleString("eng-USA-var")); + } + + public void testToOldLocaleStringFormat() { + assertArraysEqual(new String[]{"deu", "DEU", ""}, + TtsEngines.toOldLocaleStringFormat(new Locale("de", "DE"))); + assertArraysEqual(new String[]{"deu", "", ""}, + TtsEngines.toOldLocaleStringFormat(new Locale("de"))); + assertArraysEqual(new String[]{"eng", "", ""}, + TtsEngines.toOldLocaleStringFormat(new Locale("en"))); + assertArraysEqual(new String[]{"eng", "USA", ""}, + TtsEngines.toOldLocaleStringFormat(new Locale("foo"))); + } + + public void testNormalizeLocale() { + assertEquals(Locale.UK, + TtsEngines.normalizeTTSLocale(new Locale("eng", "gbr"))); + assertEquals(Locale.UK, + TtsEngines.normalizeTTSLocale(new Locale("eng", "GBR"))); + assertEquals(Locale.GERMANY, + TtsEngines.normalizeTTSLocale(new Locale("deu", "deu"))); + assertEquals(Locale.GERMAN, + TtsEngines.normalizeTTSLocale(new Locale("deu"))); + assertEquals(new Locale("yyy", "DE"), + TtsEngines.normalizeTTSLocale(new Locale("yyy", "DE"))); + } + + public void testGetLocalePrefForEngine() { + assertEquals(new Locale("en", "US"), + mTtsHelper.getLocalePrefForEngine("foo","foo:en-US")); + assertEquals(new Locale("en", "US"), + mTtsHelper.getLocalePrefForEngine("foo","foo:eng-usa")); + assertEquals(new Locale("en", "US"), + mTtsHelper.getLocalePrefForEngine("foo","foo:eng_USA")); + assertEquals(new Locale("de", "DE"), + mTtsHelper.getLocalePrefForEngine("foo","foo:deu-deu")); + assertEquals(Locale.getDefault(), + mTtsHelper.getLocalePrefForEngine("foo","foo:,bar:xx")); + assertEquals(Locale.getDefault(), + mTtsHelper.getLocalePrefForEngine("other","foo:,bar:xx")); + } + + private void assertArraysEqual(String[] expected, String[] actual) { + assertEquals("array length", expected.length, actual.length); + for (int i = 0; i < expected.length; i++) { + assertEquals("index " + i, expected[i], actual[i]); + } + } +}
\ No newline at end of file diff --git a/tests/TtsTests/src/com/android/speech/tts/TtsTextTest.java b/tests/TtsTests/src/com/android/speech/tts/TtsTextTest.java new file mode 100644 index 0000000..35fd453 --- /dev/null +++ b/tests/TtsTests/src/com/android/speech/tts/TtsTextTest.java @@ -0,0 +1,74 @@ +/* + * 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 com.android.speech.tts; + +import android.test.InstrumentationTestCase; + +import android.speech.tts.Markup; +import android.speech.tts.Utterance; +import android.speech.tts.Utterance.TtsText; + +public class TtsTextTest extends InstrumentationTestCase { + + public void testConstruct() { + assertNotNull(new TtsText()); + } + + public void testFluentAPI() { + new TtsText() + .setPlainText("a plaintext") // from AbstractTts + .setGender(Utterance.GENDER_MALE) // from AbstractTtsSemioticClass + .setText("text"); // from TtsText + } + + public void testConstructEmptyString() { + assertTrue(new TtsText("").getText().isEmpty()); + } + + public void testConstructString() { + assertEquals("this is a test.", new TtsText("this is a test.").getText()); + } + + public void testSetText() { + assertEquals("This is a test.", new TtsText().setText("This is a test.").getText()); + } + + public void testEmptyMarkup() { + TtsText t = new TtsText(); + Markup m = t.getMarkup(); + assertEquals("text", m.getType()); + assertNull(m.getPlainText()); + assertEquals(0, m.nestedMarkupSize()); + } + + public void testConstructStringMarkup() { + TtsText t = new TtsText("test"); + Markup m = t.getMarkup(); + assertEquals("text", m.getType()); + assertEquals("test", m.getParameter("text")); + assertEquals(0, m.nestedMarkupSize()); + } + + public void testSetStringMarkup() { + TtsText t = new TtsText(); + t.setText("test"); + Markup m = t.getMarkup(); + assertEquals("text", m.getType()); + assertEquals("test", m.getParameter("text")); + assertEquals(0, m.nestedMarkupSize()); + } +} diff --git a/tests/TtsTests/src/com/android/speech/tts/UtteranceTest.java b/tests/TtsTests/src/com/android/speech/tts/UtteranceTest.java new file mode 100644 index 0000000..8014dd1 --- /dev/null +++ b/tests/TtsTests/src/com/android/speech/tts/UtteranceTest.java @@ -0,0 +1,248 @@ +/* + * 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 com.android.speech.tts; + +import android.speech.tts.Markup; +import android.speech.tts.Utterance; +import android.speech.tts.Utterance.TtsCardinal; +import android.speech.tts.Utterance.TtsText; + +import android.test.InstrumentationTestCase; + +public class UtteranceTest extends InstrumentationTestCase { + + public void testEmptyUtterance() { + Utterance utt = new Utterance(); + assertEquals(0, utt.size()); + } + + public void testSizeCardinal() { + Utterance utt = new Utterance() + .append(new TtsCardinal(42)); + assertEquals(1, utt.size()); + } + + public void testSizeCardinalString() { + Utterance utt = new Utterance() + .append(new TtsCardinal(42)) + .append(new TtsText("is the answer")); + assertEquals(2, utt.size()); + } + + public void testMarkupEmpty() { + Markup m = new Utterance().createMarkup(); + assertEquals("utterance", m.getType()); + assertEquals("", m.getPlainText()); + } + + public void testMarkupCardinal() { + Utterance utt = new Utterance() + .append(new TtsCardinal(42)); + Markup markup = utt.createMarkup(); + assertEquals("utterance", markup.getType()); + assertEquals("42", markup.getPlainText()); + assertEquals("42", markup.getNestedMarkup(0).getParameter("integer")); + assertEquals("42", markup.getNestedMarkup(0).getPlainText()); + } + + public void testMarkupCardinalString() { + Utterance utt = new Utterance() + .append(new TtsCardinal(42)) + .append(new TtsText("is not just a number.")); + Markup markup = utt.createMarkup(); + assertEquals("utterance", markup.getType()); + assertEquals("42 is not just a number.", markup.getPlainText()); + assertEquals("cardinal", markup.getNestedMarkup(0).getType()); + assertEquals("42", markup.getNestedMarkup(0).getParameter("integer")); + assertEquals("42", markup.getNestedMarkup(0).getPlainText()); + assertEquals("text", markup.getNestedMarkup(1).getType()); + assertEquals("is not just a number.", markup.getNestedMarkup(1).getParameter("text")); + assertEquals("is not just a number.", markup.getNestedMarkup(1).getPlainText()); + } + + public void testTextCardinalToFromString() { + Utterance utt = new Utterance() + .append(new TtsCardinal(55)) + .append(new TtsText("this is a text.")); + String str = utt.toString(); + assertEquals( + "type: \"utterance\" " + + "markup { " + + "type: \"cardinal\" " + + "integer: \"55\" " + + "} " + + "markup { " + + "type: \"text\" " + + "text: \"this is a text.\" " + + "}" + , str); + + Utterance utt_new = Utterance.utteranceFromString(str); + assertEquals(str, utt_new.toString()); + } + + public void testNotUtteranceFromString() { + String str = + "type: \"this_is_not_an_utterance\" " + + "markup { " + + "type: \"cardinal\" " + + "plain_text: \"55\" " + + "integer: \"55\" " + + "}"; + try { + Utterance.utteranceFromString(str); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) {} + } + + public void testFromMarkup() { + String markup_str = + "type: \"utterance\" " + + "markup { " + + "type: \"cardinal\" " + + "plain_text: \"55\" " + + "integer: \"55\" " + + "} " + + "markup { " + + "type: \"text\" " + + "plain_text: \"this is a text.\" " + + "text: \"this is a text.\" " + + "}"; + Utterance utt = Utterance.utteranceFromString(markup_str); + assertEquals(markup_str, utt.toString()); + } + + public void testsetPlainText() { + Utterance utt = new Utterance() + .append(new TtsCardinal(-100).setPlainText("minus one hundred")); + assertEquals("minus one hundred", utt.get(0).getPlainText()); + } + + public void testRemoveTextThroughSet() { + Utterance utt = new Utterance() + .append(new TtsText().setText("test").setText(null)); + assertNull(((TtsText) utt.get(0)).getText()); + } + + public void testUnknownNodeWithPlainText() { + String str = + "type: \"utterance\" " + + "markup { " + + "type: \"some_future_feature\" " + + "plain_text: \"biep bob bob\" " + + "bombom: \"lorum ipsum\" " + + "}"; + Utterance utt = Utterance.utteranceFromString(str); + assertNotNull(utt); + assertEquals("text", utt.get(0).getType()); + assertEquals("biep bob bob", ((TtsText) utt.get(0)).getText()); + } + + public void testUnknownNodeWithNoPlainTexts() { + String str = + "type: \"utterance\" " + + "markup { " + + "type: \"some_future_feature\" " + + "bombom: \"lorum ipsum\" " + + "markup { type: \"cardinal\" integer: \"10\" } " + + "markup { type: \"text\" text: \"pears\" } " + + "}"; + Utterance utt = Utterance.utteranceFromString(str); + assertEquals( + "type: \"utterance\" " + + "markup { type: \"cardinal\" integer: \"10\" } " + + "markup { type: \"text\" text: \"pears\" }", utt.toString()); + } + + public void testCreateWarningOnFallbackTrue() { + Utterance utt = new Utterance() + .append(new TtsText("test")) + .setNoWarningOnFallback(true); + assertEquals( + "type: \"utterance\" " + + "no_warning_on_fallback: \"true\" " + + "markup { " + + "type: \"text\" " + + "text: \"test\" " + + "}", utt.toString()); + } + + public void testCreateWarningOnFallbackFalse() { + Utterance utt = new Utterance() + .append(new TtsText("test")) + .setNoWarningOnFallback(false); + assertEquals( + "type: \"utterance\" " + + "no_warning_on_fallback: \"false\" " + + "markup { " + + "type: \"text\" " + + "text: \"test\" " + + "}", utt.toString()); + } + + public void testCreatePlainTexts() { + Utterance utt = new Utterance() + .append(new TtsText("test")) + .append(new TtsCardinal(-55)); + assertEquals( + "type: \"utterance\" " + + "plain_text: \"test -55\" " + + "markup { type: \"text\" plain_text: \"test\" text: \"test\" } " + + "markup { type: \"cardinal\" plain_text: \"-55\" integer: \"-55\" }", + utt.createMarkup().toString() + ); + } + + public void testDontOverwritePlainTexts() { + Utterance utt = new Utterance() + .append(new TtsText("test").setPlainText("else")) + .append(new TtsCardinal(-55).setPlainText("44")); + assertEquals( + "type: \"utterance\" " + + "plain_text: \"else 44\" " + + "markup { type: \"text\" plain_text: \"else\" text: \"test\" } " + + "markup { type: \"cardinal\" plain_text: \"44\" integer: \"-55\" }", + utt.createMarkup().toString() + ); + } + + public void test99BottlesOnWallMarkup() { + Utterance utt = new Utterance() + .append("there are") + .append(99) + .append("bottles on the wall."); + assertEquals( + "type: \"utterance\" " + + "plain_text: \"there are 99 bottles on the wall.\" " + + "markup { type: \"text\" plain_text: \"there are\" text: \"there are\" } " + + "markup { type: \"cardinal\" plain_text: \"99\" integer: \"99\" } " + + "markup { type: \"text\" plain_text: \"bottles on the wall.\" text: \"bottles on the wall.\" }", + utt.createMarkup().toString()); + assertEquals("99", utt.createMarkup().getNestedMarkup(1).getPlainText()); + Markup markup = new Markup(utt.createMarkup()); + assertEquals("99", markup.getNestedMarkup(1).getPlainText()); + } + + public void testWhat() { + Utterance utt = new Utterance() + .append("there are") + .append(99) + .append("bottles on the wall."); + Markup m = utt.createMarkup(); + m.getNestedMarkup(1).getPlainText().equals("99"); + } +} |