使用SpeechRecognizer API将语音输入到editText的Espresso测试



我正在使用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.")));
}

}

然而,这并不奏效。

您使用的是意式浓缩咖啡,看起来不错,但尝试使用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)));

相关内容

  • 没有找到相关文章

最新更新