In this article, we will test an Android application that we have developed in the article titled “ListView with custom layout in Android“. We will make use the testcase class called ActivityInstrumentationTestCase2 to test the custom ListView.
This test application is developed in Eclipse (4.2.1) with ADT plugin ( 20.0.3 ) with Android SDK ( R20.0.3 ).
1. What to test
We will test the following things in the listview:
- The text in the listview item should be centralized
- The text in the listview item should be bold
- The text in the listview item should be italic
2. Download the application “ListViewCustomLayout” from the given below link

3. Extract the downloaded file and import into Eclipse IDE
4. Create an Android Test Project with the given below details
Project Name : ListViewCustomLayoutTest
Location : Default Location
Build Target : Android 4.0 ( API Level 14 )
Test Target : ListViewCustomLayout (The application, downloaded in the above step)
5. Create a TestCase class namely MainActivityTest with the given below code in the file src/in/wptrafficanalyzer/listviewcustomlayout/test/MainActivityTest.java
package in.wptrafficanalyzer.listviewcustomlayout.test; import in.wptrafficanalyzer.listviewcustomlayout.MainActivity; import android.graphics.Typeface; import android.test.ActivityInstrumentationTestCase2; import android.view.Gravity; import android.widget.ListView; import android.widget.TextView; public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity>{ /** Activity containing listview to be tested */ MainActivity mActivity; /** The listview to be tested */ ListView mListView; public MainActivityTest() { super(MainActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); /** Getting reference to the activity containing listview to be tested */ mActivity = getActivity(); /** Getting reference to the listview to be tested */ mListView = (ListView) mActivity.findViewById(in.wptrafficanalyzer.listviewcustomlayout.R.id.listView); } /** Testing whether text in the list item is centered, bold and italicized */ public void testListView(){ /** Number of items in the listview */ int itemCount = mListView.getAdapter().getCount(); /** Iterates through each item in the listview */ for(int i=0;i<itemCount;i++){ /** Getting view object corresponding to the i-th item in the listview */ TextView tvItem = (TextView)mListView.getAdapter().getView(0, null, null); int gravity = tvItem.getGravity(); Typeface typeface = tvItem.getTypeface(); /** Asserting that, the text is center aligned */ assertEquals(Gravity.CENTER, gravity); /** Asserting that, the text is bold */ assertTrue(typeface.isBold()); /** Asserting that, the text is italic */ assertTrue(typeface.isItalic()); } } }
6. Run this test project
7. Download

8. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com