Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2016 01:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/7699042cc86ee86bb04b to your computer and use it in GitHub Desktop.
Save anonymous/7699042cc86ee86bb04b to your computer and use it in GitHub Desktop.
Matchers file from Episode 29 of Caster.IO - Creating Custom ViewMatchers
package com.donnfelker.tasko;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class TaskoMatchers {
public static Matcher<View> withTaskViewName(final String expected) {
return new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View item) {
if(item != null && item.findViewById(R.id.task_item_task_name) != null) {
TextView taskName = (TextView) item.findViewById(R.id.task_item_task_name);
if(TextUtils.isEmpty(taskName.getText())) {
return false;
} else {
return taskName.getText().equals(expected);
}
} else {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("Looked for " + expected + " in the task_item.xml file");
}
};
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment