浓缩咖啡测试通过设备,但在仿真器上失败

时间:2019-04-05 15:22:45

标签: android android-espresso

Android Studio 3.2

我写了一个Espresso测试来检查TextView的文本大小

@Test
    fun quoteTextViewTextSize() {
        onView(withId(R.id.quoteTextView)).check(matches(withFontSizeResId(R.dimen.text_view_text_size)))
}


fun withFontSizeResId(expectedResourceId: Int): Matcher<View> {
        return object : BoundedMatcher<View, View>(View::class.java!!) {
            private var expectedValueDp: Float = 0.toFloat()

            public override fun matchesSafely(view: View): Boolean {
                if (view !is TextView) {
                    return false
                }
                val textView = view as TextView
                val actualSizePx = textView.textSize
                val actualValueDp = AndroidUtil.px2dp(view.getContext(), actualSizePx.toFloat())
                expectedValueDp = AndroidUtil.getDimensionDp(view.getContext(), expectedResourceId)
                return java.lang.Float.compare(actualValueDp, expectedValueDp) == 0
            }

            override fun describeTo(description: Description) {
                description.appendText("with fontSize: ")
                description.appendValue(expectedResourceId)
            }
        }
}

  public static float px2dp(Context context, float px) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
        return dp;
    }

 public static float getDimensionDp(Context context, int dimenResourceId) {
        float dimenDp = (context.getResources().getDimension(dimenResourceId) / context.getResources().getDisplayMetrics().density);
        return dimenDp;
    }

在设备 Android 6.0 上运行,并通过测试成功。 很好。

现在,我在 Android Emulator 4.4和Android Emulator 9.0 上开始相同的测试,并且测试失败

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with fontSize: <2131099870>' doesn't match the selected view.
Expected: with fontSize: <2131099870>
Got: "AppCompatTextView{id=2131230926, res-name=quoteTextView, visibility=VISIBLE, width=432, height=28, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@afbaa150, tag=null, root-is-layout-requested=false, has-input-connection=false, x=24.0, y=232.0, text=Quote, input-type=0, ime-target=false, has-links=false}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:318)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
at com.myproject.android.activity.AddTraderActivityTest.quoteTextViewTextSize(AddTraderActivityTest.kt:175)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:389)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: junit.framework.AssertionFailedError: 'with fontSize: <2131099870>' doesn't match the selected view.
Expected: with fontSize: <2131099870>
Got: "AppCompatTextView{id=2131230926, res-name=quoteTextView, visibility=VISIBLE, width=432, height=28, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@afbaa150, tag=null, root-is-layout-requested=false, has-input-connection=false, x=24.0, y=232.0, text=Quote, input-type=0, ime-target=false, has-links=false}"

at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:539)
at androidx.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:103)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:419)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:282)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:268)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

为什么在模拟器上测试失败?

0 个答案:

没有答案