Programming for Mobiles - Practical 5
Testing mobile applications
Resources
- Web page and guides on the GeoMessaging application [html]
- Android documentation on testing and tutorial [html]
- Stackoverflow post on testing intents and onActivityResult [html]. Note that on my device the call to waitForMonitorWithTimeout doesn't seem to work and you need to poll getHits() on the Monitor itself instead - don't forget to remove it yourself too if you do this.
- Managing Activity caching through a screen orientation change [below]
- Mock locations [below]
Screen orientation change
You can trigger an orientation change by calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) on the Activity under test.
However, when you do this the default Android lifecycle will call onDestroy() on the current activity, dispose of it and create a new one in the new orientation. Unfortunately, the ActivityInstrumentationTestCase2 class which we are using doesn't notice this and so will continue to give out pointers to the old activity. To force it to re-evaluate you should also call setActivity(null) and then use getActivity() again (I'll expect you to explain why that works!)