TextViewHasTextMatcher.java revision 4ac725f9b4cebbf46805fc5e9b2f0eaf3fdd9b29
1package com.xtremelabs.robolectric.matchers; 2 3import android.widget.TextView; 4import org.hamcrest.Description; 5import org.hamcrest.Factory; 6import org.hamcrest.Matcher; 7import org.junit.internal.matchers.TypeSafeMatcher; 8 9public class TextViewHasTextMatcher<T extends TextView> extends TypeSafeMatcher<T> { 10 private String expected; 11 private String actualText; 12 13 public TextViewHasTextMatcher(String expected) { 14 this.expected = expected; 15 } 16 17 @Override 18 public boolean matchesSafely(T actual) { 19 if (actual == null) { 20 return false; 21 } 22 final CharSequence charSequence = actual.getText(); 23 if (charSequence == null || charSequence.toString() == null) { 24 return false; 25 } 26 actualText = charSequence.toString(); 27 return actualText.equals(expected); 28 } 29 30 31 @Override 32 public void describeTo(Description description) { 33 description.appendText("[" + actualText + "]"); 34 description.appendText(" to equal "); 35 description.appendText("[" + expected + "]"); 36 } 37 38 @Factory 39 public static <T extends TextView> Matcher<T> hasText(String expectedTextViewText) { 40 return new TextViewHasTextMatcher<T>(expectedTextViewText); 41 } 42} 43