Tôi có một trường hợp thử nghiệm sử dụng Instrumentation.ActivityMonitor để kiểm tra xem Intent đã được gửi chưa. Điều này làm việc tốt khi thử nghiệm thành công. Nếu xác nhận không thành công, trường hợp kiểm tra tiếp theo sẽ treo trên lời gọi getActivity() trong setUp().Hoạt động AndroidĐối tượng thất bại gây ra getActivity() không bao giờ trả về
Tôi có phải gọi điện một số phương pháp để dọn dẹp không?
Nó dường như xoay quanh một hoạt động khởi chạy, nhưng ActivityMonitor không bắt nó. Đó là, IntentFilter không được kích hoạt. Kiểm tra không thành công nhưng Hoạt động mới không bao giờ bị loại bỏ và dường như ảnh hưởng đến cuộc gọi getActivity() tiếp theo.
Vấn đề này là similar to another question, nhưng giải pháp này ở đó (gọi super.tearDown()) không khắc phục được sự cố của tôi.
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
}
}
bạn đã giải quyết được sự cố này chưa? Tôi có cùng, và không biết làm thế nào để sửa chữa nó: ( – dexxtr