What is touch mode and why is it important for the *ActivityTestRule* class?

Touch mode affect how view focus and selection work.

The touch mode is a state of the view hierarchy that depends solely on the user interaction with the phone. By itself, the touch mode is something very easy to understand as it simply indicates whether the last user interaction was performed with the touch screen.

...

In touch mode, there is no focus and no selection.

http://android-developers.blogspot.com/2008/12/touch-mode.html


I think the following explanation from a medium article is excellent to understand touch mode in ActivityTestRule.

'In touch mode, there is no focus and no selection.'

In other words, when your finger touches the screen it will not produce side effects. E.g., views will not keep the focus. It will not make sense until you recall the behavior of Android OS on non-touchable platforms. The best modern example that does not operate in ‘touch mode’ is Android TV. With D-Pad control, we are capable of selecting or focusing a view, and as soon as the view is focused, we can perform the click.

Be careful with RecyclerView and touch mode! Assume we want to perform click action on the view inside a RecyclerView.

onView(withId(R.id.recyclerView))
  .perform(RecyclerViewActions.actionOnItem(
            hasDescendant(withId(R.id.someAction)), click()))

The code is straightforward unless you will make a mistake and launch Activity under test with a disabled touch mode.

val initialTouchMode = false
val launchActivity = true
@JvmField @Rule var activityRule = ActivityTestRule(
    MainActivity::class.java, initialTouchMode, launchActivity
)

What you will end up with is that your underlying click listener, will not be fired and you need to hack and repeat the click!

onView(withId(R.id.recyclerView))
  .perform(RecyclerViewActions.actionOnItem(
            hasDescendant(withId(R.id.someAction)), click()))
  .perform(RecyclerViewActions.actionOnItem(
            hasDescendant(withId(R.id.someAction)), click()))

The answer to this mystery is the fact that RecyclerView inflated via XML will have setFocusableInTouchMode(true) during a construction phase. Our whole page is launched in non-touch mode and interprets the most first click as a focus event and all other clicks as you would expect in touch mode. The fix is as simple as launching activity with enabled touch mode.

val initialTouchMode = true
val launchActivity = true
@JvmField @Rule var activityRule = ActivityTestRule(
    MainActivity::class.java, initialTouchMode, launchActivity
)

The explantion can be found in this link: https://medium.com/@tom.koptel/espresso-initialtouchmode-can-shoot-you-in-the-leg-85c5f922754