我尝试在 Android Espresso 上使用
Matcher<Intent>
来模拟以下内容:
Intent { act=android.intent.action.CHOOSER (has extras) } handling packages:[[android, com.android.systemui]], extras:
[Bundle[{android.intent.extra.CHOSEN_COMPONENT_INTENT_SENDER=IntentSender{531e378:
android.os.BinderProxy@5c9a51},
android.intent.extra.INTENT=Intent { act=android.intent.action.SEND typ=text/plain flg=0x1 clip={text/plain {T(148)}} (has extras) },
android.intent.extra.TITLE=}]]
如
Matcher<Intent> intentMatcher = allOf(hasExtra(Intent.EXTRA_INTENT,
hasAction(Intent.ACTION_SEND)),
hasType(MIME_TYPE_TEXT_PLAIN),
hasExtra(Intent.EXTRA_TITLE, ""));
Matcher<Intent> expected = allOf(hasAction(Intent.ACTION_CHOOSER),
hasExtra(Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER, intentMatcher));
intended(expected);
但是这是错误的,我错过了什么吗?
尝试将
hasExtra("Key", Matcher<?>)
替换为 hasExtra(is("Key"), Matcher<?>)
Matcher<Intent> intentMatcher = allOf(hasExtra(is(Intent.EXTRA_INTENT),
hasAction(Intent.ACTION_SEND)),
hasType(MIME_TYPE_TEXT_PLAIN),
hasExtra(Intent.EXTRA_TITLE, ""));
Matcher<Intent> expected = allOf(hasAction(Intent.ACTION_CHOOSER),
hasExtra(is(Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER), intentMatcher));
intended(expected);
说明:
我依稀记得有类似的问题。我认为这与
hasExtra
有(太多)重载有关。
当你这样做时:
hasExtra("StringKey", intentMatcher)
您有理由认为 Expresso 会根据 extra 的值调用
intentMatcher.matches()
并确定它是否匹配。
然而,由于泛型的乐趣,以及前面提到的多重重载,
hasExtra(String, Matcher<Intent>)
不解析为hasExtra(String, Matcher