1

Robotiumユニットテストクラスを作成します。私のアプリにはボタンがあります。このボタンは、コードの結果に応じて背景色を変更します。私の質問は、ボタンの色をどのように主張できるかということです。

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

しかし、これは私に負の大きな数を表示します。どうすればもっと適切なものを入手できますか?ありがとう

4

1 に答える 1

0

まず、getPaint()メソッドを使用してボタンの背景色を取得することはできません。 getPaint().getColor()ボタンのテキストの色をint値として表示します。次のようなコードの答えとして負の値を持つのはごく普通のことです。

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

以下は、 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 」は、「コード内のいくつかの結果に応じて」とあなたが言及したものです。

于 2012-08-30T05:58:12.700 に答える