简体   繁体   中英

Android Unit Testing with Robotium

I make a Robotium Unit Test class. In my app i have a button. This button changes background color depending on some results into the code. My question is how can i assert the color of the button I try something like this

assertEquals("", scStatusButton.getPaint().getColor());

But this displays me a negative big number. How can i obtain something more apropriate? Thanks

First of all, you cannot get the Button backgroung color by using getPaint() method. getPaint().getColor() will gives you the Text color of the button as int value. It is pretty normal to having a negative vale as the answer for a code like;

int i = colorButton.getPaint().getColor();

Following is a way of assert the color of the button in unit testing with Robotium .

package com.anuja.bu.test;

import android.graphics.drawable.ColorDrawable;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;

import com.anuja.bu.BuHomeActivity;
import com.anuja.bu.R;
import com.jayway.android.robotium.solo.Solo;

public class TestBuHomeActivity extends ActivityInstrumentationTestCase2<BuHomeActivity> {

    private Solo solo;

    public TestBuHomeActivity() {
        super("com.anuja.bu", BuHomeActivity.class);        
    }

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

        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testButtonColor(){

        int i = 1;

        Button colorTestButton = (Button) solo.getView(R.id.buHomeActivity_color_button);

        solo.clickOnButton("Color");

        ColorDrawable colorDrawable = (ColorDrawable) colorTestButton.getBackground();
        int buttonColorValue = colorDrawable.getColor();

        if(i == 0){
            assertTrue(buttonColorValue == -65536); // Red
        }else{
            assertTrue(buttonColorValue == -16711936); // Green
        }
    }

    @Override
    protected void tearDown() throws Exception {

        solo.finishOpenedActivities();
    }
}

" i " is the thing that you have mentioned as " depending on some results in the code ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM