Jüri Kiin
Jüri Kiin

Reputation: 809

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?

Upvotes: 8

Views: 6264

Answers (1)

Jüri Kiin
Jüri Kiin

Reputation: 809

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.

Upvotes: 7

Related Questions