Skip to content

Commit

Permalink
Fix for ShadowSpeechRecognizer on Android V
Browse files Browse the repository at this point in the history
With Android V, the internal implementation for SpeechRecognizer has changed. Prepare ShadowSpeechRecognizer to be extended in the future to support V's changed implementations.

PiperOrigin-RevId: 629501622
  • Loading branch information
Googler authored and Copybara-Service committed May 8, 2024
1 parent 3cdc3f1 commit 6a442b7
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -42,6 +43,11 @@ public void setUp() {
supportCallback = new TestRecognitionSupportCallback();
}

@After
public void tearDown() {
speechRecognizer.destroy();
}

@Test
public void onErrorCalled() {
startListening();
Expand Down Expand Up @@ -118,6 +124,7 @@ public void startAndStopListening() {
shadowOf(speechRecognizer).triggerOnResults(new Bundle());
speechRecognizer.stopListening();

shadowOf(getMainLooper()).idle();
assertNoErrorLogs();
}

Expand All @@ -136,6 +143,8 @@ public void startListeningThenDestroyAndStartListening() {
/** Verify the startlistening flow works when using custom component name. */
@Test
public void startListeningWithCustomComponent() {
speechRecognizer.destroy();

speechRecognizer =
SpeechRecognizer.createSpeechRecognizer(
ApplicationProvider.getApplicationContext(),
Expand All @@ -157,6 +166,7 @@ public void getLatestSpeechRecognizer() {
shadowOf(getMainLooper()).idle();
assertThat(ShadowSpeechRecognizer.getLatestSpeechRecognizer())
.isSameInstanceAs(newSpeechRecognizer);
newSpeechRecognizer.destroy();
}

@Test
Expand All @@ -170,6 +180,7 @@ public void getLastRecognizerIntent() {
newSpeechRecognizer.startListening(intent);
shadowOf(getMainLooper()).idle();
assertThat(shadowOf(newSpeechRecognizer).getLastRecognizerIntent()).isEqualTo(intent);
newSpeechRecognizer.destroy();
}

private void startListening() {
Expand Down Expand Up @@ -236,6 +247,7 @@ public void onRmsChanged(float rmsdB) {
@Config(minSdk = TIRAMISU)
@Test
public void onCreateOnDeviceRecognizer_setsLatestSpeechRecognizer() {
speechRecognizer.destroy();
speechRecognizer = SpeechRecognizer.createOnDeviceSpeechRecognizer(applicationContext);

assertThat(speechRecognizer)
Expand Down

0 comments on commit 6a442b7

Please sign in to comment.