使用 SpeechRecognizer API 对 editText 中的语音输入进行 Espresso 测试

Espresso test for Speech input into editText using SpeechRecognizer API

我正在使用 Espresso 测试一个基本的应用程序。 mainActivity 有一个编辑文本和一个语音输入按钮。 XML文件如下:

<?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>

点击按钮后,使用以下代码通过 SpeechRecognition 输入文本:

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();
}

现在,使用 Espresso,我想测试是否在 editText 字段中正确输入了用户提供的语音输入。

我的尝试:

@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.")));
    }



}

但是这不起作用。

您正在使用 espresso-intents 并且看起来不错,但请尝试使用 putStringArrayListExtra 并传递一个 ArrayList,因为这是 onActivityResult 期望收到的:

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)));