Picasso用Espresso加载测试图像

时间:2018-06-07 18:40:03

标签: android-espresso android-testing functional-testing uitest

我正在开发一个Android应用程序,我正在尝试测试一个显示元素列表的屏幕。该列表使用RecyclerView实现,每个项目都有一个名称(TextView)和一个图像(ImageView)。 为了测试第一个元素名称,我使用下一个代码:

private static int ANY_ELEMENT_POSITION = 0;

@Test
public void thatAnyElementTitleIsDisplayed() {
    givenBuildingActivity();

    whenNoAction();

    thenAnyElementTitleIsDisplayed();
}

private void givenBuildingActivity() {

}

private void whenNoAction() {

}

private void thenAnyElementTitleIsDisplayed() {
    onView(withRecyclerView(R.id.buildings_container)
            .atPositionOnView(ANY_ELEMENT_POSITION, R.id.building_title))
            .check(matches(withText("some name")));
}

该代码片段正在运行。但是现在,我试图测试一个项目的图像正在加载并在屏幕上显示。 首先,我有一个ImageMatcher测试图像资源与图像视图显示的相同。但问题是图像是通过Picasso库显示的,所以它是异步加载的,然后当我运行测试时,它不起作用。我的代码是下一个:

@Test
public void thatAnyElementImageIsDisplayed() {
    givenBuildingActivity();

    whenNoAction();

    thenAnyElementImageIsDisplayed();
}

private void thenAnyElementImageIsDisplayed() {
    onView(withRecyclerView(R.id.buildings_container)
            .atPositionOnView(ANY_ELEMENT_POSITION, R.id.building_image))
            .check(matches(withDrawable(R.drawable.building_placeholder)));
}

DrawableMatcher是下一个:

public class DrawableMatcher extends TypeSafeMatcher<View> {

private final int expectedId;
private String resourceName;
static final int EMPTY = -1;
static final int ANY = -2;

DrawableMatcher(int expectedId) {
    super(View.class);
    this.expectedId = expectedId;
}

@Override
protected boolean matchesSafely(View target) {
    if (!(target instanceof ImageView)) {
        return false;
    }
    ImageView imageView = (ImageView) target;
    if (expectedId == EMPTY) {
        return imageView.getDrawable() == null;
    }
    if (expectedId == ANY) {
        return imageView.getDrawable() != null;
    }
    Resources resources = target.getContext().getResources();
    Drawable expectedDrawable = resources.getDrawable(expectedId);
    resourceName = resources.getResourceEntryName(expectedId);

    if (expectedDrawable == null) {
        return false;
    }

    Bitmap bitmap = getBitmap(imageView.getDrawable());
    Bitmap otherBitmap = getBitmap(expectedDrawable);
    return bitmap.sameAs(otherBitmap);
}

private Bitmap getBitmap(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

@Override
public void describeTo(Description description) {
    description.appendText("with drawable from resource id: ");
    description.appendValue(expectedId);
    if (resourceName != null) {
        description.appendText("[");
        description.appendText(resourceName);
        description.appendText("]");
    }
}

}

总结一下,我认为主要问题是Picasso异步图像加载。那么,有一种方法可以进行我想要的测试吗?

感谢您的回答

0 个答案:

没有答案