George Kylafas
08/04/2021, 12:19 PMText()
node has text with a certain style, e.g. bold? Is there such a matcher/assertion available?
The testing cheat sheet doesn't seem to include something similar.Zach Klippenstein (he/him) [MOD]
08/04/2021, 4:46 PMSemanticsProperties.Text
property which gives you a list of `AnnotatedString`s, and check those strings for the styles you expect.
You can look at the implementation of the existing assertions, eg assertTextContains
, as a starting point.George Kylafas
08/06/2021, 12:58 PMSemanticsProperties.Text
's AnnotatedString
s have no spanStyles nor paragraphStyles (both are empty lists).
However, the node also contained a SemanticsActions.GetTextLayoutResult
property, which is a lambda whose enclosing class is a TextController
which contains a TextState
which contains a TextDelegate
which contains a TextStyle
which contains the desired FontWeight
. Since half of those classes are internal, I had to do a fair amount of reflection to come up with this matcher:
@OptIn(InternalFoundationTextApi::class)
fun hasFontWeight(
weight: FontWeight,
): SemanticsMatcher {
val propertyName = SemanticsActions.GetTextLayoutResult
return SemanticsMatcher(
"$propertyName contains $weight text"
) {
val actualWeight = it.config.getOrNull(propertyName)
?.action
?.javaPrivateField("this$0")
?.javaPrivateField("state")
?.javaPrivateField("textDelegate")
?.run { this as TextDelegate }
?.style
?.fontWeight
actualWeight == weight
}
}
where
fun Any.javaPrivateField(fieldName: String): Any? =
this.javaClass
.getDeclaredField(fieldName)
.also { it.isAccessible = true }
.get(this)
I am certain this is NOT the proper way to do this, but until the framework supports this officially I guess it will have to do.Zach Klippenstein (he/him) [MOD]
08/06/2021, 1:21 PMZach Klippenstein (he/him) [MOD]
08/06/2021, 3:12 PMGeorge Kylafas
08/06/2021, 8:58 PMGeorge Kylafas
08/10/2021, 8:27 AM