Espresso:保存和恢复状态

1995次阅读  |  发布于5年以前

Espresso:保存和恢复状态

你保存并且恢复activities,fragments和自定义视图的状态吗?你有测试过吗?

一种测试保存和恢复状态的方法就是在你的Espresso测试中旋转屏幕.

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);
}

有了上面的辅助方法,你可以写出如下的测试:

@Test
public void incrementTwiceAndRotateScreen() {
    onView(withId(R.id.count))
    .check(matches(withText("0")));

    onView(withId(R.id.increment_button))
    .perform(click(), click());
    onView(withId(R.id.count))
    .check(matches(withText("2")));

    rotateScreen();

    onView(withId(R.id.count))
    .check(matches(withText("2")));
}

源码

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8