Dayvid Oliveira
Dayvid Oliveira

Reputation: 1217

Test if when a Button is clicked the correct activity is lauched

I'm writing a test for an activity that have a few buttons, each one of than starts a new Activity,

How can I know if the button is starting the correct activity?

This is what I have so far:

public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {

    private Intent mMainIntent;


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


    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mMainIntent = new Intent(Intent.ACTION_MAIN);
    }

    @MediumTest
    public void testButtonActivityA () {
        MainActivity activity = startActivity(mMainIntent, null, null);
        Button buttonActivityA = (Button) activity.findViewById(com.project.R.id.button_activity_a);
        buttonVoice.performClick();
        Intent i = getStartedActivityIntent();
        assertNotNull(i);
        assertTrue(isFinishCalled());
    }
}

PS: the 'isFinishedCalled()' is failing, how can this be if I raise a new fullscreen Activity? Thanks,

Upvotes: 1

Views: 1516

Answers (1)

Blundell
Blundell

Reputation: 76536

It's failing because finish() isn't called.

you have to finish an activity yourself, otherwise when you open a new one it comes up over the top on the 'stack' and the original activity has onPause called but is still 'alive'. finish() is an implicit call that you can code into your app to destroy an activity.

Please go read about the Android Activity Lifecycle

Then learn to call when you should call finish()

Upvotes: 1

Related Questions