简体   繁体   English

使用Robotium进行Android单元测试

[英]Android Unit Testing with Robotium

I make a Robotium Unit Test class. 我参加了Robotium单元测试课程。 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()方法获取Button背景的颜色。 getPaint().getColor() will gives you the Text color of the button as int value. getPaint().getColor()将给您按钮的文本颜色作为int值。 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 . 下面是在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 ". i ”是您提到的“ 取决于代码中的某些结果 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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