如何使用Espresso存根Intent.createChooser Intent [英] How to stub Intent.createChooser Intent using Espresso

查看:76
本文介绍了如何使用Espresso存根Intent.createChooser Intent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我的应用程序中有一个图像,并且正在将其共享给其他任何可以处理图像共享的应用程序,并且该功能正常运行.

I have an image inside my app, and am sharing it to any other app that can handle image sharing, and the feature is working perectlty.

我正在编写一个Espresso UI测试以拦截意图,并确保它具有正确的操作和其他功能,但似乎无法使其起作用.

I am writing an Espresso UI test to intercept the intent and ensure it has the correct action and extras, but cannot seem to get it to work.

代码

这是创建意图时的代码:

Here is the code when creating the intent:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType(MediaType.PNG.toString());
startActivity(Intent.createChooser(intent, "send");

这是我尝试在测试中匹配Intent的尝试,但未找到匹配项:

and here is my attempt to match the Intent in my test, but fails to find a match:

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();

错误

IntentMatcher :(具有操作:为"android.intent.action.CHOOSER",并具有其他功能:与以下内容捆绑在一起:key:为"android.intent.extra.STREAM"值:为"[[我的uri出现了这里])

其他信息

在调试时,创建的意图会导致动作为"android.intent.action.CHOOSER"的意​​图,并具有意图类型的附加内容,动作为"android.intent.action.SEND",并键入"image"/png",然后又有一个带有uriString的HierarchicalUri.

When debugging, the intent that is created results in an intent with action "android.intent.action.CHOOSER", and has an extra of type Intent, with action "android.intent.action.SEND" and type "image/png", and in turn has an extra, a HierarchicalUri with a uriString.

摘要

有人知道我在做什么错吗?我找不到一种将所有这些结合在一起并为此目的创建匹配器的方法.任何帮助将不胜感激!

Anyone know what I am doing wrong? I can't find a way to tie all this together and create a matcher for this intent. Any help would be greatly appreciated!

推荐答案

如果由于意图不匹配而在此行上出现错误 intended(expectedIntent),可能是因为 Intent.createChooser 使用键 Intent.EXTRA_INTENT 将您的意图作为额外的数据.在您的情况下,您只需要选择器一个额外的意图匹配器即可:

If you're getting an error on this line intended(expectedIntent) because the intent didn't match, it's probably because Intent.createChooser put your intent as an extra data with the key Intent.EXTRA_INTENT. In your case, you only need an extra intent matcher for the chooser:

Matcher<Intent> intent = allOf(
    hasAction(Intent.ACTION_SEND),
    hasExtra(Intent.EXTRA_STREAM, EXPECTED_SHARE_URI)
);

Matcher<Intent> expectedIntent = allOf(
    hasAction(Intent.ACTION_CHOOSER),
    // Intent.createChooser put your intent with the key EXTRA_INTENT
    hasExtra(Intent.EXTRA_INTENT, intent)
);

intending(anyIntent()).respondWith(new Instrumentation.ActivityResult(0, null));

MyScreen.clickShareButton();

intended(expectedIntent);

这篇关于如何使用Espresso存根Intent.createChooser Intent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆