Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing background color espresso Android

Is it possible to check if the background color matches a given color with espresso?

Update:

I made a custom matcher, similar to what @Irfan suggested, thanks!

public static Matcher<Object> backgroundShouldHaveColor(int expectedColor) {
    return buttondShouldHaveBackgroundColor(equalTo(expectedColor));
}
private static Matcher<Object> buttonShouldHaveBackgroundColor(final Matcher<Integer> expectedObject) {
    final int[] color = new int[1];
    return new BoundedMatcher<Object, Button>( Button.class) {
        @Override
        public boolean matchesSafely(final Button actualObject) {

            color[0] =((ColorDrawable) actualObject.getBackground()).getColor();


            if( expectedObject.matches(color[0])) {
                return true;
            } else {
                return false;
            }
        }
        @Override
        public void describeTo(final Description description) {
            // Should be improved!
            description.appendText("Color did not match "+color[0]);
        }
    };
}
like image 303
HowieH Avatar asked Feb 26 '15 12:02

HowieH


People also ask

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.

What is Espresso UI testing?

Espresso is an open source android user interface (UI) testing framework developed by Google. The term Espresso is of Italian origin, meaning Coffee. Espresso is a simple, efficient and flexible testing framework.

What is the use of espresso in Android?

Espresso is a testing framework that helps developers write automation test cases for user interface (UI) testing. It has been developed by Google and aims to provide a simple yet powerful framework.


1 Answers

In my tests I have the following matcher for testing EditText color:

public static Matcher<View> withTextColor(final int color) {
    Checks.checkNotNull(color);
    return new BoundedMatcher<View, EditText>(EditText.class) {
        @Override
        public boolean matchesSafely(EditText warning) {
            return color == warning.getCurrentTextColor();
        }
        @Override
        public void describeTo(Description description) {
            description.appendText("with text color: ");
        }
    };
}

And usage is :

onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED)));
like image 81
denys Avatar answered Sep 19 '22 20:09

denys