I am writing an Android app with ActionBar enabled. In my case I use ActionBarSherlock to provide compatibility, but I think it doesn’t matter in this case.
I have an Up button displaying at the left of ActionBar in my Activity. It appears after a call to setHomeAsUpEnabled(true); and it leads to the HomeActivity.
I want to write a test case which checks that Up button press leads to that HomeActivity.
I am using Robotium to write tests on UI side so I can check the class of the current Activity by following assertion solo.assertCurrentActivity(HomeActivity.class);
But I have not found a way to invoke a press on an Up button.
I’ve tried solo.clickOnView(solo.getView(android.R.id.home)) and getInstrumentation().invokeMenuActionSync(solo.getCurrentActivity(), android.R.id.home, 0); but none of them works.
Any help would be appreciated.
Found a workaround myself, so posting it here in case anybody will need this.
This code worked for me:
First you need to obtain ActionBarSherlockCompat object by calling
getSherlock()on SherlockFragmentActivity. This method is protected, so I used Reflection API to call it:You need to pass
solo.getCurrentActivity()to myclickOnUpActionBarButton(Activity activity)method and Up button will be pressed.