Sometimes, when testing UIs, it is helpful to simulate different kinds of touch events. These touch events can be generated in many different ways but probably android.test.TouchUtils
is the simplest to use. This class provides reusable methods for generating touch events in test cases that are derived from InstrumentationTestCase
.
Featured methods allow simulated interaction with the UI under test. TouchUtils
provides the infrastructure to inject the events using the correct UI or main thread, so no special handling is needed and you don't need to annotate the test using @UIThreadTest
.
The mentioned methods support:
Clicking on a View and releasing it
Tapping on a View, that is touching it and quickly releasing
Long clicking on a View
Dragging the screen
Dragging Views
The following test represents a typical usage of TouchUtils:
public void testListScrolling() { mListView.scrollTo(0, 0); TouchUtils.dragQuarterScreenUp(this, mActivity); TouchUtils.dragQuarterScreenUp...