Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rotate the android emulator from test code?

I want to test different behavior of my app in landscape and portrait mode. I've got a subclass of android.test.InstrumentationTestCase that does a fine job of testing the behavior of my app in portrait mode, but if I (manually) put the emulator into landscape before running the test, it fails. That's fine - the behavior in landscape shouldn't pass the portrait tests - and I know how to check the orientation from my test code, so I can avoid the test failures - but what I want to do is (from test code) put the emulator into the correct state for each of my tests. How can I do that?

like image 381
Carl Manaster Avatar asked Dec 09 '10 17:12

Carl Manaster


People also ask

How do you rotate an emulator?

Use Ctrl + F11 . This will rotate your emulator.

How do you handle rotation on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

How do I force Android to landscape?

When on the main screen, under the orientation section, you will see a number of options like 'Auto-rotate OFF', 'Auto-rotate ON', 'Forced Portrait' and 'Forced Landscape'. As the names suggest, you can use these buttons as one-tap shortcuts to toggle the orientation of your device.


3 Answers

If you want to rotate the Activity, you'll want to use this : http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)

like image 149
Matthieu Avatar answered Oct 22 '22 13:10

Matthieu


What I wound up doing was extracting the bit of code in my Activity that obtained the orientation into an OrientationGetter interface and giving the Activity a DefaultOrientationGetter that returned the device (or emulator) orientation. Now my test can inject a dumb stub OrientationGetter that simply returns portrait or landscape as needed, and I can test my Activity's code without actually controlling the emulator's state. It works fine.

like image 34
Carl Manaster Avatar answered Oct 22 '22 15:10

Carl Manaster


I'm not seeing anything of obvious use like an emulator console command. From the host, you could perhaps inject the F11/F12 into the emulator using Xtest or some win32 code (and possibly determine the current state by reading the window geometry). If the test logic is running in the emulator you could tcp out to something listening on a port on the host which would appear as 10.0.2.2 from the emulator and ask it to inject that key. But this seems inelegant.

Rebuilding the emulator to add a console command would be a little better.

like image 1
Chris Stratton Avatar answered Oct 22 '22 13:10

Chris Stratton