The android.support.test.rule.ActivityTestRule
class (see here) 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? And what are the implications of setting initialTouchMode
in ActivityTestRule
to true
or false
? (I see that the default value for this parameter is false
).
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.
This rule provides functional testing of a single Activity .
STICK TO THE RULES -- water should be brought to 90-95 degrees Celsius (194-203 degrees Fahrenheit), not to a boiling point. -- freshly ground coffee should be used within one day; -- time of brewing a standard 30 ml (1.01 ounce) cup of espresso should be 25 to 30 seconds.
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 theview
inside aRecyclerView
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With