-
-
Save lsurvila/d205bb933d6c6a5cb655c35ce048927f 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
/** | |
* Custom Espresso matchers. Includes image drawable matching. | |
*/ | |
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) { | |
final Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); | |
final Bitmap otherBitmap = ((BitmapDrawable) otherDrawable).getBitmap(); | |
return bitmap.sameAs(otherBitmap); | |
} else if (drawable instanceof VectorDrawable) { | |
final Bitmap bitmap = getBitmapFromVectorDrawable(drawable); | |
final Bitmap otherBitmap = getBitmapFromVectorDrawable(otherDrawable); | |
return bitmap.sameAs(otherBitmap); | |
} | |
return false; | |
} | |
private static Bitmap getBitmapFromVectorDrawable(@NonNull Drawable vectorDrawable) { | |
final Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), | |
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); | |
final Canvas canvas = new Canvas(bitmap); | |
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
vectorDrawable.draw(canvas); | |
return bitmap; | |
} | |
} |
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