Android Open Source - droidcon-android-espresso Drawable Matcher






From Project

Back to project page droidcon-android-espresso.

License

The source code is released under:

Apache License

If you think the Android project droidcon-android-espresso listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.xrigau.droidcon.espresso.helper;
/*from www . j ava2s . c  o m*/
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

public class DrawableMatcher extends TypeSafeMatcher<View> {

    private final int resourceId;

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

    private String resourceName = null;
    private Drawable expectedDrawable = null;

    @Override
    public boolean matchesSafely(View target) {
        if (expectedDrawable == null) {
            loadDrawableFromResources(target.getResources());
        }
        if (invalidExpectedDrawable()) {
            return false;
        }

        if (target instanceof ImageView) {
            return hasImage((ImageView) target) || hasBackground(target);
        }
        if (target instanceof TextView) {
            return hasCompoundDrawable((TextView) target) || hasBackground(target);
        }
        return hasBackground(target);
    }

    private void loadDrawableFromResources(Resources resources) {
        try {
            expectedDrawable = resources.getDrawable(resourceId);
            resourceName = resources.getResourceEntryName(resourceId);
        } catch (Resources.NotFoundException ignored) {
            // view could be from a context unaware of the resource id.
        }
    }

    private boolean invalidExpectedDrawable() {
        return expectedDrawable == null;
    }

    private boolean hasImage(ImageView target) {
        return isSameDrawable(target.getDrawable());
    }

    private boolean hasCompoundDrawable(TextView target) {
        if (target.getCompoundDrawables() == null) {
            return false;
        }
        for (Drawable drawable : target.getCompoundDrawables()) {
            if (isSameDrawable(drawable)) {
                return true;
            }
        }
        return false;
    }

    private boolean hasBackground(View target) {
        return isSameDrawable(target.getBackground());
    }

    private boolean isSameDrawable(Drawable drawable) {
        if (drawable == null) {
            return false;
        }
        return expectedDrawable.getConstantState().equals(drawable.getConstantState());
    }

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




Java Source Code List

com.xrigau.droidcon.core.debug.MockClient.java
com.xrigau.droidcon.core.model.PostList.java
com.xrigau.droidcon.core.model.PostShould.java
com.xrigau.droidcon.core.model.Post.java
com.xrigau.droidcon.core.service.Services.java
com.xrigau.droidcon.core.task.NewsTask.java
com.xrigau.droidcon.core.task.TaskExecutorShould.java
com.xrigau.droidcon.core.task.TaskExecutor.java
com.xrigau.droidcon.core.task.Task.java
com.xrigau.droidcon.espresso.AndroidUnitTest.java
com.xrigau.droidcon.espresso.helper.DrawableMatcher.java
com.xrigau.droidcon.espresso.helper.EspressoTestsMatchers.java
com.xrigau.droidcon.espresso.loader.LoaderListener.java
com.xrigau.droidcon.espresso.loader.PostsTaskLoaderCallbacks.java
com.xrigau.droidcon.espresso.loader.PostsTaskLoader.java
com.xrigau.droidcon.espresso.presentation.IdlingResourceActivityTest.java
com.xrigau.droidcon.espresso.presentation.IdlingResourceActivity.java
com.xrigau.droidcon.espresso.presentation.MainActivityTest.java
com.xrigau.droidcon.espresso.presentation.MainActivity.java
com.xrigau.droidcon.espresso.presentation.PostListActivityTest.java
com.xrigau.droidcon.espresso.presentation.PostListActivity.java
com.xrigau.droidcon.espresso.presentation.VisitorActivityTest.java
com.xrigau.droidcon.espresso.presentation.VisitorActivity.java
com.xrigau.droidcon.espresso.presentation.WorldDestructionCountdown.java
com.xrigau.droidcon.espresso.presentation.adapter.EmptyAdapter.java
com.xrigau.droidcon.espresso.presentation.adapter.PostsAdapter.java
com.xrigau.droidcon.espresso.util.DeveloperError.java