Advertisement
Guest User

Untitled

a guest
Nov 8th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.53 KB | None | 0 0
  1. package your_package
  2.  
  3. import android.content.res.ColorStateList
  4. import android.graphics.Bitmap
  5. import android.graphics.Canvas
  6. import android.graphics.PorterDuff
  7. import android.graphics.drawable.BitmapDrawable
  8. import android.graphics.drawable.Drawable
  9. import android.graphics.drawable.StateListDrawable
  10. import android.os.Build
  11. import android.view.View
  12. import android.widget.ImageView
  13. import androidx.annotation.ColorRes
  14. import androidx.annotation.DrawableRes
  15. import androidx.core.graphics.drawable.DrawableCompat
  16. import androidx.test.espresso.DataInteraction
  17. import androidx.test.espresso.assertion.ViewAssertions
  18. import com.agoda.kakao.common.assertions.BaseAssertions
  19. import com.agoda.kakao.common.builders.ViewBuilder
  20. import com.agoda.kakao.common.utilities.getResourceColor
  21. import com.agoda.kakao.common.utilities.getResourceDrawable
  22. import com.agoda.kakao.common.views.KBaseView
  23. import com.agoda.kakao.image.KImageView
  24. import org.hamcrest.Description
  25. import org.hamcrest.Matcher
  26. import org.hamcrest.TypeSafeMatcher
  27.  
  28. class KImageView2 : KBaseView<KImageView>, ImageViewAssertions2 {
  29.     constructor(function: ViewBuilder.() -> Unit) : super(function)
  30.     constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function)
  31.     constructor(parent: DataInteraction, function: ViewBuilder.() -> Unit) : super(parent, function)
  32. }
  33.  
  34.  
  35. interface ImageViewAssertions2 : BaseAssertions {
  36.     /**
  37.      * Checks if the view displays given drawable
  38.      *
  39.      * @param resId Drawable resource to be matched
  40.      * @param toBitmap Lambda with custom Drawable -> Bitmap converter (default is null)
  41.      */
  42.     fun hasDrawable(@DrawableRes resId: Int, toBitmap: ((drawable: Drawable) -> Bitmap)? = null) {
  43.         view.check(ViewAssertions.matches(DrawableMatcher2(resId = resId, toBitmap = toBitmap)))
  44.     }
  45. }
  46.  
  47.  
  48. class DrawableMatcher2(
  49.     @DrawableRes private val resId: Int = -1,
  50.     @ColorRes private val tintColorId: Int? = null,
  51.     private val toBitmap: ((drawable: Drawable) -> Bitmap)? = null
  52. ) : TypeSafeMatcher<View>(View::class.java) {
  53.  
  54.     override fun describeTo(desc: Description) {
  55.         desc.appendText("with drawable id $resId or provided instance")
  56.     }
  57.  
  58.     override fun matchesSafely(view: View?): Boolean {
  59.         if (view !is ImageView) {
  60.             return false
  61.         }
  62.  
  63.         if (resId < 0) {
  64.             return view.drawable == null
  65.         }
  66.         val bitmap = extractFromImageView(view)
  67.         view.setImageResource(resId)
  68.         val otherBitmap = extractFromImageView(view)
  69.  
  70.         return bitmap?.sameAs(otherBitmap) ?: false
  71.     }
  72.  
  73.     private fun extractFromImageView(view: View?): Bitmap? {
  74.         return view?.let { imageView ->
  75.             var expectedDrawable: Drawable? = getResourceDrawable(resId)?.mutate()
  76.  
  77.             if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && expectedDrawable != null) {
  78.                 expectedDrawable = DrawableCompat.wrap(expectedDrawable).mutate()
  79.             }
  80.  
  81.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  82.                 tintColorId?.let { tintColorId ->
  83.                     val tintColor = getResourceColor(tintColorId)
  84.                     expectedDrawable?.apply {
  85.                         setTintList(ColorStateList.valueOf(tintColor))
  86.                         setTintMode(PorterDuff.Mode.SRC_IN)
  87.                     }
  88.                 }
  89.             }
  90.  
  91.             if (expectedDrawable == null) {
  92.                 return null
  93.             }
  94.  
  95.             val convertDrawable = (imageView as ImageView).drawable.mutate()
  96.             toBitmap?.invoke(convertDrawable) ?: convertDrawable.toBitmap()
  97.         }
  98.     }
  99. }
  100.  
  101. internal fun Drawable.toBitmap(): Bitmap {
  102.     if (this is BitmapDrawable && this.bitmap != null) {
  103.         return this.bitmap
  104.     }
  105.  
  106.     if (this is StateListDrawable && this.getCurrent() is BitmapDrawable) {
  107.         val bitmapDrawable = this.getCurrent() as BitmapDrawable
  108.         if (bitmapDrawable.bitmap != null) {
  109.             return bitmapDrawable.bitmap
  110.         }
  111.     }
  112.  
  113.     val bitmap = if (this.intrinsicWidth <= 0 || this.intrinsicHeight <= 0) {
  114.         Bitmap.createBitmap(
  115.             1,
  116.             1,
  117.             Bitmap.Config.ARGB_8888
  118.         ) // Single color bitmap will be created of 1x1 pixel
  119.     } else {
  120.         Bitmap.createBitmap(this.intrinsicWidth, this.intrinsicHeight, Bitmap.Config.ARGB_8888)
  121.     }
  122.  
  123.     val canvas = Canvas(bitmap)
  124.     this.setBounds(0, 0, canvas.width, canvas.height)
  125.     this.draw(canvas)
  126.     return bitmap
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement