【问题标题】:Testing background color espresso Android测试背景颜色浓缩咖啡Android
【发布时间】:2015-02-26 12:28:48
【问题描述】:

是否可以检查背景颜色是否与浓缩咖啡的给定颜色匹配?

更新:

我做了一个自定义匹配器,类似于@Irfan 建议的,谢谢!

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]);
        }
    };
}

【问题讨论】:

    标签: android testing android-espresso


    【解决方案1】:

    在我的测试中,我有以下匹配器用于测试 EditText 颜色:

    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: ");
            }
        };
    }
    

    而用法是:

    onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED)));
    

    【讨论】:

    • 您应该将 EditText 替换为 TextView,因为第一个继承自后者,而 getCurrentTextColor() 是在 TextView 中实现的。我还没有测试过,但它应该适用于 EditText 和 TextView。
    • 或者:withTextColor(ContextCompat.getColor(mActivityTestRule.getActivity(), R.color.your_color)) :)
    • 我觉得应该是:return getTargetContext().getColor(color) == warning.getCurrentTextColor();
    • 有没有人见过这种方法,一般来说hasBackground 方法在 Android 28 上失败了?
    • Kotlin 不知道'withTextColor'
    【解决方案2】:

    我不确定,但我们可以检索一些元素的颜色,例如按钮和文本视图`

    Button button = (Button) findViewById(R.id.my_button);
    Drawable buttonBackground = button.getBackground();
    

    你可以这样尝试

    ColorDrawable b_color = (ColorDrawable) button.getBackground();
    

    然后

    int color = b_color.getColor();
    if (color == R.color.green) {
        log("color is green");
    }
    

    希望这能让你开始。

    【讨论】:

      【解决方案3】:

      我迟到了,但这可能对其他人有帮助

          public static Matcher<View> matchesBackgroundColor(final int expectedResourceId) {
          return new BoundedMatcher<View, View>(View.class) {
              int actualColor;
              int expectedColor;
              String message;
      
              @Override
              protected boolean matchesSafely(View item) {
                  if (item.getBackground() == null) {
                      message = item.getId() + " does not have a background";
                      return false;
                  }
                  Resources resources = item.getContext().getResources();
                  expectedColor = ResourcesCompat.getColor(resources, expectedResourceId, null);
      
                  try {
                      actualColor = ((ColorDrawable) item.getBackground()).getColor();
                  }
                  catch (Exception e) {
                      actualColor = ((GradientDrawable) item.getBackground()).getColor().getDefaultColor();
                  }
                  finally {
                      if (actualColor == expectedColor) {
                          Timber.i("Success...: Expected Color " + String.format("#%06X", (0xFFFFFF & expectedColor))
                                  + " Actual Color " + String.format("#%06X", (0xFFFFFF & actualColor)));
                      }
                  }
                  return actualColor == expectedColor;
              }
              @Override
              public void describeTo(final Description description) {
                  if (actualColor != 0) { message = "Background color did not match: Expected "
                          +  String.format("#%06X", (0xFFFFFF & expectedColor))
                          + " was " + String.format("#%06X", (0xFFFFFF & actualColor));
                  }
                  description.appendText(message);
              }
          };
      }
      

      我还以十六进制格式记录了预期和实际颜色,这可能会有所帮助。

      用法:onView(withId(R.id.viewId)).check(matches(matchesBackgroundColor(R.color.colorId)));

      【讨论】:

      • 非常感谢。这有助于检查工具栏的背景颜色。
      【解决方案4】:

      检查 TextView 文本颜色的另一种方法是通过 hasTextColor(int color),因为它直接来自 Espresso

      import static android.support.test.espresso.matcher.ViewMatchers.hasTextColor;
      
      onView(withId(R.id.anyTextView)).check(matches(hasTextColor(R.color.red)));
      

      请注意,此方法目前处于 Espresso 3.0.1 的测试版中

      【讨论】:

        【解决方案5】:

        可以进行颜色测试。我创建了一种方法来测试TextView 背景颜色,如下所示。另请注意,我正在传递颜色资源。

         private fun withBackgroundColor(@ColorInt color: Int): Matcher<View> {
                Checks.checkNotNull(color)
                return object : BoundedMatcher<View, TextView>(TextView::class.java) {
                    override fun describeTo(description: Description?) {
                        description?.appendText("TextView background color to be $color")
                    }
        
                    override fun matchesSafely(item: TextView?): Boolean {
                        val backgroundColor = item?.background as ColorDrawable
                        val colorDrawable = ColorDrawable(ContextCompat.getColor(item.context, color))
                        return colorDrawable.color == backgroundColor.color
                    }
        
                }
            }
        

        我从颜色资源创建ColorDrawable 对象并从对象中获取颜色。

        比较没有ColorDrawable 的资源的颜色不会通过。希望对您有所帮助。

        【讨论】:

          【解决方案6】:
          private fun hasBackgroundColor(colorRes: Int): Matcher<View> {
              Checks.checkNotNull(colorRes)
          
              return object : TypeSafeMatcher<View>() {
                  override fun describeTo(description: Description?) {
                      description?.appendText("background color: $colorRes")
                  }
          
                  override fun matchesSafely(item: View?): Boolean {
          
                      val context = item?.context
                      val textViewColor = (item?.background as ColorDrawable).color
                      val expectedColor: Int?
          
                      if (Build.VERSION.SDK_INT <= 22) {
                          expectedColor = context?.resources?.getColor(colorRes)
                      } else {
                          expectedColor = context?.getColor(colorRes)
                      }
          
                      return textViewColor == expectedColor
                  }
              }
          }
          

          【讨论】:

            【解决方案7】:

            这是我在测试中使用的内容

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

            并将其称为

            onView(withId(R.id.price_value)).check(matches(withTextColor(R.color.black)));

            【讨论】:

              【解决方案8】:

              通过 SAM 转换在 Kotlin 中实现更简洁优雅的方式

              onView(withId(R.id.tv_login_result)).check{
                          view, _ ->
                      val actualColor = ((view as MaterialTextView).background as 
                                         ColorDrawable).color
                      assertEquals(Color.RED, actualColor)
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2018-06-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-12-15
                • 1970-01-01
                相关资源
                最近更新 更多