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?
If you want to rotate the Activity, you'll want to use this : http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)
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.
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.
You can does it by pressing following key sequence Ctrl + F12
精彩评论