How do you force an orientation change in an Android Instrumentation test?

后端 未结 3 1363
心在旅途
心在旅途 2020-12-29 04:41

I\'m writing some acceptance tests for an application using the ActivityInstrumentationTestCase2 class. I want to cause an orientation change from within the test to ensure

相关标签:
3条回答
  • 2020-12-29 04:53

    You do not actually have to use Robotium for this at all. In fact, if you view the source of Robotium all it is doing when you call

    solo.setActivityOrientation(Solo.LANDSCAPE);
    

    is

    myActivity = this.getActivity(); // In your setUp method()
    
    ...
    
    myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
    0 讨论(0)
  • 2020-12-29 05:05

    As AndrewKS wrote you can use

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    assertTrue(...);
    

    to request an orientation change. But the rotation itself is executed asynchronous. To really test the state after the orientation change you need to wait a short time after the request:

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    Thread.sleep(50); // depends on performance of the testing device/emulator
    assertTrue(...);
    
    0 讨论(0)
  • 2020-12-29 05:09

    Use Robotium for it. There is a class called Solo, using which you can change orientation by just calling a method:

    solo.setActivityOrientation(Solo.LANDSCAPE);
    

    That's it! Your orientation would get changed. You can google Robotium and obtain its jar and add it to your Test project. The Robotium site also gives an example Test project on Android's Notepad App (which is available as a sample project with Android SDK) which shows how powerful it is and how easily it could be used.

    0 讨论(0)
提交回复
热议问题