Programming for Mobiles - Practical 5

Testing mobile applications


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!)

Mock locations

You can inject mock locations into your application under test by using LocationManager.addTestProvider and LocationManager.setTestProcviderEnabled. To receive these locations you will need to give ACCESS_MOCK_LOCATION permission to the application under test. You can either write the application under test to use the mock provider if its available or more sneakily call your mock provider by the same name as the real provider (this works on my handset at least). Make sure you give a valid time to each location event you generate or they'll get dropped by the framework.