1 package com.android.speech.tts; 2 3 import android.speech.tts.TtsEngines; 4 import android.test.InstrumentationTestCase; 5 6 import java.util.Locale; 7 8 public class TtsEnginesTests extends InstrumentationTestCase { 9 private TtsEngines mTtsHelper; 10 11 @Override setUp()12 public void setUp() { 13 mTtsHelper = new TtsEngines(getInstrumentation().getContext()); 14 } 15 testParseLocaleString()16 public void testParseLocaleString() { 17 assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("eng-usa")); 18 assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("eng-USA")); 19 assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("en-US")); 20 assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("en_us")); 21 assertEquals(new Locale("en", "US"), mTtsHelper.parseLocaleString("eng_US")); 22 assertEquals(new Locale("en", "US", "foobar"), 23 mTtsHelper.parseLocaleString("eng_US-foobar")); 24 assertEquals(new Locale("en", "", "foobar"), mTtsHelper.parseLocaleString("eng__foobar")); 25 assertNull(mTtsHelper.parseLocaleString("cc_xx_barbar")); 26 assertNull(mTtsHelper.parseLocaleString("cc--barbar")); 27 28 assertEquals(new Locale("en"), mTtsHelper.parseLocaleString("eng")); 29 assertEquals(new Locale("en","US","var"), mTtsHelper.parseLocaleString("eng-USA-var")); 30 } 31 testToOldLocaleStringFormat()32 public void testToOldLocaleStringFormat() { 33 assertArraysEqual(new String[]{"deu", "DEU", ""}, 34 TtsEngines.toOldLocaleStringFormat(new Locale("de", "DE"))); 35 assertArraysEqual(new String[]{"deu", "", ""}, 36 TtsEngines.toOldLocaleStringFormat(new Locale("de"))); 37 assertArraysEqual(new String[]{"eng", "", ""}, 38 TtsEngines.toOldLocaleStringFormat(new Locale("en"))); 39 assertArraysEqual(new String[]{"eng", "USA", ""}, 40 TtsEngines.toOldLocaleStringFormat(new Locale("foo"))); 41 } 42 testNormalizeLocale()43 public void testNormalizeLocale() { 44 assertEquals(Locale.UK, 45 TtsEngines.normalizeTTSLocale(new Locale("eng", "gbr"))); 46 assertEquals(Locale.UK, 47 TtsEngines.normalizeTTSLocale(new Locale("eng", "GBR"))); 48 assertEquals(Locale.GERMANY, 49 TtsEngines.normalizeTTSLocale(new Locale("deu", "deu"))); 50 assertEquals(Locale.GERMAN, 51 TtsEngines.normalizeTTSLocale(new Locale("deu"))); 52 assertEquals(new Locale("yyy", "DE"), 53 TtsEngines.normalizeTTSLocale(new Locale("yyy", "DE"))); 54 } 55 testGetLocalePrefForEngine()56 public void testGetLocalePrefForEngine() { 57 assertEquals(new Locale("en", "US"), 58 mTtsHelper.getLocalePrefForEngine("foo","foo:en-US")); 59 assertEquals(new Locale("en", "US"), 60 mTtsHelper.getLocalePrefForEngine("foo","foo:eng-usa")); 61 assertEquals(new Locale("en", "US"), 62 mTtsHelper.getLocalePrefForEngine("foo","foo:eng_USA")); 63 assertEquals(new Locale("de", "DE"), 64 mTtsHelper.getLocalePrefForEngine("foo","foo:deu-deu")); 65 assertEquals(Locale.getDefault(), 66 mTtsHelper.getLocalePrefForEngine("foo","foo:,bar:xx")); 67 assertEquals(Locale.getDefault(), 68 mTtsHelper.getLocalePrefForEngine("other","foo:,bar:xx")); 69 } 70 assertArraysEqual(String[] expected, String[] actual)71 private void assertArraysEqual(String[] expected, String[] actual) { 72 assertEquals("array length", expected.length, actual.length); 73 for (int i = 0; i < expected.length; i++) { 74 assertEquals("index " + i, expected[i], actual[i]); 75 } 76 } 77 }