27

The ActivityTestRule class takes in an initialTouchMode parameter in its constructor. This is not explained in the class reference (or anywhere online) except as follows:

initialTouchMode - true if the Activity should be placed into "touch mode" when started

What exactly does "touch mode" mean? What are the implications of setting initialTouchMode in ActivityTestRule to true or false? (I see that the default value for this parameter is false).

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147

2 Answers2

15

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

yogurtearl
  • 3,035
  • 18
  • 24
  • 6
    Thanks for the link. That explains what touch mode is for sure. The only question I have now is what are the implications of setting `initialTouchMode` in `ActivityTestRule ` to `true` or `false`? – Adil Hussain Mar 14 '16 at 11:00
  • 1
    From that link: For example, if you are using a G1 phone, selecting a widget with the trackball will take you out of touch mode; however, if you touch a button on the screen with your finger, you will enter touch mode. When the user is not in touch mode, we talk about the trackball mode, navigation mode or keyboard navigation, so do not be surprised if you encounter these terms. – jorgeavilae Apr 16 '17 at 18:48
1

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

Jakaria Rabbi
  • 471
  • 4
  • 16