https://kotlinlang.org logo
#compose
Title
# compose
t

Tom Truyen

02/26/2024, 6:54 AM
For testing purposes in an app we use
semantics
on our
Composables
. Now for a custom component we had to use
Canvas
te draw it. Does anyone know of a way to access the contents of the
Canvas
in testing? e.g.: a
drawText
is used and we would like to validate the value displayed. Can this be done by reading it directly from the
Canvas
in testing or must we attempt to validate this value in our testing before getting to the point of drawing it on the
Canvas
a

Albert Chang

02/26/2024, 10:12 AM
You’ll need to either validate the semantics property or validate the snapshot. Btw if you draw text manually, you should properly set the semantics property (namely
text
) anyway for accessibility (so that the screen reader can read the text).
t

Tom Truyen

02/26/2024, 10:19 AM
But you can't add a semantics modifier, or any modifier for that matter to a
drawText
so is there an alternative?
a

Albert Chang

02/26/2024, 10:20 AM
Apply the semantics modifier to the canvas.
t

Tom Truyen

02/26/2024, 10:23 AM
Yea but in the
DrawScope
how can I access the value of the
drawText
that can't be done with the
semantics
on the Canvas itself
a

Albert Chang

02/26/2024, 10:24 AM
You can’t.
z

Zach Klippenstein (he/him) [MOD]

02/26/2024, 6:23 PM
How are you getting the text inside the draw function? Just pass that same text to the semantics modifier
t

Tom Truyen

02/26/2024, 6:27 PM
Oh yea like that is possible I guess. Unfortunately we have decided to globally use contentDescription on each component and since I have multiple texts this won't work for our specific use case but it is a good idea nonetheless