意式浓缩咖啡与我的 ACTION_SEND 意式不符

Ric*_*ard 5 android android-intent android-testing android-espresso

我有一个来自我创建的活动的意图,如下所示:

    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)
    }
Run Code Online (Sandbox Code Playgroud)

当我单击工具栏上的图标时,该功能就会运行。我想对其运行 Espresso UI 测试,以检查启动的活动是否确实符合上述意图。为此,我只是首先检查意图是否实际上是上述意图,如下所示:

    @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()
    }
Run Code Online (Sandbox Code Playgroud)

但是,这会返回一个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) }}]])

Run Code Online (Sandbox Code Playgroud)

我怎样才能使测试符合意图?从错误信息看来,两者已经相同了。

Ski*_*ᴉʞS 5

问题是你需要添加一个CHOOSERas :

fun chooser(matcher: Matcher<Intent>): Matcher<Intent> {
      return allOf(
          hasAction(Intent.ACTION_CHOOSER),
          hasExtra(`is`(Intent.EXTRA_INTENT), matcher))
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

intended(chooser(expectedIntent))

Run Code Online (Sandbox Code Playgroud)

创建一个像这样的变量来匹配你的 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")
        )
Run Code Online (Sandbox Code Playgroud)

然后将您的测试更改为:

@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()
    }
Run Code Online (Sandbox Code Playgroud)