1 /* 2 * Copyright (C) 2019 The Android Open Source Project 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.android.car.settings.testutils; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.speech.tts.TextToSpeech; 22 import android.speech.tts.Voice; 23 24 import org.robolectric.annotation.Implementation; 25 import org.robolectric.annotation.Implements; 26 import org.robolectric.annotation.Resetter; 27 28 import java.util.Locale; 29 30 @Implements(TextToSpeech.class) 31 public class ShadowTextToSpeech { 32 33 private static TextToSpeech sInstance; 34 private static TextToSpeech.OnInitListener sOnInitListener; 35 private static String sEngine; 36 setInstance(TextToSpeech textToSpeech)37 public static void setInstance(TextToSpeech textToSpeech) { 38 sInstance = textToSpeech; 39 } 40 41 /** 42 * Override constructor and only store the name of the last constructed engine and init 43 * listener. 44 */ __constructor__(Context context, TextToSpeech.OnInitListener listener, String engine, String packageName, boolean useFallback)45 public void __constructor__(Context context, TextToSpeech.OnInitListener listener, 46 String engine, 47 String packageName, boolean useFallback) { 48 sOnInitListener = listener; 49 sEngine = engine; 50 } 51 __constructor__(Context context, TextToSpeech.OnInitListener listener, String engine)52 public void __constructor__(Context context, TextToSpeech.OnInitListener listener, 53 String engine) { 54 __constructor__(context, listener, engine, null, false); 55 } 56 __constructor__(Context context, TextToSpeech.OnInitListener listener)57 public void __constructor__(Context context, TextToSpeech.OnInitListener listener) { 58 __constructor__(context, listener, null, null, false); 59 } 60 61 @Implementation getCurrentEngine()62 protected String getCurrentEngine() { 63 return sInstance.getCurrentEngine(); 64 } 65 66 @Implementation setLanguage(final Locale loc)67 protected int setLanguage(final Locale loc) { 68 return sInstance.setLanguage(loc); 69 } 70 71 @Implementation shutdown()72 protected void shutdown() { 73 sInstance.shutdown(); 74 } 75 76 @Implementation setSpeechRate(float speechRate)77 protected int setSpeechRate(float speechRate) { 78 return sInstance.setSpeechRate(speechRate); 79 } 80 81 @Implementation setPitch(float pitch)82 protected int setPitch(float pitch) { 83 return sInstance.setPitch(pitch); 84 } 85 86 @Implementation getVoice()87 protected Voice getVoice() { 88 return sInstance.getVoice(); 89 } 90 91 @Implementation isLanguageAvailable(final Locale loc)92 protected int isLanguageAvailable(final Locale loc) { 93 return sInstance.isLanguageAvailable(loc); 94 } 95 96 @Implementation speak(final CharSequence text, final int queueMode, final Bundle params, final String utteranceId)97 protected int speak(final CharSequence text, 98 final int queueMode, 99 final Bundle params, 100 final String utteranceId) { 101 return sInstance.speak(text, queueMode, params, utteranceId); 102 } 103 104 @Resetter reset()105 public static void reset() { 106 sInstance = null; 107 sOnInitListener = null; 108 sEngine = null; 109 } 110 111 /** Check for the last constructed engine name. */ getLastConstructedEngine()112 public static String getLastConstructedEngine() { 113 return sEngine; 114 } 115 116 /** Trigger the initializtion callback given the input status. */ callInitializationCallbackWithStatus(int status)117 public static void callInitializationCallbackWithStatus(int status) { 118 sOnInitListener.onInit(status); 119 } 120 } 121