Skip to content

Commit

Permalink
feat(TextView): add compound tint support (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vacxe authored Jun 9, 2023
1 parent f0a162a commit 279170b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

package io.github.kakaocup.kakao.common.matchers

import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.test.espresso.matcher.BoundedMatcher
import io.github.kakaocup.kakao.common.extentions.toBitmap
import io.github.kakaocup.kakao.common.utilities.getResourceColor
import io.github.kakaocup.kakao.common.utilities.getResourceDrawable
import org.hamcrest.Description

Expand All @@ -18,13 +23,15 @@ import org.hamcrest.Description
* @param topDrawableResId - id res of top compound drawable
* @param rightDrawableResId - id res of right compound drawable
* @param bottomDrawableResId - id res of bottom compound drawable
* @param tintColorId - id res of compound drawable tint
*/
@Suppress("MagicNumber")
class CompoundDrawableMatcher(
@DrawableRes private val leftDrawableResId: Int? = null,
@DrawableRes private val topDrawableResId: Int? = null,
@DrawableRes private val rightDrawableResId: Int? = null,
@DrawableRes private val bottomDrawableResId: Int? = null
@DrawableRes private val bottomDrawableResId: Int? = null,
@ColorRes private val tintColorId: Int? = null,
) : BoundedMatcher<View, TextView>(TextView::class.java) {

override fun matchesSafely(view: TextView?) = view?.let {
Expand All @@ -33,23 +40,38 @@ class CompoundDrawableMatcher(
val rightActual: Drawable? = it.compoundDrawables[2]
val bottomActual: Drawable? = it.compoundDrawables[3]

compare(leftDrawableResId, leftActual)
&& compare(topDrawableResId, topActual)
&& compare(rightDrawableResId, rightActual)
&& compare(bottomDrawableResId, bottomActual)
compare(leftDrawableResId, tintColorId, leftActual)
&& compare(topDrawableResId, tintColorId, topActual)
&& compare(rightDrawableResId, tintColorId, rightActual)
&& compare(bottomDrawableResId, tintColorId, bottomActual)

} ?: false

override fun describeTo(description: Description) {
description.appendText("Compound drawables doesn't match")
}

private fun compare(@DrawableRes expectedResId: Int? = null, actualDrawable: Drawable?): Boolean {
private fun compare(
@DrawableRes expectedResId: Int? = null,
@ColorRes tintColorId: Int? = null,
actualDrawable: Drawable?
): Boolean {
if (expectedResId == null && actualDrawable == null) return true
if (expectedResId != null && actualDrawable == null) return false
if (expectedResId == null && actualDrawable != null) return false

val expectedBitmap = getResourceDrawable(expectedResId!!)?.mutate()?.toBitmap()
val expectedDrawable = getResourceDrawable(expectedResId!!)?.mutate()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tintColorId?.let {
val tintColor = getResourceColor(it)
expectedDrawable?.apply {
setTintList(ColorStateList.valueOf(tintColor))
setTintMode(PorterDuff.Mode.SRC_IN)
}
}
}
val expectedBitmap = expectedDrawable?.toBitmap()
val actualBitmap = actualDrawable?.toBitmap()

if (expectedBitmap != null && actualBitmap != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,18 @@ interface TextViewAssertions : BaseAssertions {
* @param left left compound drawable resId
* @param top top compound drawable resId
* @param right right compound drawable resId
* @param bottom bottom compound drawable resId \
* @param bottom bottom compound drawable resId
*/
fun hasCompoundDrawable(
@DrawableRes left: Int? = null,
@DrawableRes top: Int? = null,
@DrawableRes right: Int? = null,
@DrawableRes bottom: Int? = null
@DrawableRes bottom: Int? = null,
@ColorRes tintColorId: Int? = null
) {
view.check(
ViewAssertions.matches(
CompoundDrawableMatcher(left, top, right, bottom)
CompoundDrawableMatcher(left, top, right, bottom, tintColorId)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class TextViewTest {
}
}

@Test
fun testTintedCompoundDrawables() {
Screen.onScreen<TextScreen> {
textViewWithLeftTintedDrawable.hasCompoundDrawable(left = R.drawable.ic_android_black_24dp, tintColorId = R.color.red)
textViewWithRightTintedDrawable.hasCompoundDrawable(right = R.drawable.ic_android_black_24dp, tintColorId = R.color.red)
textViewWithTopTintedDrawable.hasCompoundDrawable(top = R.drawable.ic_android_black_24dp, tintColorId = R.color.red)
textViewWithBottomTintedDrawable.hasCompoundDrawable(bottom = R.drawable.ic_android_black_24dp, tintColorId = R.color.red)
}
}

@Test
fun testTextGravity() {
Screen.onScreen<TextScreen> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class TextScreen : Screen<TextScreen>() {
val textViewWithTopDrawable = KTextView { withId(R.id.text_view_drawable_top) }
val textViewWithBottomDrawable = KTextView { withId(R.id.text_view_drawable_bottom) }

val textViewWithLeftTintedDrawable = KTextView { withId(R.id.text_view_drawable_tinted_left) }
val textViewWithRightTintedDrawable = KTextView { withId(R.id.text_view_drawable_tinted_right) }
val textViewWithTopTintedDrawable = KTextView { withId(R.id.text_view_drawable_tinted_top) }
val textViewWithBottomTintedDrawable = KTextView { withId(R.id.text_view_drawable_tinted_bottom) }

val textViewCentered = KTextView { withId(R.id.text_view_gravity_center) }
val textViewOnStart = KTextView { withId(R.id.text_view_gravity_start) }
val textViewOnEnd = KTextView { withId(R.id.text_view_gravity_end) }
Expand Down
33 changes: 33 additions & 0 deletions sample/src/main/res/layout/activity_text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@
android:drawableBottom="@drawable/ic_android_black_24dp"
android:text="Drawable bottom" />


<TextView
android:id="@+id/text_view_drawable_tinted_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_android_black_24dp"
android:drawableTint="@color/red"
android:text="Drawable Tinted left" />

<TextView
android:id="@+id/text_view_drawable_tinted_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/ic_android_black_24dp"
android:drawableTint="@color/red"
android:text="Drawable Tinted right" />

<TextView
android:id="@+id/text_view_drawable_tinted_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_android_black_24dp"
android:drawableTint="@color/red"
android:text="Drawable Tinted top" />

<TextView
android:id="@+id/text_view_drawable_tinted_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@drawable/ic_android_black_24dp"
android:drawableTint="@color/red"
android:text="Drawable Tinted bottom" />

<TextView
android:id="@+id/text_view_gravity_center"
android:layout_width="match_parent"
Expand Down

0 comments on commit 279170b

Please sign in to comment.