In this article, we will create an automated testing program for the application “ListViewCheckAll”, that we have developed in the articled titled “Implementing CheckAll and UncheckAll for a listview in Android“.
The application is having a checkbox and a listview. On ticking the checkbox, all the items in the listview will be ticked and on unticking the checkbox, all the items in the listview will be unticked. We will create a testing application in Android Testing Framework to test this functionality.
This testing application is developed in Eclipse (4.2.1) with ADT plugin(20.0.3) and Android SDK(20.0.3).
1. What to test
The screenshot of the test target is given below. On ticking the “All” checkbox, all the listview items should be checked. On unticking the “All” checkbox, all the listview items should be unchecked. In this article we will test this functionality using Android Testing Framework. We will create our testcase class by extending the class ActivityInstrumentationTestCase2.
2. Download the application “ListViewCheckAll“ 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 : ListViewCheckAllTest
Location : Default Location
Build Target : Android 2.3.3 ( API Level 10 )
Test Target : ListViewCheckAll (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/checkall/test/MainActivityTest.java
package in.wptrafficanalyzer.checkall.test; import in.wptrafficanalyzer.checkall.MainActivity; import android.test.ActivityInstrumentationTestCase2; import android.test.TouchUtils; import android.test.suitebuilder.annotation.SmallTest; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ListView; public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> { /** Activity Containing ListView */ MainActivity mActivity; /** Checkbox for Check All and UnCheck All */ CheckBox mChkAll; /** ListView containing checkboxes to be tested */ ListView mListView; public MainActivityTest() { super(MainActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); /** Getting reference to the activity */ mActivity = getActivity(); /** Getting reference to the checkbox */ mChkAll = (CheckBox) mActivity.findViewById(in.wptrafficanalyzer.checkall.R.id.chkAll); /** Getting reference to the listview */ mListView = mActivity.getListView(); } /** If checkbox is ticked, then all the items in the listview should be checked. * If checkbox is unticked, then all the items in the listview should be unchecked */ @SmallTest public void testCheckAll(){ ArrayAdapter<String> mAdapter = (ArrayAdapter<String>) mListView.getAdapter(); /** Changing the state of the checkbox */ TouchUtils.tapView(this, mChkAll); boolean expected = mChkAll.isChecked(); for(int i=0;i<mAdapter.getCount();i++){ assertEquals(expected, mListView.isItemChecked(i)); } /** Changing the state of the checkbox */ TouchUtils.tapView(this, mChkAll); expected = mChkAll.isChecked(); for(int i=0;i<mAdapter.getCount();i++){ assertEquals(expected, mListView.isItemChecked(i)); } } }
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