Не могу найти изображение в пункте меню - PullRequest
0 голосов
/ 04 июня 2019

Я хочу проверить, есть ли в пункте меню изображение.

Здесь menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <item
        android:id="@+id/add_trader"
        android:icon="@drawable/ic_add"
        app:showAsAction="always"
        tools:ignore="MenuTitle" />
</menu>

Вот тест Эспрессо:

@Test
    fun menu_item_addTrader_icon_display() {
        onView(withId(add_trader))
                .check(matches(withImageDrawableResId(R.drawable.ic_add)))
    }

Здесь настраиваемое совпадение:

fun withImageDrawableResId(expectedResourceId: Int): Matcher<View> {

        return object : BoundedMatcher<View, ImageView>(ImageView::class.java!!) {
            private var resourceName: String? = null

            override fun matchesSafely(targetImageView: ImageView): Boolean {
                if (expectedResourceId < 0) {
                    return targetImageView.getDrawable() == null
                }
                resourceName = targetImageView.getContext().getResources().getResourceEntryName(expectedResourceId)
                //Log.d(TAG, "withImageDrawableResId.matchesSafely: expectedId = " + expectedResourceId + " ---> resourceName = " + resourceName);
                return sameBitmap(targetImageView.getContext(), targetImageView.getDrawable(), expectedResourceId)
            }

            override fun describeTo(description: Description) {
                description.appendText("with drawable from resource id: ")
                description.appendValue(expectedResourceId)
                if (resourceName != null) {
                    description.appendText("[")
                    description.appendText(resourceName)
                    description.appendText("]")
                }
            }
        }
    }

Но тест не пройден с сообщением:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with drawable from resource id: <2131165293>' doesn't match the selected view.
Expected: with drawable from resource id: <2131165293>
Got: "ActionMenuItemView{id=2131230780, res-name=add_trader, visibility=VISIBLE, width=144, height=144, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.appcompat.widget.ActionMenuView$LayoutParams@daaf409, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0, text=, input-type=0, ime-target=true, has-links=false}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
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.TradersActivityTest.menu_item_addTrader_icon_display(TradersActivityTest.kt:379)
at java.lang.reflect.Method.invoke(Native Method)
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.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: junit.framework.AssertionFailedError: 'with drawable from resource id: <2131165293>' doesn't match the selected view.
Expected: with drawable from resource id: <2131165293>
Got: "ActionMenuItemView{id=2131230780, res-name=add_trader, visibility=VISIBLE, width=144, height=144, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.appcompat.widget.ActionMenuView$LayoutParams@daaf409, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0, text=, input-type=0, ime-target=true, has-links=false}"

Метод BoundedMatcher.describeTo является вызовом, но метод BoundedMatcher.matchesSafely не является вызовом. Почему?

...