Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用浓缩咖啡存根Intent.createChooser Intent_Android_Android Intent_Android Espresso - Fatal编程技术网

Android 如何使用浓缩咖啡存根Intent.createChooser Intent

Android 如何使用浓缩咖啡存根Intent.createChooser Intent,android,android-intent,android-espresso,Android,Android Intent,Android Espresso,问题 我的应用程序中有一个图像,我正在将它共享给任何其他可以处理图像共享的应用程序,并且该功能正在正常工作 我正在编写一个意式浓缩咖啡用户界面测试,以拦截意图并确保其具有正确的操作和附加功能,但似乎无法使其正常工作 代码 以下是创建意图时的代码: Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType(MediaType.PNG.toStri

问题

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

我正在编写一个意式浓缩咖啡用户界面测试,以拦截意图并确保其具有正确的操作和附加功能,但似乎无法使其正常工作

代码

以下是创建意图时的代码:

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();
Intents.init();
启动活动(MyFragment.newIntent(getTargetContext());
匹配器期望值=所有(
hasAction(意图.动作\u选择器),
额外的(
意向.行动(发送),
hasExtra(Intent.EXTRA\u STREAM,EXPECTED\u SHARE\u URI)//调试时已从extras“uriString”值复制了EXPECTED URI
)
);
意图(预期)。响应(新的检测。活动结果(0,null));
MyScreen.clickShareButton();//单击共享按钮
预期的(预期的);
意图。释放();
错误

IntentMatcher:(has action:is“android.intent.action.CHOOSER”和has extra:has bundle with:key:is“android.intent.extra.STREAM”value:is“[我的uri出现在这里]”

其他信息

在调试时,创建的意图会产生一个带有操作“android.intent.action.CHOOSER”的意图,并有一个额外的类型intent,带有操作“android.intent.action.SEND”和类型“image/png”,反过来还有一个额外的带有uriString的层次URI

摘要


有人知道我做错了什么吗?我找不到一种方法将所有这些联系起来,并为此目的创建一个匹配器。如果您在这一行
intentied(expectedent)上遇到错误,将非常感谢任何帮助!

因为意图不匹配,可能是因为
intent.createChooser
使用键
intent.extra\u intent
将您的意图作为额外数据。在您的情况下,您只需要为选择器添加一个额外的意图匹配器:

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);
Matcher intent=allOf(
hasAction(意图、行动和发送),
hasExtra(Intent.EXTRA\u流,预期的\u共享\u URI)
);
匹配器期望值=所有(
hasAction(意图.动作\u选择器),
//Intent.createChooser将您的意图与键EXTRA_Intent放在一起
hasExtra(Intent.EXTRA_Intent,Intent)
);
intenting(anyIntent()).respondWith(new Instrumentation.ActivityResult(0,null));
MyScreen.clickShareButton();
预期的(预期的);

感谢您的帮助,但不幸的是,这并没有解决问题。事实证明,每次使用
hasExtra()
时,都需要将第一个参数(意图类型)包装在
equalTo()
中,所以现在问题已经解决:)是的,哇,我的matcher在同一个问题上花了60分钟,谢谢你,伙计