eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextToSpeechTestCase.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package com.reecedunn.espeak.test;
  17. import android.os.Build;
  18. import android.speech.tts.TextToSpeech;
  19. import android.speech.tts.TextToSpeech.OnInitListener;
  20. import android.test.AndroidTestCase;
  21. import android.util.Log;
  22. import java.util.Locale;
  23. import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
  24. import static org.hamcrest.MatcherAssert.assertThat;
  25. import static org.hamcrest.Matchers.*;
  26. public class TextToSpeechTestCase extends AndroidTestCase
  27. {
  28. private TextToSpeech mEngine = null;
  29. private boolean mInitialised = false;
  30. private int mStatus = TextToSpeech.ERROR;
  31. private OnInitListener mInitCallback = new OnInitListener()
  32. {
  33. @Override
  34. public void onInit(int status)
  35. {
  36. mStatus = status;
  37. mInitialised = true;
  38. }
  39. };
  40. @Override
  41. public void setUp() throws Exception
  42. {
  43. try
  44. {
  45. // Wait until the text-to-speech engine is initialised (max: 20 seconds):
  46. mEngine = new TextToSpeech(getContext(), mInitCallback);
  47. for (int count = 0; !mInitialised && count < (4 * 20); ++count)
  48. {
  49. Thread.sleep(250);
  50. }
  51. assertThat(mInitialised, is(true));
  52. assertThat(mStatus, is(TextToSpeech.SUCCESS));
  53. assertThat(mEngine.getDefaultEngine(), is("com.reecedunn.espeak"));
  54. // Ensure that the voice data is installed (max: 20 seconds):
  55. Locale en = new Locale("en");
  56. int available = mEngine.isLanguageAvailable(en);
  57. for (int count = 0; available != TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE && count < (4 * 20); ++count)
  58. {
  59. Thread.sleep(250);
  60. available = mEngine.isLanguageAvailable(en);
  61. Log.d("TextToSpeechTestCase", "setUp: available = " + available);
  62. }
  63. assertThat(available, isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
  64. }
  65. catch (Exception e)
  66. {
  67. tearDown();
  68. throw e;
  69. }
  70. }
  71. @Override
  72. public void tearDown()
  73. {
  74. if (mEngine != null)
  75. {
  76. mEngine.shutdown();
  77. mEngine = null;
  78. }
  79. }
  80. public TextToSpeech getEngine()
  81. {
  82. return mEngine;
  83. }
  84. @SuppressWarnings("deprecation")
  85. public static Locale getLanguage(TextToSpeech engine) {
  86. if (engine != null) {
  87. return engine.getLanguage();
  88. }
  89. return null;
  90. }
  91. }