Jan Skrasek
09/08/2021, 5:33 PMEspresso.onView(ViewMatchers.isRoot()).perform(ViewActions.pressBack())
With Compose I'm trying this:
val keyEvent = NativeKeyEvent(NativeKeyEvent.ACTION_DOWN, NativeKeyEvent.KEYCODE_BACK)
composeTestRule.onRoot().performKeyPress(KeyEvent(keyEvent))
but getting:
KeyEvent can't be processed because this key input node is not active.Ian Lake
09/08/2021, 5:35 PMBackHandler? https://developer.android.com/reference/kotlin/androidx/activity/compose/package-summary#backhandlerIan Lake
09/08/2021, 5:36 PMLocalOnBackPressedDispatcher and your code will use that - then you can just call onBackPressedDispatcher.onBackPressed() from your testIan Lake
09/08/2021, 5:38 PMComponentActivity anyways, you can just retrieve it via LocalOnBackPressedDispatcherOwner.current!!.onBackPressedDispatcher inside your composeTestRule.setContentIan Lake
09/08/2021, 5:39 PMonBackPressed() on thatJan Skrasek
09/08/2021, 5:55 PMJan Skrasek
09/08/2021, 5:56 PMcreateEmptyComposeRule() and just accommodating the way the screen was tested for new composer rewrite.Ian Lake
09/08/2021, 5:58 PMOnBackPressedDispatcherJan Skrasek
09/08/2021, 5:59 PMIan Lake
09/08/2021, 6:03 PMonBackPressedDispatcher directly from the activity (ComponentActivity implements OnBackPressedDispatcherOwner)Jan Skrasek
09/08/2021, 6:40 PMactivity.runOnUiThread {
val dispatcherOwner = (activity as OnBackPressedDispatcherOwner)
val dispatcher = dispatcherOwner.onBackPressedDispatcher
dispatcher.onBackPressed()
}