FileDocCategorySizeDatePackage
UiDevice.javaAPI DocAndroid 5.1 API29249Thu Mar 12 22:22:08 GMT 2015com.android.uiautomator.core

UiDevice

public class UiDevice extends Object
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.
since
API Level 16

Fields Summary
private static final String
LOG_TAG
private static final long
KEY_PRESS_EVENT_TIMEOUT
private final HashMap
mWatchers
private final List
mWatchersTriggers
private boolean
mInWatcherContext
private UiAutomatorBridge
mUiAutomationBridge
private static UiDevice
sDevice
Constructors Summary
private UiDevice()


      
        /* hide constructor */
    
Methods Summary
public voidclearLastTraversedText()
Clears the text from the last UI traversal event. See {@link #getLastTraversedText()}.

since
API Level 16

        Tracer.trace();
        getAutomatorBridge().getQueryController().clearLastTraversedText();
    
public booleanclick(int x, int y)
Perform a click at arbitrary coordinates specified by the user

param
x coordinate
param
y coordinate
return
true if the click succeeded else false
since
API Level 16

        Tracer.trace(x, y);
        if (x >= getDisplayWidth() || y >= getDisplayHeight()) {
            return (false);
        }
        return getAutomatorBridge().getInteractionController().clickNoSync(x, y);
    
public booleandrag(int startX, int startY, int endX, int endY, int steps)
Performs a swipe from one coordinate to another coordinate. You can control the smoothness and speed of the swipe by specifying the number of steps. Each step execution is throttled to 5 milliseconds per step, so for a 100 steps, the swipe will take around 0.5 seconds to complete.

param
startX X-axis value for the starting coordinate
param
startY Y-axis value for the starting coordinate
param
endX X-axis value for the ending coordinate
param
endY Y-axis value for the ending coordinate
param
steps is the number of steps for the swipe action
return
true if swipe is performed, false if the operation fails or the coordinates are invalid
since
API Level 18

        Tracer.trace(startX, startY, endX, endY, steps);
        return getAutomatorBridge().getInteractionController()
                .swipe(startX, startY, endX, endY, steps, true);
    
public voiddumpWindowHierarchy(java.lang.String fileName)
Helper method used for debugging to dump the current window's layout hierarchy. The file root location is /data/local/tmp

param
fileName
since
API Level 16

        Tracer.trace(fileName);
        AccessibilityNodeInfo root =
                getAutomatorBridge().getQueryController().getAccessibilityRootNode();
        if(root != null) {
            Display display = getAutomatorBridge().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            AccessibilityNodeInfoDumper.dumpWindowToFile(root,
                    new File(new File(Environment.getDataDirectory(), "local/tmp"), fileName),
                    display.getRotation(), size.x, size.y);
        }
    
public voidfreezeRotation()
Disables the sensors and freezes the device rotation at its current rotation state.

throws
RemoteException
since
API Level 16

        Tracer.trace();
        getAutomatorBridge().getInteractionController().freezeRotation();
    
UiAutomatorBridgegetAutomatorBridge()
Provides access the {@link QueryController} and {@link InteractionController}

return
{@link ShellUiAutomatorBridge}

        if (mUiAutomationBridge == null) {
            throw new RuntimeException("UiDevice not initialized");
        }
        return mUiAutomationBridge;
    
public java.lang.StringgetCurrentActivityName()
Retrieves the last activity to report accessibility events.

deprecated
The results returned should be considered unreliable
return
String name of activity
since
API Level 16

        Tracer.trace();
        return getAutomatorBridge().getQueryController().getCurrentActivityName();
    
public java.lang.StringgetCurrentPackageName()
Retrieves the name of the last package to report accessibility events.

return
String name of package
since
API Level 16

        Tracer.trace();
        return getAutomatorBridge().getQueryController().getCurrentPackageName();
    
public intgetDisplayHeight()
Gets the height of the display, in pixels. The size is adjusted based on the current orientation of the display.

return
height in pixels or zero on failure
since
API Level 16

        Tracer.trace();
        Display display = getAutomatorBridge().getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);
        return p.y;
    
public intgetDisplayRotation()
Returns the current rotation of the display, as defined in {@link Surface}

since
API Level 17

        Tracer.trace();
        waitForIdle();
        return getAutomatorBridge().getRotation();
    
public android.graphics.PointgetDisplaySizeDp()
Returns the display size in dp (device-independent pixel) The returned display size is adjusted per screen rotation. Also this will return the actual size of the screen, rather than adjusted per system decorations (like status bar).

return
a Point containing the display size in dp

        Tracer.trace();
        Display display = getAutomatorBridge().getDefaultDisplay();
        Point p = new Point();
        display.getRealSize(p);
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        float dpx = p.x / metrics.density;
        float dpy = p.y / metrics.density;
        p.x = Math.round(dpx);
        p.y = Math.round(dpy);
        return p;
    
public intgetDisplayWidth()
Gets the width of the display, in pixels. The width and height details are reported based on the current orientation of the display.

return
width in pixels or zero on failure
since
API Level 16

        Tracer.trace();
        Display display = getAutomatorBridge().getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);
        return p.x;
    
public static com.android.uiautomator.core.UiDevicegetInstance()
Retrieves a singleton instance of UiDevice

return
UiDevice instance
since
API Level 16

        if (sDevice == null) {
            sDevice = new UiDevice();
        }
        return sDevice;
    
public java.lang.StringgetLastTraversedText()
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.

return
text of the last traversal event, else return an empty string
since
API Level 16

        Tracer.trace();
        return getAutomatorBridge().getQueryController().getLastTraversedText();
    
public java.lang.StringgetProductName()
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.

return
product name of the device
since
API Level 17

        Tracer.trace();
        return Build.PRODUCT;
    
public booleanhasAnyWatcherTriggered()
Checks if any registered {@link UiWatcher} have triggered. See {@link #registerWatcher(String, UiWatcher)} See {@link #hasWatcherTriggered(String)}

since
API Level 16

        Tracer.trace();
        return mWatchersTriggers.size() > 0;
    
public booleanhasWatcherTriggered(java.lang.String watcherName)
Checks if a specific registered {@link UiWatcher} has triggered. See {@link #registerWatcher(String, UiWatcher)}. If a UiWatcher runs and its {@link UiWatcher#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.

param
watcherName
return
true if triggered else false
since
API Level 16

        Tracer.trace(watcherName);
        return mWatchersTriggers.contains(watcherName);
    
public voidinitialize(UiAutomatorBridge uiAutomatorBridge)

hide

        mUiAutomationBridge = uiAutomatorBridge;
    
booleanisInWatcherContext()

        return mInWatcherContext;
    
public booleanisNaturalOrientation()
Check if the device is in its natural orientation. This is determined by checking if the orientation is at 0 or 180 degrees.

return
true if it is in natural orientation
since
API Level 17

        Tracer.trace();
        waitForIdle();
        int ret = getAutomatorBridge().getRotation();
        return ret == UiAutomation.ROTATION_FREEZE_0 ||
                ret == UiAutomation.ROTATION_FREEZE_180;
    
public booleanisScreenOn()
Checks the power manager if the screen is ON.

return
true if the screen is ON else false
throws
RemoteException
since
API Level 16

        Tracer.trace();
        return getAutomatorBridge().getInteractionController().isScreenOn();
    
public booleanopenNotification()
Opens the notification shade.

return
true if successful, else return false
since
API Level 18

        Tracer.trace();
        waitForIdle();
        return  getAutomatorBridge().getInteractionController().openNotification();
    
public booleanopenQuickSettings()
Opens the Quick Settings shade.

return
true if successful, else return false
since
API Level 18

        Tracer.trace();
        waitForIdle();
        return getAutomatorBridge().getInteractionController().openQuickSettings();
    
public booleanpressBack()
Simulates a short press on the BACK button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        waitForIdle();
        return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent(
                KeyEvent.KEYCODE_BACK, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
                KEY_PRESS_EVENT_TIMEOUT);
    
public booleanpressDPadCenter()
Simulates a short press on the CENTER button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_DPAD_CENTER);
    
public booleanpressDPadDown()
Simulates a short press on the DOWN button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_DPAD_DOWN);
    
public booleanpressDPadLeft()
Simulates a short press on the LEFT button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_DPAD_LEFT);
    
public booleanpressDPadRight()
Simulates a short press on the RIGHT button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_DPAD_RIGHT);
    
public booleanpressDPadUp()
Simulates a short press on the UP button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_DPAD_UP);
    
public booleanpressDelete()
Simulates a short press on the DELETE key.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_DEL);
    
public booleanpressEnter()
Simulates a short press on the ENTER key.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_ENTER);
    
public booleanpressHome()
Simulates a short press on the HOME button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        waitForIdle();
        return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent(
                KeyEvent.KEYCODE_HOME, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
                KEY_PRESS_EVENT_TIMEOUT);
    
public booleanpressKeyCode(int keyCode)
Simulates a short press using a key code. See {@link KeyEvent}

return
true if successful, else return false
since
API Level 16

        Tracer.trace(keyCode);
        waitForIdle();
        return getAutomatorBridge().getInteractionController().sendKey(keyCode, 0);
    
public booleanpressKeyCode(int keyCode, int metaState)
Simulates a short press using a key code. See {@link KeyEvent}.

param
keyCode the key code of the event.
param
metaState an integer in which each bit set to 1 represents a pressed meta key
return
true if successful, else return false
since
API Level 16

        Tracer.trace(keyCode, metaState);
        waitForIdle();
        return getAutomatorBridge().getInteractionController().sendKey(keyCode, metaState);
    
public booleanpressMenu()
Simulates a short press on the MENU button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        waitForIdle();
        return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent(
                KeyEvent.KEYCODE_MENU, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
                KEY_PRESS_EVENT_TIMEOUT);
    
public booleanpressRecentApps()
Simulates a short press on the Recent Apps button.

return
true if successful, else return false
throws
RemoteException
since
API Level 16

        Tracer.trace();
        waitForIdle();
        return getAutomatorBridge().getInteractionController().toggleRecentApps();
    
public booleanpressSearch()
Simulates a short press on the SEARCH button.

return
true if successful, else return false
since
API Level 16

        Tracer.trace();
        return pressKeyCode(KeyEvent.KEYCODE_SEARCH);
    
public voidregisterWatcher(java.lang.String name, UiWatcher watcher)
Registers a {@link UiWatcher} to run automatically when the testing framework is unable to find a match using a {@link UiSelector}. See {@link #runWatchers()}

param
name to register the UiWatcher
param
watcher {@link UiWatcher}
since
API Level 16

        Tracer.trace(name, watcher);
        if (mInWatcherContext) {
            throw new IllegalStateException("Cannot register new watcher from within another");
        }
        mWatchers.put(name, watcher);
    
public voidremoveWatcher(java.lang.String name)
Removes a previously registered {@link UiWatcher}. See {@link #registerWatcher(String, UiWatcher)}

param
name used to register the UiWatcher
since
API Level 16

        Tracer.trace(name);
        if (mInWatcherContext) {
            throw new IllegalStateException("Cannot remove a watcher from within another");
        }
        mWatchers.remove(name);
    
public voidresetWatcherTriggers()
Resets a {@link UiWatcher} that has been triggered. If a UiWatcher runs and its {@link UiWatcher#checkForCondition()} call returned true, then the UiWatcher is considered triggered. See {@link #registerWatcher(String, UiWatcher)}

since
API Level 16

        Tracer.trace();
        mWatchersTriggers.clear();
    
public voidrunWatchers()
This method forces all registered watchers to run. See {@link #registerWatcher(String, UiWatcher)}

since
API Level 16

        Tracer.trace();
        if (mInWatcherContext) {
            return;
        }

        for (String watcherName : mWatchers.keySet()) {
            UiWatcher watcher = mWatchers.get(watcherName);
            if (watcher != null) {
                try {
                    mInWatcherContext = true;
                    if (watcher.checkForCondition()) {
                        setWatcherTriggered(watcherName);
                    }
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Exceuting watcher: " + watcherName, e);
                } finally {
                    mInWatcherContext = false;
                }
            }
        }
    
public voidsetCompressedLayoutHeirarchy(boolean compressed)
Enables or disables layout hierarchy compression. If compression is enabled, the layout hierarchy derived from the Acessibility framework will only contain nodes that are important for uiautomator testing. Any unnecessary surrounding layout nodes that make viewing and searching the hierarchy inefficient are removed.

param
compressed true to enable compression; else, false to disable
since
API Level 18

        getAutomatorBridge().setCompressedLayoutHierarchy(compressed);
    
public voidsetOrientationLeft()
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 {@link #unfreezeRotation()}.

throws
RemoteException
since
API Level 17

        Tracer.trace();
        getAutomatorBridge().getInteractionController().setRotationLeft();
        waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
    
public voidsetOrientationNatural()
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 {@link #unfreezeRotation()}.

throws
RemoteException
since
API Level 17

        Tracer.trace();
        getAutomatorBridge().getInteractionController().setRotationNatural();
        waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
    
public voidsetOrientationRight()
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 {@link #unfreezeRotation()}.

throws
RemoteException
since
API Level 17

        Tracer.trace();
        getAutomatorBridge().getInteractionController().setRotationRight();
        waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
    
private voidsetWatcherTriggered(java.lang.String watcherName)
Used internally by this class to set a {@link UiWatcher} state as triggered.

param
watcherName

        Tracer.trace(watcherName);
        if (!hasWatcherTriggered(watcherName)) {
            mWatchersTriggers.add(watcherName);
        }
    
public voidsleep()
This method simply presses the power button if the screen is ON else it does nothing if the screen is already OFF.

throws
RemoteException
since
API Level 16

        Tracer.trace();
        getAutomatorBridge().getInteractionController().sleepDevice();
    
public booleanswipe(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. Each step execution is throttled to 5ms per step. So for a 100 steps, the swipe will take about 1/2 second to complete.

param
startX
param
startY
param
endX
param
endY
param
steps is the number of move steps sent to the system
return
false if the operation fails or the coordinates are invalid
since
API Level 16

        Tracer.trace(startX, startY, endX, endY, steps);
        return getAutomatorBridge().getInteractionController()
                .swipe(startX, startY, endX, endY, steps);
    
public booleanswipe(android.graphics.Point[] segments, int segmentSteps)
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

param
segments is Point array containing at least one Point object
param
segmentSteps steps to inject between two Points
return
true on success
since
API Level 16

        Tracer.trace(segments, segmentSteps);
        return getAutomatorBridge().getInteractionController().swipe(segments, segmentSteps);
    
public booleantakeScreenshot(java.io.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

param
storePath where the PNG should be written to
return
true if screen shot is created successfully, false otherwise
since
API Level 17

        Tracer.trace(storePath);
        return takeScreenshot(storePath, 1.0f, 90);
    
public booleantakeScreenshot(java.io.File storePath, float scale, int quality)
Take a screenshot of current window and store it as PNG The screenshot is adjusted per screen rotation

param
storePath where the PNG should be written to
param
scale scale the screenshot down if needed; 1.0f for original size
param
quality quality of the PNG compression; range: 0-100
return
true if screen shot is created successfully, false otherwise
since
API Level 17

        Tracer.trace(storePath, scale, quality);
        return getAutomatorBridge().takeScreenshot(storePath, quality);
    
public voidunfreezeRotation()
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.

throws
RemoteException

        Tracer.trace();
        getAutomatorBridge().getInteractionController().unfreezeRotation();
    
public voidwaitForIdle()
Waits for the current application to idle. Default wait timeout is 10 seconds

since
API Level 16

        Tracer.trace();
        waitForIdle(Configurator.getInstance().getWaitForIdleTimeout());
    
public voidwaitForIdle(long timeout)
Waits for the current application to idle.

param
timeout in milliseconds
since
API Level 16

        Tracer.trace(timeout);
        getAutomatorBridge().waitForIdle(timeout);
    
public booleanwaitForWindowUpdate(java.lang.String packageName, long timeout)
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.

param
packageName the specified window package name (can be null). If null, a window update from any front-end window will end the wait
param
timeout the timeout for the wait
return
true if a window update occurred, false if timeout has elapsed or if the current window does not have the specified package name
since
API Level 16

        Tracer.trace(packageName, timeout);
        if (packageName != null) {
            if (!packageName.equals(getCurrentPackageName())) {
                return false;
            }
        }
        Runnable emptyRunnable = new Runnable() {
            @Override
            public void run() {
            }
        };
        AccessibilityEventFilter checkWindowUpdate = new AccessibilityEventFilter() {
            @Override
            public boolean accept(AccessibilityEvent t) {
                if (t.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
                    return packageName == null || packageName.equals(t.getPackageName());
                }
                return false;
            }
        };
        try {
            getAutomatorBridge().executeCommandAndWaitForAccessibilityEvent(
                    emptyRunnable, checkWindowUpdate, timeout);
        } catch (TimeoutException e) {
            return false;
        } catch (Exception e) {
            Log.e(LOG_TAG, "waitForWindowUpdate: general exception from bridge", e);
            return false;
        }
        return true;
    
public voidwakeUp()
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.

throws
RemoteException
since
API Level 16

        Tracer.trace();
        if(getAutomatorBridge().getInteractionController().wakeDevice()) {
            // sync delay to allow the window manager to start accepting input
            // after the device is awakened.
            SystemClock.sleep(500);
        }