`createAndroidComposeRule` doesn't allow calling `...
# compose
r
createAndroidComposeRule
doesn't allow calling
setContent
anymore if the root content of the Activity has already been set. I remember this used to work in compose beta versions but it's now throwing an
IllegalStateException
. I found it to be a really handy way in compose UI testing. Is there any reason why the behaviour was changed?
a
@Jelle Fresen [G]
j
Yes. The test rule's
setContent
is intended as a way to test content in isolation. When there is already content present, it is a sign that the content is actually not as isolated as suggested. This can lead to subtle bugs where you think you're testing one thing, but are actually testing something else. This becomes especially error prone when testing interoperability between Compose and Views surrounding it. Imaging you're setting up a view hierarchy that contains a ComposeView in which you want to set some content, and you want to verify that certain things do not happen in the View hierarchy when you interact with the compose content. You inadvertently use the test rule's
setContent
which wipes away your View hierarchy, sets the content, and you're test then checks the removed view hierarchy to see if the thing didn't happen and passes. Instead of silently allowing this kind of behavior, we now throw the exception to make sure developers don't accidentally make this kind of mistake. If you do in fact need this kind of "wipe and replace" functionality, I think you could add it as an (internal) feature to your Activity. In general though I'd recommend either to use a clean activity and use the test rule's
setContent
, or don't use the test rule's
setContent
and let the activity set the correct content for your test.
1
r
I ended up using your recommended approach by creating a clean activity without content for debug builds, which makes
setContent
possible. I wanted to understand the reasoning behind this change so thank you very much for the detailed explanation.
j
Your welcome 🙂