我有一個測試用例,它使用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