Testing onPause (), onDestroy () using Robotium

How can I use Robotium to verify that my activity handles onPause() , onDestroy() , onResume() and similar calls correctly?

In my Robotium test, I can name stuff like

 solo.getCurrentActivity().onKeyDown(0, null); 

but how can I imitate destructible and recreated activity? I don't see any

 solo.getCurrentActivity().onPause() 

or

 solo.getCurrentActivity().onDestroy() 

methods that i can use? Isn't that what Robotium is for?

+2
source share
2 answers

Use a test case base class that has access to Instumentation , and then use Instrumentation # callActivityOnPause () and Instrumentation # callActivityOnDestroy () .

+2
source

This will destroy your activity and create a new one:

  getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { activity.recreate(); } }); setActivity(null); activity = getActivity(); 

This only works in API 11 and above due to the call to the activity.recreate () function. If you do not care about saving / restoring the state of an instance in this test, you can instead call "activity.finish ()", which is available for all versions.

+1
source

All Articles