How can I write a test for the rotation of the activit?
In the general case, it would be great to test the re-creation of activations on any configuration change. Generally perfect if this can be done in a robolectric.

I need to test the correct behavior of the retain fragment. If you call activity.recreate() in the robo-infrastructure, then the fragment is also recreated there, which is not true, in my case.

If you do this:

 activity.baseContext.resources.updateConfiguration(newConfig, activity.resources.displayMetrics) 

It seems to be working as I expect, but doubts are overwhelming that it works out exactly as it will in real life.

    2 answers 2

    In general, I found a way. In the robotics it will happen.
    I had to use espresso and add UIAutomator there. Everything is just done.
    We initialize it

    device = UiDevice.getInstance (InstrumentationRegistry.getInstrumentation ())

    and at the right moments we call

      device.setOrientationLeft() device.waitForWindowUpdate(basePackage, 5000) 

    basePackage is the package in which your activity is located. Actually before the call, there will be one orientation, after the call - the other.

      ...но одолевают сомнения, что это отрабатывает именно так, как и будет в реальной жизни. It is worth remembering that Robolectric does not provide 100% imitation of the Android environment. And in any case, it will not happen as it would in reality. With a great deal of similarity - yes, but not completely. For such things, it is better to use frameworks designed for them, such as UiAutomator or Espresso .

      If the task is to use Robolectric, then this can be done by a hack, imitating the life cycles that pass when the screen is rotated. And he is

       onPause() onStop() onDestroy() onCreate() onStart() onResume() 

      And you can do it like this:

        ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class); activity = controller.pause() .stop() .destroy() .create() .start() .resume().get(); 

      The code is not testing, but it should work.

      So answering your question, you can emulate screen rotation using Robolectric . The question is, is it rational?

      • the robot is not suitable for my purposes, I had to test it on a real turn :) - andreich