Reputation: 4419
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?
}
Upvotes: 1
Views: 389
Reputation: 13957
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.
p.s.: 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");
Upvotes: 2