ITestDevice

public interface ITestDevice
implements INativeDevice

com.android.tradefed.device.ITestDevice
Known Indirect Subclasses
IManagedTestDevice A ITestDevice whose lifecycle is managed. 
NativeDevice Default implementation of a ITestDevice Non-full stack android devices. 
RemoteAndroidDevice Implementation of a ITestDevice for a full stack android device connected via adb connect. 
TestDevice Implementation of a ITestDevice for a full stack android device  


Provides an reliable and slightly higher level API to a ddmlib IDevice.

Retries device commands for a configurable amount, and provides a device recovery interface for devices which are unresponsive.

Summary

Nested classes

class ITestDevice.MountPointInfo

A simple struct class to store information about a single mountpoint  

enum ITestDevice.RecoveryMode

 

Public methods

abstract boolean checkConnectivity()

Check that device has network connectivity.

abstract boolean clearErrorDialogs()

Attempt to dismiss any error dialogs currently displayed on device UI.

abstract void clearLastConnectedWifiNetwork()

Clears the last connected wifi network.

abstract boolean connectToWifiNetwork(String wifiSsid, String wifiPsk)

Connects to a wifi network.

abstract boolean connectToWifiNetwork(String wifiSsid, String wifiPsk, boolean scanSsid)

Connects to a wifi network.

abstract boolean connectToWifiNetworkIfNeeded(String wifiSsid, String wifiPsk, boolean scanSsid)

A variant of connectToWifiNetwork(String, String) that only connects if device currently does not have network connectivity.

abstract boolean connectToWifiNetworkIfNeeded(String wifiSsid, String wifiPsk)

A variant of connectToWifiNetwork(String, String) that only connects if device currently does not have network connectivity.

abstract int createUser(String name)

Create a user with a given name and default flags 0.

abstract int createUser(String name, boolean guest, boolean ephemeral)

Create a user with a given name and the provided flags

abstract void disableKeyguard()

Attempts to disable the keyguard.

abstract boolean disableNetworkMonitor()

Disables network monitoring on device.

abstract boolean disconnectFromWifi()

Disconnects from a wifi network.

abstract boolean enableNetworkMonitor()

Enables network monitoring on device.

abstract String getAndroidId(int userId)

Find and return the android-id associated to a userId, null if not found.

abstract Map<Integer, String> getAndroidIds()

Create a Map of android ids found matching user ids.

abstract PackageInfo getAppPackageInfo(String packageName)

Fetch information about a package installed on device.

abstract int getCurrentUser()

Return the id of the current running user.

abstract Set<String> getInstalledPackageNames()

Fetch the application package names present on the device.

abstract String getIpAddress()

Gets the device's IP address.

abstract KeyguardControllerState getKeyguardState()

Return an object to get the current state of the keyguard or null if not supported.

abstract int getMaxNumberOfUsersSupported()

Get the maximum number of supported users.

abstract TestDeviceOptions getOptions()

Fetch the test options for the device.

abstract Integer getPrimaryUserId()

Returns the primary user id.

abstract InputStreamSource getScreenshot()

Grabs a screenshot from the device.

abstract InputStreamSource getScreenshot(String format, boolean rescale)

Grabs a screenshot from the device.

abstract InputStreamSource getScreenshot(String format)

Grabs a screenshot from the device.

abstract String getSetting(String namespace, String key)

See getSetting(int, String, String) and performed on system user.

abstract String getSetting(int userId, String namespace, String key)

Return the value of the requested setting.

abstract Set<String> getUninstallablePackageNames()

Fetch the application package names that can be uninstalled.

abstract int getUserFlags(int userId)

Find and return the flags of a given user.

abstract int getUserSerialNumber(int userId)

Return the serial number associated to the userId if found, -10000 in any other cases.

abstract boolean hasFeature(String feature)

Check if a feature is available on a device.

abstract String installPackage(File packageFile, boolean reinstall, String... extraArgs)

Install an Android package on device.

abstract String installPackage(File packageFile, boolean reinstall, boolean grantPermissions, String... extraArgs)

Install an Android package on device.

abstract String installPackageForUser(File packageFile, boolean reinstall, boolean grantPermissions, int userId, String... extraArgs)

Install an Android package on device for a given user.

abstract String installPackageForUser(File packageFile, boolean reinstall, int userId, String... extraArgs)

Install an Android package on device for a given user.

abstract boolean isMultiUserSupported()

Determines if multi user is supported.

abstract boolean isUserRunning(int userId)

Check if a given user is running.

abstract boolean isWifiEnabled()

Test if wifi is enabled.

abstract ArrayList<Integer> listUsers()

Gets the list of users on the device.

abstract boolean removeAdmin(String componentName, int userId)

Remove given device admin in given user and return true if it is successful, false otherwise.

abstract void removeOwners()

Remove all existing device profile owners with the best effort.

abstract boolean removeUser(int userId)

Remove a given user from the device.

abstract boolean setDeviceOwner(String componentName, int userId)

Set a device admin component as device owner in given user.

abstract void setSetting(String namespace, String key, String value)

See setSetting(int, String, String, String) and performed on system user.

abstract void setSetting(int userId, String namespace, String key, String value)

Add a setting value to the namespace of a given user.

abstract boolean startUser(int userId)

Starts a given user in the background if it is currently stopped.

abstract boolean stopUser(int userId, boolean waitFlag, boolean forceFlag)

Stop a given user.

abstract boolean stopUser(int userId)

Stops a given user.

abstract boolean switchUser(int userId)

Switch to another userId with a default timeout.

abstract boolean switchUser(int userId, long timeout)

Switch to another userId with the provided timeout as deadline.

abstract String uninstallPackage(String packageName)

Uninstall an Android package from device.

Public methods

checkConnectivity

boolean checkConnectivity ()

Check that device has network connectivity.

Returns
boolean true if device has a working network connection, false overwise.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

clearErrorDialogs

boolean clearErrorDialogs ()

Attempt to dismiss any error dialogs currently displayed on device UI.

Returns
boolean true if no dialogs were present or dialogs were successfully cleared. false otherwise.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

clearLastConnectedWifiNetwork

void clearLastConnectedWifiNetwork ()

Clears the last connected wifi network. This should be called when starting a new invocation to avoid connecting to the wifi network used in the previous test after device reboots.

connectToWifiNetwork

boolean connectToWifiNetwork (String wifiSsid, 
                String wifiPsk)

Connects to a wifi network.

Turns on wifi and blocks until a successful connection is made to the specified wifi network. Once a connection is made, the instance will try to restore the connection after every reboot until disconnectFromWifi() or clearLastConnectedWifiNetwork() is called.

Parameters
wifiSsid String: the wifi ssid to connect to

wifiPsk String: PSK passphrase or null if unencrypted

Returns
boolean true if connected to wifi network successfully. false otherwise

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

connectToWifiNetwork

boolean connectToWifiNetwork (String wifiSsid, 
                String wifiPsk, 
                boolean scanSsid)

Connects to a wifi network.

Turns on wifi and blocks until a successful connection is made to the specified wifi network. Once a connection is made, the instance will try to restore the connection after every reboot until disconnectFromWifi() or clearLastConnectedWifiNetwork() is called.

Parameters
wifiSsid String: the wifi ssid to connect to

wifiPsk String: PSK passphrase or null if unencrypted

scanSsid boolean: whether to scan for hidden SSID for this network.

Returns
boolean true if connected to wifi network successfully. false otherwise

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

connectToWifiNetworkIfNeeded

boolean connectToWifiNetworkIfNeeded (String wifiSsid, 
                String wifiPsk, 
                boolean scanSsid)

A variant of connectToWifiNetwork(String, String) that only connects if device currently does not have network connectivity.

Parameters
scanSsid boolean: whether to scan for hidden SSID for this network

Returns
boolean true if connected to wifi network successfully. false otherwise

Throws
DeviceNotAvailableException

connectToWifiNetworkIfNeeded

boolean connectToWifiNetworkIfNeeded (String wifiSsid, 
                String wifiPsk)

A variant of connectToWifiNetwork(String, String) that only connects if device currently does not have network connectivity.

Returns
boolean true if connected to wifi network successfully. false otherwise

Throws
DeviceNotAvailableException

createUser

int createUser (String name)

Create a user with a given name and default flags 0.

Parameters
name String: of the user to create on the device

Returns
int the integer for the user id created

Throws
DeviceNotAvailableException
IllegalStateException

createUser

int createUser (String name, 
                boolean guest, 
                boolean ephemeral)

Create a user with a given name and the provided flags

Parameters
name String: of the user to create on the device

guest boolean: enable the user flag --guest during creation

ephemeral boolean: enable the user flag --ephemeral during creation

Returns
int id of the created user

Throws
DeviceNotAvailableException
IllegalStateException

disableKeyguard

void disableKeyguard ()

Attempts to disable the keyguard.

First wait for the input dispatch to become ready, this happens around the same time when the device reports BOOT_COMPLETE, apparently asynchronously, because current framework implementation has occasional race condition. Then command is sent to dismiss keyguard (works on non-secure ones only)

Throws
DeviceNotAvailableException

disableNetworkMonitor

boolean disableNetworkMonitor ()

Disables network monitoring on device.

Returns
boolean true if monitoring is disabled successfully. false if it failed.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

disconnectFromWifi

boolean disconnectFromWifi ()

Disconnects from a wifi network.

Removes all networks from known networks list and disables wifi.

Returns
boolean true if disconnected from wifi network successfully. false if disconnect failed.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

enableNetworkMonitor

boolean enableNetworkMonitor ()

Enables network monitoring on device.

Returns
boolean true if monitoring is enabled successfully. false if it failed.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

getAndroidId

String getAndroidId (int userId)

Find and return the android-id associated to a userId, null if not found.

Parameters
userId int

Returns
String

Throws
DeviceNotAvailableException

getAndroidIds

Map<Integer, String> getAndroidIds ()

Create a Map of android ids found matching user ids. There is no insurance that each user id will found an android id associated in this function so some user ids may match null.

Returns
Map<Integer, String> Map of android ids found matching user ids.

Throws
DeviceNotAvailableException

getAppPackageInfo

PackageInfo getAppPackageInfo (String packageName)

Fetch information about a package installed on device.

Parameters
packageName String

Returns
PackageInfo the PackageInfo or null if information could not be retrieved

Throws
DeviceNotAvailableException

getCurrentUser

int getCurrentUser ()

Return the id of the current running user.

Returns
int

Throws
DeviceNotAvailableException

getInstalledPackageNames

Set<String> getInstalledPackageNames ()

Fetch the application package names present on the device.

Returns
Set<String> Set of String package names currently installed on the device.

Throws
DeviceNotAvailableException

getIpAddress

String getIpAddress ()

Gets the device's IP address.

Returns
String the device's IP address, or null if device has no IP address

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

getKeyguardState

KeyguardControllerState getKeyguardState ()

Return an object to get the current state of the keyguard or null if not supported.

Returns
KeyguardControllerState a KeyguardControllerState containing a snapshot of the state of the keyguard and returns Null if the Keyguard query is not supported.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

getMaxNumberOfUsersSupported

int getMaxNumberOfUsersSupported ()

Get the maximum number of supported users. Defaults to 0.

Returns
int an integer indicating the number of supported users

Throws
DeviceNotAvailableException

getOptions

TestDeviceOptions getOptions ()

Fetch the test options for the device.

Returns
TestDeviceOptions TestDeviceOptions related to the device under test.

getPrimaryUserId

Integer getPrimaryUserId ()

Returns the primary user id.

Returns
Integer the userId of the primary user if there is one, and null if there is no primary user.

Throws
DeviceNotAvailableException

getScreenshot

InputStreamSource getScreenshot ()

Grabs a screenshot from the device.

Returns
InputStreamSource a InputStreamSource of the screenshot in png format, or null if the screenshot was not successful.

Throws
DeviceNotAvailableException

getScreenshot

InputStreamSource getScreenshot (String format, 
                boolean rescale)

Grabs a screenshot from the device. Recommended to use getScreenshot(String) instead with JPEG encoding for smaller size.

Parameters
format String: supported PNG, JPEG

rescale boolean: if screenshot should be rescaled to reduce the size of resulting image

Returns
InputStreamSource a InputStreamSource of the screenshot in format, or null if the screenshot was not successful.

Throws
DeviceNotAvailableException

getScreenshot

InputStreamSource getScreenshot (String format)

Grabs a screenshot from the device. Recommended to use getScreenshot(format) instead with JPEG encoding for smaller size

Parameters
format String: supported PNG, JPEG

Returns
InputStreamSource a InputStreamSource of the screenshot in format, or null if the screenshot was not successful.

Throws
DeviceNotAvailableException

getSetting

String getSetting (String namespace, 
                String key)

See getSetting(int, String, String) and performed on system user.

Parameters
namespace String

key String

Returns
String

Throws
DeviceNotAvailableException

getSetting

String getSetting (int userId, 
                String namespace, 
                String key)

Return the value of the requested setting. namespace must be one of: {"system", "secure", "global"}

Parameters
userId int

namespace String

key String

Returns
String the value associated with the namespace:key of a user. Null if not found.

Throws
DeviceNotAvailableException

getUninstallablePackageNames

Set<String> getUninstallablePackageNames ()

Fetch the application package names that can be uninstalled. This is presently defined as non-system packages, and updated system packages.

Returns
Set<String> Set of uninstallable String package names currently installed on the device.

Throws
DeviceNotAvailableException

getUserFlags

int getUserFlags (int userId)

Find and return the flags of a given user. Flags are defined in "android.content.pm.UserInfo" class in Android Open Source Project.

Parameters
userId int

Returns
int the flags associated with the userId provided if found, -10000 in any other cases.

Throws
DeviceNotAvailableException

getUserSerialNumber

int getUserSerialNumber (int userId)

Return the serial number associated to the userId if found, -10000 in any other cases.

Parameters
userId int

Returns
int

Throws
DeviceNotAvailableException

hasFeature

boolean hasFeature (String feature)

Check if a feature is available on a device.

Parameters
feature String: which format should be "feature:".

Returns
boolean True if feature is found, false otherwise.

Throws
DeviceNotAvailableException

installPackage

String installPackage (File packageFile, 
                boolean reinstall, 
                String... extraArgs)

Install an Android package on device.

Parameters
packageFile File: the apk file to install

reinstall boolean: true if a reinstall should be performed

extraArgs String: optional extra arguments to pass. See 'adb shell pm install --help' for available options.

Returns
String a String with an error code, or null if success.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

installPackage

String installPackage (File packageFile, 
                boolean reinstall, 
                boolean grantPermissions, 
                String... extraArgs)

Install an Android package on device.

Note: Only use cases that requires explicit control of granting runtime permission at install time should call this function.

Parameters
packageFile File: the apk file to install

reinstall boolean: true if a reinstall should be performed

grantPermissions boolean: if all runtime permissions should be granted at install time

extraArgs String: optional extra arguments to pass. See 'adb shell pm install --help' for available options.

Returns
String a String with an error code, or null if success.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.
UnsupportedOperationException if runtime permission is not supported by the platform on device.

installPackageForUser

String installPackageForUser (File packageFile, 
                boolean reinstall, 
                boolean grantPermissions, 
                int userId, 
                String... extraArgs)

Install an Android package on device for a given user.

Note: Only use cases that requires explicit control of granting runtime permission at install time should call this function.

Parameters
packageFile File: the apk file to install

reinstall boolean: true if a reinstall should be performed

grantPermissions boolean: if all runtime permissions should be granted at install time

userId int: the integer user id to install for.

extraArgs String: optional extra arguments to pass. See 'adb shell pm install --help' for available options.

Returns
String a String with an error code, or null if success.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.
UnsupportedOperationException if runtime permission is not supported by the platform on device.

installPackageForUser

String installPackageForUser (File packageFile, 
                boolean reinstall, 
                int userId, 
                String... extraArgs)

Install an Android package on device for a given user.

Parameters
packageFile File: the apk file to install

reinstall boolean: true if a reinstall should be performed

userId int: the integer user id to install for.

extraArgs String: optional extra arguments to pass. See 'adb shell pm install --help' for available options.

Returns
String a String with an error code, or null if success.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

isMultiUserSupported

boolean isMultiUserSupported ()

Determines if multi user is supported.

Returns
boolean true if multi user is supported, false otherwise

Throws
DeviceNotAvailableException

isUserRunning

boolean isUserRunning (int userId)

Check if a given user is running.

Parameters
userId int

Returns
boolean True if the user is running, false in every other cases.

Throws
DeviceNotAvailableException

isWifiEnabled

boolean isWifiEnabled ()

Test if wifi is enabled.

Checks if wifi is enabled on device. Useful for asserting wifi status before tests that shouldn't run with wifi, e.g. mobile data tests.

Returns
boolean true if wifi is enabled. false if disabled

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.

listUsers

ArrayList<Integer> listUsers ()

Gets the list of users on the device. Defaults to null.

Returns
ArrayList<Integer> the list of user ids or null if there was an error.

Throws
DeviceNotAvailableException

removeAdmin

boolean removeAdmin (String componentName, 
                int userId)

Remove given device admin in given user and return true if it is successful, false otherwise.

Parameters
componentName String: of device admin to be removed.

userId int: of user that the device admin lives in.

Returns
boolean True if it is successful, false otherwise.

Throws
DeviceNotAvailableException

removeOwners

void removeOwners ()

Remove all existing device profile owners with the best effort.

Throws
DeviceNotAvailableException

removeUser

boolean removeUser (int userId)

Remove a given user from the device.

Parameters
userId int: of the user to remove

Returns
boolean true if we were succesful in removing the user, false otherwise.

Throws
DeviceNotAvailableException

setDeviceOwner

boolean setDeviceOwner (String componentName, 
                int userId)

Set a device admin component as device owner in given user.

Parameters
componentName String: of device admin to be device owner.

userId int: of the user that the device owner lives in.

Returns
boolean True if it is successful, false otherwise.

Throws
DeviceNotAvailableException

setSetting

void setSetting (String namespace, 
                String key, 
                String value)

See setSetting(int, String, String, String) and performed on system user.

Parameters
namespace String

key String

value String

Throws
DeviceNotAvailableException

setSetting

void setSetting (int userId, 
                String namespace, 
                String key, 
                String value)

Add a setting value to the namespace of a given user. Some settings will only be available after a reboot. namespace must be one of: {"system", "secure", "global"}

Parameters
userId int

namespace String

key String

value String

Throws
DeviceNotAvailableException

startUser

boolean startUser (int userId)

Starts a given user in the background if it is currently stopped. If the user is already running in the background, this method is a NOOP.

Parameters
userId int: of the user to start in the background

Returns
boolean true if the user was successfully started in the background.

Throws
DeviceNotAvailableException

stopUser

boolean stopUser (int userId, 
                boolean waitFlag, 
                boolean forceFlag)

Stop a given user. Possible to provide extra flags to wait for the operation to have effect, and force terminate the user. Cannot stop current and system user.

Parameters
userId int: of the user to stop.

waitFlag boolean: will make the command wait until user is stopped.

forceFlag boolean: will force stop the user.

Returns
boolean true if the user was successfully stopped.

Throws
DeviceNotAvailableException

stopUser

boolean stopUser (int userId)

Stops a given user. If the user is already stopped, this method is a NOOP. Cannot stop current and system user.

Parameters
userId int: of the user to stop.

Returns
boolean true if the user was successfully stopped.

Throws
DeviceNotAvailableException

switchUser

boolean switchUser (int userId)

Switch to another userId with a default timeout. switchUser(int, long).

Parameters
userId int

Returns
boolean True if the new userId matches the userId provider. False otherwise.

Throws
DeviceNotAvailableException

switchUser

boolean switchUser (int userId, 
                long timeout)

Switch to another userId with the provided timeout as deadline. Attempt to disable keyguard after user change is successful.

Parameters
userId int

timeout long: to wait before returning false for switch-user failed.

Returns
boolean True if the new userId matches the userId provider. False otherwise.

Throws
DeviceNotAvailableException

uninstallPackage

String uninstallPackage (String packageName)

Uninstall an Android package from device.

Parameters
packageName String: the Android package to uninstall

Returns
String a String with an error code, or null if success.

Throws
DeviceNotAvailableException if connection with device is lost and cannot be recovered.