LinearLayout getBackground().getColor() return 错误的 int 值
LinearLayout getBackground().getColor() return wrong int value
我正在使用 espresso 进行 UI 测试。
我的老板要我检查某个动作后 linearLayout 是否具有新的正确颜色。我写了一个自定义匹配器,看起来像这样
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: ");
}
};
}
我的问题是提供的颜色和背景颜色的对比不一样。在应用程序中,我可以看到设置了正确的颜色。它是这样完成的:
holder.linearLayout.setBackgroundColor(ctx.getResources().getColor(R.color.grey_300));
一旦测试尝试比较它们彼此不同的值:
Log: CustomMatcher: 17170432 vs -2039584
我这样调用匹配器
.check(matches(withBgColor(R.color.grey_300)));
谁能帮我看看颜色是否匹配?我的方法每次都失败...谢谢
问题是 color 和 color 资源 ID 都实现为整数。您传递的是 R.color.grey_300
的值,它是代表资源 ID 的生成数字,而不是颜色本身。
您应该改为这样匹配:
.check(matches(withBgColor(context.getColor(R.color.grey_300))));
如果您担心 getColor()
被弃用,请改用 ContextCompat.getColor(context, R.color.grey_300)
。
我正在使用 espresso 进行 UI 测试。 我的老板要我检查某个动作后 linearLayout 是否具有新的正确颜色。我写了一个自定义匹配器,看起来像这样
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: ");
}
};
}
我的问题是提供的颜色和背景颜色的对比不一样。在应用程序中,我可以看到设置了正确的颜色。它是这样完成的:
holder.linearLayout.setBackgroundColor(ctx.getResources().getColor(R.color.grey_300));
一旦测试尝试比较它们彼此不同的值:
Log: CustomMatcher: 17170432 vs -2039584
我这样调用匹配器
.check(matches(withBgColor(R.color.grey_300)));
谁能帮我看看颜色是否匹配?我的方法每次都失败...谢谢
问题是 color 和 color 资源 ID 都实现为整数。您传递的是 R.color.grey_300
的值,它是代表资源 ID 的生成数字,而不是颜色本身。
您应该改为这样匹配:
.check(matches(withBgColor(context.getColor(R.color.grey_300))));
如果您担心 getColor()
被弃用,请改用 ContextCompat.getColor(context, R.color.grey_300)
。