Skip to content

Commit

Permalink
Merge pull request #6165 from robolectric/piper_351512163
Browse files Browse the repository at this point in the history
Tweak ShadowTextToSpeech OnInitListener API
  • Loading branch information
hoisie committed Jan 22, 2021
2 parents 428b984 + ec54f5a commit a972c9d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.Engine;
import android.speech.tts.UtteranceProgressListener;
Expand Down Expand Up @@ -50,17 +49,17 @@ public void onInitListener_success_getsCalledAsynchronously() {
TextToSpeech.OnInitListener listener = onInitCalled::set;
TextToSpeech textToSpeech = new TextToSpeech(activity, listener);
assertThat(textToSpeech).isNotNull();
Shadows.shadowOf(Looper.getMainLooper()).idle();
Shadows.shadowOf(textToSpeech).getOnInitListener().onInit(TextToSpeech.SUCCESS);
assertThat(onInitCalled.get()).isEqualTo(TextToSpeech.SUCCESS);
}

@Test
public void onInitListener_error_getsCalledSynchronously() {
public void onInitListener_error() {
AtomicReference<Integer> onInitCalled = new AtomicReference<>();
ShadowTextToSpeech.setOnInitStatus(TextToSpeech.ERROR);
TextToSpeech.OnInitListener listener = onInitCalled::set;
TextToSpeech textToSpeech = new TextToSpeech(activity, listener);
assertThat(textToSpeech).isNotNull();
Shadows.shadowOf(textToSpeech).getOnInitListener().onInit(TextToSpeech.ERROR);
assertThat(onInitCalled.get()).isEqualTo(TextToSpeech.ERROR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class ShadowTextToSpeech {
private static final Set<Locale> languageAvailabilities = new HashSet<>();
private static final Set<Voice> voices = new HashSet<>();
private static TextToSpeech lastTextToSpeechInstance;
private static int onInitStatus = TextToSpeech.SUCCESS;

@RealObject private TextToSpeech tts;

Expand Down Expand Up @@ -72,24 +71,9 @@ protected void __constructor__(

@Implementation
protected int initTts() {
// Attempt to model real Android code, where success callbacks occur asynchronously and error
// callbacks occur immediately.
if (listener != null) {
if (onInitStatus == TextToSpeech.SUCCESS) {
new Handler(Looper.getMainLooper()).post(() -> listener.onInit(onInitStatus));
} else {
listener.onInit(onInitStatus);
}
}
return onInitStatus;
}

/**
* Sets the code used by the {@link android.speech.tts.TextToSpeech.OnInitListener} callback
* during initialization. This can test cases where {@link TextToSpeech.ERROR} is used.
*/
public static void setOnInitStatus(int status) {
onInitStatus = status;
// Has to be overridden because the real code attempts to connect to a non-existent TTS
// system service.
return TextToSpeech.SUCCESS;
}

/**
Expand Down Expand Up @@ -282,6 +266,5 @@ public static void reset() {
languageAvailabilities.clear();
voices.clear();
lastTextToSpeechInstance = null;
onInitStatus = TextToSpeech.SUCCESS;
}
}

0 comments on commit a972c9d

Please sign in to comment.