PLN*_*ech 6 android speech-recognition android-intent android-testing android-espresso
我正在向应用程序添加仪器测试。我想测试围绕语音识别的交互:
R.id.buttonVoice),显示DialogFragment用于语音识别的 SpeechRecognizer踢入,聆听用户输入 TextView( R.id.results)使用Espresso-Intents 进行仪器测试听起来是确保我的测试不依赖于系统的SpeechRecognizer.
这是我的测试,它与intended()存根意图进行相同的交互:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityWithPermissionTest {
@Rule
public IntentsTestRule intentsTestRule = new IntentsTestRule<>(MainActivity.class);
@Test
public void clickInput_sendsSpeakIntentAndDisplaysResults() {
// stub intent returning recognition results
intending(hasAction(equalTo(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)))
.respondWith(new ActivityResult(Activity.RESULT_OK, new Intent().putExtra(
SpeechRecognizer.RESULTS_RECOGNITION, new String[]{"I am uttering."})));
// when clicking on the buttonVoice, thus opening the dialog and starting the speechRecognizer
onView(withId(R.id.buttonVoice))
.perform(click());
// expect the recognition results to be displayed
onView(withId(R.id.results))
.check(matches(withText("I am uttering.")));
}
}
Run Code Online (Sandbox Code Playgroud)
但是,此测试失败并出现错误 'with text: is "I am uttering."' doesn't match the selected view.
事实上,当我查看设备上发生的检测时,我看到真实SpeechRecognizer被触发(并且在模拟器上运行时失败)。所以看起来我的意图存根不起作用。
RecognizerIntent?是否有另一种方法来测试交互SpeechRecognizer而不让您的测试依赖于真实的语音识别?