android.support.test.espresso.matcher.BoundedMatcher Java Examples

The following examples show how to use android.support.test.espresso.matcher.BoundedMatcher. 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: RecyclerViewEspressoFactory.java    From ChipsLayoutManager with Apache License 2.0 6 votes vote down vote up
public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) {
    checkNotNull(itemMatcher);
    return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has item at position " + position + ":\n");
            itemMatcher.describeTo(description);
        }

        @Override
        protected boolean matchesSafely(final RecyclerView view) {
            RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position);
            return viewHolder != null && itemMatcher.matches(viewHolder.itemView);
        }
    };
}
 
Example #2
Source File: RegularExpressionMatcher.java    From aws-device-farm-sample-app-for-android with Apache License 2.0 6 votes vote down vote up
/**
 * Matches a string to a specific pattern
 *
 * @param pattern regular expression
 * @return the matcher result
 */
public static Matcher<View> matchesPattern(final String pattern){
    checkNotNull(pattern);
    return new BoundedMatcher<View, TextView>(TextView.class) {
        @Override
        public void describeTo(final Description description) {
            description.appendText("The textview does not conform to the pattern: ")
                    .appendText(pattern);
        }

        @Override
        protected boolean matchesSafely(TextView textView) {
            return textView.getText().toString().matches(pattern);
        }
    };
}
 
Example #3
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 6 votes vote down vote up
public static Matcher<View> withColors(final int... colors) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public boolean matchesSafely(TextView textView) {
      SpannedString text = (SpannedString) textView.getText();
      ForegroundColorSpan[] spans = text.getSpans(0, text.length(), ForegroundColorSpan.class);
      if (spans.length != colors.length) {
        return false;
      }
      for (int i = 0; i < colors.length; ++i) {
        if (spans[i].getForegroundColor() != colors[i]) {
          return false;
        }
      }
      return true;
    }
    @Override public void describeTo(Description description) {
      description.appendText("has colors:");
      for (int color : colors) {
        description.appendText(" " + getHexColor(color));
      }
    }
  };
}
 
Example #4
Source File: LoginDevAuthTest.java    From zulip-android with Apache License 2.0 6 votes vote down vote up
private Matcher<View> emailFilter() {

        return new BoundedMatcher<View, Button>(Button.class) {
            @Override
            public void describeTo(Description description) {
                description.appendText("ERROR");
            }

            @Override
            protected boolean matchesSafely(Button item) {
                if (!matchedBefore && item.getText().toString().contains("@")) {
                    EMAIL_TEST = item.getText().toString();
                    matchedBefore = true;
                    return true;
                }
                return false;
            }
        };
    }
 
Example #5
Source File: Matchers.java    From zulip-android with Apache License 2.0 6 votes vote down vote up
public static Matcher<RecyclerView.ViewHolder> withMessageHeaderHolder(final MessageType messageType) {
    return new BoundedMatcher<RecyclerView.ViewHolder, MessageHeaderParent.MessageHeaderHolder>(MessageHeaderParent.MessageHeaderHolder.class) {
        private boolean found = false;

        @Override
        public void describeTo(Description description) {
            description.appendText("No ViewHolder found with text: " + messageType.toString());
        }

        @Override
        protected boolean matchesSafely(MessageHeaderParent.MessageHeaderHolder item) {
            if (found) return false;
            MessageHeaderParent messageHeaderParent = item.onItemClickListener.getMessageHeaderParentAtPosition(item.getLayoutPosition());
            found = (messageType == messageHeaderParent.getMessageType());
            return found;
        }
    };
}
 
Example #6
Source File: Matchers.java    From zulip-android with Apache License 2.0 6 votes vote down vote up
public static Matcher<RecyclerView.ViewHolder> withMessageHolder(final String text, final int textViewId) {
    return new BoundedMatcher<RecyclerView.ViewHolder, MessageHolder>(MessageHolder.class) {
        private boolean found = false;

        @Override
        public void describeTo(Description description) {
            description.appendText("No ViewHolder found with text: ");
        }

        @Override
        protected boolean matchesSafely(MessageHolder item) {
            if (found) return false;
            found = ((TextView) item.itemView.findViewById(textViewId)).getText().toString().matches(text);
            return found;
        }
    };
}
 
Example #7
Source File: Helpers.java    From BreadcrumbsView with Apache License 2.0 6 votes vote down vote up
private static Matcher<View> breadcrumbsMatcher(final Func func, final Matcher<Integer> expectedCount,
    final String msgDescription) {
  return new BoundedMatcher<View, BreadcrumbsView>(BreadcrumbsView.class) {
    @Override protected boolean matchesSafely(BreadcrumbsView view) {
      int nDots = 0;
      for (int i = 0; i < view.getChildCount(); i++) {
        View viewChild = view.getChildAt(i);
        if (viewChild.getTag() != null) {
          if (func.call((ViewGroup) viewChild)) nDots++;
        }
      }
      return expectedCount.matches(nDots);
    }

    @Override public void describeTo(Description description) {
      description.appendText(msgDescription);
      expectedCount.describeTo(description);
    }
  };
}
 
Example #8
Source File: HintMatcher.java    From testing-cin with MIT License 6 votes vote down vote up
static Matcher<View> withHint(final Matcher<String> stringMatcher) {
    checkNotNull(stringMatcher);
    return new BoundedMatcher<View, EditText>(EditText.class) {

        @Override
        public boolean matchesSafely(EditText view) {
            final CharSequence hint = view.getHint();
            return hint != null && stringMatcher.matches(hint.toString());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with hint: ");
            stringMatcher.describeTo(description);
        }
    };
}
 
Example #9
Source File: RecyclerViewEspressoFactory.java    From ChipsLayoutManager with Apache License 2.0 6 votes vote down vote up
public static <T extends RecyclerView.ViewHolder> Matcher<View> atPosition(final int position, @NonNull final ViewHolderMatcher<T> itemMatcher) {
    checkNotNull(itemMatcher);
    return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has item at position " + position + ":\n");
            itemMatcher.describeTo(description);
        }

        @Override
        protected boolean matchesSafely(final RecyclerView view) {
            RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position);
            return viewHolder != null && itemMatcher.matches(viewHolder);
        }
    };
}
 
Example #10
Source File: Utils.java    From ProgressButton with Apache License 2.0 6 votes vote down vote up
public static Matcher<View> withCompoundDrawable(final int resourceId) {
    return new BoundedMatcher<View, Button>(Button.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has compound drawable resource " + resourceId);
        }

        @Override
        public boolean matchesSafely(Button textView) {
            for (Drawable drawable : textView.getCompoundDrawables()) {
                if (sameBitmap(textView.getContext(), drawable, resourceId)) {
                    return true;
                }
            }
            return false;
        }
    };
}
 
Example #11
Source File: RecyclerViewMatchers.java    From android-espresso-revealed with Apache License 2.0 6 votes vote down vote up
public static Matcher<RecyclerView.ViewHolder> withTitle(final String taskTitle) {
    Checks.checkNotNull(taskTitle);

    return new BoundedMatcher<RecyclerView.ViewHolder, TasksFragment.TasksAdapter.ViewHolder>(
            TasksAdapter.ViewHolder.class) {
        @Override
        protected boolean matchesSafely(TasksAdapter.ViewHolder holder) {
            final String holderTaskTitle = holder.getHolderTask().getTitle();
            return taskTitle.equals(holderTaskTitle);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with task title: " + taskTitle);
        }
    };
}
 
Example #12
Source File: RecyclerViewMatchers.java    From android-espresso-revealed with Apache License 2.0 6 votes vote down vote up
public static Matcher<RecyclerView.ViewHolder> withTask(final TodoItem taskItem) {
    Checks.checkNotNull(taskItem);

    return new BoundedMatcher<RecyclerView.ViewHolder, TasksFragment.TasksAdapter.ViewHolder>(
            TasksAdapter.ViewHolder.class) {
        @Override
        protected boolean matchesSafely(TasksAdapter.ViewHolder holder) {
            final String holderTaskTitle = holder.getHolderTask().getTitle();
            final String holderTaskDesc = holder.getHolderTask().getDescription();
            return taskItem.getTitle().equals(holderTaskTitle)
                    && taskItem.getDescription().equals(holderTaskDesc);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("task with title: " + taskItem.getTitle()
                    + " and description: " + taskItem.getDescription());
        }
    };
}
 
Example #13
Source File: RecyclerViewMatchers.java    From android-espresso-revealed with Apache License 2.0 6 votes vote down vote up
public static Matcher<RecyclerView.ViewHolder> withTaskTitleFromTextView(final String taskTitle) {
    Checks.checkNotNull(taskTitle);

    return new BoundedMatcher<RecyclerView.ViewHolder, TasksFragment.TasksAdapter.ViewHolder>(
            TasksAdapter.ViewHolder.class) {
        @Override
        protected boolean matchesSafely(TasksAdapter.ViewHolder holder) {
            final TextView titleTextView = (TextView) holder.itemView.findViewById(R.id.title);
            return taskTitle.equals(titleTextView.getText().toString());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with task title: " + taskTitle);
        }
    };
}
 
Example #14
Source File: TestUtils.java    From BrainPhaser with GNU General Public License v3.0 6 votes vote down vote up
public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) {
    checkNotNull(itemMatcher);
    return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has item at position " + position + ": ");
            itemMatcher.describeTo(description);
        }

        @Override
        protected boolean matchesSafely(final RecyclerView view) {
            RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position);
            return viewHolder != null && itemMatcher.matches(viewHolder.itemView);
        }
    };
}
 
Example #15
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withoutCompoundDrawable(final int position) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public void describeTo(Description description) {
      description.appendText(
          "does not have compound drawable at position " + position);
    }
    @Override public boolean matchesSafely(TextView textView) {
      Drawable drawables[] = textView.getCompoundDrawables();
      return (drawables[position] == null);
    }
  };
}
 
Example #16
Source File: CustomViewMatchers.java    From android-espresso-revealed with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> todoWithTitle(final String expectedTitle) {
    return new BoundedMatcher<View, LinearLayout>(LinearLayout.class) {

        @Override
        protected boolean matchesSafely(LinearLayout linearLayout) {
            TextView textView = linearLayout.findViewById(R.id.todo_title);
            return expectedTitle.equals(textView.getText().toString());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with TO-DO title: " + expectedTitle);
        }
    };
}
 
Example #17
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withTextColor(final int color) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public boolean matchesSafely(TextView textView) {
      return (textView.getCurrentTextColor() == color);
    }
    @Override public void describeTo(Description description) {
      description.appendText("has colors: " + getHexColor(color));
    }
  };
}
 
Example #18
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> hasChildCount(final int count) {
  return new BoundedMatcher<View, ViewGroup>(ViewGroup.class) {
    @Override public boolean matchesSafely(ViewGroup viewGroup) {
      return (viewGroup.getChildCount() == count);
    }
    @Override public void describeTo(Description description) {
      description.appendText("has child count: " + count);
    }
  };
}
 
Example #19
Source File: ImageViewHasDrawableMatcher.java    From androidtestdebug with MIT License 5 votes vote down vote up
public static BoundedMatcher<View, ImageView> hasDrawable() {
    return new BoundedMatcher<View, ImageView>(ImageView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has drawable");
        }

        @Override
        public boolean matchesSafely(ImageView imageView) {
            return imageView.getDrawable() != null;
        }
    };
}
 
Example #20
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withCompoundDrawable(
    final int position, @DrawableRes final int resId) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public void describeTo(Description description) {
      description.appendText(
          "has compound drawable resource " + resId + " at position " + position);
    }
    @Override public boolean matchesSafely(TextView textView) {
      Drawable drawables[] = textView.getCompoundDrawables();
      return sameBitmap(textView.getContext(), drawables[position], resId);
    }
  };
}
 
Example #21
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withCompoundDrawable(final int position) {
  return new BoundedMatcher<View, TextView>(TextView.class) {
    @Override public void describeTo(Description description) {
      description.appendText(
          "has compound drawable resource at position " + position);
    }
    @Override public boolean matchesSafely(TextView textView) {
      Drawable drawables[] = textView.getCompoundDrawables();
      return (drawables[position] != null);
    }
  };
}
 
Example #22
Source File: MainActivityTest.java    From fontster with Apache License 2.0 5 votes vote down vote up
public static Matcher<Object> withFontName(final String fontName) {
  return new BoundedMatcher<Object, FontPackage>(FontPackage.class) {
    @Override public boolean matchesSafely(FontPackage fontPackage) {
      return fontPackage.getName().matches(fontName);
    }

    @Override public void describeTo(Description description) {
      description.appendText("with font name: ");
    }
  };
}
 
Example #23
Source File: TestUtil.java    From friendspell with Apache License 2.0 5 votes vote down vote up
private static Matcher<Object> withToolbarTitle(
    final Matcher<CharSequence> textMatcher) {
  return new BoundedMatcher<Object, Toolbar>(Toolbar.class) {
    @Override public boolean matchesSafely(Toolbar toolbar) {
      return textMatcher.matches(toolbar.getTitle());
    }
    @Override public void describeTo(Description description) {
      description.appendText("with toolbar title: ");
      textMatcher.describeTo(description);
    }
  };
}
 
Example #24
Source File: TokenMatchers.java    From TokenAutoComplete with Apache License 2.0 5 votes vote down vote up
static Matcher<View> tokenCount(final Matcher<Integer> intMatcher) {
    checkNotNull(intMatcher);
    return new BoundedMatcher<View, ContactsCompletionView>(ContactsCompletionView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("token count: ");
            intMatcher.describeTo(description);
        }
        @Override
        public boolean matchesSafely(ContactsCompletionView view) {
            return intMatcher.matches(view.getObjects().size());
        }
    };
}
 
Example #25
Source File: TokenMatchers.java    From TokenAutoComplete with Apache License 2.0 5 votes vote down vote up
static Matcher<View> emailForPerson(final int position, final Matcher<String> stringMatcher) {
    checkNotNull(stringMatcher);
    return new BoundedMatcher<View, ContactsCompletionView>(ContactsCompletionView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText(String.format(Locale.US, "email for person %d: ", position));
            stringMatcher.describeTo(description);
        }
        @Override
        public boolean matchesSafely(ContactsCompletionView view) {
            if (view.getObjects().size() <= position) { return stringMatcher.matches(null); }
            return stringMatcher.matches(view.getObjects().get(position).getEmail());
        }
    };
}
 
Example #26
Source File: ImageViewHasDrawableMatcher.java    From androidtestdebug with MIT License 5 votes vote down vote up
public static BoundedMatcher<View, ImageView> hasDrawable() {
    return new BoundedMatcher<View, ImageView>(ImageView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has drawable");
        }

        @Override
        public boolean matchesSafely(ImageView imageView) {
            return imageView.getDrawable() != null;
        }
    };
}
 
Example #27
Source File: EspressoTestUtils.java    From Equate with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Note that ideal the method below should implement a describeMismatch
 * method (as used by BaseMatcher), but this method is not invoked by
 * ViewAssertions.matches() and it won't get called. This means that I'm not
 * sure how to implement a custom error message.
 */
private static Matcher<View> expressionEquals(final Matcher<String> testString) {
	return new BoundedMatcher<View, EditText>(EditText.class) {
		@Override
		public void describeTo(Description description) {
			description.appendText("with expression text: " + testString);
		}

		@Override
		protected boolean matchesSafely(EditText item) {
			return testString.matches(item.getText().toString());
		}
	};
}
 
Example #28
Source File: ImageViewHasDrawableMatcher.java    From testing-cin with MIT License 5 votes vote down vote up
public static BoundedMatcher<View, ImageView> hasDrawable() {
    return new BoundedMatcher<View, ImageView>(ImageView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has drawable");
        }

        @Override
        public boolean matchesSafely(ImageView imageView) {
            return imageView.getDrawable() != null;
        }
    };
}
 
Example #29
Source File: BasicUITests.java    From restcomm-android-sdk with GNU Affero General Public License v3.0 5 votes vote down vote up
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 color == textView.getCurrentTextColor();
        }
        @Override
        public void describeTo(Description description) {
            description.appendText("Expected Color: " + color);
        }
    };
}
 
Example #30
Source File: NavInputFieldMatchers.java    From espresso-cucumber with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withWarningState() {
    return new BoundedMatcher<View, StockInputField>(StockInputField.class) {
        @Override
        public boolean matchesSafely(final StockInputField inputField) {
            final InputFieldMode inputMode = inputField.getModel().getEnum(Attributes.INPUT_MODE);

            return InputFieldMode.WARN.equals(inputMode);
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("with warning state");
        }
    };
}