Java Code Examples for androidx.test.espresso.util.HumanReadables#getViewHierarchyErrorMessage()

The following examples show how to use androidx.test.espresso.util.HumanReadables#getViewHierarchyErrorMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AmbiguousViewMatcherException.java    From android-test with Apache License 2.0 6 votes vote down vote up
private static String getErrorMessage(Builder builder) {
  String errorMessage = "";
  if (builder.includeViewHierarchy) {
    ImmutableSet<View> ambiguousViews =
        ImmutableSet.<View>builder()
            .add(builder.view1, builder.view2)
            .add(builder.others)
            .build();
    errorMessage =
        HumanReadables.getViewHierarchyErrorMessage(
            builder.rootView,
            Lists.newArrayList(ambiguousViews),
            String.format(
                Locale.ROOT,
                "'%s' matches multiple views in the hierarchy.",
                builder.viewMatcher),
            "****MATCHES****");
  } else {
    errorMessage =
        String.format(
            Locale.ROOT, "Multiple Ambiguous Views found for matcher %s", builder.viewMatcher);
  }
  return errorMessage;
}
 
Example 2
Source File: NoMatchingViewException.java    From android-test with Apache License 2.0 6 votes vote down vote up
private static String getErrorMessage(Builder builder) {
  String errorMessage = "";
  if (builder.includeViewHierarchy) {
    String message =
        String.format(
            Locale.ROOT, "No views in hierarchy found matching: %s", builder.viewMatcher);
    if (builder.adapterViewWarning.isPresent()) {
      message = message + builder.adapterViewWarning.get();
    }
    errorMessage =
        HumanReadables.getViewHierarchyErrorMessage(
            builder.rootView, null /* problemViews */, message, null /* problemViewSuffix */);
  } else {
    errorMessage =
        String.format(Locale.ROOT, "Could not find a view that matches %s", builder.viewMatcher);
  }
  return errorMessage;
}
 
Example 3
Source File: ViewAssertions.java    From android-test with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void check(View view, NoMatchingViewException noViewException) {
  Preconditions.checkNotNull(view);

  final Predicate<View> viewPredicate =
      new Predicate<View>() {
        @Override
        public boolean apply(View input) {
          return selector.matches(input);
        }
      };

  Iterator<View> selectedViewIterator =
      Iterables.filter(breadthFirstViewTraversal(view), viewPredicate).iterator();

  List<View> nonMatchingViews = new ArrayList<>();
  while (selectedViewIterator.hasNext()) {
    View selectedView = selectedViewIterator.next();

    if (!matcher.matches(selectedView)) {
      nonMatchingViews.add(selectedView);
    }
  }

  if (nonMatchingViews.size() > 0) {
    String errorMessage =
        HumanReadables.getViewHierarchyErrorMessage(
            view,
            nonMatchingViews,
            String.format(
                Locale.ROOT,
                "At least one view did not match the required matcher: %s",
                matcher),
            "****DOES NOT MATCH****");
    throw new AssertionFailedError(errorMessage);
  }
}