Any way to get a link to Actionbar elements for testing Junit in android?

How to write junit test cases for taskbar items in android? Any way to get a link to click on it?

+6
source share
5 answers

You can simulate a click of an ActionBar as follows:

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(); } }); } 
+3
source

In the following example, I can get the action tab navigation tab button (native or ActionBarSherlock). Then I click on them using 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); } } 
+1
source

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)); 
+1
source

I decided to create my own MenuItem class and call onOptionsItemSelected (MenuItem element) for the Activity manually. Any other right way to do this for testing Junit?

0
source

I am using the ActionBar provided by ActionBarSherlock and have encountered this problem. The best way I've found for this is:

  • In the test activity, save an instance of the Menu object passed to onCreateOptionsMenu and make it available for your test case.
  • In the test example, you will need a link to your Toolkit and a menu that allows 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); } }); 

    }

0
source

Source: https://habr.com/ru/post/905742/


All Articles