How to change the screen orientation during automated testing?
I am testing my Android application using A开发者_Go百科ctivityInstrumentationTestCase2, and I need to test that the screen orientation change works correctly. However, I cannot find any way to cause an orientation to occur. What am I missing?
Check this example where I tried extending Android ActivityInstrumentationTestsCase2 to use different screen orientations: iliasbartolini / AgileDayConferenceApp
Basically you need to change the Resources configuration. I found this example here: Tip for unit-testing: loading Resources for a specific screen orientation/
Resources res = getInstrumentation().getTargetContext().getResources();
Configuration oldResourcesConfiguration = res.getConfiguration();
Configuration newConfiguration = new Configuration(oldResourcesConfiguration);
newConfiguration.orientation = configurationOrientation;
res.updateConfiguration(newConfiguration, res.getDisplayMetrics());
Here is a dummy Landscape test example on how to use it.
It actually only checks that the Landscape layout and resources loaded by the activity are not broken: don't know if there are better ways to do it.
And here the Portrait test
精彩评论