Issue
I am implementig UI Tests using espresso. My boss wants me to check that after a certain action a linearLayout has a new and the correct color. I've wrote a custom matcher which looks like this
public static Matcher<View> withBgColor(final int color) {
Checks.checkNotNull(color);
return new BoundedMatcher<View, LinearLayout>(LinearLayout.class) {
@Override
public boolean matchesSafely(LinearLayout layout) {
MyLog.debug(String.valueOf(color) + " vs " + String.valueOf(((ColorDrawable) layout.getBackground()).getColor()));
return color == ((ColorDrawable) layout.getBackground()).getColor();
//return color == (((PaintDrawable) layout.getBackground()).getPaint()).getColor();
}
@Override
public void describeTo(Description description) {
description.appendText("With background color: ");
}
};
}
My problem is that the comparison of the provided color and the color from the background are not the same. In the app I can see that the right color is set. It is done like this:
holder.linearLayout.setBackgroundColor(ctx.getResources().getColor(R.color.grey_300));
As soon as the test tries to compare the values they differ from each other:
Log: CustomMatcher: 17170432 vs -2039584
I call the matcher like this
.check(matches(withBgColor(R.color.grey_300)));
Can anyone help me how I can check if the color matches? The way I did fails everytime... Thanks
Solution
The problem is that both a color and a color resource ID are implemented as integers. You are passing the value of R.color.grey_300
, which is a generated number representing the resource ID, instead of the color itself.
You should instead match in this way:
.check(matches(withBgColor(context.getColor(R.color.grey_300))));
If you are worried that getColor()
is deprecated, use ContextCompat.getColor(context, R.color.grey_300)
instead.
Answered By - npace
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.