Testing ListView with ToggleButton using Android Testing Framework

November 30, 2012
By

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() {
        super("in.wptrafficanalyzer.listviewtogglebutton",MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        /** 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.
    */
    @SmallTest
    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);
            if(!tButton.isChecked()){
                expected = true;
                sendRepeatedKeys(1,KeyEvent.KEYCODE_DPAD_DOWN,1,KeyEvent.KEYCODE_DPAD_CENTER);
                actual = mActivity.status[i];
            }else{
                expected = false;
                sendRepeatedKeys(1,KeyEvent.KEYCODE_DPAD_CENTER);
                actual = mActivity.status[i];
            }
            assertEquals(expected, actual);
        }
    }
}


6. Run this test project

Test Result

Figure 2 : TestCase is successfully executed


7. Download


8. Reference

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


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
store4js.blogspot.com


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