Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose UI Testing Button onClick

I'm trying to test how a buttonClick would change the UI.

The test is setup like this:

composeRule.setContent {
    var i by remember { mutableStateOf(0) }
    Button(modifier = Modifier.testTag("TEST"), onClick = { i++ }) {
        Text(text = i.toString())
    }
}

And my actual test looks like this:

val button = composeRule.onNodeWithTag("TEST")

button.assertTextEquals("0")
button.performClick()
button.printToLog("Test")
button.assertTextEquals("1")

The first assertion passes, however it fails when checking that the text should equal 1:

java.lang.AssertionError: Failed to assert the following: (Text + EditableText = [1])
Semantics of the node:
Node #2 at (l=0.0, t=66.0, r=176.0, b=165.0)px, Tag: 'TEST'
Role = 'Button'
Text = '[0]'
Actions = [OnClick, GetTextLayoutResult]
MergeDescendants = 'true'
Selector used: (TestTag = 'TEST')

The printToLog() after the click looks like this:

Printing with useUnmergedTree = 'false'
Node #2 at (l=0.0, t=66.0, r=176.0, b=165.0)px, Tag: 'TEST'
Role = 'Button'
Text = '[0]'
Actions = [OnClick, GetTextLayoutResult]
MergeDescendants = 'true'

So it appears that when the click is performed, either the content is not recomposed, or for some reason, the click isn't actually happening.

Does anyone know what might be going on here?

like image 868
Jüri Kiin Avatar asked Jun 26 '26 19:06

Jüri Kiin


1 Answers

I resolved this issue!

My emulator was running API level 32.. Looks like the Compose Interaction Library does not fully work with 32 yet.

Code worked without any changes after downgrading API level.

like image 112
Jüri Kiin Avatar answered Jun 28 '26 09:06

Jüri Kiin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!