Espresso-Intents 是 Espresso 的一个扩展,它可以对被测应用发出的 intent 进行验证和桩接。它类似于 Mockito,但专用于 Android Intent。
如果您的应用将功能委托给其他应用或平台,您可以使用 Espresso-Intents 专注于您自己的应用逻辑,同时假设其他应用或平台能够正常运行。借助 Espresso-Intents,您可以匹配和验证您的传出 intent,甚至提供桩响应来代替实际的 intent 响应。
将 Espresso-Intents 添加到您的项目
在您的应用的 app/build.gradle
文件中,将以下行添加到 dependencies
中
Groovy
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'
Kotlin
androidTestImplementation('androidx.test.espresso:espresso-intents:3.6.1')
Espresso-Intents 仅与 Espresso 2.1+ 和 Android 测试库 0.3+ 版本兼容,因此请务必也更新这些行。
Groovy
androidTestImplementation 'androidx.test:runner:1.6.1' androidTestImplementation 'androidx.test:rules:1.6.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
Kotlin
androidTestImplementation('androidx.test:runner:1.6.1') androidTestImplementation('androidx.test:rules:1.6.1') androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
编写测试规则
在编写 Espresso-Intents 测试之前,请设置 IntentsTestRule
。它是 ActivityTestRule
类的一个扩展,使得在功能性 UI 测试中轻松使用 Espresso-Intents API 成为可能。一个 IntentsTestRule
会在每个使用 @Test
注解的测试之前初始化 Espresso-Intents,并在每次测试运行后释放 Espresso-Intents。
以下代码段是 IntentsTestRule
的示例
Kotlin
@get:Rule val intentsTestRule = IntentsTestRule(MyActivity::class.java)
Java
@Rule public IntentsTestRule<MyActivity> intentsTestRule = new IntentsTestRule<>(MyActivity.class);
匹配
Espresso-Intents 提供了根据特定匹配条件拦截传出 intent 的能力,这些条件是使用 Hamcrest Matchers 定义的。Hamcrest 允许您
- 使用现有 intent 匹配器: 最简单的选项,几乎应该始终优先选择。
- 实现您自己的 intent 匹配器: 最灵活的选项。更多详细信息请参阅 Hamcrest 教程中“编写自定义匹配器”一节。
Espresso-Intents 分别提供用于 intent 验证的 intended()
方法和用于桩接的 intending()
方法。两者都接受一个 Hamcrest Matcher<Intent>
对象作为参数。
以下代码段显示了 intent 验证,它使用现有 intent 匹配器来匹配启动浏览器的传出 intent
Kotlin
assertThat(intent).hasAction(Intent.ACTION_VIEW) assertThat(intent).categories().containsExactly(Intent.CATEGORY_BROWSABLE) assertThat(intent).hasData(Uri.parse("www.google.com")) assertThat(intent).extras().containsKey("key1") assertThat(intent).extras().string("key1").isEqualTo("value1") assertThat(intent).extras().containsKey("key2") assertThat(intent).extras().string("key2").isEqualTo("value2")
Java
assertThat(intent).hasAction(Intent.ACTION_VIEW); assertThat(intent).categories().containsExactly(Intent.CATEGORY_BROWSABLE); assertThat(intent).hasData(Uri.parse("www.google.com")); assertThat(intent).extras().containsKey("key1"); assertThat(intent).extras().string("key1").isEqualTo("value1"); assertThat(intent).extras().containsKey("key2"); assertThat(intent).extras().string("key2").isEqualTo("value2");
验证 intent
Espresso-Intents 记录所有尝试从被测应用启动 Activity 的 intent。使用 intended()
方法(类似于 Mockito.verify()
),您可以断言某个 intent 已经出现。但是,除非您明确配置,否则 Espresso-Intents 不会桩接对 intent 的响应。
以下代码段是一个示例测试,用于验证(但不桩接响应)启动外部“电话”Activity 的传出 intent
Kotlin
@Test fun validateIntentSentToPackage() { // User action that results in an external "phone" activity being launched. user.clickOnView(system.getView(R.id.callButton)) // Using a canned RecordedIntentMatcher to validate that an intent resolving // to the "phone" activity has been sent. intended(toPackage("com.android.phone")) }
Java
@Test public void validateIntentSentToPackage() { // User action that results in an external "phone" activity being launched. user.clickOnView(system.getView(R.id.callButton)); // Using a canned RecordedIntentMatcher to validate that an intent resolving // to the "phone" activity has been sent. intended(toPackage("com.android.phone")); }
桩接
使用 intending()
方法(类似于 Mockito.when()
),您可以为通过 startActivityForResult()
启动的 Activity 提供桩响应。这对于外部 Activity 尤其有用,因为您无法操纵外部 Activity 的用户界面,也无法控制返回给被测 Activity 的 ActivityResult
。
以下代码段实现了一个示例 activityResult_DisplaysContactsPhoneNumber()
测试,该测试验证当用户在被测应用中启动“联系人”Activity 时,联系电话号码是否显示。
构建在启动特定 Activity 时返回的结果。此示例测试会拦截发送到“联系人”的所有 Intent,并使用有效的
ActivityResult
(使用结果代码RESULT_OK
)来桩接它们的响应。Kotlin
val resultData = Intent() val phoneNumber = "123-345-6789" resultData.putExtra("phone", phoneNumber) val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)
Java
Intent resultData = new Intent(); String phoneNumber = "123-345-6789"; resultData.putExtra("phone", phoneNumber); ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);
指示 Espresso 在所有“联系人”intent 调用中提供桩结果对象。
Kotlin
intending(toPackage("com.android.contacts")).respondWith(result)
Java
intending(toPackage("com.android.contacts")).respondWith(result);
验证用于启动 Activity 的操作是否生成了预期的桩结果。在此示例中,该测试检查当“联系人 Activity”启动时,电话号码 "123-345-6789" 是否返回并显示。
Kotlin
onView(withId(R.id.pickButton)).perform(click()) onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)))
Java
onView(withId(R.id.pickButton)).perform(click()); onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)));
这是完整的 activityResult_DisplaysContactsPhoneNumber()
测试
Kotlin
@Test fun activityResult_DisplaysContactsPhoneNumber() { // Build the result to return when the activity is launched. val resultData = Intent() val phoneNumber = "123-345-6789" resultData.putExtra("phone", phoneNumber) val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData) // Set up result stubbing when an intent sent to "contacts" is seen. intending(toPackage("com.android.contacts")).respondWith(result) // User action that results in "contacts" activity being launched. // Launching activity expects phoneNumber to be returned and displayed. onView(withId(R.id.pickButton)).perform(click()) // Assert that the data we set up above is shown. onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber))) }
Java
@Test public void activityResult_DisplaysContactsPhoneNumber() { // Build the result to return when the activity is launched. Intent resultData = new Intent(); String phoneNumber = "123-345-6789"; resultData.putExtra("phone", phoneNumber); ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData); // Set up result stubbing when an intent sent to "contacts" is seen. intending(toPackage("com.android.contacts")).respondWith(result); // User action that results in "contacts" activity being launched. // Launching activity expects phoneNumber to be returned and displayed. onView(withId(R.id.pickButton)).perform(click()); // Assert that the data we set up above is shown. onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber))); }
其他资源
如需了解有关在 Android 测试中使用 Espresso-Intents 的更多信息,请查阅以下资源。
示例
- IntentsBasicSample:
intended()
和intending()
的基本用法。 - IntentsAdvancedSample:模拟用户使用相机获取位图。