2

被测试的方法(在主要活动中)

//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);
}

测试项目中的测试:(活动和 i(意图)已设置)

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]);
 }

堆栈跟踪:

junit.framework.AssertionFailedError:预期:android.graphics.drawable.BitmapDrawable@413e7428 但是:android.graphics.drawable.BitmapDrawable@413e75c0

问题

我假设对象不一样。所以我试图研究获取资源 ID 并比较它们,但我认为这不可能?

还有另一种方法来比较按钮上显示的 Drawable 吗?

4

1 回答 1

3

遗憾的是,如果您的代码中没有任何工作,这是不可能的,因为您正确地注意到没有办法从中获取可绘制资源 ID。你要做的是在你的代码中设置drawable让它做一些事情,比如在imageview上设置一个标签,并用它来检查它是否有正确的标签。尽管这可能是对您的欺骗,并且不是一个精确的测试,但只要您测试此按钮的所有状态以及应该显示什么可绘制对象,它可能就足够了。因此,在同一视图上设置 Drawable id 后,调用 setTag(R.id.xxx) 为其提供与您期望的 drawabe 相关的 int。

于 2012-11-20T23:24:48.193 回答