Testing ItemSelectedListener for Spinner using Android Testing Framework

November 18, 2012
By

In this article, we will create a test application in Android Testing Framework, to test itemselectedlistener for a Spinner widget. The application developed in the article titled “ItemSelectedListener for Android Spinner Widget – Example” is tested in this application.

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

In this test application, we will test whether the item selected in the Spinner widget is correctly displayed in the TextView widget


2. Download the application SpinnerWidgetItemSelectedListener 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 : SpinnerWidgetItemSelectedListenerTest

Location : Default Location

Build Target : Android 1.6

Test Target : SpinnerWidgetItemSelectedListener (The application to be tested )


5. Create a TestCase class namely MainActivityTest with the given below code in the file src/in/wptrafficanalyzer/spinnerwidgetitemselectedlistener/test/MainActivityTest.java


package in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.test;

import in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.KeyEvent;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivityTest extends ActivityInstrumentationTestCase2 {

    /** The activity containing spinner widget */
    MainActivity mActivity;

    /** The Spinner widget to be tested */
    Spinner mSpinner;

    /** The selected item is displayed in this TextView */
    TextView mTvCountry;
    public MainActivityTest() {
        super("in.wptrafficanalyzer.spinnerwidgetitemselectedlistener",MainActivity.class);
    }

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

        /** Getting a reference to the MainActivity of the target application */
        mActivity = getActivity();

        /** Getting a reference to the Spinner widget */
        mSpinner = (Spinner) mActivity.findViewById(in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.R.id.spr_country_list);

        /** Getting a reference to the TextView widget */
        mTvCountry = (TextView) mActivity.findViewById(in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.R.id.tv_country);
    }

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

    @SmallTest
    public void testSpinner(){
        final int INITIAL_POSITION=0;
        final int FINAL_POSITION=mSpinner.getAdapter().getCount();
        String expected = "";
        String actual = "";

        /** Iterates through each item in the Spinner widget */
        for(int i=INITIAL_POSITION;i<=FINAL_POSITION;i++){
            try {
                runTestOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        /** Sets the first item as the current item in the Spinner widget */
                        mSpinner.setSelection(INITIAL_POSITION);
                    }
                });
            }catch (Throwable e) {
                e.printStackTrace();
            }

            /** Taps the Spinner Widget */
            TouchUtils.tapView(this, mSpinner);

            /** Send i number of Down KeyPad event to Spinner widget,
            * since it is the currently selected widget */
            sendRepeatedKeys(i,KeyEvent.KEYCODE_DPAD_DOWN);

            /** Sets the currently selected item as the current item of the Spinner */
            sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);

            /** Getting the currently selected item */
            expected = mSpinner.getSelectedItem().toString();

            /** Getting the text displayed in the TextView widget */
            actual = mTvCountry.getText().toString();

            /** Checks, whether currently selected Spinner item and the text displayed are same
            * If same, then the test is success, otherwise failed*/
            assertEquals(expected, actual);

            /** This Thread.sleep() is not necessary, but provided just to slow down the testing process
            * so that we can manually monitor the happenings while testing in progress
            */
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


6. Screenshots of the application

Automated Testing in Progress

Figure 1 : Automated Testing in Progress

 

Showing results after automated testing

Figure 2 : Showing results after automated testing


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