android - 意式 Espresso 与我的 ACTION_SEND Intent 不匹配

标签 android android-intent android-espresso android-testing

我从我这样创建的 Activity 中获得了一个 Intent :

    private fun startShareIntent() {
        val sendIntent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_TEXT, "Watch ${viewmodel.movie.value?.title} with me!\n\n${viewmodel.movie.value?.summary}")
            type="text/plain"
        }
        val shareIntent = Intent.createChooser(sendIntent, null)
        startActivity(shareIntent)
    }
当我单击工具栏上的图标时,该功能就会运行。我想对其运行 Espresso UI 测试以检查启动的 Activity 是否确实符合上述 Intent 。为此,我只是首先检查 Intent 是否实际上是上述 Intent ,如下所示:
    @Test
    fun test_clicking_share_icon_shows_sharing_sheet() {
        val scenario = activityScenarioRule.scenario
        Intents.init()
        val expectedIntent = allOf(
            hasAction(Intent.ACTION_SEND),
            hasExtra(Intent.EXTRA_TEXT, "Watch ${dummyMovieData.title} with me!\n\n${dummyMovieData.summary}"),
            hasType("text/plain")
        )

        onView(withText(dummyMovieData.title)).perform(click())
        onView(withId(R.id.share_detail)).perform(click())
        intended(expectedIntent)
        Intents.release()
    }
但是,这会给我返回一个 AssertionFailedError :
junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: (has action: is "android.intent.action.SEND" and has extras: has bundle with: key: is "android.intent.extra.TEXT" value: is "Watch Enola Holmes with me!\n\nWhile searching for her missing mother, intrepid teen Enola Holmes uses her sleuthing skills to outsmart big brother Sherlock and help a runaway lord." and has type: is "text/plain")

Matched intents:[]

Recorded intents:
-Intent { act=android.intent.action.CHOOSER (has extras) } handling packages:[[android]], extras:[Bundle[{android.intent.extra.INTENT=Intent { act=android.intent.action.SEND typ=text/plain flg=0x1 clip={text/plain T:Watch Enola Holmes with me!

While searching for her missing mother, intrepid teen Enola Holmes uses her sleuthing skills to outsmart big brother Sherlock and help a runaway lord.} (has extras) }}]])

我怎样才能做到这一点,以便测试可以匹配 Intent ?从错误消息看来,两者已经相同。

最佳答案

问题是您需要添加 CHOOSER作为 :

fun chooser(matcher: Matcher<Intent>): Matcher<Intent> {
      return allOf(
          hasAction(Intent.ACTION_CHOOSER),
          hasExtra(`is`(Intent.EXTRA_INTENT), matcher))
}
然后你可以这样做:
intended(chooser(expectedIntent))

创建一个像这样的变量来匹配你的Intent
private val expectedIntent = Matchers.allOf(
            IntentMatchers.hasAction(Intent.ACTION_SEND),
            IntentMatchers.hasExtra("Your key", "Watch ${dummyMovieData.title} with me!\n\n${dummyMovieData.summary}"),
            IntentMatchers.hasType("text/plain")
        )
然后将您的测试更改为:
@Test
    fun test_clicking_share_icon_shows_sharing_sheet() {
        Intents.init()
        onView(withText(dummyMovieData.title)).perform(click())
        onView(withId(R.id.share_detail)).perform(click())
        intended(expectedIntent)
        Intents.release()
    }

关于android - 意式 Espresso 与我的 ACTION_SEND Intent 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64576388/

相关文章:

android - 有没有办法在 Espresso 测试期间暂停和恢复 Activity ?

android - Espresso 嵌套 ScrollView

android - 为不同的 TextView 使用多个字符串资源

android - 在android中的 Canvas 上旋转图像

android - 通知将应用程序置于最前面而无需更改 Activity

android - 使用 Espresso 我如何检查警报对话框中的项目数

android - 在模拟器上使用 c2dm 获取注册 ID

Android 版本控制和 Git

java - 如何防止 startCommand 在应用程序关闭时返回 null

android - 服务- fragment 通信