浓缩咖啡如何测试活动是否结束? [英] Espresso how to test if Activity is finished?
本文介绍了浓缩咖啡如何测试活动是否结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想断言,当执行某些操作时,我当前正在测试的Activty已完成。不幸的是,到目前为止,我只是通过在测试结束时添加一些睡眠来断言这一点。有没有更好的办法?
import android.content.Context;
import android.os.Build;
import android.support.test.rule.ActivityTestRule;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertTrue;
@SuppressWarnings("unchecked")
@RunWith(JUnit4.class)
@LargeTest
public class MyActivityTest {
Context context;
@Rule
public ActivityTestRule<MyActivity> activityRule
= new ActivityTestRule(MyActivity.class, true, false);
@Before
public void setup() {
super.setup();
// ...
}
@Test
public void finishAfterSomethingIsPerformed() throws Exception {
activityRule.launchActivity(MyActivity.createIntent(context));
doSomeTesting();
activityRule.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
fireEventThatResultsInTheActivityToFinishItself();
}
});
Thread.sleep(2000); // this is needed :(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
assertTrue(activityRule.getActivity().isDestroyed());
}
}
}
推荐答案
在我的情况下,我可以测试isFinishing()
:
assertTrue(activityTestRule.getActivity().isFinishing());
而不是:
Thread.sleep(2000); // this is needed :(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
assertTrue(activityRule.getActivity().isDestroyed());
}
isFinishing()
的另一个优点是,您不需要版本检查。
这篇关于浓缩咖啡如何测试活动是否结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文