Problem
我的应用程序中有一张图像,并将其共享给任何其他可以处理图像共享的应用程序,并且该功能运行良好。
我正在编写一个 Espresso UI 测试来拦截意图并确保它具有正确的操作和附加功能,但似乎无法让它工作。
Code
这是创建意图时的代码:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType(MediaType.PNG.toString());
startActivity(Intent.createChooser(intent, "send");
这是我在测试中尝试匹配意图的尝试,但未能找到匹配项:
Intents.init();
launchActivity(MyFragment.newIntent(getTargetContext());
Matcher<Intent> expectedIntent = allOf(
hasAction(Intent.ACTION_CHOOSER),
hasExtra(
Intent.ACTION_SEND,
hasExtra(Intent.EXTRA_STREAM, EXPECTED_SHARE_URI) // Expected URI has been copied from the extras 'uriString' value when debugging
)
);
intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null));
MyScreen.clickShareButton(); // performs click on the share button
intended(expectedIntent);
Intents.release();
Error
IntentMatcher: (has action: is "android.intent.action.CHOOSER" and has extras: has bundle with: key: is "android.intent.extra.STREAM" value: is "[my uri appears here]")
附加信息
调试时,创建的 Intent 会产生带有操作“android.intent.action.CHOOSER”的 Intent,并且具有额外的类型 Intent、操作“android.intent.action.SEND”和类型“image/png” ,并且反过来还有一个额外的,一个带有 uriString 的 HierarchicalUri。
Summary
有人知道我做错了什么吗?我找不到一种方法将所有这些联系在一起并为此意图创建一个匹配器。任何帮助将不胜感激!