我有一个测试用例,它使用Instrumentation.ActivityMonitor来检查是否发送了一个Intent。测试成功时,此工作正常。如果断言失败,则下一个测试用例挂在setUp()中的getActivity()调用上。Android ActivityMonitor失败导致getActivity()永远不会返回
我应该打个电话来清理一下吗?
它似乎围绕着一个Activity启动,但ActivityMonitor没有捕获它。也就是说,IntentFilter没有被触发。测试失败,但新的Activity从不解散,似乎干扰下一次getActivity()调用。
这个问题是similar to another question,但这里的解决方案(调用super.tearDown())并没有解决我的问题。
public class SimpleActivityTest
extends ActivityInstrumentationTestCase2<SimpleActivity> {
private SimpleActivity activity;
@Override
protected void setUp() throws Exception {
super.setUp();
this.getInstrumentation().setInTouchMode(false);
Intent intent = new Intent();
intent.putExtra("DATA_ITEM_1", 1);
intent.putExtra("DATA_ITEM_2", 2);
this.setActivityIntent(intent);
this.activity = getActivity(); // this call hangs on second test
}
public void testOtherActivityCalled() {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_VIEW);
ifilter.addDataScheme("http");
ifilter.addDataAuthority("some.domain.com", null);
ifilter.addDataPath("foobar", PatternMatcher.PATTERN_PREFIX);
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(
ifilter, null, false);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// launch other activity somehow
}
});
getInstrumentation().waitForIdleSync();
Activity otherActivity = activityMonitor.waitForActivityWithTimeout(2000);
assertNotNull(otherActivity);
otherActivity.finish();
}
public void testSomethingElse() {
// This code will never run because getActivity() in setUp() will
// never return
}
}
你解决了这个问题吗?我有同样的,不知道如何解决它:( – dexxtr 2014-12-26 11:52:03