简体   繁体   中英

Robotium: verify that activity gets finished and the underlying one restored when “home” button clicked

I' m using Android annotations and I have the following code in my Activity:

@OptionsItem
void homeSelected() {
    finish();
}

obviously, this code should finish the current activity, when the user has clicked the home button in ActionBar. But how can I test this code? I would like to be able to verify somehow that after clicking on the home button the current activity finished, and the previous activity restored. Is this possible? Or at least can I verify that my homeSelected method gets called after clicking the home button?

I'm using Robotium and I have the following test method:

@SmallTest
public void testGoBack() {
    solo.clickOnActionBarHomeButton();
    // ???? How do I verify the homeSelected method was actually called?
}

You could use waitForActivity(...) .

waitForActivity(Class<? extends android.app.Activity> activityClass)

Waits for an Activity matching the specified class.

Check out the documentation .

@SmallTest
public void testGoBack() {
    solo.clickOnActionBarHomeButton();

    solo.waitForActivity(com.example.UpActivity.class);
}

The default routine uses a 20 seconds timeout.

ps: alternatively add a debug log to the onDestroy() method of your activity:

public class MyActivity extends Activity {

    ...
    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.d ("TAG", "Finishing MyActivity");
    }
} 

and wait for this log message inside of your test method:

waitForLogMessage("Finishing MyActivity");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM