Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 810
Adil Hussain Avatar asked Mar 13 '16 12:03

Adil Hussain


People also ask

What is touch mode android?

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.

What is the significance of Activityrule in instrumented testing?

This rule provides functional testing of a single Activity .

What is rule in espresso?

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.


2 Answers

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

like image 104
yogurtearl Avatar answered Oct 19 '22 05:10

yogurtearl


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

like image 1
Jakaria Rabbi Avatar answered Oct 19 '22 03:10

Jakaria Rabbi