我正在为启动设备默认邮件客户端的活动编写JUnit测试。我想验证“发送到”活动是否已启动,然后将“点击”事件发送到“发送”按钮。
我确实设置了一个带有intent过滤器的ActivityMonitor,以便获得有关“Send To”活动的参考。我可以看到在运行测试时出现了发送邮件活动,但不幸的是监视器永远不会被点击。
以下是尝试查找“发送至”活动的单元测试代码:
// register activity monitor for the send mail activity
Instrumentation instrumentation = getInstrumentation();
IntentFilter filter = new IntentFilter(Intent.ACTION_SENDTO);
ActivityMonitor monitor = instrumentation.addMonitor(filter, null, false);
// click on the "Send Feedback" button (use Robotium here)
solo.clickOnButton(0);
// wait for the send mail activity to start
Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5000);
assertNotNull(currentActivity);
以下是应用程序中“发送到”活动的启动方式:
Uri uri = Uri.parse("mailto:address@mail.com");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_SUBJECT, "Message Title");
i.putExtra(Intent.EXTRA_TEXT, "Hello");
startActivity(i);
意图过滤器是否设置错误?或者是否无法监控项目中未定义的活动?
感谢您的帮助。
答案 0 :(得分:2)
我也有这方面的问题,事实证明ActivityMonitor不起作用,因为Robotium的独奏安装了自己的显示器。此监视器捕获所有意图,因此永远不会调用您的自定义监视器。
我最终做的是首先移除独奏显示器,插入我自己的,然后重新添加独奏显示器,以便首先匹配显示器。您的代码看起来像这样:
// register activity monitor for the send mail activity
Instrumentation instrumentation = getInstrumentation();
IntentFilter filter = new IntentFilter(Intent.ACTION_SENDTO);
ActivityMonitor monitor = new ActivityMonitor(filter, null, false);
ActivityMonitor soloMonitor = solo.getActivityMonitor();
// Remove the solo monitor, so your monitor is first on the list.
instrumentation.removeMonitor(soloMonitor);
// add your own monitor.
instrumentation.addMonitor(monitor);
// Re-add the solo monitor
instrumentation.addMonitor(soloMonitor);
// click on the "Send Feedback" button (use Robotium here)
solo.clickOnButton(0);
// wait for the send mail activity to start
Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5000);
assertNotNull(currentActivity);
作为旁注:当启动另一个活动(邮件应用程序)时,您将失去对测试环境的控制,因此使监视器阻塞(即new ActivityMonitor(filter, null, true);
)
答案 1 :(得分:0)
我的测试遇到了同样的问题,这对我有用。
此时您的测试工作正常,但您的显示器不知道预期的数据,因此您的意图过滤器没有捕获任何内容。
// register activity monitor for the send mail activity
Instrumentation instrumentation = getInstrumentation();
IntentFilter filter = new IntentFilter(Intent.ACTION_SENDTO);
//Here you just need to say to your intent filter what exactly to look for
filter.addDataType(CommonDataKinds.Email.CONTENT_TYPE);
ActivityMonitor monitor = instrumentation.addMonitor(filter, null, true);
// click on the "Send Feedback" button (use Robotium here)
solo.clickOnButton(0);
// wait for the send mail activity to start
Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5000);
assertNotNull(currentActivity);
如果您这样做是为了发送短信,那么该行将是:
filter.addDataScheme(Constants.SCHEME_SMS);