浓咖啡点击drawables

时间:2017-07-03 17:33:36

标签: java android unit-testing android-espresso

我正在使用Espresso测试Android中的屏幕。在我的屏幕中,我有一个我正在测试的可绘制图像。我发现测试有困难。以下是我正在使用的代码:

@Test
public void clickOnRefreshMenuButton_showsRecipesSuccesfullySyncedMessage() 
{
    // Checks clicking on the refresh menu item.
    onView(withId(R.id.action_refresh))
            .check(matches(withDrawable(R.drawable.ic_refresh_white_24dp)));

    // Checks if the "Recipe sync completed." message is displayed.
    onView(withText(R.string.recipe_list_sync_completed)).
            inRoot(withDecorView(not(is(activityTestRule
                .getActivity()
                .getWindow()
                .getDecorView()))))
            .check(matches(isDisplayed()));
}

static class DrawableMatcher extends TypeSafeMatcher<View> {
    private final int expectedId;
    private String resourceName;
    static final int EMPTY = -1;
    static final int ANY = -2;

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

    @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, null);
        resourceName = resources.getResourceEntryName(expectedId);

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

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

    @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("]");
        }
    }

    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;
    }
}

我的XML文件是一个菜单,它会刷新列表中的项目:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_refresh_white_24dp"
    android:title="@string/action_refresh"
    app:showAsAction="ifRoom"/>

</menu>

StackTrace Caused by: junit.framework.AssertionFailedError: 'with drawable 
from resource id: <2130837605>' doesn't match the selected view. Expected: 
with drawable from resource id: <2130837605> Got: 
"ActionMenuItemView{id=2131558605, res-name=action_refresh, desc=Refresh, 
visibility=VISIBLE, width=123, height=122, has-focus=false, has-
focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, 
is-focused=false"

0 个答案:

没有答案