Testing ListView with ToggleButton using Android Testing Framework

November 30, 2012

In this article, we will develop an Android Testing application using Android Testing Framework. This testing application will test the application, that we have developed in the articled titled “Enabling Multi Selection mode in ListView by adding ToggleButton using custom layout in Android“.

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

As shown in the given below figure 1, each item in the ListView contain a toggle button. On Clicking on an item in the listview, the state of the toggle button will be toggled. In this test application, we will test whether the state of the button is successfully toggled or not.

ListView with ToggleButtons

Figure 1 : ListView with ToggleButtons

2. Download the application “ListViewToggleButton from the given below link

This is the application, we are going to test in this article.

3. Extract the downloaded file and import into Eclipse IDE

4. Create an Android Test Project with the given below details

Project Name : ListViewToggleButtonTest

Location : Default Location

Build Target : Android 1.6 ( API Level 4 )

Test Target : ListViewToggleButton (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/listviewtogglebutton/test/MainActivityTest.java

package in.wptrafficanalyzer.listviewtogglebutton.test;

import in.wptrafficanalyzer.listviewtogglebutton.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.ToggleButton;

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity>{

    /** Activity containing ListView with ToggleButton */
    MainActivity mActivity;

    /** ListView with ToggleButton */
    ListView mListView;

    public MainActivityTest() {

    protected void setUp() throws Exception {

        /** Getting reference to MainActivity */
        mActivity = (MainActivity)getActivity();

        /** Getting reference to ListView */
        mListView = (ListView) mActivity.findViewById(in.wptrafficanalyzer.listviewtogglebutton.R.id.lv_countries);

    * Testcase presses each item in the listview and checks whether the state
    * of the item is changed or not. If state is changed, then test is
    * success. Otherwise the test is failed.
    public void testToggleButton(){

        SimpleAdapter adapter = (SimpleAdapter) mListView.getAdapter();
        int count = adapter.getCount();
        boolean expected = false;
        boolean actual = false;

        for(int i=0;i<count;i++){
            RelativeLayout rLayout = (RelativeLayout) mListView.getAdapter().getView(i, null, null);
            ToggleButton tButton = (ToggleButton) rLayout.getChildAt(1);
                expected = true;
                actual = mActivity.status[i];
                expected = false;
                actual = mActivity.status[i];
            assertEquals(expected, actual);

6. Run this test project

Test Result

Figure 2 : TestCase is successfully executed

7. Download

8. Reference


How to hire me?

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

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner