# Print output for @column tags ?>
public
final
class
UiAutomation
extends Object
java.lang.Object | |
↳ | android.app.UiAutomation |
Class for interacting with the device's UI by simulation user actions and
introspection of the screen content. It relies on the platform accessibility
APIs to introspect the screen and to perform some actions on the remote view
tree. It also allows injecting of arbitrary raw input events simulating user
interaction with keyboards and touch devices. One can think of a UiAutomation
as a special type of AccessibilityService
which does not provide hooks for the service life cycle and exposes other
APIs that are useful for UI test automation.
The APIs exposed by this class are low-level to maximize flexibility when
developing UI test automation tools and libraries. Generally, a UiAutomation
client should be using a higher-level library or implement high-level functions.
For example, performing a tap on the screen requires construction and injecting
of a touch down and up events which have to be delivered to the system by a
call to injectInputEvent(android.view.InputEvent, boolean)
.
The APIs exposed by this class operate across applications enabling a client to write tests that cover use cases spanning over multiple applications. For example, going to the settings application to change a setting and then interacting with another application whose behavior depends on that setting.
Nested classes | |
---|---|
interface |
UiAutomation.AccessibilityEventFilter
Listener for filtering accessibility events. |
interface |
UiAutomation.OnAccessibilityEventListener
Listener for observing the |
Constants | |
---|---|
int |
FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES
UiAutomation supresses accessibility services by default. |
int |
ROTATION_FREEZE_0
Rotation constant: Freeze rotation to 0 degrees (natural orientation) |
int |
ROTATION_FREEZE_180
Rotation constant: Freeze rotation to 180 degrees . |
int |
ROTATION_FREEZE_270
Rotation constant: Freeze rotation to 270 degrees . |
int |
ROTATION_FREEZE_90
Rotation constant: Freeze rotation to 90 degrees . |
int |
ROTATION_FREEZE_CURRENT
Rotation constant: Freeze rotation to its current state. |
int |
ROTATION_UNFREEZE
Rotation constant: Unfreeze rotation (rotating the device changes its rotation state). |
Public methods | |
---|---|
void
|
adoptShellPermissionIdentity()
Adopt the permission identity of the shell UID for all permissions. |
void
|
adoptShellPermissionIdentity(String... permissions)
Adopt the permission identity of the shell UID only for the provided permissions. |
void
|
clearWindowAnimationFrameStats()
Clears the window animation rendering statistics. |
boolean
|
clearWindowContentFrameStats(int windowId)
Clears the frame statistics for the content of a given window. |
void
|
dropShellPermissionIdentity()
Drop the shell permission identity adopted by a previous call to
|
AccessibilityEvent
|
executeAndWaitForEvent(Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeoutMillis)
Executes a command and waits for a specific accessibility event up to a given wait timeout. |
ParcelFileDescriptor
|
executeShellCommand(String command)
Executes a shell command. |
AccessibilityNodeInfo
|
findFocus(int focus)
Find the view that has the specified focus type. |
AccessibilityNodeInfo
|
getRootInActiveWindow()
Gets the root |
AccessibilityServiceInfo
|
getServiceInfo()
Gets the an |
WindowAnimationFrameStats
|
getWindowAnimationFrameStats()
Gets the window animation frame statistics. |
WindowContentFrameStats
|
getWindowContentFrameStats(int windowId)
Gets the frame statistics for a given window. |
List<AccessibilityWindowInfo>
|
getWindows()
Gets the windows on the screen of the default display. |
SparseArray<List<AccessibilityWindowInfo>>
|
getWindowsOnAllDisplays()
Gets the windows on the screen of all displays. |
void
|
grantRuntimePermission(String packageName, String permission)
Grants a runtime permission to a package. |
void
|
grantRuntimePermissionAsUser(String packageName, String permission, UserHandle userHandle)
Grants a runtime permission to a package for a user. |
boolean
|
injectInputEvent(InputEvent event, boolean sync)
A method for injecting an arbitrary input event. |
boolean
|
performGlobalAction(int action)
Performs a global action. |
void
|
revokeRuntimePermission(String packageName, String permission)
Revokes a runtime permission from a package. |
void
|
revokeRuntimePermissionAsUser(String packageName, String permission, UserHandle userHandle)
Revokes a runtime permission from a package. |
void
|
setOnAccessibilityEventListener(UiAutomation.OnAccessibilityEventListener listener)
Sets a callback for observing the stream of |
boolean
|
setRotation(int rotation)
Sets the device rotation. |
void
|
setRunAsMonkey(boolean enable)
Sets whether this UiAutomation to run in a "monkey" mode. |
void
|
setServiceInfo(AccessibilityServiceInfo info)
Sets the |
Bitmap
|
takeScreenshot()
Takes a screenshot. |
String
|
toString()
Returns a string representation of the object. |
void
|
waitForIdle(long idleTimeoutMillis, long globalTimeoutMillis)
Waits for the accessibility event stream to become idle, which is not to
have received an accessibility event within |
Inherited methods | |
---|---|
public static final int FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES
UiAutomation supresses accessibility services by default. This flag specifies that
existing accessibility services should continue to run, and that new ones may start.
This flag is set when obtaining the UiAutomation from
Instrumentation#getUiAutomation(int)
.
Constant Value: 1 (0x00000001)
public static final int ROTATION_FREEZE_0
Rotation constant: Freeze rotation to 0 degrees (natural orientation)
Constant Value: 0 (0x00000000)
public static final int ROTATION_FREEZE_180
Rotation constant: Freeze rotation to 180 degrees .
Constant Value: 2 (0x00000002)
public static final int ROTATION_FREEZE_270
Rotation constant: Freeze rotation to 270 degrees .
Constant Value: 3 (0x00000003)
public static final int ROTATION_FREEZE_90
Rotation constant: Freeze rotation to 90 degrees .
Constant Value: 1 (0x00000001)
public static final int ROTATION_FREEZE_CURRENT
Rotation constant: Freeze rotation to its current state.
Constant Value: -1 (0xffffffff)
public static final int ROTATION_UNFREEZE
Rotation constant: Unfreeze rotation (rotating the device changes its rotation state).
Constant Value: -2 (0xfffffffe)
public void adoptShellPermissionIdentity ()
Adopt the permission identity of the shell UID for all permissions. This allows
you to call APIs protected permissions which normal apps cannot hold but are
granted to the shell UID. If you already adopted all shell permissions by calling
this method or adoptShellPermissionIdentity(java.lang.String)
a subsequent call
would be a no-op. Note that your permission state becomes that of the shell UID
and it is not a combination of your and the shell UID permissions.
Note: Calling this method adopts all shell permissions and overrides
any subset of adopted permissions via adoptShellPermissionIdentity(java.lang.String)
.
public void adoptShellPermissionIdentity (String... permissions)
Adopt the permission identity of the shell UID only for the provided permissions.
This allows you to call APIs protected permissions which normal apps cannot hold
but are granted to the shell UID. If you already adopted the specified shell
permissions by calling this method or adoptShellPermissionIdentity()
a
subsequent call would be a no-op. Note that your permission state becomes that of the
shell UID and it is not a combination of your and the shell UID permissions.
Note: Calling this method adopts only the specified shell permissions
and overrides all adopted permissions via adoptShellPermissionIdentity()
.
Parameters | |
---|---|
permissions |
String : The permissions to adopt or null to adopt all.
This value may be null . |
public void clearWindowAnimationFrameStats ()
Clears the window animation rendering statistics. These statistics contain information about the most recently rendered window animation frames, i.e. for window transition animations.
public boolean clearWindowContentFrameStats (int windowId)
Clears the frame statistics for the content of a given window. These statistics contain information about the most recently rendered content frames.
Parameters | |
---|---|
windowId |
int : The window id. |
Returns | |
---|---|
boolean |
Whether the window is present and its frame statistics were cleared. |
public void dropShellPermissionIdentity ()
Drop the shell permission identity adopted by a previous call to
adoptShellPermissionIdentity()
. If you did not adopt the shell permission
identity this method would be a no-op.
See also:
public AccessibilityEvent executeAndWaitForEvent (Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeoutMillis)
Executes a command and waits for a specific accessibility event up to a given wait timeout. To detect a sequence of events one can implement a filter that keeps track of seen events of the expected sequence and returns true after the last event of that sequence is received.
Note: It is caller's responsibility to recycle the returned event.
Parameters | |
---|---|
command |
Runnable : The command to execute. |
filter |
UiAutomation.AccessibilityEventFilter : Filter that recognizes the expected event. |
timeoutMillis |
long : The wait timeout in milliseconds. |
Returns | |
---|---|
AccessibilityEvent |
Throws | |
---|---|
TimeoutException |
If the expected event is not received within the timeout. |
public ParcelFileDescriptor executeShellCommand (String command)
Executes a shell command. This method returns a file descriptor that points
to the standard output stream. The command execution is similar to running
"adb shell
Note: It is your responsibility to close the returned file
descriptor once you are done reading.
Parameters | |
---|---|
command |
String : The command to execute. |
Returns | |
---|---|
ParcelFileDescriptor |
A file descriptor to the standard output stream. |
See also:
public AccessibilityNodeInfo findFocus (int focus)
Find the view that has the specified focus type. The search is performed across all windows.
Note: In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS
flag.
Otherwise, the search will be performed only in the active window.
Parameters | |
---|---|
focus |
int : The focus to find. One of AccessibilityNodeInfo#FOCUS_INPUT or
AccessibilityNodeInfo#FOCUS_ACCESSIBILITY . |
Returns | |
---|---|
AccessibilityNodeInfo |
The node info of the focused view or null. |
public AccessibilityNodeInfo getRootInActiveWindow ()
Gets the root AccessibilityNodeInfo
in the active window.
Returns | |
---|---|
AccessibilityNodeInfo |
The root info. |
public AccessibilityServiceInfo getServiceInfo ()
Gets the an AccessibilityServiceInfo
describing this UiAutomation.
This method is useful if one wants to change some of the dynamically
configurable properties at runtime.
Returns | |
---|---|
AccessibilityServiceInfo |
The accessibility service info. |
See also:
public WindowAnimationFrameStats getWindowAnimationFrameStats ()
Gets the window animation frame statistics. These statistics contain information about the most recently rendered window animation frames, i.e. for window transition animations.
A typical usage requires clearing the window animation frame statistics via
clearWindowAnimationFrameStats()
followed by an interaction that causes
a window transition which uses a window animation and finally getting the window
animation frame statistics by calling this method.
// Start with a clean slate. uiAutimation.clearWindowAnimationFrameStats(); // Do stuff to trigger a window transition. // Get the frame statistics. WindowAnimationFrameStats stats = uiAutomation.getWindowAnimationFrameStats();
Returns | |
---|---|
WindowAnimationFrameStats |
The window animation frame statistics. |
public WindowContentFrameStats getWindowContentFrameStats (int windowId)
Gets the frame statistics for a given window. These statistics contain information about the most recently rendered content frames.
A typical usage requires clearing the window frame statistics via clearWindowContentFrameStats(int)
followed by an interaction with the UI and
finally getting the window frame statistics via calling this method.
// Assume we have at least one window. final int windowId = getWindows().get(0).getId(); // Start with a clean slate. uiAutimation.clearWindowContentFrameStats(windowId); // Do stuff with the UI. // Get the frame statistics. WindowContentFrameStats stats = uiAutomation.getWindowContentFrameStats(windowId);
Parameters | |
---|---|
windowId |
int : The window id. |
Returns | |
---|---|
WindowContentFrameStats |
The window frame statistics, or null if the window is not present. |
public List<AccessibilityWindowInfo> getWindows ()
Gets the windows on the screen of the default display. This method returns only the windows that a sighted user can interact with, as opposed to all windows. For example, if there is a modal dialog shown and the user cannot touch anything behind it, then only the modal window will be reported (assuming it is the top one). For convenience the returned windows are ordered in a descending layer order, which is the windows that are higher in the Z-order are reported first.
Note: In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS
flag.
Returns | |
---|---|
List<AccessibilityWindowInfo> |
The windows if there are windows such, otherwise an empty list. |
public SparseArray<List<AccessibilityWindowInfo>> getWindowsOnAllDisplays ()
Gets the windows on the screen of all displays. This method returns only the windows that a sighted user can interact with, as opposed to all windows. For example, if there is a modal dialog shown and the user cannot touch anything behind it, then only the modal window will be reported (assuming it is the top one). For convenience the returned windows are ordered in a descending layer order, which is the windows that are higher in the Z-order are reported first.
Note: In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS
flag.
Returns | |
---|---|
SparseArray<List<AccessibilityWindowInfo>> |
The windows of all displays if there are windows and the service is can retrieve
them, otherwise an empty list. The key of SparseArray is display ID.
This value cannot be null . |
public void grantRuntimePermission (String packageName, String permission)
Grants a runtime permission to a package.
Parameters | |
---|---|
packageName |
String : The package to which to grant. |
permission |
String : The permission to grant. |
Throws | |
---|---|
SecurityException |
if unable to grant the permission. |
public void grantRuntimePermissionAsUser (String packageName, String permission, UserHandle userHandle)
Grants a runtime permission to a package for a user.
Parameters | |
---|---|
packageName |
String : The package to which to grant. |
permission |
String : The permission to grant. |
userHandle |
UserHandle |
Throws | |
---|---|
SecurityException |
if unable to grant the permission. |
public boolean injectInputEvent (InputEvent event, boolean sync)
A method for injecting an arbitrary input event.
Note: It is caller's responsibility to recycle the event.
Parameters | |
---|---|
event |
InputEvent : The event to inject. |
sync |
boolean : Whether to inject the event synchronously. |
Returns | |
---|---|
boolean |
Whether event injection succeeded. |
public boolean performGlobalAction (int action)
Performs a global action. Such an action can be performed at any moment regardless of the current application or user location in that application. For example going back, going home, opening recents, etc.
Parameters | |
---|---|
action |
int : The action to perform. |
Returns | |
---|---|
boolean |
Whether the action was successfully performed. |
public void revokeRuntimePermission (String packageName, String permission)
Revokes a runtime permission from a package.
Parameters | |
---|---|
packageName |
String : The package to which to grant. |
permission |
String : The permission to grant. |
Throws | |
---|---|
SecurityException |
if unable to revoke the permission. |
public void revokeRuntimePermissionAsUser (String packageName, String permission, UserHandle userHandle)
Revokes a runtime permission from a package.
Parameters | |
---|---|
packageName |
String : The package to which to grant. |
permission |
String : The permission to grant. |
userHandle |
UserHandle |
Throws | |
---|---|
SecurityException |
if unable to revoke the permission. |
public void setOnAccessibilityEventListener (UiAutomation.OnAccessibilityEventListener listener)
Sets a callback for observing the stream of AccessibilityEvent
s.
The callbacks are delivered on the main application thread.
Parameters | |
---|---|
listener |
UiAutomation.OnAccessibilityEventListener : The callback. |
public boolean setRotation (int rotation)
Sets the device rotation. A client can freeze the rotation in desired state or freeze the rotation to its current state or unfreeze the rotation (rotating the device changes its rotation state).
Parameters | |
---|---|
rotation |
int : The desired rotation. |
Returns | |
---|---|
boolean |
Whether the rotation was set successfully. |
public void setRunAsMonkey (boolean enable)
Sets whether this UiAutomation to run in a "monkey" mode. Applications can query whether they are executed in a "monkey" mode, i.e. run by a test framework, and avoid doing potentially undesirable actions such as calling 911 or posting on public forums etc.
Parameters | |
---|---|
enable |
boolean : whether to run in a "monkey" mode or not. Default is not. |
See also:
public void setServiceInfo (AccessibilityServiceInfo info)
Sets the AccessibilityServiceInfo
that describes how this
UiAutomation will be handled by the platform accessibility layer.
Parameters | |
---|---|
info |
AccessibilityServiceInfo : The info. |
See also:
public Bitmap takeScreenshot ()
Takes a screenshot.
Returns | |
---|---|
Bitmap |
The screenshot bitmap on success, null otherwise. |
public String toString ()
Returns a string representation of the object. In general, the
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns | |
---|---|
String |
a string representation of the object. |
public void waitForIdle (long idleTimeoutMillis, long globalTimeoutMillis)
Waits for the accessibility event stream to become idle, which is not to
have received an accessibility event within idleTimeoutMillis
.
The total time spent to wait for an idle accessibility event stream is bounded
by the globalTimeoutMillis
.
Parameters | |
---|---|
idleTimeoutMillis |
long : The timeout in milliseconds between two events
to consider the device idle. |
globalTimeoutMillis |
long : The maximal global timeout in milliseconds in
which to wait for an idle state. |
Throws | |
---|---|
TimeoutException |
If no idle state was detected within
globalTimeoutMillis. |