Alex Styl
08/10/2025, 4:56 AMAlex Styl
08/10/2025, 5:05 AM@Test
fun assertTextIsRed() = runComposeUiTest {
setContent {
Text("Hello World", color = Color.Red)
}
onNodeWithText("Hello World")
.assertTextStyle { it.color == Color.Red }
}
private fun SemanticsNodeInteraction.assertTextStyle(matcher: (TextStyle) -> Boolean): SemanticsNodeInteraction {
return this.assert(
SemanticsMatcher("TextStyle") { node ->
val textLayoutResults = mutableListOf<TextLayoutResult>()
node.config[SemanticsActions.GetTextLayoutResult].action?.invoke(textLayoutResults)
if (textLayoutResults.isNotEmpty()) {
val textStyle = textLayoutResults[0].layoutInput.style
matcher(textStyle)
} else {
false
}
}
)
}