(4.5.5.5)Espresso的进阶: ViewAssertions

一、ViewAssertion

public interface ViewAssertion {

  /**
   * Checks the state of the given view (if such a view is present).
   *
   * @param view the view, if one was found during the view interaction or null if it was not
   * (which may be an acceptable option for an assertion)
   * @param noViewFoundException an exception detailing why the view could not be found or null if
   * the view was found
   */
  void check(View view, NoMatchingViewException noViewFoundException);
}

二、支持函数

2.1 matches(Matcher)

我把这个函数放在首位,就是由于借助 这个函数,我们就可以完全灵活使用 上一章 (4.5.5.4)Espresso的进阶: OnView & onData & Matchers的强大Matcher功能

ViewAssertion matches(final Matcher<? super View> viewMatcher)

2.2 Layout Assertions

android.support.test.espresso.assertion.LayoutAssertions
  • noEllipsizedText 其子view中的TextView都不是Ellipsized
  /**
   * Returns a {@link ViewAssertion} that asserts that view hierarchy does not contain
   * ellipsized or cut off text views.
   */
  public static ViewAssertion noEllipsizedText() {
    return selectedDescendantsMatch(
        isAssignableFrom(TextView.class), not(hasEllipsizedText()));
  }
  • noMultilineButtons 其子view中的Button都不是 Multiline
  /**
   * Returns a {@link ViewAssertion} that asserts that view hierarchy does not contain
   * multiline buttons.
   */
  public static ViewAssertion noMultilineButtons() {
    return selectedDescendantsMatch(
        isAssignableFrom(Button.class), not(hasMultilineText()));
  }
  • noOverlaps
  /**
   * Returns a {@link ViewAssertion} that asserts that descendant views matching the selector
   * do not overlap each other.
   * <p>
   * Example: onView(rootView).check(noOverlaps(isAssignableFrom(TextView.class));
   */
  public static ViewAssertion noOverlaps(final Matcher<View> selector)

2.3 Position Assertions

  • isLeftOf(Matcher matcher)
  • isRightOf(Matcher matcher)
  • isLeftAlignedWith(Matcher matcher)
  • isRightAlignedWith(Matcher matcher)
  • isAbove(Matcher matcher)
  • isBelow(Matcher matcher)
  • isBottomAlignedWith(Matcher matcher)
  • isTopAlignedWith(Matcher matcher)

2.4 其他

  • doesNotExist() 指定View不存在
Returns an assert that ensures the view matcher does not find any matching view in the hierarchy.
  • selectedDescendantsMatch
  /**
   * Returns a generic {@link ViewAssertion} that asserts that the descendant views selected by the
   * selector match the specified matcher.
   * selector选中的view的子节点是否都满足matcher
   *  Example: onView(rootView).check(selectedDescendantsMatch(
   * not(isAssignableFrom(TextView.class)), hasContentDescription()));
   */
  public static ViewAssertion selectedDescendantsMatch(
      final Matcher<View> selector, final Matcher<View> matcher) 

三、AdapterView包含某数据

使用matches() + Matcher< data >()

3.1 遵循adpater协议的

    /**
     * AdapterView
     * 依赖于getItem(), 如果getItem()返回数据不对,则自行重构,可参考LegWorkMatcher
     * 根据 Matcher<Object> 查找对应 view
     */
private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) {
  return new TypeSafeMatcher<View>() {

    @Override
    public void describeTo(Description description) {
      description.appendText("with class name: ");
      dataMatcher.describeTo(description);
    }

    @Override
    public boolean matchesSafely(View view) {
      if (!(view instanceof AdapterView)) {
        return false;
      }
      @SuppressWarnings("rawtypes")
      Adapter adapter = ((AdapterView) view).getAdapter();
      for (int i = 0; i < adapter.getCount(); i++) {
        if (dataMatcher.matches(adapter.getItem(i))) {
          return true;
        }
      }
      return false;
    }
  };
}
@SuppressWarnings("unchecked")
public void testDataItemNotInAdapter(){
  onView(withId(R.id.list))
      .check(matches(not(withAdaptedData(withItemContent("item: 168")))));
  }

3.2

 /**
     * AdapterView
     * 不依赖于getItem()
     * 根据 Matcher<Object> 查找对应 view
     */
    public static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) {
        return new TypeSafeMatcher<View>() {

            @Override
            public void describeTo(Description description) {
                description.appendText("with class name: ");
                dataMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                if (!(view instanceof AdapterView)) {
                    return false;
                }
                @SuppressWarnings("rawtypes")
                BaseLegWrkListActivity.BaseLegwrkListAdapter adapter = null;
                if( ((AdapterView) view).getAdapter() instanceof HeaderViewListAdapter){
                    adapter = (BaseLegWrkListActivity.BaseLegwrkListAdapter) ( (HeaderViewListAdapter) ((AdapterView) view).getAdapter()).getWrappedAdapter();
                } else{
                    adapter = (BaseLegWrkListActivity.BaseLegwrkListAdapter) ((AdapterView) view).getAdapter();
                }
               for (int i = 0; i < adapter.getCount(); i++) {
                    if (dataMatcher.matches(adapter.getLegWorkLineVo(i))) {
                        return true;
                    }
                }
                return false;
            }
        };
    }
        onView(isAssignableFrom(ListView.class)).check(matches(LegWorkMatcher.withAdaptedData(LegWorkMatcher.searchMainItemWithName(legworkName))));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值