Debian Codegraph @

android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTestCase.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2012-2015 Reece H. Dunn
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.reecedunn.espeak.test;
18
19 import android.os.Build;
20 import android.speech.tts.TextToSpeech;
21 import android.speech.tts.TextToSpeech.OnInitListener;
22 import android.test.AndroidTestCase;
23 import android.util.Log;
24
25 import java.util.Locale;
26
27 import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.Matchers.*;
30
31 public class TextToSpeechTestCase extends AndroidTestCase
32 {
33 private TextToSpeech mEngine = null;
34 private boolean mInitialised = false;
35 private int mStatus = TextToSpeech.ERROR;
36
37 private OnInitListener mInitCallback = new OnInitListener()
38 {
39 @Override
40 public void onInit(int status)
41 {
42 mStatus = status;
43 mInitialised = true;
44 }
45 };
46
47 @Override
48 public void setUp() throws Exception
49 {
50 try
51 {
52 // Wait until the text-to-speech engine is initialised (max: 20 seconds):
53
54 mEngine = new TextToSpeech(getContext(), mInitCallback);
55 for (int count = 0; !mInitialised && count < (4 * 20); ++count)
56 {
57 Thread.sleep(250);
58 }
59
60 assertThat(mInitialised, is(true));
61 assertThat(mStatus, is(TextToSpeech.SUCCESS));
62 assertThat(mEngine.getDefaultEngine(), is("com.reecedunn.espeak"));
63
64 // Ensure that the voice data is installed (max: 20 seconds):
65
66 Locale en = new Locale("en");
67 int available = mEngine.isLanguageAvailable(en);
68 for (int count = 0; available != TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE && count < (4 * 20); ++count)
69 {
70 Thread.sleep(250);
71 available = mEngine.isLanguageAvailable(en);
72 Log.d("TextToSpeechTestCase", "setUp: available = " + available);
73 }
74
75 assertThat(available, isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
76 }
77 catch (Exception e)
78 {
79 tearDown();
80 throw e;
81 }
82 }
83
84 @Override
85 public void tearDown()
86 {
87 if (mEngine != null)
88 {
89 mEngine.shutdown();
90 mEngine = null;
91 }
92 }
93
94 public TextToSpeech getEngine()
95 {
96 return mEngine;
97 }
98
99 @SuppressWarnings("deprecation")
100 public static Locale getLanguage(TextToSpeech engine) {
101 if (engine != null) {
102 return engine.getLanguage();
103 }
104 return null;
105 }
106 }