| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.reecedunn.espeak.test; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.content.Intent; |
| 21 |
import android.speech.tts.TextToSpeech; |
| 22 |
import android.test.ActivityUnitTestCase; |
| 23 |
|
| 24 |
import com.reecedunn.espeak.CheckVoiceData; |
| 25 |
|
| 26 |
import java.lang.reflect.Field; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.HashSet; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 32 |
import static org.hamcrest.Matchers.*; |
| 33 |
|
| 34 |
public class CheckVoiceDataTest extends ActivityUnitTestCase<CheckVoiceData> |
| 35 |
{ |
| 36 |
Field mResultCode; |
| 37 |
Field mResultData; |
| 38 |
|
| 39 |
public void setUp() throws Exception |
| 40 |
{ |
| 41 |
super.setUp(); |
| 42 |
|
| 43 |
mResultCode = Activity.class.getDeclaredField("mResultCode"); |
| 44 |
mResultCode.setAccessible(true); |
| 45 |
|
| 46 |
mResultData = Activity.class.getDeclaredField("mResultData"); |
| 47 |
mResultData.setAccessible(true); |
| 48 |
} |
| 49 |
|
| 50 |
public int getResultCode() throws IllegalAccessException |
| 51 |
{ |
| 52 |
return (Integer)mResultCode.get(getActivity()); |
| 53 |
} |
| 54 |
|
| 55 |
public Intent getResultData() throws IllegalAccessException |
| 56 |
{ |
| 57 |
return (Intent)mResultData.get(getActivity()); |
| 58 |
} |
| 59 |
|
| 60 |
public CheckVoiceDataTest() |
| 61 |
{ |
| 62 |
super(CheckVoiceData.class); |
| 63 |
} |
| 64 |
|
| 65 |
public Set<String> getExpectedVoices() |
| 66 |
{ |
| 67 |
Set<String> expected = new HashSet<String>(); |
| 68 |
for (VoiceData.Voice voice : VoiceData.voices) |
| 69 |
{ |
| 70 |
expected.add(voice.locale); |
| 71 |
} |
| 72 |
return expected; |
| 73 |
} |
| 74 |
|
| 75 |
public void testUnavailableVoices() throws IllegalAccessException |
| 76 |
{ |
| 77 |
Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); |
| 78 |
startActivity(intent, null, null); |
| 79 |
|
| 80 |
assertThat(getActivity(), is(notNullValue())); |
| 81 |
assertThat(isFinishCalled(), is(true)); |
| 82 |
assertThat(getResultCode(), is(TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)); |
| 83 |
assertThat(getResultData(), is(notNullValue())); |
| 84 |
|
| 85 |
Intent data = getResultData(); |
| 86 |
|
| 87 |
ArrayList<String> unavailable = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES); |
| 88 |
assertThat(unavailable, is(notNullValue())); |
| 89 |
assertThat(unavailable.toString(), is("[]")); |
| 90 |
} |
| 91 |
|
| 92 |
public void testAvailableVoicesAdded() throws IllegalAccessException |
| 93 |
{ |
| 94 |
Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); |
| 95 |
startActivity(intent, null, null); |
| 96 |
|
| 97 |
assertThat(getActivity(), is(notNullValue())); |
| 98 |
assertThat(isFinishCalled(), is(true)); |
| 99 |
assertThat(getResultCode(), is(TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)); |
| 100 |
assertThat(getResultData(), is(notNullValue())); |
| 101 |
|
| 102 |
Intent data = getResultData(); |
| 103 |
|
| 104 |
ArrayList<String> available = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES); |
| 105 |
assertThat(available, is(notNullValue())); |
| 106 |
|
| 107 |
Set<String> expected = getExpectedVoices(); |
| 108 |
|
| 109 |
Set<String> added = new HashSet<String>(); |
| 110 |
for (String voice : available) |
| 111 |
{ |
| 112 |
if (!expected.contains(voice)) |
| 113 |
{ |
| 114 |
added.add(voice); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
assertThat(added.toString(), is("[]")); |
| 119 |
} |
| 120 |
|
| 121 |
public void testAvailableVoicesRemoved() throws IllegalAccessException |
| 122 |
{ |
| 123 |
Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); |
| 124 |
startActivity(intent, null, null); |
| 125 |
|
| 126 |
assertThat(getActivity(), is(notNullValue())); |
| 127 |
assertThat(isFinishCalled(), is(true)); |
| 128 |
assertThat(getResultCode(), is(TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)); |
| 129 |
assertThat(getResultData(), is(notNullValue())); |
| 130 |
|
| 131 |
Intent data = getResultData(); |
| 132 |
|
| 133 |
ArrayList<String> available = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES); |
| 134 |
assertThat(available, is(notNullValue())); |
| 135 |
|
| 136 |
Set<String> expected = getExpectedVoices(); |
| 137 |
|
| 138 |
Set<String> removed = new HashSet<String>(); |
| 139 |
for (String voice : expected) |
| 140 |
{ |
| 141 |
if (!available.contains(voice)) |
| 142 |
{ |
| 143 |
removed.add(voice); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
assertThat(removed.toString(), is("[]")); |
| 148 |
} |
| 149 |
} |