Espresso test for Speech input into editText using SpeechRecognizer API

Issue

I am testing a basic app using Espresso. The mainActivity has an edit text and a voice Input button. The XML file is as follows:

<?xml version"1.0" encoding"utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android"http://schemas.android.com/apk/res/android"
    xmlns:app"http://schemas.android.com/apk/res-auto"
    xmlns:tools"http://schemas.android.com/tools"
    android:layout_width"match_parent"
    android:layout_height"match_parent"
    tools:context".MainActivity">

    <androidx.constraintlayout.widget.Group
        android:id"@+id/group"
        android:layout_width"wrap_content"
        android:layout_height"wrap_content" />

    <EditText
        android:id"@+id/textMessage"
        android:layout_width"0dp"
        android:layout_height"wrap_content"
        android:layout_marginStart"16dp"
        android:layout_marginTop"16dp"
        android:ems"10"
        android:hint"@string/edit_message"
        android:inputType"textPersonName"
        app:layout_constraintEnd_toStartOf"@+id/buttonSend"
        app:layout_constraintHorizontal_bias"0.5"
        app:layout_constraintStart_toStartOf"parent"
        app:layout_constraintTop_toTopOf"parent" />

    <Button
        android:id"@+id/button2"
        style"@style/Widget.AppCompat.Button.Colored"
        android:layout_width"wrap_content"
        android:layout_height"wrap_content"
        android:layout_marginStart"16dp"
        android:layout_marginTop"24dp"
        android:onClick"voiceInput"
        android:text"@string/button_voice"
        app:layout_constraintStart_toStartOf"parent"
        app:layout_constraintTop_toBottomOf"@+id/textMessage" />

</androidx.constraintlayout.widget.ConstraintLayout>

On clicking the button, text via SpeechRecognition is input using the following code:

private static final int SPEECH_REQUEST_CODE  0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent  new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    // This starts the activity and populates the intent with the speech text.
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

    // This callback is invoked when the Speech Recognizer returns.
    // This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    if (requestCode  SPEECH_REQUEST_CODE && resultCode  RESULT_OK) {
        List<String> results  data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        String spokenText  results.get(0);
        EditText editText  (EditText) findViewById(R.id.textMessage);
        editText.setText(spokenText);
    }
    super.onActivityResult(requestCode, resultCode, data);
}

public void voiceInput(View view)
{
    displaySpeechRecognizer();
}

Now, using Espresso, I want to test if the user-provided voice input is correctly entered in the editText field.

My Attempt:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest{
    @Rule
    public IntentsTestRule<MainActivity> intentsTestRule 
        new IntentsTestRule<>(MainActivity.class);
    @Test
    public void clickInput_sendsSpeakIntentAndDisplaysResults() {
        // stub intent returning recognition results
        intending(hasAction(RecognizerIntent.ACTION_RECOGNIZE_SPEECH))
                .respondWith(new ActivityResult(Activity.RESULT_OK,
                        new Intent().putExtra(RecognizerIntent.EXTRA_RESULTS, "I am uttering.")));

        // when clicking on the buttonVoice, thus opening the dialog and starting the speechRecognizer
        onView(withId(R.id.button2))
                .perform(click());

        // expect the recognition results to be displayed
        onView(withId(R.id.textMessage))
                .check(matches(withText("I am uttering.")));
    }



}

However this is not working.

Solution

You are using espresso-intents and looks good but try using putStringArrayListExtra and passing an ArrayList as that is what onActivityResult expects to receive:

ArrayList<String> resultExtras  new ArrayList<>();
resultExtras.add("I am uttering.");
intending(hasAction(RecognizerIntent.ACTION_RECOGNIZE_SPEECH))
                .respondWith(new ActivityResult(Activity.RESULT_OK,
                        new Intent().putStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS, resultExtras)));

Answered By – jeprubio

Leave a Comment