Android Testing Framework – Testing Calculator Application

November 19, 2012
By

The objective of this article is to test the Calculator application that we have developed in the article titled “A Simple Calculator in Android“. We are using Android Testing Framework to test the Calculator application.

The class, ActivityInstrumentationTestCase2 will be extended to create a testcase class 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

We know that, our calculator application can do four operations such as addition, subtraction, multiplication and division. So in this test application, we will test all these four operations of the calculator.


2. Download the application “SimpleCalculator” from the given below link

This is the application, that we are going to test.


3. Extract the downloaded file and import into Eclipse IDE


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

Project Name : SimpleCalculatorTest

Location : Default Location

Build Target : Android 1.6

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


package in.wptrafficanalyzer.simplecalculator.test;

import in.wptrafficanalyzer.simplecalculator.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    /** The Calculator  activity to be tested */
    MainActivity mActivity;

    /** Operator of the Calculator activity */
    Spinner mOperator;

    /** Operand1 */
    EditText mNumber1 ;

    /** Operand2 */
    EditText mNumber2 ;

    /** Result */
    EditText mResult ;

    /** Button to perform calculation */
    Button mBtnResult;

    public MainActivityTest() {
        super("in.wptrafficanalyzer.simplecalculator",MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        /** Starting Calculator Activity */
        mActivity = getActivity();

        /** Getting a reference to Calculator Operator */
        mOperator = (Spinner)mActivity.findViewById(
        in.wptrafficanalyzer.simplecalculator.R.id.sprOperator);

        /** Getting reference to Operand1 */
        mNumber1 = (EditText)mActivity.findViewById(
        in.wptrafficanalyzer.simplecalculator.R.id.edtNumber1);

        /** Getting reference to Operand2 */
        mNumber2 = (EditText)mActivity.findViewById(
        in.wptrafficanalyzer.simplecalculator.R.id.edtNumber2);

        /** Getting reference to Result */
        mResult = (EditText)mActivity.findViewById(
        in.wptrafficanalyzer.simplecalculator.R.id.edtResult);

        /** Getting reference to the calculation performing button */
        mBtnResult = (Button)mActivity.findViewById(
        in.wptrafficanalyzer.simplecalculator.R.id.btnResult);
    }

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

    /** Testing Add Operation */
    @SmallTest
    public void testAddition(){

        String strNum1 = "1 0 0 PERIOD 5 ";
        String strNum2 = "5 0";
        float expected = 150.5f;

        /** Inputting Operand1 */
        TouchUtils.tapView(this, mNumber1);
        sendKeys(strNum1);

        /** Inputting Operand2 */
        TouchUtils.tapView(this, mNumber2);
        sendKeys(strNum2);

        /** Initializes Spinner to first position */
        try {
            runTestOnUiThread(new Runnable(){
                @Override
                public void run() {
                    mOperator.setSelection(0);
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
        }

        /** Selects Subtraction operator from the Spinner Widget */
        TouchUtils.tapView(this, mOperator);
        this.sendRepeatedKeys(
            1,KeyEvent.KEYCODE_DPAD_CENTER,
            1,KeyEvent.KEYCODE_DPAD_DOWN,
            1,KeyEvent.KEYCODE_DPAD_CENTER
        );

        /** Performs calculation */
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mBtnResult.performClick();
                }
            });
        } catch (Throwable e1) {
            e1.printStackTrace();
        }

        /** Getting the result shown */
        float actual  = Float.parseFloat(mResult.getText().toString());

        /** Asserts, expected and actual are same */
        assertEquals(expected, actual, 0);
    }

    /** Testing Subtraction operation */
    @SmallTest
    public void testSubtraction(){

        String strNum1 = "1 0 0 PERIOD 5 ";
        String strNum2 = "5 0";
        float expected = 50.5f;

        /** Inputting Operand1 */
        TouchUtils.tapView(this, mNumber1);
        sendKeys(strNum1);

        /** Inputting Operand2 */
        TouchUtils.tapView(this, mNumber2);
        sendKeys(strNum2);

        /** Initializes Spinner to first position */
        try {
            runTestOnUiThread(new Runnable(){
                @Override
                public void run() {
                    mOperator.setSelection(0);
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
        }

        /** Selects Subtraction operator from the Spinner Widget */
        TouchUtils.tapView(this, mOperator);
        this.sendRepeatedKeys(
            1,KeyEvent.KEYCODE_DPAD_CENTER,
            2,KeyEvent.KEYCODE_DPAD_DOWN,
            1,KeyEvent.KEYCODE_DPAD_CENTER
        );

        /** Performs calculation */
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mBtnResult.performClick();
                }
            });
        } catch (Throwable e1) {
            e1.printStackTrace();
        }

        /** Getting the result shown */
        float actual  = Float.parseFloat(mResult.getText().toString());

        /** Asserts, expected and actual are same */
        assertEquals(expected, actual, 0);
    }

    /** Testing Multiplication operation */
    @SmallTest
    public void testMultiplication(){

        String strNum1 = "1 0 0";
        String strNum2 = "5 0";
        float expected = 5000;

        /** Inputting Operand1 */
        TouchUtils.tapView(this, mNumber1);
        sendKeys(strNum1);

        /** Inputting Operand2 */
        TouchUtils.tapView(this, mNumber2);
        sendKeys(strNum2);

        /** Initializes Spinner to first position */
        try {
            runTestOnUiThread(new Runnable(){
                @Override
                public void run() {
                    mOperator.setSelection(0);
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
        }

        /** Selects Multiplication operator from the Spinner Widget */
        TouchUtils.tapView(this, mOperator);
        this.sendRepeatedKeys(
            1,KeyEvent.KEYCODE_DPAD_CENTER,
            3,KeyEvent.KEYCODE_DPAD_DOWN,
            1,KeyEvent.KEYCODE_DPAD_CENTER
        );

        /** Performs calculation */
        try {
            runTestOnUiThread(new Runnable() {
            @Override
                public void run() {
                    mBtnResult.performClick();
                }
            });
        } catch (Throwable e1) {
            e1.printStackTrace();
        }

        /** Getting the result shown */
        float actual  = Float.parseFloat(mResult.getText().toString());

        /** Asserts, expected and actual are same */
        assertEquals(expected, actual, 0);
    }

    /** Testing Division operation */
    @SmallTest
    public void testDivision(){

        String strNum1 = "1 0 0";
        String strNum2 = "5 0";
        float expected = 2;

        /** Inputting Operand1 */
        TouchUtils.tapView(this, mNumber1);
        sendKeys(strNum1);

        /** Inputting Operand2 */
        TouchUtils.tapView(this, mNumber2);
        sendKeys(strNum2);

        /** Initializes Spinner to first position */
        try {
            runTestOnUiThread(new Runnable(){
                @Override
                public void run() {
                    mOperator.setSelection(0);
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
        }

        /** Selects Division operator from the Spinner Widget */
        TouchUtils.tapView(this, mOperator);
        this.sendRepeatedKeys(
            1,KeyEvent.KEYCODE_DPAD_CENTER,
            4,KeyEvent.KEYCODE_DPAD_DOWN,
            1,KeyEvent.KEYCODE_DPAD_CENTER
        );

        /** Performs calculation */
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mBtnResult.performClick();
                }
            });
        }catch (Throwable e1) {
            e1.printStackTrace();
        }

        /** Getting the result shown */
        float actual  = Float.parseFloat(mResult.getText().toString());

        /** Asserts, expected and actual are same */
        assertEquals(expected, actual, 0);
    }
}


6. Run this test project

Screenshot of testing in action

Figure 1 : Screenshot of testing in action

 

Showing Test results in JUnit Pane

Figure 2 : Showing Test results in JUnit Pane


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