2015-02-26 22 views
11

Có thể kiểm tra xem màu nền có phù hợp với màu đã cho bằng cà phê không?Kiểm tra màu nền cà phê espresso Android

Tôi đã thực hiện một đối sánh tùy chỉnh, tương tự như những gì @Irfan đã đề xuất, cảm ơn!

public static Matcher<Object> backgroundShouldHaveColor(int expectedColor) { 
    return buttondShouldHaveBackgroundColor(equalTo(expectedColor)); 
} 
private static Matcher<Object> buttonShouldHaveBackgroundColor(final Matcher<Integer> expectedObject) { 
    final int[] color = new int[1]; 
    return new BoundedMatcher<Object, Button>(Button.class) { 
     @Override 
     public boolean matchesSafely(final Button actualObject) { 

      color[0] =((ColorDrawable) actualObject.getBackground()).getColor(); 


      if(expectedObject.matches(color[0])) { 
       return true; 
      } else { 
       return false; 
      } 
     } 
     @Override 
     public void describeTo(final Description description) { 
      // Should be improved! 
      description.appendText("Color did not match "+color[0]); 
     } 
    }; 
} 

Trả lời

7

Tôi không chắc chắn về điều đó nhưng chúng tôi có thể lấy lại màu sắc của một số yếu tố như quan điểm nút và văn bản '

Button button = (Button) findViewById(R.id.my_button); 
Drawable buttonBackground = button.getBackground(); 

và bạn có thể thử như thế này

ColorDrawable b_color = (ColorDrawable) button.getBackground(); 

và sau đó

int color = b_color.getColor(); 
if (colorID == R.color.green) { 
    log("color is green"); 
} 

Hy vọng điều này sẽ giúp bạn bắt đầu.

-2

Xét rằng bạn có trong danh sách màu sắc, tạo ra một mảng trong số họ:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="black">#000000</color> 
    <color name="white">#FFFFFF</color> 
    <array name="colors"> 
     <item>@color/black</item> 
     <item>@color/white</item> 
    </array> 
</resources> 

Sau đó, bạn sẽ có được mảng này và vòng lặp trên nó:

int[] colors = context.getResources().getIntArray(R.array.colors); 

for (int i = 0; i < colors.length; i++) { 
    // Compare your color with 'colors[i]' 
} 
22

Trong các thử nghiệm của tôi, tôi có khớp sau để thử nghiệm EditText màu:

public static Matcher<View> withTextColor(final int color) { 
    Checks.checkNotNull(color); 
    return new BoundedMatcher<View, EditText>(EditText.class) { 
     @Override 
     public boolean matchesSafely(EditText warning) { 
      return color == warning.getCurrentTextColor(); 
     } 
     @Override 
     public void describeTo(Description description) { 
      description.appendText("with text color: "); 
     } 
    }; 
} 

Và cách sử dụng là:

onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED))); 
+0

Bạn nên thay thế EditText cho TextView, kể từ khi kế thừa đầu tiên kể từ sau và getCurrentTextColor() được thực hiện trong TextView. Tôi đã không thử nghiệm nó, nhưng nó sẽ làm việc cho cả EditText và TextView. – Maragues

+0

Cảm ơn bạn đã có mẹo. – denys

+0

hoặc: withTextColor (ContextCompat.getColor (mActivityTestRule.getActivity(), R.color.your_color)) :) – Ciprian

0

Đây là những gì tôi sử dụng trong các thử nghiệm của tôi

public static Matcher<View> withTextColor(final int color) { 
    Checks.checkNotNull(color); 
     return new BoundedMatcher<View, TextView>(TextView.class) { 
      @Override 
      public boolean matchesSafely(TextView textView) { 
       return ContextCompat.getColor(getTargetContext(),color)==textView.getCurrentTextColor(); 
      } 
      @Override 
      public void describeTo(Description description) { 
       description.appendText("with text color: "); 
      } 
     }; 
} 

và gọi nó như

OnView (withId (R.id.price_value)). Séc (matches (withTextColor (R. màu đen)));

1

Một cách tiếp cận khác để kiểm tra màu văn bản của TextView có thể là thông qua hasTextColor(int color) khi nó đến trực tiếp từ Espresso.

import static android.support.test.espresso.matcher.ViewMatchers.hasTextColor; 

onView(withId(R.id.anyTextView)).check(matches(hasTextColor(R.color.red))); 

Hãy nhận biết rằng phương pháp này hiện đang trong Beta cho Espresso 3.0.1

Các vấn đề liên quan