Added in API level 1
Deprecated in API level 24

TouchUtils

public class TouchUtils
extends Object

java.lang.Object
   ↳ android.test.TouchUtils


This class was deprecated in API level 24.
Use Espresso UI testing framework instead. New tests should be written using the Android Testing Support Library.

Reusable methods for generating touch events. These methods can be used with InstrumentationTestCase or ActivityInstrumentationTestCase2 to simulate user interaction with the application through a touch screen.

Summary

Public constructors

TouchUtils()

Public methods

static void clickView(InstrumentationTestCase test, View v)

Simulate touching the center of a view and releasing.

static void drag(InstrumentationTestCase test, float fromX, float toX, float fromY, float toY, int stepCount)

Simulate touching a specific location and dragging to a new location.

static void drag(ActivityInstrumentationTestCase<T extends Activity> test, float fromX, float toX, float fromY, float toY, int stepCount)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void dragQuarterScreenDown(InstrumentationTestCase test, Activity activity)

Simulate touching in the center of the screen and dragging one quarter of the way down

static void dragQuarterScreenDown(ActivityInstrumentationTestCase<T extends Activity> test)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void dragQuarterScreenUp(ActivityInstrumentationTestCase<T extends Activity> test)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void dragQuarterScreenUp(InstrumentationTestCase test, Activity activity)

Simulate touching in the center of the screen and dragging one quarter of the way up

static int dragViewBy(InstrumentationTestCase test, View v, int gravity, int deltaX, int deltaY)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static int dragViewBy(ActivityInstrumentationTestCase<T extends Activity> test, View v, int gravity, int deltaX, int deltaY)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static int dragViewTo(ActivityInstrumentationTestCase<T extends Activity> test, View v, int gravity, int toX, int toY)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static int dragViewTo(InstrumentationTestCase test, View v, int gravity, int toX, int toY)

Simulate touching a view and dragging it to a specified location.

static void dragViewToBottom(ActivityInstrumentationTestCase<T extends Activity> test, View v)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v, int stepCount)

Simulate touching the center of a view and dragging to the bottom of the screen.

static void dragViewToBottom(ActivityInstrumentationTestCase<T extends Activity> test, View v, int stepCount)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v)

Simulate touching the center of a view and dragging to the bottom of the screen.

static void dragViewToTop(InstrumentationTestCase test, View v)

Simulate touching the center of a view and dragging to the top of the screen.

static void dragViewToTop(ActivityInstrumentationTestCase<T extends Activity> test, View v)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void dragViewToTop(InstrumentationTestCase test, View v, int stepCount)

Simulate touching the center of a view and dragging to the top of the screen.

static void dragViewToTop(ActivityInstrumentationTestCase<T extends Activity> test, View v, int stepCount)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static int dragViewToX(InstrumentationTestCase test, View v, int gravity, int toX)

Simulate touching a view and dragging it to a specified location.

static int dragViewToX(ActivityInstrumentationTestCase<T extends Activity> test, View v, int gravity, int toX)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static int dragViewToY(ActivityInstrumentationTestCase<T extends Activity> test, View v, int gravity, int toY)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static int dragViewToY(InstrumentationTestCase test, View v, int gravity, int toY)

Simulate touching a view and dragging it to a specified location.

static void longClickView(ActivityInstrumentationTestCase<T extends Activity> test, View v)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void longClickView(InstrumentationTestCase test, View v)

Simulate touching the center of a view, holding until it is a long press, and then releasing.

static void scrollToBottom(ActivityInstrumentationTestCase<T extends Activity> test, ViewGroup v)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void scrollToBottom(InstrumentationTestCase test, Activity activity, ViewGroup v)

Scroll a ViewGroup to the bottom by repeatedly calling dragQuarterScreenUp(android.test.InstrumentationTestCase, android.app.Activity)

static void scrollToTop(ActivityInstrumentationTestCase<T extends Activity> test, ViewGroup v)

This method was deprecated in API level 15. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

static void scrollToTop(InstrumentationTestCase test, Activity activity, ViewGroup v)

Scroll a ViewGroup to the top by repeatedly calling dragQuarterScreenDown(android.test.InstrumentationTestCase, android.app.Activity)

static void tapView(InstrumentationTestCase test, View v)

Simulate touching the center of a view and releasing quickly (before the tap timeout).

static void touchAndCancelView(InstrumentationTestCase test, View v)

Simulate touching the center of a view and cancelling (so no onClick should fire, etc).

Inherited methods

Public constructors

TouchUtils

public TouchUtils ()

Public methods

clickView

Added in API level 1
public static void clickView (InstrumentationTestCase test, 
                View v)

Simulate touching the center of a view and releasing.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be clicked

drag

Added in API level 3
Deprecated in API level 24
public static void drag (InstrumentationTestCase test, 
                float fromX, 
                float toX, 
                float fromY, 
                float toY, 
                int stepCount)

Simulate touching a specific location and dragging to a new location.

Parameters
test InstrumentationTestCase: The test case that is being run

fromX float: X coordinate of the initial touch, in screen coordinates

toX float: Xcoordinate of the drag destination, in screen coordinates

fromY float: X coordinate of the initial touch, in screen coordinates

toY float: Y coordinate of the drag destination, in screen coordinates

stepCount int: How many move steps to include in the drag

drag

Added in API level 1
Deprecated in API level 15
public static void drag (ActivityInstrumentationTestCase<T extends Activity> test, 
                float fromX, 
                float toX, 
                float fromY, 
                float toY, 
                int stepCount)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a specific location and dragging to a new location.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

fromX float: X coordinate of the initial touch, in screen coordinates

toX float: Xcoordinate of the drag destination, in screen coordinates

fromY float: X coordinate of the initial touch, in screen coordinates

toY float: Y coordinate of the drag destination, in screen coordinates

stepCount int: How many move steps to include in the drag

dragQuarterScreenDown

Added in API level 3
Deprecated in API level 24
public static void dragQuarterScreenDown (InstrumentationTestCase test, 
                Activity activity)

Simulate touching in the center of the screen and dragging one quarter of the way down

Parameters
test InstrumentationTestCase: The test case that is being run

activity Activity: The activity that is in the foreground of the test case

dragQuarterScreenDown

Added in API level 1
Deprecated in API level 15
public static void dragQuarterScreenDown (ActivityInstrumentationTestCase<T extends Activity> test)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching in the center of the screen and dragging one quarter of the way down

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

dragQuarterScreenUp

Added in API level 1
Deprecated in API level 15
public static void dragQuarterScreenUp (ActivityInstrumentationTestCase<T extends Activity> test)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching in the center of the screen and dragging one quarter of the way up

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

dragQuarterScreenUp

Added in API level 3
Deprecated in API level 24
public static void dragQuarterScreenUp (InstrumentationTestCase test, 
                Activity activity)

Simulate touching in the center of the screen and dragging one quarter of the way up

Parameters
test InstrumentationTestCase: The test case that is being run

activity Activity: The activity that is in the foreground of the test case

dragViewBy

Added in API level 3
Deprecated in API level 15
public static int dragViewBy (InstrumentationTestCase test, 
                View v, 
                int gravity, 
                int deltaX, 
                int deltaY)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it by the specified amount.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

deltaX int: Amount to drag horizontally in pixels

deltaY int: Amount to drag vertically in pixels

Returns
int distance in pixels covered by the drag

dragViewBy

Added in API level 1
Deprecated in API level 15
public static int dragViewBy (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v, 
                int gravity, 
                int deltaX, 
                int deltaY)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it by the specified amount.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

deltaX int: Amount to drag horizontally in pixels

deltaY int: Amount to drag vertically in pixels

Returns
int distance in pixels covered by the drag

dragViewTo

Added in API level 1
Deprecated in API level 15
public static int dragViewTo (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v, 
                int gravity, 
                int toX, 
                int toY)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it to a specified location.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

toX int: Final location of the view after dragging

toY int: Final location of the view after dragging

Returns
int distance in pixels covered by the drag

dragViewTo

Added in API level 3
Deprecated in API level 24
public static int dragViewTo (InstrumentationTestCase test, 
                View v, 
                int gravity, 
                int toX, 
                int toY)

Simulate touching a view and dragging it to a specified location.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

toX int: Final location of the view after dragging

toY int: Final location of the view after dragging

Returns
int distance in pixels covered by the drag

dragViewToBottom

Added in API level 1
Deprecated in API level 15
public static void dragViewToBottom (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

dragViewToBottom

Added in API level 3
Deprecated in API level 24
public static void dragViewToBottom (InstrumentationTestCase test, 
                Activity activity, 
                View v, 
                int stepCount)

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test InstrumentationTestCase: The test case that is being run

activity Activity: The activity that is in the foreground of the test case

v View: The view that should be dragged

stepCount int: How many move steps to include in the drag

dragViewToBottom

Added in API level 1
Deprecated in API level 15
public static void dragViewToBottom (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v, 
                int stepCount)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

stepCount int: How many move steps to include in the drag

dragViewToBottom

Added in API level 3
Deprecated in API level 24
public static void dragViewToBottom (InstrumentationTestCase test, 
                Activity activity, 
                View v)

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test InstrumentationTestCase: The test case that is being run

activity Activity: The activity that is in the foreground of the test case

v View: The view that should be dragged

dragViewToTop

Added in API level 3
Deprecated in API level 24
public static void dragViewToTop (InstrumentationTestCase test, 
                View v)

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

dragViewToTop

Added in API level 1
Deprecated in API level 15
public static void dragViewToTop (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

dragViewToTop

Added in API level 3
Deprecated in API level 24
public static void dragViewToTop (InstrumentationTestCase test, 
                View v, 
                int stepCount)

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

stepCount int: How many move steps to include in the drag

dragViewToTop

Added in API level 1
Deprecated in API level 15
public static void dragViewToTop (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v, 
                int stepCount)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

stepCount int: How many move steps to include in the drag

dragViewToX

Added in API level 3
Deprecated in API level 24
public static int dragViewToX (InstrumentationTestCase test, 
                View v, 
                int gravity, 
                int toX)

Simulate touching a view and dragging it to a specified location. Only moves horizontally.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

toX int: Final location of the view after dragging

Returns
int distance in pixels covered by the drag

dragViewToX

Added in API level 1
Deprecated in API level 15
public static int dragViewToX (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v, 
                int gravity, 
                int toX)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it to a specified location. Only moves horizontally.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

toX int: Final location of the view after dragging

Returns
int distance in pixels covered by the drag

dragViewToY

Added in API level 1
Deprecated in API level 15
public static int dragViewToY (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v, 
                int gravity, 
                int toY)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it to a specified location. Only moves vertically.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

toY int: Final location of the view after dragging

Returns
int distance in pixels covered by the drag

dragViewToY

Added in API level 3
Deprecated in API level 24
public static int dragViewToY (InstrumentationTestCase test, 
                View v, 
                int gravity, 
                int toY)

Simulate touching a view and dragging it to a specified location. Only moves vertically.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be dragged

gravity int: Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)

toY int: Final location of the view after dragging

Returns
int distance in pixels covered by the drag

longClickView

Added in API level 1
Deprecated in API level 15
public static void longClickView (ActivityInstrumentationTestCase<T extends Activity> test, 
                View v)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view, holding until it is a long press, and then releasing.

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v View: The view that should be clicked

longClickView

Added in API level 3
Deprecated in API level 24
public static void longClickView (InstrumentationTestCase test, 
                View v)

Simulate touching the center of a view, holding until it is a long press, and then releasing.

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be clicked

scrollToBottom

Added in API level 1
Deprecated in API level 15
public static void scrollToBottom (ActivityInstrumentationTestCase<T extends Activity> test, 
                ViewGroup v)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Scroll a ViewGroup to the bottom by repeatedly calling dragQuarterScreenUp(android.test.InstrumentationTestCase, android.app.Activity)

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v ViewGroup: The ViewGroup that should be dragged

scrollToBottom

Added in API level 3
Deprecated in API level 24
public static void scrollToBottom (InstrumentationTestCase test, 
                Activity activity, 
                ViewGroup v)

Scroll a ViewGroup to the bottom by repeatedly calling dragQuarterScreenUp(android.test.InstrumentationTestCase, android.app.Activity)

Parameters
test InstrumentationTestCase: The test case that is being run

activity Activity: The activity that is in the foreground of the test case

v ViewGroup: The ViewGroup that should be dragged

scrollToTop

Added in API level 1
Deprecated in API level 15
public static void scrollToTop (ActivityInstrumentationTestCase<T extends Activity> test, 
                ViewGroup v)

This method was deprecated in API level 15.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Scroll a ViewGroup to the top by repeatedly calling dragQuarterScreenDown(android.test.InstrumentationTestCase, android.app.Activity)

Parameters
test ActivityInstrumentationTestCase: The test case that is being run

v ViewGroup: The ViewGroup that should be dragged

scrollToTop

Added in API level 3
Deprecated in API level 24
public static void scrollToTop (InstrumentationTestCase test, 
                Activity activity, 
                ViewGroup v)

Scroll a ViewGroup to the top by repeatedly calling dragQuarterScreenDown(android.test.InstrumentationTestCase, android.app.Activity)

Parameters
test InstrumentationTestCase: The test case that is being run

activity Activity: The activity that is in the foreground of the test case

v ViewGroup: The ViewGroup that should be dragged

tapView

Added in API level 1
public static void tapView (InstrumentationTestCase test, 
                View v)

Simulate touching the center of a view and releasing quickly (before the tap timeout).

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be clicked

touchAndCancelView

Added in API level 1
public static void touchAndCancelView (InstrumentationTestCase test, 
                View v)

Simulate touching the center of a view and cancelling (so no onClick should fire, etc).

Parameters
test InstrumentationTestCase: The test case that is being run

v View: The view that should be clicked