并非始终正确检查textView的重力

时间:2019-04-20 10:31:34

标签: android android-espresso ui-testing

Android Studio 3.4

这是我的xml:

<androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/binContainer"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_marginStart="@dimen/half_default_margin"
                        android:layout_marginTop="@dimen/default_margin"
                        android:layout_marginBottom="@dimen/half_default_margin"
                        android:background="@{item.binance_pseudo_wallet.micro_wallet.inTrade ? @color/wallet_in_trade_color : @color/wallet_not_in_trade_color}"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/bitContainer"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">

                        <TextView
                            android:id="@+id/binanceTextView"
                            style="@style/valueTextViewStyle"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/min_height"
                            android:background="@color/mcgpalette0_100"
                            android:gravity="center"
                            android:text="@string/binance"
                            android:textAllCaps="true"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintHorizontal_bias="0.5"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />

                        <TextView
                            android:id="@+id/binInTradeLabelTextView"
                            style="@style/labelTextViewStyle"
                            android:layout_width="@dimen/wallet_label_width"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/quarter_default_margin"
                            android:text="@string/in_trade"
                            app:layout_constraintBottom_toBottomOf="@+id/binInTradeValueTextView"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="@+id/binInTradeValueTextView" />

                        <TextView
                            android:id="@+id/binInTradeValueTextView"
                            style="@style/valueTextViewValueStyle"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="@dimen/half_default_margin"
                            android:layout_marginTop="@dimen/half_default_margin"
                            android:gravity="start"
                            android:text="@{String.valueOf(item.binance_pseudo_wallet.micro_wallet.inTrade)}"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toEndOf="@+id/binInTradeLabelTextView"
app:layout_constraintTop_toBottomOf="@+id/binanceTextView" />

我想检查 binanceTextView binInTradeValueTextView 上的重力。

此处是自定义匹配项:

fun withTextViewGravity(expectedValue: Int): Matcher<View> {
        return object : BoundedMatcher<View, TextView>(TextView::class.java) {

            override fun matchesSafely(textView: TextView): Boolean {
                val currentGravity = textView.gravity
                return currentGravity == expectedValue
            }

            override fun describeTo(description: Description) {
                description.appendText("with gravity: ")
                description.appendValue(expectedValue)
            }
        }
}

Espresso对 binanceTextView

的测试
    @Test
    fun binance_gravity() {
        onView(withId(R.id.binanceTextView))
                .check(matches(withTextViewGravity(Gravity.CENTER)))
}

测试通过,因为 currentGravity expectedValue = 17 。结果,安全匹配返回true

很好。

现在我要检查 binInTradeValueTextView

@Test
    fun binInTradeValue_gravity() {
        onView(withId(R.id.binInTradeValueTextView))
                .check(matches(withTextViewGravity(Gravity.START)))
}

但是此测试失败。因为: currentGravity = 8388659 expectedValue = 8388611

为什么 currentGravity expectedValue 不相等?

0 个答案:

没有答案