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

3

28

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 that a number of things happen. Among these things are ensuring that Activity state is preserved, but also I'd like to ensure that the appropriate layout for the orientation is used.

I know I can simply test the onSaveInstanceState/onRestoreInstanceState/onPause/onResume/etc. methods to make sure instance state is preserved. However, I was wondering if there is actually a mechanism for causing an orientation change event?

Would this involve injecting some kind of motion event to trick the device/emulator into thinking that it has been rotated, or is there an actual method for this provided by the Instrumentation?

Thanks & Cheers!

Gonococcus answered 18/8, 2010 at 19:37 Comment(0)
F
51

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);
Finery answered 1/6, 2011 at 19:20 Comment(4)
awesome, switched the accepted answer to yours. Kudo's for the detective work go to you sir.Gonococcus
Robotium seems shorter for this.Burne
And probably one will need to call getInstrumentation().waitForIdleSync(); after that in order to wait for the orientation change to happen because it is performed in async manner.Sclaff
Thanks but after I setted it in device, him stops of get the real orientation if I change physically. How I back to default setting?Modern
S
5

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(...);
Sulphanilamide answered 18/11, 2011 at 10:33 Comment(0)
P
3

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.

Pendley answered 30/12, 2010 at 11:56 Comment(3)
Wow, thank you! It's been a while since I asked the question and its seen a lot of views. This definitely solves the problem I had. Robotium looks like a great testing library.Gonococcus
To bad Robotium does not support fragments.Amir
it does support fragments now :)Modena

© 2022 - 2024 — McMap. All rights reserved.