8

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?

0

1 Answer 1

7
+500

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm having a similar issue but changing emulator api version and using real device didn't solve it. Do you by any chance have an idea about it?
Nope, sorry. The answer I posted was the solution to my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.