All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xtremelabs.robolectric.matchers.ImageViewHasDrawableMatcher Maven / Gradle / Ivy

The newest version!
package com.xtremelabs.robolectric.matchers;

import android.widget.ImageView;
import com.xtremelabs.robolectric.res.ResourceLoader;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.junit.internal.matchers.TypeSafeMatcher;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

public class ImageViewHasDrawableMatcher extends TypeSafeMatcher {
    private int expectedResourceId;
    private String message;

    public ImageViewHasDrawableMatcher(int expectedResourceId) {
        this.expectedResourceId = expectedResourceId;
    }

    @Override
    public boolean matchesSafely(T actualImageView) {
        if (actualImageView == null) {
            return false;
        }

        ResourceLoader resourceLoader = ResourceLoader.getFrom(actualImageView.getContext());

        int actualResourceId = shadowOf(actualImageView).getResourceId();
        String actualName = nameOrUnset(resourceLoader, actualResourceId);
        String expectedName = nameOrUnset(resourceLoader, expectedResourceId);
        message = "[" + actualResourceId + " (" + actualName + ")] to equal [" + expectedResourceId + " (" + expectedName + ")]";
        return actualResourceId == expectedResourceId;
    }

    private String nameOrUnset(ResourceLoader resourceLoader, int resourceId) {
        return resourceId == 0 ? "unset" : resourceLoader.getNameForId(resourceId);
    }


    @Override
    public void describeTo(Description description) {
        description.appendText(message);
    }

    @Factory
    public static  Matcher hasDrawable(int expectedResourceId) {
        return new ImageViewHasDrawableMatcher(expectedResourceId);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy