Has anyone successfully performed <Compose UI test...
# compose
d
Has anyone successfully performed Compose UI tests on the content of a
ComposeView
within a traditional
View
hierarchy? Any examples of this?
w
Wouldn’t you be able to test the ComposeView in isolation? I shouldn’t matter if it is displayed in a traditional view hierarchy, as long as the Composable itself can be accessed from the test.
d
I agree that would constitute valuable testing. However I'm introducing Compose into a project that already has UI tests whose scope spans several screens in a workflow.
So to keep the same tests, I have to be able to 'handle' both the new ComposeView content of each screen and the View 'glue' between them.
I'm looking to do something like, when the view inflates, cast its root View as a ComposeView, and then somehow plug that into the
composeTestRule
for inspection and all that goodness...
w
I tried to interact with the ComposeView before when doing some work with BottomSheet, and wasn’t able to make any headway. I’ll follow the thread, interested to see if anyone else has had any luck here.
d
Made some headway by using:
Copy code
@get:Rule
    val composeTestRule = createAndroidComposeRule<MainActivity>()
☝️ This allows testing
Activity
-hosted Compose content, rather than that tested directly with test
setContent
.
I can now inspect some Compose elements but the test is very flaky, erroring on most runs with e.g.
Resource Compose-Espresso link isIdleNow() is returning true, but a message indicating that the resource has transitioned from busy to idle was never sent.
🤔
w
What happens if you introduce some lengthy delays? Is the isIdleNow() method not returning at all, or is it just not returning in time?
d
Checking on that, I'm doing
Copy code
composeTestRule.waitForIdle()
before using the
composeTestRule
further.
After running more, I'd say it's passing about half the time.
I don't understand the premise of the error TBH, why should the resource have ever been 'busy' at the beginning of the test? Busy as in, Compose must have done some work to initially lay-out the screen?
w
XML renders before Compose, and ComposeView does some strange things- I’ve found that they don’t really talk to each other very well in my experience. It could be that the Compose portion returned isIdleNow(), but the XML portion hasn’t completed its last render pass yet. That would explain the flakiness, but might not help in finding a solution (aside from switching to pure Compose, which isn’t always a possibility)
(though as a side note, I did some research a few weeks ago about Compose performance, and it is faster when there isn’t an XML wrapping layer, so moving to it sooner rather than later might not be a bad idea if it can work)
d
Thanks for sharing that @Will Shelor. This is my second project in Compose. The first was 'green fields' and so pure Compose.
This time around we're retro-fitting, and it's getting uglier 😄
...around the edges, anyway.
w
We’ve started converting a large project, and I know what you mean. I’m finding that the biggest challenges are the way the XML and Compose fight against each other. We had a scrollable view in a bottom sheet with custom behavior, and the nested scrolling from XML to Compose did not play nice. Rewriting the whole thing in Compose was so much easier than expected, though!
👍 1
d
We are at least converting 'whole screens' to Compose, but we're also tied to a 3rd party navigation framework called Conductor, which deals in Views, so our root view is a ComposeView, but couched in the View framework which is imposing limitations.
One exception to this is in our 'component catalogue' App where we, very successfully, peppered the ComposeViews among the catalogue View hierarchy, to provide side-by-side comparison of old vs new component for L&F
Despite these teething troubles its going to pay off, we're loving using Compose.
I also think I just resolved the flakiness.
Up to 7 runs with no fail.
(Don't jinx it!)
...slightly embarrasing but it seems the flakiness was cause by the screen switching off on my test device 😅 after I forced it to stay on by increasing the timeout, the tests pass reliably!
🤣 1