首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓。Espresso:如何检查TextView的大写?

安卓。Espresso:如何检查TextView的大写?
EN

Stack Overflow用户
提问于 2018-06-12 20:29:49
回答 2查看 969关注 0票数 0

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

下面是我的xml布局的一小段:

代码语言:javascript
复制
  <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" />

下面是结果:

正如您所看到的,此TextView中的文本以大写显示。好的。不,我想写浓咖啡测试,检查这个textView是在大写上。下面是我的Espresso仪器测试:

代码语言:javascript
复制
@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时,我得到了错误:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-12 21:02:08

您可以从TextView本身检索转换方法,以检查它是否为textAllCaps。在这种情况下,您不需要对prod代码进行任何修改:

代码语言:javascript
复制
        @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;
            }
        }
票数 3
EN

Stack Overflow用户

发布于 2018-06-12 20:37:43

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50817281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档