Android系统。 Espresso:如何检查TextView的大写字母?

时间:2018-06-12 12:29:49

标签: android-espresso

Android 3.1.2,Gradle 4.4,Java 1.8,Espresso 3.0.1。

我的xml布局的片段:

  <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" />

结果如下: enter image description here

正如您可以看到此 TextView 中的文字以大写字母显示。尼斯。 不,我想写Espresso测试,检查 textView 是否在大写上。 所以这里是我的Espresso仪器测试:

@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);
                }
            }
        };
    }

但是当我开始测试 loginTextViewUppercase 时,我收到错误:

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)

2 个答案:

答案 0 :(得分:3)

您可以从TextView本身检索转换方法,以检查它是textAllCaps。在这种情况下,您无需对prod代码添加任何修改:

        @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;
            }
        }

答案 1 :(得分:1)

它为我解决了 loginTextView.setText(strings.toUpperCase());