(4.5.5.5)Espresso的进阶: ViewAssertions

来源:互联网 发布:数据分析研究生专业 编辑:程序博客网 时间:2024/06/06 20:33

    • 一ViewAssertion
    • 二支持函数
      • 1 matchesMatcher
      • 2 Layout Assertions
      • 3 Position Assertions
      • 4 其他
    • 三AdapterView包含某数据
      • 1 遵循adpater协议的
      • 2

一、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
原创粉丝点击