简体   繁体   English

如何使用 Espresso 测试保存和恢复 android 活动的状态?

[英]How to test saving and restoring state of an android activity with Espresso?

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.但以自动化的方式。

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.我已经测试了activity.recreate()方法,这几乎就是我正在搜索的方法,但实际上它并没有像我正在终止进程那样重置我的活动字段。 So my test can pass even if I don't implement the restoring things in my onCreate method (since my fields are unchanged...).所以我的测试可以通过,即使我没有在我的onCreate方法中实现恢复的东西(因为我的字段没有改变......)。

I am currently playing with Espresso v2 and I was wondering if this could be possible maybe by playing with the InstrumentationRegistry.getInstrumentation() ?我目前正在使用 Espresso v2,我想知道是否可以通过使用InstrumentationRegistry.getInstrumentation()

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.解决方案是使用activity.recreate()方法,但不要忘记跟随这个等待空闲状态的断言。 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();

Where XXX was a field that I expected to be null when no save/restore state handling had been implemented.当没有实现保存/恢复状态处理时, XXX是一个我希望为空的字段。 But that was not the case because my assertion was not waiting for the recreation task to be completed.但事实并非如此,因为我的断言不是在等待娱乐任务完成。

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.所以在我的情况下,当我简单地添加一个完成这项工作的浓缩咖啡断言时,我的问题就解决了,例如通过验证显示XXX字段的 TextView 为空。

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 :最后,感谢 Espresso 提供的 UI 线程同步,我的测试可以对我的活动保存/恢复状态丢失实现断言可以这样写:

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

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.请注意,建议的轮换解决方案也可以完成这项工作,但当我们只想测试活动生命周期时,它可能比activity.recreate()慢得多。 And in my case it was not relevant since my activity was not implemented for a landscape orientation.就我而言,这无关紧要,因为我的活动不是针对横向实施的。

You can rotate the screen and verify that the state is saved and restored properly.您可以旋转屏幕并验证状态是否已正确保存和恢复。

private void rotateScreen() {
  Context context = InstrumentationRegistry.getTargetContext();
  int orientation 
    = context.getResources().getConfiguration().orientation;

  Activity activity = activityRule.getActivity();
  activity.setRequestedOrientation(
      (orientation == Configuration.ORIENTATION_PORTRAIT) ?
          ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : 
          ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Full example: http://blog.sqisland.com/2015/10/espresso-save-and-restore-state.html完整示例: http : //blog.sqisland.com/2015/10/espresso-save-and-restore-state.html

The testing tools offered by Android now offer a means of writing UI tests that can recreate an activity to test the save and restore flow. Android 提供的测试工具现在提供了一种编写 UI 测试的方法,可以重新创建活动来测试保存和恢复流程。 See the Recreate the activity section within the Test your app's activities Android Developers documentation page.请参阅测试您的应用程序的活动Android 开发人员文档页面中的重新创建活动部分。 An example of the syntax – taken from that page – is the following:取自该页面的语法示例如下:

@RunWith(AndroidJUnit4::class)
class MyTestSuite {
    @Test fun testEvent() {
        val scenario = launchActivity<MyActivity>()
        scenario.recreate()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM