1 package com.android.car.messenger.tts; 2 3 import android.content.Context; 4 import android.os.Bundle; 5 import android.speech.tts.TextToSpeech; 6 import android.speech.tts.UtteranceProgressListener; 7 8 import java.util.LinkedList; 9 10 /** 11 * Fake implementation of {@link TTSEngine} for unit-testing. 12 */ 13 class FakeTTSEngine implements TTSEngine { 14 TextToSpeech.OnInitListener mOnInitListener; 15 UtteranceProgressListener mProgressListener; 16 LinkedList<Request> mRequests = new LinkedList<>(); 17 18 @Override 19 public void initialize(Context context, TextToSpeech.OnInitListener initListener) { 20 mOnInitListener = initListener; 21 } 22 23 @Override 24 public boolean isInitialized() { 25 return mOnInitListener != null; 26 } 27 28 @Override 29 public void setOnUtteranceProgressListener(UtteranceProgressListener progressListener) { 30 mProgressListener = progressListener; 31 } 32 33 @Override 34 public int speak(CharSequence text, int queueMode, Bundle params, String utteranceId) { 35 mRequests.add(new Request(text, queueMode, params, utteranceId)); 36 return TextToSpeech.SUCCESS; 37 } 38 39 @Override 40 public void stop() { 41 mRequests.clear(); 42 } 43 44 @Override 45 public boolean isSpeaking() { 46 // NOTE: currently not used in tests. 47 return false; 48 } 49 50 @Override 51 public void shutdown() { 52 stop(); 53 mOnInitListener = null; 54 } 55 56 void startRequest(String utteranceId) { 57 mProgressListener.onStart(utteranceId); 58 } 59 60 void finishRequest(String utteranceId) { 61 removeRequest(utteranceId); 62 mProgressListener.onDone(utteranceId); 63 } 64 65 void interruptRequest(String utteranceId, boolean interrupted) { 66 removeRequest(utteranceId); 67 mProgressListener.onStop(utteranceId, interrupted); 68 } 69 70 void failRequest(String utteranceId, int errorCode) { 71 removeRequest(utteranceId); 72 mProgressListener.onError(utteranceId, errorCode); 73 } 74 75 private void removeRequest(String utteranceId) { 76 mRequests.removeIf((request) -> request.mUtteranceId.equals(utteranceId)); 77 } 78 79 static class Request { 80 CharSequence mText; 81 int mQueueMode; 82 Bundle mParams; 83 String mUtteranceId; 84 85 public Request(CharSequence text, int queueMode, Bundle params, String utteranceId) { 86 mText = text; 87 mQueueMode = queueMode; 88 mParams = params; 89 mUtteranceId = utteranceId; 90 } 91 } 92 } 93