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

前端 未结 3 695
轻奢々
轻奢々 2021-02-09 09:04

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 A

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 09:34

    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

提交回复
热议问题