Skip to content

Commit

Permalink
Fix color state list color check (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
alorma authored May 28, 2020
1 parent 14c603e commit 7005df6
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ class TextColorMatcher(private val expectedColor: Int) : BoundedMatcher<View, Te

private fun matchesColorList(textView: TextView): Boolean {
val currentColorList = textView.textColors
val expectedColorInt = ContextCompat.getColorStateList(textView.context, expectedColor)
return currentColorList == expectedColorInt
val expectedColorList = ContextCompat.getColorStateList(textView.context, expectedColor)!!

val allStates = ALL_COLOR_STATE_LIST_STATES.flatMap { state -> listOf(state, -state) }.map { state -> intArrayOf(state) }

return allStates.all { state ->
val currentStateColor = currentColorList.getColorForState(state, currentColorList.defaultColor)
val expectedStateColor = expectedColorList.getColorForState(state, expectedColorList.defaultColor)
currentStateColor == expectedStateColor
}
}

override fun describeTo(description: Description) {
Expand All @@ -62,4 +69,26 @@ class TextColorMatcher(private val expectedColor: Int) : BoundedMatcher<View, Te
description.appendText("with text color: [$expectedColor]")
}
}

companion object {
private val ALL_COLOR_STATE_LIST_STATES = listOf(
android.R.attr.state_focused,
android.R.attr.state_window_focused,
android.R.attr.state_enabled,
android.R.attr.state_checkable,
android.R.attr.state_checked,
android.R.attr.state_selected,
android.R.attr.state_pressed,
android.R.attr.state_activated,
android.R.attr.state_active,
android.R.attr.state_single,
android.R.attr.state_first,
android.R.attr.state_middle,
android.R.attr.state_last,
android.R.attr.state_accelerated,
android.R.attr.state_hovered,
android.R.attr.state_drag_can_accept,
android.R.attr.state_drag_hovered
)
}
}

0 comments on commit 7005df6

Please sign in to comment.