1 package com.xtremelabs.robolectric.matchers; 2 3 import android.widget.TextView; 4 import org.hamcrest.Description; 5 import org.hamcrest.Factory; 6 import org.hamcrest.Matcher; 7 import org.junit.internal.matchers.TypeSafeMatcher; 8 9 public class TextViewHasTextMatcher<T extends TextView> extends TypeSafeMatcher<T> { 10 private String expected; 11 private String actualText; 12 TextViewHasTextMatcher(String expected)13 public TextViewHasTextMatcher(String expected) { 14 this.expected = expected; 15 } 16 17 @Override matchesSafely(T actual)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 describeTo(Description description)32 public void describeTo(Description description) { 33 description.appendText("[" + actualText + "]"); 34 description.appendText(" to equal "); 35 description.appendText("[" + expected + "]"); 36 } 37 38 @Factory hasText(String expectedTextViewText)39 public static <T extends TextView> Matcher<T> hasText(String expectedTextViewText) { 40 return new TextViewHasTextMatcher<T>(expectedTextViewText); 41 } 42 } 43