Pritam
Pritam

Reputation: 2507

Any way to get a reference for Actionbar items for Junit testing in android?

How do I write junit test cases for actionbar items in android ? Any way of getting its reference for performing click events on it ?

Upvotes: 6

Views: 3471

Answers (5)

Satyam Raikar
Satyam Raikar

Reputation: 473

use robotium.jar library

import com.jayway.android.robotium.solo.Solo;

private Solo solo;
this.solo = new Solo(getInstrumentation(),getActivity());
//R.id.menu_action_signup Menu Iten id.
this.solo.clickOnView(this.solo.getView(R.id.menu_action_signup));

Upvotes: 1

Yann
Yann

Reputation: 4181

In this following example, i'm able to retrieve the navigation tab button of the action bar (native or ActionBarSherlock). Then i click on them with TouchUtils.clickView():

try {

// Trying to get the ActionBar view '@id/android:action_bar_container' dynamically
int resId =
a.getResources().getIdentifier("action_bar_container", "id", "android");
View actionBarContainer = a.findViewById(resId);

// The class 'com.android.internal.widget.ActionBarContainer' must be in
// the classpath of this test project to be able to call
// the method 'getTabContainer' at runtime
Method getTabContainer =
com.android.internal.widget.ActionBarContainer.class.getMethod("getTabContainer",
(Class<?>[]) null);

HorizontalScrollView tabContainer =
(HorizontalScrollView) getTabContainer.invoke(actionBarContainer, (Object[]) null);
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);

} catch (Exception e) {

// Trying with SherlockActionBar
com.actionbarsherlock.internal.widget.ActionBarContainer actionBarContainer =
(com.actionbarsherlock...) a.findViewById(R.id.abs__action_bar_container);

HorizontalScrollView tabContainer =
(HorizontalScrollView) actionBarContainer.getTabContainer();
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);

}
}

Upvotes: 1

Xample
Xample

Reputation: 490

You can simulate clicking an ActionBar item like this:

public void testButton(){
    final View view = activity.findViewById(com.example.R.id.button1);
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            view.requestFocus();
            view.callOnClick();
        }
    });
}

Upvotes: 3

Shinyosan
Shinyosan

Reputation: 357

I'm using an ActionBar provided by ActionBarSherlock and ran into this issue. The best way I've found to do this is:

  1. In your test activity retain an instance of the Menu object passed in onCreateOptionsMenu and make this accessible to your test case.
  2. In your test case you will need a reference to your Instrumentation and the Menu allowing you to select your MenuItem by id:

    private void clickOnMenuItem(int menuItemId, Instrumentation instruments, Menu menuInstance) {
    final Integer itemId = menuItemId;
    final Menu menu = menuInstance;
    instruments.runOnMainSync(new Runnable() {
        @Override
        public void run() {
            menu.performIdentifierAction(itemId, 0);
        }
    });
    

    }

Upvotes: 0

Pritam
Pritam

Reputation: 2507

I resolved by creating my own custom MenuItem class and calling onOptionsItemSelected(MenuItem item) for the Activity manually. Any other proper way for doing this for Junit testing ?

Upvotes: 0

Related Questions