I am testing a simple TextField composable and I am using onNodeWithContentDescription() to locate this TextField which I have applied semantics on its modifier.
TextField(
modifier = Modifier
.fillMaxWidth()
.semantics { contentDescription = "TextField" },
value = text,
onValueChange = { onTextChange(it) },
This is my test function:
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun openSearchWidget_enterInputText_assertInputText() {
var text by mutableStateOf("")
composeTestRule.setContent {
SearchWidget(text = text, onTextChange = { text = it }))
composeTestRule.onNodeWithContentDescription("TextField")
.performTextInput("Tonnie")
composeTestRule.onNodeWithContentDescription("TextField")
.assertTextEquals("Tonnie")
}
}
The Test is failing with this error.
java.lang.IllegalStateException: Functions that involve synchronization (Assertions, Actions, Synchronization; e.g. assertIsSelected(), doClick(), runOnIdle()) cannot be run from the main thread. Did you nest such a function inside runOnIdle {}, runOnUiThread {} or setContent {}? at androidx.compose.ui.test.junit4.EspressoLink.runUntilIdle(EspressoLink.android.kt:73) at androidx.compose.ui.test.AndroidComposeUiTestEnvironment.waitForIdle(ComposeUiTest.android.kt:308) at androidx.compose.ui.test.AndroidComposeUiTestEnvironment.access$waitForIdle(ComposeUiTest.android.kt:217)
I have already wrapped my test function with setContent{} but still the text fails.
Please help me to resolve this.
Assertions cannot be run in main thread. The entire main thread is inside setContent. Put this outside the scope of setContent and it should work: