jeudi 1 octobre 2015

Unit Test for Android TV Application

I'm doing Unit Test on a simple Android TV app, where I've 2 Edit Text for taking values. Below those 2 Edit Text, I've a Text View to display results. Below that Text View, I've 2 buttons horizontally (say, Add and Multiply Button) to perform Addition & Multiplication respectively.

When I do Unit Test for "addition", it works properly but after that when I do Unit Test for "multiplication" it doesn't work, always shows the result of add operation.

@RunWith(AndroidJUnit4.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private MainActivity mainActivity;
    private TextView result;

    private String NUMBER_15 = "1 5 ENTER ";
    private String NUMBER_20 = "2 0 ENTER ";
    private String ADD_RESULT = "35";
    private String MULTIPLY_RESULT = "300";

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        setActivityInitialTouchMode(false);

        mainActivity = getActivity();
        result = (TextView) mainActivity.findViewById(R.id.result);
    }

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

    @Test
    public void addValues() {
        sendKeys(NUMBER_15);
        sendKeys(NUMBER_20);
        sendKeys("ENTER");
        String mathResult = result.getText().toString();
        assertTrue("Add result should be " + ADD_RESULT + " but was " + mathResult, mathResult.equals(ADD_RESULT));
    }

    @Test
    public void testMultiplyValues() {
        sendKeys(NUMBER_15);
        sendKeys(NUMBER_20);
        sendKeys("DPAD_RIGHT");
        sendKeys("ENTER");
        String mathResult = result.getText().toString();
        assertTrue("Multiply result should be " + MULTIPLY_RESULT + " but was " + mathResult, mathResult.equals(MULTIPLY_RESULT));
    }
}

Aucun commentaire:

Enregistrer un commentaire