Best junit code snippet using org.hamcrest.Description.NullDescription.appendDescriptionOf
Source:ViewMatchers.java
...533 String mismatchDescriptionString = getMismatchDescriptionString(actual, matcher);534 description535 .appendText(message)536 .appendText("\nExpected: ")537 .appendDescriptionOf(matcher)538 .appendText("\n Got: ")539 .appendText(mismatchDescriptionString);540 if (actual instanceof View) {541 // Get the human readable view description.542 description543 .appendText("\nView Details: ")544 .appendText(HumanReadables.describe((View) actual));545 }546 description.appendText("\n");547 throw new AssertionFailedError(description.toString());548 }549 }550 private static <T> String getMismatchDescriptionString(T actual, Matcher<T> matcher) {551 Description mismatchDescription = new StringDescription();552 matcher.describeMismatch(actual, mismatchDescription);553 String mismatchDescriptionString = mismatchDescription.toString().trim();554 return mismatchDescriptionString.isEmpty() ? actual.toString() : mismatchDescriptionString;555 }556 /**557 * Returns a matcher that matches a descendant of {@link Spinner} that is displaying the string of558 * the selected item associated with the given resource id.559 *560 * @param resourceId the resource id of the string resource the text view is expected to hold.561 */562 public static Matcher<View> withSpinnerText(final int resourceId) {563 return new WithSpinnerTextIdMatcher(resourceId);564 }565 /**566 * Returns a matcher that matches {@link Spinner Spinner's} based on {@code toString} value of the567 * selected item.568 *569 * @param stringMatcher <a570 * href="http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html"><code>Matcher571 * </code></a> of {@link String} with text to match.572 */573 public static Matcher<View> withSpinnerText(final Matcher<String> stringMatcher) {574 return new WithSpinnerTextMatcher(checkNotNull(stringMatcher));575 }576 /**577 * Returns a matcher that matches {@link Spinner} based on it's selected item's {@code toString}578 * value.579 *580 * <p>Note: Sugar for {@code withSpinnerText(is("string"))}.581 */582 public static Matcher<View> withSpinnerText(String text) {583 return withSpinnerText(is(text));584 }585 /** Returns a matcher that matches {@link WebView} if they are evaluating {@code JavaScript}. */586 public static Matcher<View> isJavascriptEnabled() {587 return new IsJavascriptEnabledMatcher();588 }589 /**590 * Returns a matcher that matches {@link EditText} based on edit text error string value.591 *592 * <p><b>Note:</b> View's error property can be <code>null</code>, to match against it use <code>593 * hasErrorText(nullValue(String.class)</code>594 */595 public static Matcher<View> hasErrorText(final Matcher<String> stringMatcher) {596 return new HasErrorTextMatcher(checkNotNull(stringMatcher));597 }598 /**599 * Returns a matcher that matches {@link EditText} based on edit text error string value.600 *601 * <p>Note: Sugar for {@code hasErrorText(is("string"))}.602 */603 public static Matcher<View> hasErrorText(final String expectedError) {604 return hasErrorText(is(expectedError));605 }606 /** Returns a matcher that matches {@link android.text.InputType}. */607 public static Matcher<View> withInputType(final int inputType) {608 return new WithInputTypeMatcher(inputType);609 }610 /** Returns a matcher that matches the child index inside the {@link ViewParent}. */611 public static Matcher<View> withParentIndex(final int index) {612 checkArgument(index >= 0, "Index %s must be >= 0", index);613 return new WithParentIndexMatcher(index);614 }615 static final class WithIdMatcher extends TypeSafeDiagnosingMatcher<View> {616 @VisibleForTesting617 @RemoteMsgField(order = 0)618 Matcher<Integer> viewIdMatcher;619 private Resources resources;620 @RemoteMsgConstructor621 private WithIdMatcher(Matcher<Integer> integerMatcher) {622 this.viewIdMatcher = integerMatcher;623 }624 @SuppressWarnings("JdkObsolete") // java.util.regex.Matcher requires the use of StringBuffer625 @Override626 public void describeTo(Description description) {627 String id = getViewIdString(viewIdMatcher.toString());628 description.appendText("view.getId() ").appendText(id);629 }630 @Override631 protected boolean matchesSafely(View view, Description mismatchDescription) {632 resources = view.getResources();633 boolean matches = viewIdMatcher.matches(view.getId());634 // Only log if no match is found and if the description isn't a NullDescription. This avoids635 // a call to query the resources which may cause some issues as it logs several errors.636 if (!matches && !(mismatchDescription instanceof NullDescription)) {637 mismatchDescription638 .appendText("view.getId() was ")639 .appendText(getViewIdString("<" + view.getId() + ">"));640 }641 return matches;642 }643 private String getViewIdString(String idDescription) {644 java.util.regex.Matcher matcher = RESOURCE_ID_PATTERN.matcher(idDescription);645 StringBuffer buffer = new StringBuffer(idDescription.length());646 while (matcher.find()) {647 if (resources != null) {648 String idString = matcher.group();649 int id = Integer.parseInt(idString);650 String resourceName = safeGetResourceName(resources, id);651 if (resourceName != null) {652 matcher.appendReplacement(buffer, idString + "/" + resourceName);653 } else {654 // No big deal, will just use the int value.655 matcher.appendReplacement(656 buffer, String.format(Locale.ROOT, "%s (resource name not found)", idString));657 }658 }659 }660 matcher.appendTail(buffer);661 return buffer.toString();662 }663 }664 static final class WithTextMatcher extends BoundedDiagnosingMatcher<View, TextView> {665 @RemoteMsgField(order = 0)666 private final Matcher<String> stringMatcher;667 @RemoteMsgConstructor668 private WithTextMatcher(Matcher<String> stringMatcher) {669 super(TextView.class);670 this.stringMatcher = stringMatcher;671 }672 @Override673 protected void describeMoreTo(Description description) {674 description.appendText("view.getText() with or without transformation to match: ");675 stringMatcher.describeTo(description);676 }677 @Override678 protected boolean matchesSafely(TextView textView, Description mismatchDescription) {679 String text = textView.getText().toString();680 // The reason we try to match both original text and the transformed one is because some UI681 // elements may inherit a default theme which behave differently for SDK below 19 and above.682 // So it is implemented in a backwards compatible way.683 if (stringMatcher.matches(text)) {684 return true;685 }686 mismatchDescription.appendText("view.getText() was ").appendValue(text);687 if (textView.getTransformationMethod() != null) {688 CharSequence transformedText =689 textView.getTransformationMethod().getTransformation(text, textView);690 mismatchDescription.appendText(" transformed text was ").appendValue(transformedText);691 if (transformedText != null) {692 return stringMatcher.matches(transformedText.toString());693 }694 }695 return false;696 }697 }698 static final class WithResourceNameMatcher extends TypeSafeDiagnosingMatcher<View> {699 @RemoteMsgField(order = 0)700 private final Matcher<String> stringMatcher;701 @RemoteMsgConstructor702 private WithResourceNameMatcher(Matcher<String> stringMatcher) {703 this.stringMatcher = stringMatcher;704 }705 @Override706 public void describeTo(Description description) {707 description708 .appendText("view.getId()'s resource name should match ")709 .appendDescriptionOf(stringMatcher);710 }711 @Override712 protected boolean matchesSafely(View view, Description mismatchDescription) {713 final int id = view.getId();714 if (id == View.NO_ID) {715 mismatchDescription.appendText("view.getId() was View.NO_ID");716 return false;717 }718 if (view.getResources() == null) {719 mismatchDescription.appendText("view.getResources() was null, can't resolve resource name");720 return false;721 }722 if (isViewIdGenerated(id)) {723 mismatchDescription.appendText(724 "view.getId() was generated by a call to View.generateViewId()");725 return false;726 }727 String resourceName = safeGetResourceEntryName(view.getResources(), view.getId());728 if (resourceName == null) {729 mismatchDescription730 .appendText("view.getId() was ")731 .appendValue(id)732 .appendText(" which fails to resolve resource name");733 return false;734 }735 if (!stringMatcher.matches(resourceName)) {736 mismatchDescription737 .appendText("view.getId() was <")738 .appendText(resourceName)739 .appendText(">");740 return false;741 }742 return true;743 }744 }745 static final class WithTagKeyMatcher extends TypeSafeDiagnosingMatcher<View> {746 @RemoteMsgField(order = 0)747 private final int key;748 @RemoteMsgField(order = 1)749 private final Matcher<?> objectMatcher;750 @RemoteMsgConstructor751 private WithTagKeyMatcher(int key, Matcher<?> objectMatcher) {752 this.key = key;753 this.objectMatcher = objectMatcher;754 }755 @Override756 public void describeTo(Description description) {757 description.appendText("view.getTag(" + key + ") ").appendDescriptionOf(objectMatcher);758 }759 @Override760 protected boolean matchesSafely(View view, Description mismatchDescription) {761 final Object tag = view.getTag(key);762 if (!objectMatcher.matches(tag)) {763 mismatchDescription.appendText("view.getTag(" + key + ") ");764 objectMatcher.describeMismatch(tag, mismatchDescription);765 return false;766 }767 return true;768 }769 }770 static final class IsAssignableFromMatcher extends TypeSafeDiagnosingMatcher<View> {771 @RemoteMsgField(order = 0)772 private final Class<?> clazz;773 @RemoteMsgConstructor774 private IsAssignableFromMatcher(@NonNull Class<?> clazz) {775 this.clazz = checkNotNull(clazz);776 }777 @Override778 public void describeTo(Description description) {779 description.appendText("is assignable from class ").appendValue(clazz);780 }781 @Override782 protected boolean matchesSafely(View view, Description mismatchDescription) {783 if (!clazz.isAssignableFrom(view.getClass())) {784 mismatchDescription.appendText("view.getClass() was ").appendValue(view.getClass());785 return false;786 }787 return true;788 }789 }790 static final class WithClassNameMatcher extends TypeSafeDiagnosingMatcher<View> {791 @VisibleForTesting792 @RemoteMsgField(order = 0)793 final Matcher<String> classNameMatcher;794 @RemoteMsgConstructor795 private WithClassNameMatcher(Matcher<String> classNameMatcher) {796 this.classNameMatcher = classNameMatcher;797 }798 @Override799 public void describeTo(Description description) {800 description801 .appendText("view.getClass().getName() matches: ")802 .appendDescriptionOf(classNameMatcher);803 }804 @Override805 protected boolean matchesSafely(View view, Description mismatchDescription) {806 final String className = view.getClass().getName();807 if (!classNameMatcher.matches(className)) {808 mismatchDescription.appendText("view.getClass().getName() ");809 classNameMatcher.describeMismatch(className, mismatchDescription);810 return false;811 }812 return true;813 }814 }815 static final class IsDisplayedMatcher extends TypeSafeDiagnosingMatcher<View> {816 private final Matcher<View> visibilityMatcher = withEffectiveVisibility(Visibility.VISIBLE);817 @RemoteMsgConstructor818 private IsDisplayedMatcher() {}819 @Override820 public void describeTo(Description description) {821 description822 .appendText("(")823 .appendDescriptionOf(visibilityMatcher)824 .appendText(" and view.getGlobalVisibleRect() to return non-empty rectangle)");825 }826 @Override827 protected boolean matchesSafely(View view, Description mismatchDescription) {828 if (!visibilityMatcher.matches(view)) {829 visibilityMatcher.describeMismatch(view, mismatchDescription);830 return false;831 }832 if (!view.getGlobalVisibleRect(new Rect())) {833 mismatchDescription.appendText("view.getGlobalVisibleRect() returned empty rectangle");834 return false;835 }836 return true;837 }838 }839 static final class IsDisplayingAtLeastMatcher extends TypeSafeDiagnosingMatcher<View> {840 @VisibleForTesting841 @RemoteMsgField(order = 0)842 final int areaPercentage;843 private final Matcher<View> visibilityMatchers = withEffectiveVisibility(Visibility.VISIBLE);844 @RemoteMsgConstructor845 private IsDisplayingAtLeastMatcher(int areaPercentage) {846 this.areaPercentage = areaPercentage;847 }848 @Override849 public void describeTo(Description description) {850 description851 .appendText("(")852 .appendDescriptionOf(visibilityMatchers)853 .appendText(" and view.getGlobalVisibleRect() covers at least ")854 .appendValue(areaPercentage)855 .appendText(" percent of the view's area)");856 }857 @Override858 protected boolean matchesSafely(View view, Description mismatchDescription) {859 if (!visibilityMatchers.matches(view)) {860 visibilityMatchers.describeMismatch(view, mismatchDescription);861 return false;862 }863 Rect visibleParts = new Rect();864 boolean visibleAtAll = view.getGlobalVisibleRect(visibleParts);865 if (!visibleAtAll) {866 mismatchDescription867 .appendText("view was ")868 .appendValue(0)869 .appendText(" percent visible to the user");870 return false;871 }872 Rect screen = getScreenWithoutStatusBarActionBar(view);873 float viewHeight = (view.getHeight() > screen.height()) ? screen.height() : view.getHeight();874 float viewWidth = (view.getWidth() > screen.width()) ? screen.width() : view.getWidth();875 if (Build.VERSION.SDK_INT >= 11) {876 // For API level 11 and above, factor in the View's scaleX and scaleY properties.877 viewHeight = Math.min(view.getHeight() * Math.abs(view.getScaleY()), screen.height());878 viewWidth = Math.min(view.getWidth() * Math.abs(view.getScaleX()), screen.width());879 }880 double maxArea = viewHeight * viewWidth;881 double visibleArea = visibleParts.height() * visibleParts.width();882 int displayedPercentage = (int) ((visibleArea / maxArea) * 100);883 if (displayedPercentage < areaPercentage) {884 mismatchDescription885 .appendText("view was ")886 .appendValue(displayedPercentage)887 .appendText(" percent visible to the user");888 return false;889 }890 return true;891 }892 private Rect getScreenWithoutStatusBarActionBar(View view) {893 DisplayMetrics m = new DisplayMetrics();894 ((WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE))895 .getDefaultDisplay()896 .getMetrics(m);897 // Get status bar height898 int resourceId =899 view.getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");900 int statusBarHeight =901 (resourceId > 0) ? view.getContext().getResources().getDimensionPixelSize(resourceId) : 0;902 // Get action bar height903 TypedValue tv = new TypedValue();904 int actionBarHeight =905 view.getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)906 ? TypedValue.complexToDimensionPixelSize(907 tv.data, view.getContext().getResources().getDisplayMetrics())908 : 0;909 return new Rect(0, 0, m.widthPixels, m.heightPixels - (statusBarHeight + actionBarHeight));910 }911 }912 static final class IsEnabledMatcher extends TypeSafeDiagnosingMatcher<View> {913 @RemoteMsgField(order = 0)914 private final boolean isEnabled;915 @RemoteMsgConstructor916 private IsEnabledMatcher(boolean isEnabled) {917 this.isEnabled = isEnabled;918 }919 @Override920 public void describeTo(Description description) {921 description.appendText("view.isEnabled() is ").appendValue(isEnabled);922 }923 @Override924 protected boolean matchesSafely(View view, Description mismatchDescription) {925 final boolean isEnabled = view.isEnabled();926 if (isEnabled != this.isEnabled) {927 mismatchDescription.appendText("view.isEnabled() was ").appendValue(isEnabled);928 return false;929 }930 return true;931 }932 }933 static final class IsFocusableMatcher extends TypeSafeDiagnosingMatcher<View> {934 @RemoteMsgField(order = 0)935 private final boolean isFocusable;936 @RemoteMsgConstructor937 private IsFocusableMatcher(boolean isFocusable) {938 this.isFocusable = isFocusable;939 }940 @Override941 public void describeTo(Description description) {942 description.appendText("view.isFocusable() is ").appendValue(isFocusable);943 }944 @Override945 protected boolean matchesSafely(View view, Description mismatchDescription) {946 final boolean isFocusable = view.isFocusable();947 if (isFocusable != this.isFocusable) {948 mismatchDescription.appendText("view.isFocusable() was ").appendValue(isFocusable);949 return false;950 }951 return true;952 }953 }954 static final class IsFocusedMatcher extends TypeSafeDiagnosingMatcher<View> {955 @RemoteMsgField(order = 0)956 private final boolean isFocused;957 @RemoteMsgConstructor958 private IsFocusedMatcher(boolean isFocused) {959 this.isFocused = isFocused;960 }961 @Override962 public void describeTo(Description description) {963 description.appendText("view.isFocused() is ").appendValue(isFocused);964 }965 @Override966 protected boolean matchesSafely(View view, Description mismatchDescription) {967 final boolean isFocused = view.isFocused();968 if (isFocused != this.isFocused) {969 mismatchDescription.appendText("view.isFocused() was ").appendValue(isFocused);970 return false;971 }972 return true;973 }974 }975 static final class HasFocusMatcher extends TypeSafeDiagnosingMatcher<View> {976 @RemoteMsgField(order = 0)977 private final boolean hasFocus;978 @RemoteMsgConstructor979 private HasFocusMatcher(boolean hasFocus) {980 this.hasFocus = hasFocus;981 }982 @Override983 public void describeTo(Description description) {984 description.appendText("view.hasFocus() is ").appendValue(hasFocus);985 }986 @Override987 protected boolean matchesSafely(View view, Description mismatchDescription) {988 final boolean hasFocus = view.hasFocus();989 if (hasFocus != this.hasFocus) {990 mismatchDescription.appendText("view.hasFocus() is ").appendValue(hasFocus);991 return false;992 }993 return true;994 }995 }996 static final class IsSelectedMatcher extends TypeSafeDiagnosingMatcher<View> {997 @RemoteMsgField(order = 0)998 private final boolean isSelected;999 @RemoteMsgConstructor1000 private IsSelectedMatcher(boolean isSelected) {1001 this.isSelected = isSelected;1002 }1003 @Override1004 public void describeTo(Description description) {1005 description.appendText("view.isSelected() is ").appendValue(isSelected);1006 }1007 @Override1008 protected boolean matchesSafely(View view, Description mismatchDescription) {1009 final boolean isSelected = view.isSelected();1010 if (isSelected != this.isSelected) {1011 mismatchDescription.appendText("view.isSelected() was ").appendValue(isSelected);1012 return false;1013 }1014 return true;1015 }1016 }1017 static final class HasSiblingMatcher extends TypeSafeDiagnosingMatcher<View> {1018 @RemoteMsgField(order = 0)1019 private final Matcher<View> siblingMatcher;1020 private final Matcher<ViewGroup> parentMatcher = Matchers.isA(ViewGroup.class);1021 @RemoteMsgConstructor1022 private HasSiblingMatcher(final Matcher<View> siblingMatcher) {1023 this.siblingMatcher = siblingMatcher;1024 }1025 @Override1026 public void describeTo(Description description) {1027 description1028 .appendText("(view.getParent() ")1029 .appendDescriptionOf(parentMatcher)1030 .appendText(" and has a sibling matching ")1031 .appendDescriptionOf(siblingMatcher)1032 .appendText(")");1033 }1034 @Override1035 protected boolean matchesSafely(View view, Description mismatchDescription) {1036 ViewParent parent = view.getParent();1037 if (!parentMatcher.matches(parent)) {1038 mismatchDescription.appendText("view.getParent() ");1039 parentMatcher.describeMismatch(parent, mismatchDescription);1040 return false;1041 }1042 ViewGroup parentGroup = (ViewGroup) parent;1043 if (parentGroup.getChildCount() == 1) {1044 // No siblings can exist for view.1045 mismatchDescription.appendText("no siblings found");1046 return false;1047 }1048 for (int i = 0; i < parentGroup.getChildCount(); i++) {1049 View child = parentGroup.getChildAt(i);1050 if (view != child && siblingMatcher.matches(child)) {1051 return true;1052 }1053 }1054 mismatchDescription1055 .appendText("none of the ")1056 .appendValue(parentGroup.getChildCount() - 1)1057 .appendText(" siblings match");1058 return false;1059 }1060 }1061 static final class WithContentDescriptionFromIdMatcher extends TypeSafeDiagnosingMatcher<View> {1062 @RemoteMsgField(order = 0)1063 private final int resourceId;1064 private String resourceName = null;1065 private String expectedText = null;1066 @RemoteMsgConstructor1067 private WithContentDescriptionFromIdMatcher(final int resourceId) {1068 this.resourceId = resourceId;1069 }1070 @Override1071 public void describeTo(Description description) {1072 description1073 .appendText("view.getContentDescription() to match resource id ")1074 .appendValue(resourceId);1075 if (resourceName != null) {1076 description.appendText("[").appendText(resourceName).appendText("]");1077 }1078 if (expectedText != null) {1079 description.appendText(" with value ").appendValue(expectedText);1080 }1081 }1082 @Override1083 protected boolean matchesSafely(View view, Description mismatchDescription) {1084 if (null == this.expectedText) {1085 try {1086 expectedText = view.getResources().getString(resourceId);1087 } catch (Resources.NotFoundException ignored) {1088 // view could be from a context unaware of the resource id.1089 }1090 resourceName = safeGetResourceEntryName(view.getResources(), resourceId);1091 }1092 if (expectedText == null) {1093 mismatchDescription.appendText("Failed to resolve resource id ").appendValue(resourceId);1094 return false;1095 }1096 final CharSequence contentDescription = view.getContentDescription();1097 if (contentDescription == null || !expectedText.contentEquals(contentDescription)) {1098 mismatchDescription1099 .appendText("view.getContentDescription() was ")1100 .appendValue(contentDescription);1101 return false;1102 }1103 return true;1104 }1105 }1106 @VisibleForTesting1107 static final class WithContentDescriptionMatcher extends TypeSafeDiagnosingMatcher<View> {1108 @RemoteMsgField(order = 0)1109 private final Matcher<? extends CharSequence> charSequenceMatcher;1110 @RemoteMsgConstructor1111 private WithContentDescriptionMatcher(Matcher<? extends CharSequence> charSequenceMatcher) {1112 this.charSequenceMatcher = charSequenceMatcher;1113 }1114 @Override1115 public void describeTo(Description description) {1116 description1117 .appendText("view.getContentDescription() ")1118 .appendDescriptionOf(charSequenceMatcher);1119 }1120 @Override1121 protected boolean matchesSafely(View view, Description mismatchDescription) {1122 final CharSequence contentDescription = view.getContentDescription();1123 if (!charSequenceMatcher.matches(contentDescription)) {1124 mismatchDescription.appendText("view.getContentDescription() ");1125 charSequenceMatcher.describeMismatch(contentDescription, mismatchDescription);1126 return false;1127 }1128 return true;1129 }1130 }1131 @VisibleForTesting1132 static final class WithContentDescriptionTextMatcher extends TypeSafeDiagnosingMatcher<View> {1133 @RemoteMsgField(order = 0)1134 private final Matcher<String> textMatcher;1135 @RemoteMsgConstructor1136 private WithContentDescriptionTextMatcher(Matcher<String> textMatcher) {1137 this.textMatcher = textMatcher;1138 }1139 @Override1140 public void describeTo(Description description) {1141 description.appendText("view.getContentDescription() ").appendDescriptionOf(textMatcher);1142 }1143 @Override1144 protected boolean matchesSafely(View view, Description mismatchDescription) {1145 String descriptionText =1146 (view.getContentDescription() != null) ? view.getContentDescription().toString() : null;1147 if (!textMatcher.matches(descriptionText)) {1148 mismatchDescription.appendText("view.getContentDescription() ");1149 textMatcher.describeMismatch(descriptionText, mismatchDescription);1150 return false;1151 }1152 return true;1153 }1154 }1155 @VisibleForTesting1156 static final class WithTagValueMatcher extends TypeSafeDiagnosingMatcher<View> {1157 @RemoteMsgField(order = 0)1158 private final Matcher<Object> tagValueMatcher;1159 @RemoteMsgConstructor1160 private WithTagValueMatcher(Matcher<Object> tagValueMatcher) {1161 this.tagValueMatcher = tagValueMatcher;1162 }1163 @Override1164 public void describeTo(Description description) {1165 description.appendText("view.getTag() ").appendDescriptionOf(tagValueMatcher);1166 }1167 @Override1168 protected boolean matchesSafely(View view, Description mismatchDescription) {1169 final Object tag = view.getTag();1170 if (!tagValueMatcher.matches(tag)) {1171 mismatchDescription.appendText("view.getTag() ");1172 tagValueMatcher.describeMismatch(tag, mismatchDescription);1173 return false;1174 }1175 return true;1176 }1177 }1178 @VisibleForTesting1179 static final class WithCharSequenceMatcher extends BoundedDiagnosingMatcher<View, TextView> {1180 @RemoteMsgField(order = 0)1181 private final int resourceId;1182 @RemoteMsgField(order = 1)1183 private final TextViewMethod method;1184 @Nullable private String resourceName;1185 @Nullable private String expectedText;1186 private enum TextViewMethod {1187 GET_TEXT,1188 GET_HINT1189 }1190 @RemoteMsgConstructor1191 private WithCharSequenceMatcher(int resourceId, TextViewMethod method) {1192 super(TextView.class);1193 this.resourceId = resourceId;1194 this.method = method;1195 }1196 @Override1197 protected void describeMoreTo(Description description) {1198 switch (method) {1199 case GET_TEXT:1200 description.appendText("view.getText()");1201 break;1202 case GET_HINT:1203 description.appendText("view.getHint()");1204 break;1205 }1206 description.appendText(" equals string from resource id: ").appendValue(resourceId);1207 if (null != resourceName) {1208 description.appendText(" [").appendText(resourceName).appendText("]");1209 }1210 if (null != expectedText) {1211 description.appendText(" value: ").appendText(expectedText);1212 }1213 }1214 @Override1215 protected boolean matchesSafely(TextView textView, Description mismatchDescription) {1216 if (null == expectedText) {1217 try {1218 expectedText = textView.getResources().getString(resourceId);1219 } catch (Resources.NotFoundException ignored) {1220 /* view could be from a context unaware of the resource id. */1221 }1222 resourceName = safeGetResourceEntryName(textView.getResources(), resourceId);1223 }1224 CharSequence actualText;1225 switch (method) {1226 case GET_TEXT:1227 actualText = textView.getText();1228 mismatchDescription.appendText("view.getText() was ");1229 break;1230 case GET_HINT:1231 actualText = textView.getHint();1232 mismatchDescription.appendText("view.getHint() was ");1233 break;1234 default:1235 throw new IllegalStateException("Unexpected TextView method: " + method);1236 }1237 mismatchDescription.appendValue(actualText);1238 // FYI: actualText may not be string ... its just a char sequence convert to string.1239 return null != expectedText && null != actualText && expectedText.contentEquals(actualText);1240 }1241 }1242 @VisibleForTesting1243 static final class WithHintMatcher extends BoundedDiagnosingMatcher<View, TextView> {1244 @RemoteMsgField(order = 0)1245 private final Matcher<String> stringMatcher;1246 @RemoteMsgConstructor1247 private WithHintMatcher(Matcher<String> stringMatcher) {1248 super(TextView.class);1249 this.stringMatcher = stringMatcher;1250 }1251 @Override1252 protected void describeMoreTo(Description description) {1253 description.appendText("view.getHint() matching: ");1254 stringMatcher.describeTo(description);1255 }1256 @Override1257 protected boolean matchesSafely(TextView textView, Description mismatchDescription) {1258 CharSequence hint = textView.getHint();1259 mismatchDescription.appendText("view.getHint() was ").appendValue(hint);1260 return stringMatcher.matches(hint);1261 }1262 }1263 @VisibleForTesting1264 static final class WithCheckBoxStateMatcher<E extends View & Checkable>1265 extends BoundedDiagnosingMatcher<View, E> {1266 @RemoteMsgField(order = 0)1267 private final Matcher<Boolean> checkStateMatcher;1268 @RemoteMsgConstructor1269 private WithCheckBoxStateMatcher(Matcher<Boolean> checkStateMatcher) {1270 super(View.class, Checkable.class);1271 this.checkStateMatcher = checkStateMatcher;1272 }1273 @Override1274 protected void describeMoreTo(Description description) {1275 description.appendText("view.isChecked() matching: ").appendDescriptionOf(checkStateMatcher);1276 }1277 @Override1278 protected boolean matchesSafely(E checkable, Description mismatchDescription) {1279 boolean isChecked = checkable.isChecked();1280 mismatchDescription.appendText("view.isChecked() was ").appendValue(isChecked);1281 return checkStateMatcher.matches(isChecked);1282 }1283 }1284 @VisibleForTesting1285 static final class HasContentDescriptionMatcher extends TypeSafeDiagnosingMatcher<View> {1286 @RemoteMsgConstructor1287 private HasContentDescriptionMatcher() {}1288 @Override1289 public void describeTo(Description description) {1290 description.appendText("view.getContentDescription() is not null");1291 }1292 @Override1293 protected boolean matchesSafely(View view, Description mismatchDescription) {1294 if (view.getContentDescription() == null) {1295 mismatchDescription.appendText("view.getContentDescription() was null");1296 return false;1297 }1298 return true;1299 }1300 }1301 @VisibleForTesting1302 static final class HasDescendantMatcher extends TypeSafeDiagnosingMatcher<View> {1303 @RemoteMsgField(order = 0)1304 private final Matcher<View> descendantMatcher;1305 private final Matcher<ViewGroup> isViewGroupMatcher = Matchers.isA(ViewGroup.class);1306 @RemoteMsgConstructor1307 private HasDescendantMatcher(Matcher<View> descendantMatcher) {1308 this.descendantMatcher = descendantMatcher;1309 }1310 @Override1311 public void describeTo(Description description) {1312 description1313 .appendText("(view ")1314 .appendDescriptionOf(isViewGroupMatcher)1315 .appendText(" and has descendant matching ")1316 .appendDescriptionOf(descendantMatcher)1317 .appendText(")");1318 }1319 @Override1320 protected boolean matchesSafely(final View view, Description mismatchDescription) {1321 if (!isViewGroupMatcher.matches(view)) {1322 mismatchDescription.appendText("view ");1323 isViewGroupMatcher.describeMismatch(view, mismatchDescription);1324 return false;1325 }1326 final Predicate<View> matcherPredicate =1327 input -> input != view && descendantMatcher.matches(input);1328 Iterator<View> matchedViewIterator =1329 Iterables.filter(breadthFirstViewTraversal(view), matcherPredicate).iterator();1330 if (!matchedViewIterator.hasNext()) {1331 mismatchDescription1332 .appendText("no descendant matching ")1333 .appendDescriptionOf(descendantMatcher)1334 .appendText(" was found");1335 return false;1336 }1337 return true;1338 }1339 }1340 @VisibleForTesting1341 static final class IsClickableMatcher extends TypeSafeDiagnosingMatcher<View> {1342 @RemoteMsgField(order = 0)1343 private final boolean isClickable;1344 @RemoteMsgConstructor1345 private IsClickableMatcher(boolean isClickable) {1346 this.isClickable = isClickable;1347 }1348 @Override1349 public void describeTo(Description description) {1350 description.appendText("view.isClickable() is ").appendValue(isClickable);1351 }1352 @Override1353 protected boolean matchesSafely(View view, Description mismatchDescription) {1354 final boolean isClickable = view.isClickable();1355 if (isClickable != this.isClickable) {1356 mismatchDescription.appendText("view.isClickable() was ").appendValue(isClickable);1357 return false;1358 }1359 return true;1360 }1361 }1362 @VisibleForTesting1363 static final class IsDescendantOfAMatcher extends TypeSafeDiagnosingMatcher<View> {1364 @RemoteMsgField(order = 0)1365 private final Matcher<View> ancestorMatcher;1366 @RemoteMsgConstructor1367 private IsDescendantOfAMatcher(Matcher<View> ancestorMatcher) {1368 this.ancestorMatcher = ancestorMatcher;1369 }1370 @Override1371 public void describeTo(Description description) {1372 description1373 .appendText("is descendant of a view matching ")1374 .appendDescriptionOf(ancestorMatcher);1375 }1376 @Override1377 protected boolean matchesSafely(View view, Description mismatchDescription) {1378 boolean matches = checkAncestors(view.getParent());1379 if (!matches) {1380 mismatchDescription1381 .appendText("none of the ancestors match ")1382 .appendDescriptionOf(ancestorMatcher);1383 }1384 return matches;1385 }1386 private boolean checkAncestors(ViewParent viewParent) {1387 if (!(viewParent instanceof View)) {1388 return false;1389 }1390 if (ancestorMatcher.matches(viewParent)) {1391 return true;1392 }1393 return checkAncestors(viewParent.getParent());1394 }1395 }1396 @VisibleForTesting1397 static final class WithEffectiveVisibilityMatcher extends TypeSafeDiagnosingMatcher<View> {1398 @RemoteMsgField(order = 0)1399 private final Visibility visibility;1400 @RemoteMsgConstructor1401 private WithEffectiveVisibilityMatcher(Visibility visibility) {1402 this.visibility = visibility;1403 }1404 @Override1405 public void describeTo(Description description) {1406 description.appendText("view has effective visibility ").appendValue(visibility);1407 }1408 @Override1409 protected boolean matchesSafely(View view, Description mismatchDescription) {1410 if (visibility.getValue() == View.VISIBLE) {1411 if (view.getVisibility() != visibility.getValue()) {1412 mismatchDescription1413 .appendText("view.getVisibility() was ")1414 .appendValue(Visibility.forViewVisibility(view));1415 return false;1416 }1417 while (view.getParent() instanceof View) {1418 view = (View) view.getParent();1419 if (view.getVisibility() != visibility.getValue()) {1420 mismatchDescription1421 .appendText("ancestor ")1422 .appendValue(view)1423 .appendText("'s getVisibility() was ")1424 .appendValue(Visibility.forViewVisibility(view));1425 return false;1426 }1427 }1428 return true;1429 } else {1430 if (view.getVisibility() == visibility.getValue()) {1431 return true;1432 }1433 while (view.getParent() instanceof View) {1434 view = (View) view.getParent();1435 if (view.getVisibility() == visibility.getValue()) {1436 return true;1437 }1438 }1439 mismatchDescription1440 .appendText("neither view nor its ancestors have getVisibility() set to ")1441 .appendValue(visibility);1442 return false;1443 }1444 }1445 }1446 /**1447 * Returns a matcher that matches {@link android.view.View} based on background resource.1448 *1449 * <p>Note: This method compares images at a pixel level and might have significant performance1450 * implications for larger bitmaps.1451 *1452 * <p><b>This API is currently in beta.</b>1453 */1454 @ExperimentalTestApi1455 public static Matcher<View> hasBackground(final int drawableId) {1456 return new HasBackgroundMatcher(drawableId);1457 }1458 /**1459 * Returns a matcher that matches {@link android.widget.TextView} based on it's color.1460 *1461 * <p><b>This API is currently in beta.</b>1462 */1463 @ExperimentalTestApi1464 public static Matcher<View> hasTextColor(final int colorResId) {1465 return new BoundedDiagnosingMatcher<View, TextView>(TextView.class) {1466 private Context context;1467 @Override1468 protected boolean matchesSafely(TextView textView, Description mismatchDescription) {1469 context = textView.getContext();1470 int textViewColor = textView.getCurrentTextColor();1471 int expectedColor;1472 if (Build.VERSION.SDK_INT <= 22) {1473 expectedColor = context.getResources().getColor(colorResId);1474 } else {1475 expectedColor = context.getColor(colorResId);1476 }1477 mismatchDescription1478 .appendText("textView.getCurrentTextColor() was ")1479 .appendText(getColorHex(textViewColor));1480 return textViewColor == expectedColor;1481 }1482 @Override1483 protected void describeMoreTo(Description description) {1484 description.appendText("textView.getCurrentTextColor() is color with ");1485 if (context == null) {1486 description.appendText("ID ").appendValue(colorResId);1487 } else {1488 int color =1489 (Build.VERSION.SDK_INT <= 22)1490 ? context.getResources().getColor(colorResId)1491 : context.getColor(colorResId);1492 description.appendText("value " + getColorHex(color));1493 }1494 }1495 private String getColorHex(int color) {1496 return String.format(1497 Locale.ROOT, "#%02X%06X", (0xFF & Color.alpha(color)), (0xFFFFFF & color));1498 }1499 };1500 }1501 @VisibleForTesting1502 static final class WithAlphaMatcher extends TypeSafeDiagnosingMatcher<View> {1503 @RemoteMsgField(order = 0)1504 private final float alpha;1505 @RemoteMsgConstructor1506 private WithAlphaMatcher(final float alpha) {1507 this.alpha = alpha;1508 }1509 @Override1510 public void describeTo(Description description) {1511 description.appendText("view.getAlpha() is ").appendValue(alpha);1512 }1513 @Override1514 protected boolean matchesSafely(View view, Description mismatchDescription) {1515 final float alpha = view.getAlpha();1516 if (alpha != this.alpha) {1517 mismatchDescription.appendText("view.getAlpha() was ").appendValue(alpha);1518 return false;1519 }1520 return true;1521 }1522 }1523 @VisibleForTesting1524 static final class WithParentMatcher extends TypeSafeDiagnosingMatcher<View> {1525 @RemoteMsgField(order = 0)1526 private final Matcher<View> parentMatcher;1527 @RemoteMsgConstructor1528 private WithParentMatcher(Matcher<View> parentMatcher) {1529 this.parentMatcher = parentMatcher;1530 }1531 @Override1532 public void describeTo(Description description) {1533 description.appendText("view.getParent() ").appendDescriptionOf(parentMatcher);1534 }1535 @Override1536 protected boolean matchesSafely(View view, Description mismatchDescription) {1537 final ViewParent parent = view.getParent();1538 if (!parentMatcher.matches(parent)) {1539 mismatchDescription.appendText("view.getParent() ");1540 parentMatcher.describeMismatch(parent, mismatchDescription);1541 return false;1542 }1543 return true;1544 }1545 }1546 @VisibleForTesting1547 static final class WithChildMatcher extends TypeSafeDiagnosingMatcher<View> {1548 @RemoteMsgField(order = 0)1549 private final Matcher<View> childMatcher;1550 private final Matcher<ViewGroup> viewGroupMatcher = Matchers.isA(ViewGroup.class);1551 @RemoteMsgConstructor1552 private WithChildMatcher(Matcher<View> childMatcher) {1553 this.childMatcher = childMatcher;1554 }1555 @Override1556 public void describeTo(Description description) {1557 description1558 .appendText("(view ")1559 .appendDescriptionOf(viewGroupMatcher)1560 .appendText(" and has child matching: ")1561 .appendDescriptionOf(childMatcher)1562 .appendText(")");1563 }1564 @Override1565 protected boolean matchesSafely(View view, Description mismatchDescription) {1566 if (!viewGroupMatcher.matches(view)) {1567 mismatchDescription.appendText("view ");1568 viewGroupMatcher.describeMismatch(view, mismatchDescription);1569 return false;1570 }1571 ViewGroup group = (ViewGroup) view;1572 for (int i = 0; i < group.getChildCount(); i++) {1573 if (childMatcher.matches(group.getChildAt(i))) {1574 return true;1575 }1576 }1577 mismatchDescription1578 .appendText("All ")1579 .appendValue(group.getChildCount())1580 .appendText(" children did not match");1581 return false;1582 }1583 }1584 @VisibleForTesting1585 static final class HasChildCountMatcher extends BoundedDiagnosingMatcher<View, ViewGroup> {1586 @RemoteMsgField(order = 0)1587 private final int childCount;1588 @RemoteMsgConstructor1589 private HasChildCountMatcher(int childCount) {1590 super(ViewGroup.class);1591 this.childCount = childCount;1592 }1593 @Override1594 protected void describeMoreTo(Description description) {1595 description.appendText("viewGroup.getChildCount() to be ").appendValue(childCount);1596 }1597 @Override1598 protected boolean matchesSafely(ViewGroup viewGroup, Description mismatchDescription) {1599 mismatchDescription1600 .appendText("viewGroup.getChildCount() was ")1601 .appendValue(viewGroup.getChildCount());1602 return viewGroup.getChildCount() == childCount;1603 }1604 }1605 @VisibleForTesting1606 static final class HasMinimumChildCountMatcher extends BoundedDiagnosingMatcher<View, ViewGroup> {1607 @RemoteMsgField(order = 0)1608 private final int minChildCount;1609 @RemoteMsgConstructor1610 private HasMinimumChildCountMatcher(int minChildCount) {1611 super(ViewGroup.class);1612 this.minChildCount = minChildCount;1613 }1614 @Override1615 protected void describeMoreTo(Description description) {1616 description1617 .appendText("viewGroup.getChildCount() to be at least ")1618 .appendValue(minChildCount);1619 }1620 @Override1621 protected boolean matchesSafely(ViewGroup viewGroup, Description mismatchDescription) {1622 mismatchDescription1623 .appendText("viewGroup.getChildCount() was ")1624 .appendValue(viewGroup.getChildCount());1625 return viewGroup.getChildCount() >= minChildCount;1626 }1627 }1628 @VisibleForTesting1629 static final class IsRootMatcher extends TypeSafeDiagnosingMatcher<View> {1630 @RemoteMsgConstructor1631 private IsRootMatcher() {}1632 @Override1633 public void describeTo(Description description) {1634 description.appendText("view.getRootView() to equal view");1635 }1636 @Override1637 protected boolean matchesSafely(View view, Description mismatchDescription) {1638 final View rootView = view.getRootView();1639 if (rootView != view) {1640 mismatchDescription.appendText("view.getRootView() was ").appendValue(rootView);1641 return false;1642 }1643 return true;1644 }1645 }1646 @VisibleForTesting1647 static final class SupportsInputMethodsMatcher extends TypeSafeDiagnosingMatcher<View> {1648 @RemoteMsgConstructor1649 private SupportsInputMethodsMatcher() {}1650 @Override1651 public void describeTo(Description description) {1652 description.appendText("view.onCreateInputConnection() is not null");1653 }1654 @Override1655 protected boolean matchesSafely(View view, Description mismatchDescription) {1656 // At first glance, it would make sense to use view.onCheckIsTextEditor, but the android1657 // javadoc is wishy-washy about whether authors are required to implement this method when1658 // implementing onCreateInputConnection.1659 if (view.onCreateInputConnection(new EditorInfo()) == null) {1660 mismatchDescription.appendText("view.onCreateInputConnection() was null");1661 return false;1662 }1663 return true;1664 }1665 }1666 @VisibleForTesting1667 static final class HasImeActionMatcher extends TypeSafeDiagnosingMatcher<View> {1668 @RemoteMsgField(order = 0)1669 private final Matcher<Integer> imeActionMatcher;1670 @RemoteMsgConstructor1671 private HasImeActionMatcher(final Matcher<Integer> imeActionMatcher) {1672 this.imeActionMatcher = imeActionMatcher;1673 }1674 @Override1675 public void describeTo(Description description) {1676 description1677 .appendText("(view.onCreateInputConnection() is not null and editorInfo.actionId ")1678 .appendDescriptionOf(imeActionMatcher)1679 .appendText(")");1680 }1681 @Override1682 protected boolean matchesSafely(View view, Description mismatchDescription) {1683 EditorInfo editorInfo = new EditorInfo();1684 InputConnection inputConnection = view.onCreateInputConnection(editorInfo);1685 if (inputConnection == null) {1686 mismatchDescription.appendText("view.onCreateInputConnection() was null");1687 return false;1688 }1689 int actionId =1690 editorInfo.actionId != 01691 ? editorInfo.actionId1692 : editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;1693 if (!imeActionMatcher.matches(actionId)) {1694 mismatchDescription.appendText("editorInfo.actionId ");1695 imeActionMatcher.describeMismatch(actionId, mismatchDescription);1696 return false;1697 }1698 return true;1699 }1700 }1701 @VisibleForTesting1702 static final class HasLinksMatcher extends BoundedDiagnosingMatcher<View, TextView> {1703 @RemoteMsgConstructor1704 private HasLinksMatcher() {1705 super(TextView.class);1706 }1707 @Override1708 protected void describeMoreTo(Description description) {1709 description.appendText("textView.getUrls().length > 0");1710 }1711 @Override1712 protected boolean matchesSafely(TextView textView, Description mismatchDescription) {1713 mismatchDescription1714 .appendText("textView.getUrls().length was ")1715 .appendValue(textView.getUrls().length);1716 return textView.getUrls().length > 0;1717 }1718 }1719 @VisibleForTesting1720 static final class WithSpinnerTextIdMatcher extends BoundedDiagnosingMatcher<View, Spinner> {1721 @RemoteMsgField(order = 0)1722 private final int resourceId;1723 private String resourceName = null;1724 private String expectedText = null;1725 @RemoteMsgConstructor1726 private WithSpinnerTextIdMatcher(int resourceId) {1727 super(Spinner.class);1728 this.resourceId = resourceId;1729 }1730 @Override1731 protected void describeMoreTo(Description description) {1732 description1733 .appendText("spinner.getSelectedItem().toString() to match string from resource id: ")1734 .appendValue(resourceId);1735 if (resourceName != null) {1736 description.appendText(" [").appendText(resourceName).appendText("]");1737 }1738 if (expectedText != null) {1739 description.appendText(" value: ").appendText(expectedText);1740 }1741 }1742 @Override1743 protected boolean matchesSafely(Spinner spinner, Description mismatchDescription) {1744 if (expectedText == null) {1745 try {1746 expectedText = spinner.getResources().getString(resourceId);1747 } catch (Resources.NotFoundException ignored) {1748 /*1749 * view could be from a context unaware of the resource id.1750 */1751 }1752 resourceName = safeGetResourceEntryName(spinner.getResources(), resourceId);1753 }1754 if (expectedText == null) {1755 mismatchDescription.appendText("failure to resolve resourceId ").appendValue(resourceId);1756 return false;1757 }1758 Object selectedItem = spinner.getSelectedItem();1759 if (selectedItem == null) {1760 mismatchDescription.appendText("spinner.getSelectedItem() was null");1761 return false;1762 }1763 mismatchDescription1764 .appendText("spinner.getSelectedItem().toString() was ")1765 .appendValue(selectedItem.toString());1766 return expectedText.equals(selectedItem.toString());1767 }1768 }1769 @VisibleForTesting1770 static final class WithSpinnerTextMatcher extends BoundedDiagnosingMatcher<View, Spinner> {1771 @RemoteMsgField(order = 0)1772 private final Matcher<String> stringMatcher;1773 @RemoteMsgConstructor1774 private WithSpinnerTextMatcher(Matcher<String> stringMatcher) {1775 super(Spinner.class);1776 this.stringMatcher = stringMatcher;1777 }1778 @Override1779 protected void describeMoreTo(Description description) {1780 description1781 .appendText("spinner.getSelectedItem().toString() to match ")1782 .appendDescriptionOf(stringMatcher);1783 }1784 @Override1785 protected boolean matchesSafely(Spinner spinner, Description mismatchDescription) {1786 Object selectedItem = spinner.getSelectedItem();1787 if (selectedItem == null) {1788 mismatchDescription.appendText("spinner.getSelectedItem() was null");1789 return false;1790 }1791 mismatchDescription1792 .appendText("spinner.getSelectedItem().toString() was ")1793 .appendValue(selectedItem.toString());1794 return stringMatcher.matches(spinner.getSelectedItem().toString());1795 }1796 }1797 @VisibleForTesting1798 static final class IsJavascriptEnabledMatcher extends BoundedDiagnosingMatcher<View, WebView> {1799 @RemoteMsgConstructor1800 private IsJavascriptEnabledMatcher() {1801 super(WebView.class);1802 }1803 @Override1804 protected void describeMoreTo(Description description) {1805 description.appendText("webView.getSettings().getJavaScriptEnabled() is ").appendValue(true);1806 }1807 @Override1808 protected boolean matchesSafely(WebView webView, Description mismatchDescription) {1809 mismatchDescription1810 .appendText("webView.getSettings().getJavaScriptEnabled() was ")1811 .appendValue(webView.getSettings().getJavaScriptEnabled());1812 return webView.getSettings().getJavaScriptEnabled();1813 }1814 }1815 @VisibleForTesting1816 static final class HasErrorTextMatcher extends BoundedDiagnosingMatcher<View, EditText> {1817 @RemoteMsgField(order = 0)1818 private final Matcher<String> stringMatcher;1819 @RemoteMsgConstructor1820 private HasErrorTextMatcher(Matcher<String> stringMatcher) {1821 super(EditText.class);1822 this.stringMatcher = stringMatcher;1823 }1824 @Override1825 protected void describeMoreTo(Description description) {1826 description.appendText("editText.getError() to match ").appendDescriptionOf(stringMatcher);1827 }1828 @Override1829 protected boolean matchesSafely(EditText view, Description mismatchDescription) {1830 mismatchDescription.appendText("editText.getError() was ").appendValue(view.getError());1831 return stringMatcher.matches(view.getError());1832 }1833 }1834 @VisibleForTesting1835 static final class WithInputTypeMatcher extends BoundedDiagnosingMatcher<View, EditText> {1836 @RemoteMsgField(order = 0)1837 private final int inputType;1838 @RemoteMsgConstructor1839 private WithInputTypeMatcher(int inputType) {1840 super(EditText.class);1841 this.inputType = inputType;1842 }1843 @Override1844 protected void describeMoreTo(Description description) {1845 description.appendText("editText.getInputType() is ").appendValue(inputType);1846 }1847 @Override1848 protected boolean matchesSafely(EditText view, Description mismatchDescription) {1849 mismatchDescription1850 .appendText("editText.getInputType() was ")1851 .appendValue(view.getInputType());1852 return view.getInputType() == inputType;1853 }1854 }1855 @VisibleForTesting1856 static final class WithParentIndexMatcher extends TypeSafeDiagnosingMatcher<View> {1857 @RemoteMsgField(order = 0)1858 private final int index;1859 private final Matcher<ViewGroup> parentViewGroupMatcher = Matchers.isA(ViewGroup.class);1860 @RemoteMsgConstructor1861 private WithParentIndexMatcher(int index) {1862 this.index = index;1863 }1864 @Override1865 public void describeTo(Description description) {1866 description1867 .appendText("(view.getParent() ")1868 .appendDescriptionOf(parentViewGroupMatcher)1869 .appendText(" and is at child index ")1870 .appendValue(index)1871 .appendText(")");1872 }1873 @Override1874 protected boolean matchesSafely(View view, Description mismatchDescription) {1875 ViewParent parent = view.getParent();1876 if (!parentViewGroupMatcher.matches(parent)) {1877 mismatchDescription.appendText("view.getParent() ");1878 parentViewGroupMatcher.describeMismatch(parent, mismatchDescription);1879 return false;1880 }1881 if (((ViewGroup) parent).getChildCount() <= index) {1882 mismatchDescription...
Source:IsAvroObjectEqual.java
...290 matches = false;291 }292 }293 if (!matches && objectPath.isEmpty()) { // mismatch and we are the top-level element294 mismatchDescription.appendDescriptionOf(mismatchList);295 }296 return matches;297 }298 }299 private static abstract class AbstractFieldMatcher<T> extends AvroDiagnosingMatcher<T> {300 private final Matcher<?> valueMatcher;301 public AbstractFieldMatcher(Class<?> expectedType, List<String> fieldPath, Matcher<?> valueMatcher) {302 super(expectedType, fieldPath);303 this.valueMatcher = valueMatcher;304 }305 @Override306 protected boolean matchesSafely(T item, Description mismatchDescription) {307 Object value = getValue(item);308 if (valueMatcher.matches(value)) {...
Source:InvocationMatcher_ESTest.java
...190 public void test10() throws Throwable {191 Class<StringEndsWith> class0 = StringEndsWith.class;192 StringStartsWith stringStartsWith0 = new StringStartsWith("UU!iWT2y=4wfNK~");193 Description.NullDescription description_NullDescription0 = new Description.NullDescription();194 Description description0 = description_NullDescription0.appendDescriptionOf(stringStartsWith0);195 stringStartsWith0.describeMismatchSafely(".Win", description0);196 IsEventFrom isEventFrom0 = new IsEventFrom(class0, stringStartsWith0);197 Object[] objectArray0 = new Object[6];198 objectArray0[2] = (Object) ".Win";199 Object object0 = new Object();200 objectArray0[4] = (Object) stringStartsWith0;201 IsInstanceOf isInstanceOf0 = new IsInstanceOf(class0);202 Invocation invocation0 = mock(Invocation.class, new ViolatedAssumptionAnswer());203 Invocation invocation1 = mock(Invocation.class, new ViolatedAssumptionAnswer());204 doReturn("UU!iWT2y=4wfNK~", "").when(invocation1).toString();205 doReturn(objectArray0).when(invocation1).getArguments();206 doReturn(isInstanceOf0, "UU!iWT2y=4wfNK~", (Object) null, (Object) null).when(invocation1).getMock();207 InvocationMatcher invocationMatcher0 = new InvocationMatcher(invocation1);208 Answer<StringStartsWith> answer0 = (Answer<StringStartsWith>) mock(Answer.class, new ViolatedAssumptionAnswer());...
Source:ExpectationCheckResult.java
...51 return new ExpectationCheckResult<E, Q>(true, foundElements);52 }53 54 public String toString(){55 return Describer.newDescription().appendDescriptionOf(this).toString();56 }57}...
Source:DescriptionBuilder.java
...49 }50 private <T> void addError(String reason, T actual, Matcher<? super T> matcher) {51 description.appendText(reason)52 .appendText("\nExpected: ")53 .appendDescriptionOf(matcher)54 .appendText("\n but: ");55 matcher.describeMismatch(actual, description);56 description.appendText("\nLocation: ")57 .appendText(Throwables.getStackTraceAsString(new Exception()));58 matches = false;59 }60 public <T> DescriptionBuilder expected(String reason) {61 description.appendText(reason).appendText("\nExpected to not be reachable");62 description.appendText("\nLocation: ").appendText(63 Throwables.getStackTraceAsString(new Exception()));64 matches = false;65 return this;66 }67 public Description getDescription() {...
Source:CastingMatcher.java
...48 @Override49 public void describeTo(Description description) {50 description.appendText(typeDescription);51 if (matcher != null) {52 description.appendText(" ").appendDescriptionOf(matcher);53 }54 }55}...
Source:Description$NullDescription.java
1public final class org.hamcrest.Description$NullDescription implements org.hamcrest.Description {2 public org.hamcrest.Description$NullDescription();3 public org.hamcrest.Description appendDescriptionOf(org.hamcrest.SelfDescribing);4 public org.hamcrest.Description appendList(java.lang.String, java.lang.String, java.lang.String, java.lang.Iterable<? extends org.hamcrest.SelfDescribing>);5 public org.hamcrest.Description appendText(java.lang.String);6 public org.hamcrest.Description appendValue(java.lang.Object);7 public <T> org.hamcrest.Description appendValueList(java.lang.String, java.lang.String, java.lang.String, T...);8 public <T> org.hamcrest.Description appendValueList(java.lang.String, java.lang.String, java.lang.String, java.lang.Iterable<T>);9 public java.lang.String toString();10}...
appendDescriptionOf
Using AI Code Generation
1import org.hamcrest.Description;2import org.hamcrest.StringDescription;3import org.hamcrest.TextDescription;4import org.hamcrest.core.IsEqual;5public class NullDescriptionExample {6 public static void main(String[] args) {7 Description description = new StringDescription();8 description.appendDescriptionOf(new IsEqual("test"));9 System.out.println(description);10 description = new TextDescription(System.out);11 description.appendDescriptionOf(new IsEqual("test"));12 System.out.println(description);13 description = new NullDescription();14 description.appendDescriptionOf(new IsEqual("test"));15 System.out.println(description);16 }17}
appendDescriptionOf
Using AI Code Generation
1package com.java2novice.hamcrest;2import org.hamcrest.Description;3import org.hamcrest.Matcher;4import org.hamcrest.StringDescription;5import org.junit.Assert;6import org.junit.Test;7public class MyNullDescriptionTest {8 public void testNullDescription() {9 Matcher<String> matcher = new MyStringMatcher();10 Description desc = new StringDescription();11 matcher.describeTo(desc);12 Assert.assertEquals("My custom string matcher", desc.toString());13 }14 private class MyStringMatcher extends org.hamcrest.TypeSafeMatcher<String> {15 public void describeTo(Description desc) {16 desc.appendText("My custom string matcher");17 }18 protected boolean matchesSafely(String str) {19 return false;20 }21 }22}
appendDescriptionOf
Using AI Code Generation
1import org.hamcrest.Description2import org.hamcrest.Description.NullDescription3import org.hamcrest.Matcher4import org.hamcrest.Matchers.equalTo5import org.hamcrest.Matchers.is6def description = new NullDescription()7def matcher = is(equalTo(1))8matcher.appendDescriptionOf(description)9println description.toString()10import org.hamcrest.Description11import org.hamcrest.Description.StringDescription12import org.hamcrest.Matcher13import org.hamcrest.Matchers.equalTo14import org.hamcrest.Matchers.is15def description = new StringDescription()16def matcher = is(equalTo(1))17matcher.appendDescriptionOf(description)
appendDescriptionOf
Using AI Code Generation
1package com.example;2import org.hamcrest.Description;3import org.hamcrest.Description.NullDescription;4public class NullDescriptionAppendDescriptionOfExample {5 public static void main(String[] args) {6 NullDescription nullDescription = new NullDescription();7 nullDescription.appendDescriptionOf(null);8 System.out.println(nullDescription.toString());9 }10}
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!