Testing Filterable ListView in Android

November 22, 2012

In this article, we will see how to create an automated testing program using Android Testing Framework. As an example we will test the built in filtering feature of ListView.

We already created an Android application containing a filterable ListView in the articled titled “Filterable ListView in Android“. So, that application will be used in this article.

This test application is developed in Eclipse (4.2.1) with ADT plugin (20.0.3) and Android SDK (R20.0.3).

1. What to test

The ListView contains ten items. From our testcase, we will send the character “S” to the activity. Then the ListView should display only two items that starts with the character “S”. We will test this, in this application.

2. Download the application “ListViewFilterable” 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 : ListViewFilterableTest

Location : Default Location

Build Target : Android 4.0 ( API Level 14 )

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

package in.wptrafficanalyzer.listviewfilterable.test;

import in.wptrafficanalyzer.listviewfilterable.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.KeyEvent;
import android.widget.ListView;

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    /** The activity containing the ListView to be tested */
    MainActivity mActivity;

    /** The filterable listview to be tested */
    ListView mListView;
    public MainActivityTest() {

    protected void setUp() throws Exception {

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

        /** Getting reference to the ListView */
        mListView = (ListView) mActivity.findViewById(in.wptrafficanalyzer.listviewfilterable.R.id.listview);

    public void testFilterableListView(){

        /** Sending the key "S" to the activity */

        /** There are only 2 items, start with "S" */
        /** So Expected result is 2 */
        assertEquals(2, mListView.getAdapter().getCount());

6. Run this test project

Screenshot of the test application "ListViewFilterableTest" in progress

Figure 1 : Screenshot of the test application "ListViewFilterableTest" in progress

Showing Test Result

Figure 2 : Showing Test Result

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