BaseHostJUnit4Test

public abstract class BaseHostJUnit4Test
extends Object implements IAbiReceiver, IBuildReceiver, IDeviceTest, IInvocationContextReceiver

java.lang.Object
   ↳ com.android.tradefed.testtype.junit4.BaseHostJUnit4Test


Base test class for running host JUnit4 style tests. This class provides support to install, run and clean up instrumentation tests from the host side. This class is multi-devices compatible. Should be the single source of truth to run instrumentation tests from host side in order to avoid duplicated utility and base class.

Summary

Constants

String AJUR_RUNNER

long DEFAULT_TEST_TIMEOUT_MS

Public constructors

BaseHostJUnit4Test()

Public methods

final void autoTearDown()

Automatic tear down for all the apk installed.

final IAbi getAbi()
final IBuildInfo getBuild()
final ITestDevice getDevice()

Get the device under test.

final TestRunResult getLastDeviceRunResults()

Returns the TestRunResult resulting from the latest runDeviceTests that ran.

final List<ITestDevice> getListDevices()
boolean hasDeviceFeature(String feature)
final void installPackage(ITestDevice device, String apkFileName, String... options)

Install an apk given its name on a given device.

final void installPackage(String apkFileName, String... options)

Install an apk given its name on the device.

final void installPackageAsUser(String apkFileName, boolean grantPermission, int userId, String... options)

Install an apk given its name for a specific user.

final void installPackageAsUser(ITestDevice device, String apkFileName, boolean grantPermission, int userId, String... options)

Install an apk given its name for a specific user on a given device.

final boolean isPackageInstalled(String pkg)

Checks if a package of a given name is installed on the device

final boolean isPackageInstalled(ITestDevice device, String pkg)

Checks if a package of a given name is installed on the device

final boolean runDeviceTests(ITestDevice device, String runner, String pkgName, String testClassName, String testMethodName, Integer userId, Long testTimeoutMs, Long maxTimeToOutputMs, Long maxInstrumentationTimeoutMs, boolean checkResults, boolean isHiddenApiCheckDisabled)

Method to run an installed instrumentation package.

final boolean runDeviceTests(ITestDevice device, String pkgName, String testClassName, String testMethodName, Integer userId, Long testTimeoutMs)

Method to run an installed instrumentation package.

final boolean runDeviceTests(ITestDevice device, String pkgName, String testClassName, String testMethodName, Long testTimeoutMs, Long maxTimeToOutputMs, Long maxInstrumentationTimeoutMs)

Method to run an installed instrumentation package.

final boolean runDeviceTests(ITestDevice device, String pkgName, String testClassName, String testMethodName, Long testTimeoutMs)

Method to run an installed instrumentation package.

final boolean runDeviceTests(String pkgName, String testClassName, String testMethodName)

Method to run an installed instrumentation package.

final boolean runDeviceTests(DeviceTestRunOptions options)

Runs the instrumentation base on the information in DeviceTestRunOptions.

final boolean runDeviceTests(ITestDevice device, String pkgName, String testClassName, String testMethodName)

Method to run an installed instrumentation package.

final boolean runDeviceTests(String pkgName, String testClassName, Integer userId, Long testTimeoutMs)

Method to run an installed instrumentation package.

final boolean runDeviceTests(String pkgName, String testClassName)

Method to run an installed instrumentation package.

final boolean runDeviceTests(String pkgName, String testClassName, Long testTimeoutMs)

Method to run an installed instrumentation package.

final boolean runDeviceTests(String runner, String pkgName, String testClassName, String testMethodName)

Method to run an installed instrumentation package.

final void setAbi(IAbi abi)
final void setBuild(IBuildInfo buildInfo)
final void setDevice(ITestDevice device)

Inject the device under test.

final void setInvocationContext(IInvocationContext invocationContext)
final String uninstallPackage(String pkgName)

Uninstalls a package on the device.

final String uninstallPackage(ITestDevice device, String pkgName)

Uninstalls a package on the device

Constants

AJUR_RUNNER

static final String AJUR_RUNNER

Constant Value: "android.support.test.runner.AndroidJUnitRunner"

DEFAULT_TEST_TIMEOUT_MS

static final long DEFAULT_TEST_TIMEOUT_MS

Constant Value: 600000 (0x00000000000927c0)

Public constructors

BaseHostJUnit4Test

public BaseHostJUnit4Test ()

Public methods

autoTearDown

public final void autoTearDown ()

Automatic tear down for all the apk installed. This will uninstall all the apk from the device they where installed on.

Throws
DeviceNotAvailableException

getAbi

public final IAbi getAbi ()

Returns
IAbi

getBuild

public final IBuildInfo getBuild ()

Returns
IBuildInfo

getDevice

public final ITestDevice getDevice ()

Get the device under test.

Returns
ITestDevice the ITestDevice

getLastDeviceRunResults

public final TestRunResult getLastDeviceRunResults ()

Returns the TestRunResult resulting from the latest runDeviceTests that ran. Or null if no results available.

Returns
TestRunResult

getListDevices

public final List<ITestDevice> getListDevices ()

Returns
List<ITestDevice>

hasDeviceFeature

public boolean hasDeviceFeature (String feature)

Parameters
feature String

Returns
boolean

Throws
DeviceNotAvailableException

installPackage

public final void installPackage (ITestDevice device, 
                String apkFileName, 
                String... options)

Install an apk given its name on a given device. Apk will be auto-cleaned.

Parameters
device ITestDevice: the ITestDevice on which to install the apk.

apkFileName String: The name of the apk file.

options String: extra options given to the install command

Throws
DeviceNotAvailableException
TargetSetupError

installPackage

public final void installPackage (String apkFileName, 
                String... options)

Install an apk given its name on the device. Apk will be auto-cleaned.

Parameters
apkFileName String: The name of the apk file.

options String: extra options given to the install command

Throws
DeviceNotAvailableException
TargetSetupError

installPackageAsUser

public final void installPackageAsUser (String apkFileName, 
                boolean grantPermission, 
                int userId, 
                String... options)

Install an apk given its name for a specific user.

Parameters
apkFileName String: The name of the apk file.

grantPermission boolean: whether to pass the grant permission flag when installing the apk.

userId int: the user id of the user where to install the apk.

options String: extra options given to the install command

Throws
DeviceNotAvailableException
TargetSetupError

installPackageAsUser

public final void installPackageAsUser (ITestDevice device, 
                String apkFileName, 
                boolean grantPermission, 
                int userId, 
                String... options)

Install an apk given its name for a specific user on a given device.

Parameters
device ITestDevice: the ITestDevice on which to install the apk.

apkFileName String: The name of the apk file.

grantPermission boolean: whether to pass the grant permission flag when installing the apk.

userId int: the user id of the user where to install the apk.

options String: extra options given to the install command

Throws
DeviceNotAvailableException
TargetSetupError

isPackageInstalled

public final boolean isPackageInstalled (String pkg)

Checks if a package of a given name is installed on the device

Parameters
pkg String: the name of the package

Returns
boolean true if the package is found on the device

Throws
DeviceNotAvailableException

isPackageInstalled

public final boolean isPackageInstalled (ITestDevice device, 
                String pkg)

Checks if a package of a given name is installed on the device

Parameters
device ITestDevice: the device that should uninstall the package.

pkg String: the name of the package

Returns
boolean true if the package is found on the device

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (ITestDevice device, 
                String runner, 
                String pkgName, 
                String testClassName, 
                String testMethodName, 
                Integer userId, 
                Long testTimeoutMs, 
                Long maxTimeToOutputMs, 
                Long maxInstrumentationTimeoutMs, 
                boolean checkResults, 
                boolean isHiddenApiCheckDisabled)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
device ITestDevice: the device agaisnt which to run the instrumentation.

runner String

pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testMethodName String: the name of the test method in the class to be run.

userId Integer: the id of the user to run the test against. can be null.

testTimeoutMs Long: the timeout in millisecond to be applied to each test case.

maxTimeToOutputMs Long: the max timeout the test has to start outputting something.

maxInstrumentationTimeoutMs Long: the max timeout the full instrumentation has to complete.

checkResults boolean: whether or not the results are checked for crashes.

isHiddenApiCheckDisabled boolean: whether or not we should disable the hidden api check.

Returns
boolean True if it succeeded without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (ITestDevice device, 
                String pkgName, 
                String testClassName, 
                String testMethodName, 
                Integer userId, 
                Long testTimeoutMs)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
device ITestDevice: the device agaisnt which to run the instrumentation.

pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testMethodName String: the name of the test method in the class to be run.

userId Integer: the id of the user to run the test against. can be null.

testTimeoutMs Long: the timeout in millisecond to be applied to each test case.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (ITestDevice device, 
                String pkgName, 
                String testClassName, 
                String testMethodName, 
                Long testTimeoutMs, 
                Long maxTimeToOutputMs, 
                Long maxInstrumentationTimeoutMs)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
device ITestDevice: the device agaisnt which to run the instrumentation.

pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testMethodName String: the name of the test method in the class to be run.

testTimeoutMs Long: the timeout in millisecond to be applied to each test case.

maxTimeToOutputMs Long: the max timeout the test has to start outputting something.

maxInstrumentationTimeoutMs Long: the max timeout the full instrumentation has to complete.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (ITestDevice device, 
                String pkgName, 
                String testClassName, 
                String testMethodName, 
                Long testTimeoutMs)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
device ITestDevice: the device agaisnt which to run the instrumentation.

pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testMethodName String: the name of the test method in the class to be run.

testTimeoutMs Long: the timeout in millisecond to be applied to each test case.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (String pkgName, 
                String testClassName, 
                String testMethodName)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testMethodName String: the name of the test method in the class to be run.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (DeviceTestRunOptions options)

Runs the instrumentation base on the information in DeviceTestRunOptions.

Parameters
options DeviceTestRunOptions: the DeviceTestRunOptions driving the instrumentation setup.

Returns
boolean True if it succeeded without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (ITestDevice device, 
                String pkgName, 
                String testClassName, 
                String testMethodName)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
device ITestDevice: the device agaisnt which to run the instrumentation.

pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testMethodName String: the name of the test method in the class to be run.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (String pkgName, 
                String testClassName, 
                Integer userId, 
                Long testTimeoutMs)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

userId Integer: the id of the user to run the test against. can be null.

testTimeoutMs Long: the timeout in millisecond to be applied to each test case.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (String pkgName, 
                String testClassName)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (String pkgName, 
                String testClassName, 
                Long testTimeoutMs)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testTimeoutMs Long: the timeout in millisecond to be applied to each test case.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

runDeviceTests

public final boolean runDeviceTests (String runner, 
                String pkgName, 
                String testClassName, 
                String testMethodName)

Method to run an installed instrumentation package. Use getLastDeviceRunResults() right after to get the details of results.

Parameters
runner String: the instrumentation runner to be used.

pkgName String: the name of the package to run.

testClassName String: the name of the test class to run.

testMethodName String: the name of the test method in the class to be run.

Returns
boolean True if it succeed without failure. False otherwise.

Throws
DeviceNotAvailableException

setAbi

public final void setAbi (IAbi abi)

Parameters
abi IAbi

setBuild

public final void setBuild (IBuildInfo buildInfo)

Parameters
buildInfo IBuildInfo

setDevice

public final void setDevice (ITestDevice device)

Inject the device under test.

Parameters
device ITestDevice: the ITestDevice to use

setInvocationContext

public final void setInvocationContext (IInvocationContext invocationContext)

Parameters
invocationContext IInvocationContext

uninstallPackage

public final String uninstallPackage (String pkgName)

Uninstalls a package on the device.

Parameters
pkgName String: the Android package to uninstall

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

Throws
DeviceNotAvailableException

uninstallPackage

public final String uninstallPackage (ITestDevice device, 
                String pkgName)

Uninstalls a package on the device

Parameters
device ITestDevice: the device that should uninstall the package.

pkgName String: the Android package to uninstall

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

Throws
DeviceNotAvailableException