Loading...
Loading...
Use this skill to mix Compose finders and Espresso `onView` in the same test — for Android `Dialog` windows, IME (soft keyboard) state, `ComposeView` inside an Android View hierarchy, or focus interactions that cross the Compose/View boundary. Covers `createAndroidComposeRule<A>()` setup, the `EspressoLink` bridge that auto-registers Compose's `IdlingResource` with Espresso, the `onRootWithViewInteraction` API, and why `Espresso.onView(...)` MUST run from the test thread (not from `runOnIdle` / `runOnUiThread`). Use when the developer mentions "Espresso onView and Compose at the same time", "ComposeView inside a View", "soft keyboard test", "Dialog focus", "compose interop test", or shows a test that calls Espresso from inside `runOnIdle` and deadlocks.
npx skill4agent add skydoves/android-testing-skills testing-with-espresso-interopComposeTestRuleonViewActivityIdlingResourceEspressoLinkEspresso.onViewrule.runOnIdle { }rule.runOnUiThread { }androidx.compose.foundationDialogWindowonView(supportsInputMethods()).perform(click())ActivityComposeViewEspresso.onView(...)runOnIdle { }../../patterns/structuring-a-compose-test/SKILL.md../../synchronization/synchronizing-with-idle/SKILL.mdandroidx.compose.ui.window.DialogisDialog()../../finders/composing-semantics-matchers/SKILL.mdandroidx.test.espresso:espresso-coreandroidTestImplementationandroidx.compose.ui:ui-test-junit4androidTestImplementationandroidx.compose.ui:ui-test-manifestdebugImplementation../../setup/configuring-test-dependencies/SKILL.mdActivityFragmentActivityActivitysrc/androidTest/AndroidManifest.xml../../patterns/structuring-a-compose-test/SKILL.mdcreateAndroidComposeRule<A>()compose/foundation/foundation/src/androidDeviceTest/kotlin/androidx/compose/foundation/textfield/TextFieldFocusCustomDialogTest.kt:60import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.test.StandardTestDispatcher
@get:Rule
val rule = createAndroidComposeRule<FragmentActivity>(StandardTestDispatcher())rule.setContent { }rule.waitForIdle()Espresso.onView(...)runOnIdlerunOnUiThreadimport androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers
rule.waitForIdle()
Espresso.onView(ViewMatchers.supportsInputMethods()).perform(ViewActions.click())TextFieldFocusCustomDialogTest.kt:117-119@Test
fun keyboardShown_forFieldInAndroidDialog_…() {
val focusRequester = FocusRequester()
val keyboardHelper = KeyboardHelper(rule)
rule.setContent {
wrapContent {
keyboardHelper.initialize()
LaunchedEffect(Unit) { focusRequester.requestFocus() }
BasicTextField(
value = "",
onValueChange = {},
modifier = Modifier.focusRequester(focusRequester),
)
}
}
rule.waitForIdle()
// The dialog's window must be focused for the IME to actually show.
Espresso.onView(ViewMatchers.supportsInputMethods()).perform(ViewActions.click())
keyboardHelper.waitForKeyboardVisibility(visible = true)
}rule.onNodeWithTag("submit").performClick()
Espresso.onView(ViewMatchers.withId(R.id.legacy_toast)).check(matches(ViewMatchers.isDisplayed()))ViewInteractiononRootWithViewInteraction(viewInteraction)AndroidComposeTestRule"This implementation of ComposeTestRule does not support onRootWithViewInteraction."val nestedComposeView = Espresso.onView(ViewMatchers.withId(R.id.nested_compose))
rule.onRootWithViewInteraction(nestedComposeView).onNodeWithTag("submit").performClick()EspressoLinkIdlingResourceComposeIdlingResourceIdlingResource"Compose-Espresso link"compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/EspressoLink.android.kt:34-84withStrategy { }IdlingRegistry.getInstance().register(...)IdlingRegistry.getInstance().register(…)rule.waitForIdle()Espresso.onViewrunOnIdle// WRONG
@Test
fun submit() {
rule.setContent { /* Compose UI with a button that opens a Dialog */ }
rule.onNodeWithTag("open").performClick()
rule.runOnIdle {
Espresso.onView(ViewMatchers.withId(R.id.confirm)).perform(ViewActions.click())
}
}
// WRONG because: runOnIdle posts to the UI thread. Espresso.onView calls Espresso.onIdle()
// internally, and EspressoLink.runUntilIdle (EspressoLink.android.kt:75-83) explicitly
// throws on UI-thread invocations:
// "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 {}?"// RIGHT
@Test
fun submit() {
rule.setContent { /* Compose UI with a button that opens a Dialog */ }
rule.onNodeWithTag("open").performClick()
rule.waitForIdle()
Espresso.onView(ViewMatchers.withId(R.id.confirm)).perform(ViewActions.click())
}// WRONG — assuming Compose's IME helper alone is enough
@Test
fun dialogKeyboard() {
rule.setContent { CustomDialog { BasicTextField(/* … */) } }
keyboardHelper.waitForKeyboardVisibility(visible = true) // never visible
}
// WRONG because: Android Dialogs live in a separate Window. Even if the TextField requests
// focus, the soft keyboard does not appear until the dialog window itself takes input
// focus. Only Espresso can drive that — `onView(supportsInputMethods()).perform(click())`.// RIGHT
@Test
fun dialogKeyboard() {
rule.setContent { CustomDialog { BasicTextField(/* … */) } }
rule.waitForIdle()
Espresso.onView(ViewMatchers.supportsInputMethods()).perform(ViewActions.click())
keyboardHelper.waitForKeyboardVisibility(visible = true)
}createComposeRule()// WRONG
@get:Rule val rule = createComposeRule(StandardTestDispatcher()) // ComponentActivity host
@Test fun customActivityFlow() {
Espresso.onView(ViewMatchers.withId(R.id.my_activity_view)) // does not exist
.perform(ViewActions.click())
}
// WRONG because: createComposeRule launches the empty ComponentActivity from ui-test-manifest.
// To interact with a custom Activity's view hierarchy, launch that Activity via
// createAndroidComposeRule<MyActivity>().// RIGHT
@get:Rule val rule = createAndroidComposeRule<MyActivity>(StandardTestDispatcher())Espresso.onView(...)rule.waitForIdle()rule.runOnIdle { }rule.runOnUiThread { }EspressoLink.android.kt:75-83createAndroidComposeRule<A>(StandardTestDispatcher())createComposeRule()ComponentActivityui-test-manifestIdlingResourceIdlingRegistry.getInstance()EspressoLink"Compose-Espresso link"EspressoLink.android.kt:34-84rule.onRootWithViewInteraction(viewInteraction)ComposeViewAndroidComposeTestRuleIllegalStateExceptionerror("This implementation of ComposeTestRule does not support onRootWithViewInteraction.")ComposeTestRuleExt.android.kt:38-42rule.setContent { … }rule.waitForIdle()Espresso.onView(supportsInputMethods()).perform(click())keyboardHelper.waitForKeyboardVisibility(visible = true)TextFieldFocusCustomDialogTest.kt:117-128androidx.compose.ui.window.DialogisDialog()../../finders/composing-semantics-matchers/SKILL.mdcreateAndroidComposeRule<HostActivity>(StandardTestDispatcher())createComposeRule()Espresso.onView(...)runOnIdle { }runOnUiThread { }setContent { }rule.waitForIdle()Espresso.onView(...)IdlingRegistry.getInstance().register(composeIdlingResource)EspressoLinkComposeViewrule.onRootWithViewInteraction(...)Thread.sleeprule.waitForIdle()../../synchronization/synchronizing-with-idle/SKILL.mdcompose/foundation/foundation/src/androidDeviceTest/kotlin/androidx/compose/foundation/textfield/TextFieldFocusCustomDialogTest.kt:57-128EspressoLinkcompose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/EspressoLink.android.kt:34-84onRootWithViewInteractioncompose/ui/ui-test-junit4/src/androidMain/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule.android.ktcreateAndroidComposeRulecompose/ui/ui-test-junit4/src/androidMain/kotlin/androidx/compose/ui/test/junit4/v2/AndroidComposeTestRule.android.ktcompose/foundation/foundation/src/androidDeviceTest/kotlin/androidx/compose/foundation/text/KeyboardHelper.kt