美文网首页
【Espresso】withText

【Espresso】withText

作者: 秀叶寒冬 | 来源:发表于2019-08-28 22:36 被阅读0次
    /**
       * Returns a matcher that matches {@link TextView}s based on text property value.
       *
       * <p><b>Note:</b> A View text property is never {@code null}. If you call {@link
       * TextView#setText(CharSequence)} with a {@code null} value it will still be "" (empty string).
       * Do not use a null matcher.
       *
       * @param stringMatcher <a
       *     href="http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html"><code>Matcher
       *     </code></a> of {@link String} with text to match
       */
      public static Matcher<View> withText(final Matcher<String> stringMatcher) {
        return new WithTextMatcher(checkNotNull(stringMatcher));
      }
    

    由上述注释可知,该方法适用于TextView,所以使用onData时不能使用这条属性,即不能像下面那样调用

    onData(withText("item1"));
    

    它可以与onView配合使用,withText返回的是一个WithTextMatcher类型,WithTextMatcher继承自BoundedMatcher,重写了BoundedMatcher的一些方法,其中matchesSafely方法如下:

    @Override
        protected boolean matchesSafely(TextView textView) {
          String text = textView.getText().toString();
          // The reason we try to match both original text and the transformated one is because some UI
          // elements may inherit a default theme which behave differently for SDK below 19 and above.
          // So it is implemented in a backwards compatible way.
          if (stringMatcher.matches(text)) {
            return true;
          } else if (textView.getTransformationMethod() != null) {
            CharSequence transformedText =
                textView.getTransformationMethod().getTransformation(text, textView);
            if (transformedText != null) {
              return stringMatcher.matches(transformedText.toString());
            }
          }
          return false;
        }
    

    由此可见,withText最终找到的对象必须是TextView类型的。

    相关文章

      网友评论

          本文标题:【Espresso】withText

          本文链接:https://www.haomeiwen.com/subject/gcpvectx.html