如何使用Espresso测试Android活动的保存和恢复状态? [英] How to test saving and restoring state of an android activity with Espresso?

查看:101
本文介绍了如何使用Espresso测试Android活动的保存和恢复状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以以编程方式测试活动的保存和还原状态代码?我的意思是:

Is there a way of testing the save and restore state code of an activity programmatically ? I mean doing this :

如何测试构建的代码保存/恢复活动的生命周期?,但以自动化方式进行.

How to test code built to save/restore Lifecycle of an Activity? but in an automated way.

我已经测试了activity.recreate()方法,这几乎是我要搜索的方法,但是实际上,它不会重置我活动的字段,就像我要杀死该进程一样.因此,即使我未在onCreate方法中实现恢复操作(因为我的字段未更改...),我的测试仍可以通过.

I have tested activity.recreate() method which is almost what I am searching, but in fact it does not reset the fields of my activity like if I was killing the process. So my test can pass even if I don't implement the restoring things in my onCreate method (since my fields are unchanged...).

我目前正在使用Espresso v2,我想知道是否有可能通过与InstrumentationRegistry.getInstrumentation()一起使用?

I am currently playing with Espresso v2 and I was wondering if this could be possible maybe by playing with the InstrumentationRegistry.getInstrumentation() ?

推荐答案

解决方案是使用activity.recreate()方法,但不要忘记在其后跟随一个等待空闲状态的断言. 我第一次尝试的问题是我正在编写的测试就像:

The solution is to use the activity.recreate() method BUT do not forget to follow this by an assertion which waits for an idle state. My problem with my first attempt was that the test I was writing was like :

instrumentation.runOnMainSync(new Runnable() {
    @Override
    public void run() {
        activity.recreate();
    }
});
assertThat(activityTestRule.getActivity().getXXX()).isNull();

其中XXX是一个字段,当未实现任何保存/恢复状态处理时,我希望该字段为null.但是事实并非如此,因为我的主张不是在等待娱乐任务完成.

Where XXX was a field that I expected to be null when no save/restore state handling had been implemented. But that was not the case because my assertion was not waiting for the recreation task to be completed.

因此,在我的情况下,我只需添加一个可以完成此任务的espresso断言就可以解决问题,例如,通过验证显示XXX字段的TextView是否为空.

So in my situation, my problem was solved when I simply add an espresso assertion which does the job, for example by verifiying that the TextView which displayed the XXX field was empty.

最后,由于Espresso提供了UI线程同步,我的测试可以对我的活动保存/恢复状态缺失的实现进行断言,可以这样写:

Finally, thanks to the UI thread synchronization provided by Espresso, my test which can assert on my activity save/restore state missing implementation can be written like :

instrumentation.runOnMainSync(new Runnable() {
    @Override
    public void run() {
        activity.recreate();
    }
});
onView(withText("a string depending on XXX value")).check(doesNotExist());

请注意,建议的轮换解决方案也可以执行此操作,但是当我们只想测试活动生命周期时,它可能比activity.recreate()慢得多.在我的情况下,这是不相关的,因为我的活动不是针对横向进行的.

Note that the rotation solution suggested does the job either, but can be much slower than activity.recreate() when we just want to test the activity lifecycle. And in my case it was not relevant since my activity was not implemented for a landscape orientation.

这篇关于如何使用Espresso测试Android活动的保存和恢复状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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