使用测试支持库销毁并重新启动活动 [英] Destroy and restart Activity with Testing Support Library

查看:92
本文介绍了使用测试支持库销毁并重新启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Android中旧的JUnit3风格的测试,我可以执行以下操作来销毁并重新启动活动:

Using the old JUnit3-style tests in Android, I could do the following to destory and restart an activity:

Instrumentation inst = getInstrumentation();
Activity activity = inst.getActivity();
// do something
activity.finish();
Assert.assertTrue(this.activity.isFinishing());
activity = inst.getActivity();
// assert that activity's state is restored

如何使用新的测试支持库来完成相同的工作?我可以使用Espresso和/或UI Automator或新库提供的任何其他机制.

How can I accomplish the same thing using the new Testing Support Library? I'm fine with using either Espresso and/or UI Automator or any other mechanism provided by the new library.

更新:

我尝试了以下操作:

Activity activity = activityTestRule.getActivity();
// do something
activity.finish();
Assert.assertTrue(this.activity.isFinishing());
activity = activityTestRule.getActivity();
// assert that activity's state is restored

但是,ActivityTestRule.getActivity()似乎没有重新启动活动.

However, it appears that ActivityTestRule.getActivity() does not restart the activity.

推荐答案

如@CommonsWare所述,自定义规则非常有用.这是我的简单解决方案(可以进行很多改进,但这只是可以构建的快速框架):

As @CommonsWare mentioned, a custom rule is pretty useful. Here's my simple solution (many improvements are possible, but this is just a quick framework that can be built on):

public class ControlledActivityTestRule<T extends Activity> extends ActivityTestRule<T> {
    public ControlledActivityTestRule(Class<T> activityClass) {
        super(activityClass, false);
    }

    public ControlledActivityTestRule(Class<T> activityClass, boolean initialTouchMode) {
        super(activityClass, initialTouchMode, true);
    }

    public ControlledActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) {
        super(activityClass, initialTouchMode, launchActivity);
    }

    public void finish() {
        finishActivity();
    }

    public void relaunchActivity() {
        finishActivity();
        launchActivity();
    }

    public void launchActivity() {
        launchActivity(getActivityIntent());
    }
}

注意,如果您这样做,则此类必须位于包android.support.test.rule中,才能访问包私有方法ActivityTestRule#finishActivity.然后在您的测试用例中,执行以下规则:

Note, if you do it like this, this class needs to be in the package android.support.test.rule to access the package private method ActivityTestRule#finishActivity. Then in your test case, implement this rule:

@Rule
public ControlledActivityTestRule<TestFountainPreferenceActivity> actRule = new ControlledActivityTestRule<>(TestFountainPreferenceActivity.class);

然后在您的单独测试用例中,调用actRule.finish()actRule.launchActivity()杀死并重新启动它.或者,如果您在杀死它和重新启动它之间不需要执行任何操作,则可以致电actRule.relaunchActivity().请注意,如果您初次启动,则可以传递第三个参数false来延迟启动活动,然后调用actRule.launchActivity()使其继续进行,但是您将失去对某些内置句柄(如#afterActivityLaunched)的访问权限.和#afterActivityFinished().

Then in your individual test case, call actRule.finish() and actRule.launchActivity() to kill and restart it. Or you can call actRule.relaunchActivity() if you don't need to do anything in between killing it and starting it up again. Note, you can pass the third parameter false to delay starting the activity if you have initial startup, and then call actRule.launchActivity() to get it going, but you will loose access to some of the built in handles such as #afterActivityLaunched and #afterActivityFinished().

这篇关于使用测试支持库销毁并重新启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆