如何检查ACTION_SEND意图开始了吗? [英] How to check ACTION_SEND intent is started?

查看:218
本文介绍了如何检查ACTION_SEND意图开始了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用的文字标准的简单共享。在我的测试中,我想检查我的活动开始共享意图。这可能吗?

我使用 ActivityInstrumentationTestCase2 测试。

活动:

 最终意图sendIntent =新意图();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,消息);
sendIntent.setType(text / plain的);
startActivity(Intent.createChooser(sendIntent,「股份」));

测试:

 最后的IntentFilter的IntentFilter =新的IntentFilter();
intentFilter.addAction(Intent.ACTION_SEND);
intentFilter.addDataType(text / plain的);
最后活动监视器receiverActivityMonitor = getInstrumentation()。添加监视器(
            IntentFilter的,空,假);TouchUtils.clickView(这一点,getActivity()findViewById(R.id.share_button));
最后活动shareActivity = receiverActivityMonitor.waitForActivityWithTimeout(500);
assertNotNull(shareActivity); //失败

上述试验不起作用。有没有一种方法来测试ACTION_SEND意图已经启动?

临时解决方法

现在,在活动我现在的储蓄意向的成员变量:

  mSendIntent =新意图();

所以我可以从试验验证:

 的assertEquals(android.intent.action.SEND,getActivity()mSharingIntent.getAction());
的assertEquals(text / plain的,getActivity()mSharingIntent.getType());
字符串sharedText = getActivity()mSharingIntent.getStringExtra(Intent.EXTRA_TEXT)。
的assertEquals(测试我共享,sharedText);


解决方案

您逝去的 Intent.createChooser 的结果 startActivity ,所以你需要监控 Intent.ACTION_CHOOSER 操作。

 最后的IntentFilter的IntentFilter =新的IntentFilter();
intentFilter.addAction(Intent.ACTION_CHOOSER);
最后活动监视器receiverActivityMonitor = getInstrumentation()添加监视器(IntentFilter的,空,假的)。TouchUtils.clickView(这一点,getActivity()findViewById(R.id.share_button));
最后活动shareActivity = receiverActivityMonitor.waitForActivityWithTimeout(500);
assertNotNull(shareActivity);

My app uses standard simple sharing of text. In my test I want to check that my activity started the sharing intent. Is it possible?

I am using ActivityInstrumentationTestCase2 test.

Activity:

final Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Share"));

Test:

final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SEND);
intentFilter.addDataType("text/plain");
final ActivityMonitor receiverActivityMonitor = getInstrumentation().addMonitor(
            intentFilter, null, false);

TouchUtils.clickView(this, getActivity().findViewById(R.id.share_button));
final Activity shareActivity = receiverActivityMonitor.waitForActivityWithTimeout(500);
assertNotNull(shareActivity); // Fails

The above test does not work. Is there a way to test that ACTION_SEND intent has started?

Temporary solution

For now, in activity I am saving intent to a member variable:

mSendIntent = new Intent();

So I can verify it from test:

assertEquals("android.intent.action.SEND", getActivity().mSharingIntent.getAction());
assertEquals("text/plain", getActivity().mSharingIntent.getType());
String sharedText = getActivity().mSharingIntent.getStringExtra(Intent.EXTRA_TEXT);
assertEquals("test I shared", sharedText);

解决方案

You are passing the result of Intent.createChooser to startActivity, so you need to monitor Intent.ACTION_CHOOSER action.

final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_CHOOSER);
final ActivityMonitor receiverActivityMonitor = getInstrumentation().addMonitor(intentFilter, null, false);

TouchUtils.clickView(this, getActivity().findViewById(R.id.share_button));
final Activity shareActivity = receiverActivityMonitor.waitForActivityWithTimeout(500);
assertNotNull(shareActivity);

这篇关于如何检查ACTION_SEND意图开始了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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