Matthew
Matthew

Reputation: 836

Instrumentation: Control Lifecycle

How is it possible to control the lifecycle of an Android Activity from within a TestCase using Instrumentation?

On the official documentation, it is stated "Lifecycle control: With instrumentation, you can start the activity under test, pause it, and destroy it, using methods provided by the test case classes. ". Of course, using this testcase the Acitivity is automatically created when calling getActivity() and it is stopped after each test-case. But how to control the lifecycle externally in order to check if all lifecycle methods are implemented correctly?

The lifecycle methods onActivityXXX just help to call the respective methods but do not cause the Activity to pause or stop. Can anyone please help and tell me which methods i have to use?

Are there any methods to test the lifecycle implementation of an Android application?

Upvotes: 1

Views: 692

Answers (1)

Eytan
Eytan

Reputation: 798

This is will not give you full control over the life cycle but it is the example found here:

// Start the main activity of the application under test
    mActivity = getActivity();

    // Get a handle to the Activity object's main UI widget, a Spinner
    mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);

    // Set the Spinner to a known position
    mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);

    // Stop the activity - The onDestroy() method should save the state of the Spinner
    mActivity.finish();

    // Re-start the Activity - the onResume() method should restore the state of the Spinner
    mActivity = getActivity();

    // Get the Spinner's current position
    int currentPosition = mActivity.getSpinnerPosition();

    // Assert that the current position is the same as the starting position
    assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);

which gives you some control over the main life cycle events. I got as im currently taking care of the same issue, im looking into robotium which should help

Upvotes: 1

Related Questions