문제

Method being Tested (In Main Activity)

//This Method allows you to set the LeftDrawable Image to a grayed out (drawableDown)
//image for 300 millseconds then returns the LeftDrawable Image back to the orignal 
//(drawableUp).On a button.

void buttonPressFeedbackLeft(final Button button, final int drawableDown,
        final int drawableUp) {
    button.setCompoundDrawablesWithIntrinsicBounds(drawableDown, 0, 0, 0);

    button.postDelayed(new Runnable() {
        public void run() {
            button.setCompoundDrawablesWithIntrinsicBounds(drawableUp, 0,
                    0, 0);
        }
    }, 300);
}

Test in Test Project: (activity and i (intent) already set)

public void testbuttonPressFeedback(){
         /*
          * Start activity
          */
         activity = startActivity(i, null, null);

         /*
          * get the backButton
          */
         Button backButton = (Button) activity.findViewById(R.id.bottem_bar_prev);

         /*
          * get the backButton image (up and down image) as a Drawable
          */
         int drawableDown = R.drawable.ic_menu_back_gray;
         int drawableUp = R.drawable.ic_menu_back;
         /*
          * create a Drawable with the ResId
          */
         Drawable drawableDownImage = activity.getResources().getDrawable(drawableDown);
         Drawable drawableUpImage = activity.getResources().getDrawable(drawableDown);

         /*
          * Simulate button being pressed (running the buttonPressFeedback() method)
          */
         activity.buttonPressFeedbackLeft(prevButton, drawableDown, drawableUp);

         /*
          * get the drawable array [left, top, right, bottom]
          * compare drawable with the drawable found on the button.
          */
         Drawable[] d = prevButton.getCompoundDrawables();
         assertEquals(drawableDownImage,d[0]);
 }

Stack Trace:

junit.framework.AssertionFailedError: expected:android.graphics.drawable.BitmapDrawable@413e7428 but was:android.graphics.drawable.BitmapDrawable@413e75c0

Question

I am assuming that the objects are not the same. So I have tried to look into getting the resource Id and comparing them but I don't think this is possible?

Is there another way to compare the Drawable which is being displayed on the Button?

도움이 되었습니까?

해결책

Sadly this not possible without any work in your code, as you correctly note there is no way to get the drawables resource ID from it. What you are going to have to do is in your code that sets the drawable have it do something like set a tag on the imageview and use that to check it has the correct tag. It is possible that this might be lying to you though and is not an exact test but as long as you test all the states of this button and what drawable should be shown it is likely to be sufficient. So after you set the Drawable id on the same view call the setTag(R.id.xxx) to give it the int that relates to the drawabe you expect.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top