How to check the conservation and restoration of the state of android activity using Espresso?

Is there a way to check the program code for saving and restoring the state of the program? I mean:

How to check the code created to save / restore the life cycle of an activity? but in an automatic way.

I tested a method activity.recreate()that is almost what I am looking for, but actually it does not reset the fields of my activity, as if I were killing a process. Therefore, my test can pass even if I do not implement recovery things in my method onCreate(since my fields do not change ...).

I am currently playing with Espresso v2 and I was wondering if this is possible, perhaps playing with InstrumentationRegistry.getInstrumentation()?

+4
source share
2 answers

The solution is to use the method activity.recreate(), but do not forget to follow this statement, awaiting a state of inaction. My problem with my first attempt was that the test I wrote looked like this:

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

Where XXXwas the field that I expected would be null if the save / restore state was not processed. But this was not so, because my statement did not expect the completion of the rest task.

So, in my situation, my problem was solved when I simply add an espresso statement that does the job, for example, confirming that the TextView that displayed the field XXXwas empty.

, , Espresso, , . /. . :

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

, , , activity.recreate(), . , .

+3

, .

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

: http://blog.sqisland.com/2015/10/espresso-save-and-restore-state.html

+2

Source: https://habr.com/ru/post/1610112/


All Articles