Issue
Android 3.1.2, Gradle 4.4, Java 1.8, Espresso 3.0.1.
Here snippet of my xml layout:
<TextView
android:id="@+id/loginTextView"
android:layout_width="255dp"
android:layout_height="60dp"
android:layout_marginBottom="15dp"
android:background="@drawable/sign_in_login_bg"
android:gravity="center"
android:onClick="@{ () -> presenter.doLogin()}"
android:text="@string/login"
android:textAllCaps="true"
android:textColor="@android:color/white"
app:layout_constraintBottom_toTopOf="@+id/registerTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
As you can see text in this TextView show in uppercase. Nice. No I want to write Espresso test that check is this textView is on uppercase. So here my Espresso instrumentation test:
@Test
public void loginTextViewUppercase() {
onView(withId(R.id.loginTextView)).check(matches(withUppercaseText(R.string.login)));
}
public static Matcher<View> withUppercaseText(final int resourceId) {
return new BoundedMatcher<View, TextView>(TextView.class) {
private String resourceName = null;
private String expectedText = null;
@Override
public boolean matchesSafely(TextView textView) {
if (null == expectedText) {
try {
expectedText = textView.getResources().getString(resourceId).toUpperCase();
resourceName = textView.getResources().getResourceEntryName(resourceId);
} catch (Resources.NotFoundException ignored) {
/* view could be from a context unaware of the resource id. */
}
}
CharSequence actualText = textView.getText();
if (null != expectedText && null != actualText) {
return expectedText.equals(actualText.toString());
} else {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("with uppercase string from resource id: ");
description.appendValue(resourceId);
if (null != resourceName) {
description.appendText("[");
description.appendText(resourceName);
description.appendText("]");
}
if (null != expectedText) {
description.appendText(" value: ");
description.appendText(expectedText);
}
}
};
}
But when I start test loginTextViewUppercase I get error:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with uppercase string from resource id: <2131624014>' doesn't match the selected view.
Expected: with uppercase string from resource id: <2131624014>[login] value: LOGIN
Got: "AppCompatTextView{id=2131296429, res-name=loginTextView, visibility=VISIBLE, width=765, height=180, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@7f5fced, tag=null, root-is-layout-requested=false, has-input-connection=false, x=158.0, y=1283.0, text=Login, input-type=0, ime-target=false, has-links=false}"
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90)
Solution
You could retrieve the transformation method from the TextView
itself to check that it's textAllCaps. In this case, you don't need to add any modifications to the prod code:
@Override
public boolean matchesSafely(TextView textView) {
if (null == expectedText) {
try {
expectedText = textView.getResources().getString(resourceId).toUpperCase();
resourceName = textView.getResources().getResourceEntryName(resourceId);
} catch (Resources.NotFoundException ignored) {
/* view could be from a context unaware of the resource id. */
}
}
String actualText = textView.getText().toString();
if (null != expectedText) {
TransformationMethod currentMethod = textView.getTransformationMethod();
//if the transformation is AllCapsTransformationMethod then it means that the text is uppercase
return expectedText.equalsIgnoreCase(actualText) && currentMethod != null && AllCapsTransformationMethod.class.getClass().isInstance(currentMethod.getClass());
} else {
return false;
}
}
Answered By - Anatolii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.