如何从Fresco的SimpleDraweeView获取图像?

时间:2018-09-12 13:25:00

标签: kotlin android-espresso android-testing

我想测试图像是否已加载。因此,我正在比较占位符图像和作为位图加载的图像。但是我无法到达已加载的图像。如何获得?

测试

onView(withId(R.id.sdv_profile_image)).check(matches(CustomMatchers.hasSameImage(R.drawable.ic_lock_white_24_dp)))

CustomMatchers.class

fun hasSameImage(expectedId: Int): Matcher<View> {
        return object : TypeSafeMatcher<View>(SimpleDraweeView::class.java) {
            override fun describeTo(description: Description) {
            }

            override fun matchesSafely(target: View?): Boolean {
                val image = target as SimpleDraweeView
                val resources = target.context.resources
                val expectedDrawable = resources.getDrawable(expectedId)

                if (expectedDrawable == null) return false

                val bitmap = getBitmap(image.background)
                val otherBitmap = getBitmap(expectedDrawable)
                return bitmap.sameAs(otherBitmap)
            }
        }}
 private fun getBitmap(drawable: Drawable): Bitmap {
        val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        drawable.setBounds(0, 0, canvas.width, canvas.height)
        drawable.draw(canvas)
        return bitmap
    }

错误

java.lang.IllegalStateException: image.background must not be null

0 个答案:

没有答案