2012-11-20 27 views
2

方法進行測試(在主要活動中)Android的測試:檢查LeftDrawable圖像是正確的

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

測試在測試項目:(活動和我(意向)已設置)

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: expected:[email protected] but was:[email protected]

問題

我假設對象不一樣。所以我試圖調查獲取資源ID並比較它們,但我認爲這不可能?

是否有另一種方法來比較顯示在按鈕上的Drawable?

回答

3

不幸的是,這是不可能的,沒有任何工作在你的代碼中,因爲你正確地注意到沒有辦法從它得到drawables資源ID。你將不得不做的是在你的代碼中設置drawable讓它做一些事情,比如在imageview上設置一個標籤,並用它來檢查它是否有正確的標籤。儘管這可能是對你的說謊,並不是一個確切的測試,但只要你測試了這個按鈕的所有狀態以及應該顯示什麼樣的drawable,它可能就足夠了。因此,在同一個視圖中設置Drawable id後,調用setTag(R.id.xxx),將其與您期望的drawabe相關的int賦予它。

+0

我想標籤是唯一的出路。謝謝。 –

相關問題