-
-
Save drakeet/a28b21bb96b54fd1be990b9a3b0923e3 to your computer and use it in GitHub Desktop.
Espresso & Brioche
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomMatchers { | |
public static Matcher<View> withBackground(final int resourceId) { | |
return new TypeSafeMatcher<View>() { | |
@Override | |
public boolean matchesSafely(View view) { | |
return sameBitmap(view.getContext(), view.getBackground(), resourceId); | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("has background resource " + resourceId); | |
} | |
}; | |
} | |
public static Matcher<View> withCompoundDrawable(final int resourceId) { | |
return new BoundedMatcher<View, TextView>(TextView.class) { | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("has compound drawable resource " + resourceId); | |
} | |
@Override | |
public boolean matchesSafely(TextView textView) { | |
for (Drawable drawable : textView.getCompoundDrawables()) { | |
if (sameBitmap(textView.getContext(), drawable, resourceId)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
}; | |
} | |
public static Matcher<View> withImageDrawable(final int resourceId) { | |
return new BoundedMatcher<View, ImageView>(ImageView.class) { | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("has image drawable resource " + resourceId); | |
} | |
@Override | |
public boolean matchesSafely(ImageView imageView) { | |
return sameBitmap(imageView.getContext(), imageView.getDrawable(), resourceId); | |
} | |
}; | |
} | |
private static boolean sameBitmap(Context context, Drawable drawable, int resourceId) { | |
Drawable otherDrawable = context.getResources().getDrawable(resourceId); | |
if (drawable == null || otherDrawable == null) { | |
return false; | |
} | |
if (drawable instanceof StateListDrawable && otherDrawable instanceof StateListDrawable) { | |
drawable = drawable.getCurrent(); | |
otherDrawable = otherDrawable.getCurrent(); | |
} | |
if (drawable instanceof BitmapDrawable) { | |
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); | |
Bitmap otherBitmap = ((BitmapDrawable) otherDrawable).getBitmap(); | |
return bitmap.sameAs(otherBitmap); | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EspressoTest extends ActivityInstrumentationTestCase2<ShoppingActivity> { | |
@Override | |
public void setUp() throws Exception { | |
super.setUp(); | |
setupInjections(); | |
setupPreferences(); | |
getActivity(); | |
} | |
public void test_the_background_is_visible() throws Exception { | |
onView(withImageDrawable(R.drawable.some_background)).check(matches(isDisplayed())); | |
} | |
public void test_the_card_has_a_specific_image() throws Exception { | |
onView(withId(R.id.some_card)).check(matches(withImageDrawable(R.drawable.my_card)); | |
} | |
public void test_this_textview_is_reused_so_we_have_to_manually_look_for_it() throws Exception { | |
onView(allOf(withId(R.id.some_card), withParent(withId(R.id.textview_parent_left)))).check(matches(withCompoundDrawable(R.drawable.left_drawable)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment