Class Overview
UiDevice provides access to state information about the device. You can also use this class to simulate user actions on the device, such as pressing the d-pad or pressing the Home and Menu buttons.
UiDevice provides access to state information about the device. You can also use this class to simulate user actions on the device, such as pressing the d-pad or pressing the Home and Menu buttons.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
clearLastTraversedText()
Clears the text from the last UI traversal event.
| ||||||||||
boolean |
click(int x, int y)
Perform a click at arbitrary coordinates specified by the user
| ||||||||||
void |
dumpWindowHierarchy(String fileName)
Helper method used for debugging to dump the current window's layout hierarchy.
| ||||||||||
void |
freezeRotation()
Disables the sensors and freezes the device rotation at its
current rotation state.
| ||||||||||
String |
getCurrentActivityName()
This method is deprecated.
The results returned should be considered unreliable
| ||||||||||
String |
getCurrentPackageName()
Retrieves the name of the last package to report accessibility events.
| ||||||||||
int |
getDisplayHeight()
Gets the height of the display, in pixels.
| ||||||||||
int |
getDisplayRotation()
Returns the current rotation of the display, as defined in Surface@return
| ||||||||||
int |
getDisplayWidth()
Gets the width of the display, in pixels.
| ||||||||||
static UiDevice |
getInstance()
Retrieves a singleton instance of UiDevice
| ||||||||||
String |
getLastTraversedText()
Retrieves the text from the last UI traversal event received.
| ||||||||||
String |
getProductName()
Retrieves the product name of the device.
| ||||||||||
boolean |
hasAnyWatcherTriggered()
Checks if any registered
UiWatcher have triggered. | ||||||||||
boolean |
hasWatcherTriggered(String watcherName)
Checks if a specific registered
UiWatcher has triggered. | ||||||||||
boolean |
isNaturalOrientation()
Check if the device is in its natural orientation.
| ||||||||||
boolean |
isScreenOn()
Checks the power manager if the screen is ON.
| ||||||||||
boolean |
pressBack()
Simulates a short press on the BACK button.
| ||||||||||
boolean |
pressDPadCenter()
Simulates a short press on the CENTER button.
| ||||||||||
boolean |
pressDPadDown()
Simulates a short press on the DOWN button.
| ||||||||||
boolean |
pressDPadLeft()
Simulates a short press on the LEFT button.
| ||||||||||
boolean |
pressDPadRight()
Simulates a short press on the RIGHT button.
| ||||||||||
boolean |
pressDPadUp()
Simulates a short press on the UP button.
| ||||||||||
boolean |
pressDelete()
Simulates a short press on the DELETE key.
| ||||||||||
boolean |
pressEnter()
Simulates a short press on the ENTER key.
| ||||||||||
boolean |
pressHome()
Simulates a short press on the HOME button.
| ||||||||||
boolean |
pressKeyCode(int keyCode)
Simulates a short press using a key code.
| ||||||||||
boolean |
pressKeyCode(int keyCode, int metaState)
Simulates a short press using a key code.
| ||||||||||
boolean |
pressMenu()
Simulates a short press on the MENU button.
| ||||||||||
boolean |
pressRecentApps()
Simulates a short press on the Recent Apps button.
| ||||||||||
boolean |
pressSearch()
Simulates a short press on the SEARCH button.
| ||||||||||
void |
registerWatcher(String name, UiWatcher watcher)
Registers a
UiWatcher to run automatically when the testing framework is unable to
find a match using a UiSelector . | ||||||||||
void |
removeWatcher(String name)
Removes a previously registered
UiWatcher . | ||||||||||
void |
resetWatcherTriggers()
Resets a
UiWatcher that has been triggered. | ||||||||||
void |
runWatchers()
This method forces all registered watchers to run.
| ||||||||||
void |
setOrientationLeft()
Simulates orienting the device to the left and also freezes rotation
by disabling the sensors.
| ||||||||||
void |
setOrientationNatural()
Simulates orienting the device into its natural orientation and also freezes rotation
by disabling the sensors.
| ||||||||||
void |
setOrientationRight()
Simulates orienting the device to the right and also freezes rotation
by disabling the sensors.
| ||||||||||
void |
sleep()
This method simply presses the power button if the screen is ON else
it does nothing if the screen is already OFF.
| ||||||||||
boolean |
swipe(Point[] segments, int segmentSteps)
Performs a swipe between points in the Point array.
| ||||||||||
boolean |
swipe(int startX, int startY, int endX, int endY, int steps)
Performs a swipe from one coordinate to another using the number of steps
to determine smoothness and speed.
| ||||||||||
boolean |
takeScreenshot(File storePath, float scale, int quality)
Take a screenshot of current window and store it as PNG
The screenshot is adjusted per screen rotation
| ||||||||||
boolean |
takeScreenshot(File storePath)
Take a screenshot of current window and store it as PNG
Default scale of 1.0f (original size) and 90% quality is used
The screenshot is adjusted per screen rotation
| ||||||||||
void |
unfreezeRotation()
Re-enables the sensors and un-freezes the device rotation allowing its contents
to rotate with the device physical rotation.
| ||||||||||
void |
waitForIdle(long time)
Waits for the current application to idle.
| ||||||||||
void |
waitForIdle()
Waits for the current application to idle.
| ||||||||||
boolean |
waitForWindowUpdate(String packageName, long timeout)
Waits for a window content update event to occur.
| ||||||||||
void |
wakeUp()
This method simulates pressing the power button if the screen is OFF else
it does nothing if the screen is already ON.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Clears the text from the last UI traversal event.
See getLastTraversedText()
.
Perform a click at arbitrary coordinates specified by the user
x | coordinate |
---|---|
y | coordinate |
Helper method used for debugging to dump the current window's layout hierarchy. The file root location is /data/local/tmp
Disables the sensors and freezes the device rotation at its current rotation state.
RemoteException | |
RemoteException |
This method is deprecated.
The results returned should be considered unreliable
Retrieves the last activity to report accessibility events.
Retrieves the name of the last package to report accessibility events.
Gets the height of the display, in pixels. The size is adjusted based on the current orientation of the display.
Returns the current rotation of the display, as defined in Surface@return
Gets the width of the display, in pixels. The width and height details are reported based on the current orientation of the display.
Retrieves a singleton instance of UiDevice
Retrieves the text from the last UI traversal event received. You can use this method to read the contents in a WebView container because the accessibility framework fires events as each text is highlighted. You can write a test to perform directional arrow presses to focus on different elements inside a WebView, and call this method to get the text from each traversed element. If you are testing a view container that can return a reference to a Document Object Model (DOM) object, your test should use the view's DOM instead.
Retrieves the product name of the device. This method provides information on what type of device the test is running on. This value is the same as returned by invoking #adb shell getprop ro.product.name.
Checks if any registered UiWatcher
have triggered.
See registerWatcher(String, UiWatcher)
See hasWatcherTriggered(String)
Checks if a specific registered UiWatcher
has triggered.
See registerWatcher(String, UiWatcher)
. If a UiWatcher runs and its
checkForCondition()
call returned true
, then
the UiWatcher is considered triggered. This is helpful if a watcher is detecting errors
from ANR or crash dialogs and the test needs to know if a UiWatcher has been triggered.
Check if the device is in its natural orientation. This is determined by checking if the orientation is at 0 or 180 degrees.
Checks the power manager if the screen is ON.
RemoteException | |
RemoteException |
Simulates a short press on the BACK button.
Simulates a short press on the CENTER button.
Simulates a short press on the DOWN button.
Simulates a short press on the LEFT button.
Simulates a short press on the RIGHT button.
Simulates a short press on the UP button.
Simulates a short press on the DELETE key.
Simulates a short press on the ENTER key.
Simulates a short press on the HOME button.
Simulates a short press using a key code. See KeyEvent
Simulates a short press using a key code. See KeyEvent.
keyCode | the key code of the event. |
---|---|
metaState | an integer in which each bit set to 1 represents a pressed meta key |
Simulates a short press on the MENU button.
Simulates a short press on the Recent Apps button.
RemoteException | |
RemoteException |
Simulates a short press on the SEARCH button.
Registers a UiWatcher
to run automatically when the testing framework is unable to
find a match using a UiSelector
. See runWatchers()
name | to register the UiWatcher |
---|---|
watcher | UiWatcher |
Removes a previously registered UiWatcher
.
See registerWatcher(String, UiWatcher)
name | used to register the UiWatcher |
---|
UiAutomationException |
Resets a UiWatcher
that has been triggered.
If a UiWatcher runs and its checkForCondition()
call
returned true
, then the UiWatcher is considered triggered.
See registerWatcher(String, UiWatcher)
This method forces all registered watchers to run.
See registerWatcher(String, UiWatcher)
Simulates orienting the device to the left and also freezes rotation
by disabling the sensors.
If you want to un-freeze the rotation and re-enable the sensors
see unfreezeRotation()
.
RemoteException | |
RemoteException |
Simulates orienting the device into its natural orientation and also freezes rotation
by disabling the sensors.
If you want to un-freeze the rotation and re-enable the sensors
see unfreezeRotation()
.
RemoteException | |
RemoteException |
Simulates orienting the device to the right and also freezes rotation
by disabling the sensors.
If you want to un-freeze the rotation and re-enable the sensors
see unfreezeRotation()
.
RemoteException | |
RemoteException |
This method simply presses the power button if the screen is ON else it does nothing if the screen is already OFF.
RemoteException | |
RemoteException |
Performs a swipe between points in the Point array. Each step execution is throttled to 5ms per step. So for a 100 steps, the swipe will take about 1/2 second to complete
segments | is Point array containing at least one Point object |
---|---|
segmentSteps | steps to inject between two Points |
Performs a swipe from one coordinate to another using the number of steps to determine smoothness and speed. Each step execution is throttled to 5ms per step. So for a 100 steps, the swipe will take about 1/2 second to complete.
steps | is the number of move steps sent to the system |
---|
Take a screenshot of current window and store it as PNG The screenshot is adjusted per screen rotation
storePath | where the PNG should be written to |
---|---|
scale | scale the screenshot down if needed; 1.0f for original size |
quality | quality of the PNG compression; range: 0-100 |
Take a screenshot of current window and store it as PNG Default scale of 1.0f (original size) and 90% quality is used The screenshot is adjusted per screen rotation
storePath | where the PNG should be written to |
---|
Re-enables the sensors and un-freezes the device rotation allowing its contents to rotate with the device physical rotation. During a test execution, it is best to keep the device frozen in a specific orientation until the test case execution has completed.
RemoteException |
---|
Waits for the current application to idle.
Waits for the current application to idle. Default wait timeout is 10 seconds
Waits for a window content update event to occur. If a package name for the window is specified, but the current window does not have the same package name, the function returns immediately.
packageName | the specified window package name (can be null ).
If null , a window update from any front-end window will end the wait |
---|---|
timeout | the timeout for the wait |
This method simulates pressing the power button if the screen is OFF else it does nothing if the screen is already ON. If the screen was OFF and it just got turned ON, this method will insert a 500ms delay to allow the device time to wake up and accept input.
RemoteException | |
RemoteException |