Search code examples
androidfunctional-testingrobotiumonresumeondestroy

How to automatically test onResume behaviour by calling onDestroy using Robotium?


I am using Robotium to functionally test an Android app. I'd like to test the onResume behaviour after onDestroy has been called. This post hints on using Instrumentation, but i can't get it to work. I have included the following, but this fails with an IllegalStateException. Is it possible to destroy the app and restart it?

public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;

public MainActivityFunctionalTest() {
    super(MainActivity.class);
}

public void testActionList() {
    getInstrumentation().callActivityOnDestroy(solo.getCurrentActivity());
    ...
}

Results in the following exception:

java.lang.IllegalStateException: Must be called from main thread of process at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1373) at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1825) at android.app.Activity.performDestroy(Activity.java:5171) at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109) at nl.handypages.trviewer.test.MainActivityFunctionalTest.testActionList(MainActivityFunctionalTest.java:81) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)

Solution

  • Well you have two issues here, the first issue is that the instrumentation callactivityondestroy methods require you to call it from the main thread. see runOnUiThread usage tips for details on how to do this.

    The second issue is that (and i might be wrong here) is never suppose to go from onDestroy to onResume http://developer.android.com/reference/android/app/Activity.html says that after ondestroy the activity is literally destroyed. You would have to create a new instance of the activity to get another onResume. with you could do by launching your activity again. I suppose you could do it your way but im not sure how valid it would be, if you wanted to do it in such a way, just call the onResume in the same way as onDestroy as noted in the question i linked to.