被测试的方法(在主要活动中)
//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 吗?