android-如何检查ACTION_SEND意图是否启动?
作者:互联网
我的应用程序使用标准的简单文本共享.在测试中,我想检查我的活动是否启动了共享意图.可能吗?
我正在使用ActivityInstrumentationTestCase2测试.
活动:
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"));
测试:
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
以上测试无效.有没有办法测试ACTION_SEND意图已经开始?
临时解决方案
现在,在活动中,我将意图保存到成员变量中:
mSendIntent = new Intent();
所以我可以从测试中验证它:
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);
解决方法:
您要将Intent.createChooser的结果传递给startActivity,因此您需要监视Intent.ACTION_CHOOSER操作.
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);
标签:android-intent,android-testing,android 来源: https://codeday.me/bug/20191029/1961513.html