BaseHostJUnit4Test

public abstract class BaseHostJUnit4Test
extends Object implements IAbiReceiver, ITestInformationReceiver

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

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()
final IInvocationContext getInvocationContext()
final TestRunResult getLastDeviceRunResults()

Returns the TestRunResult resulting from the latest runDeviceTests that ran.

final getListDevices()
TestInformation getTestInformation()
boolean hasDeviceFeature(String feature)
final void installPackage(DeviceTestRunOptions options)

Install an apk based on the DeviceTestRunOptions on the device.

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

Install an apk given its name on the device.

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

Install an apk given its name on a given device.

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 void installPackageAsUser(String apkFileName, boolean grantPermission, int userId, String... options)

Install an apk given its name for a specific user.

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 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 runner, String pkgName, String testClassName, String testMethodName, Integer userId, Long testTimeoutMs, Long maxTimeToOutputMs, Long maxInstrumentationTimeoutMs, boolean checkResults, boolean isHiddenApiCheckDisabled, boolean isTestApiCheckDisabled, boolean isIsolatedStorageDisabled, boolean isWindowAnimationDisabled, instrumentationArgs, extraListeners)

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(ITestDevice device, String runner, String pkgName, String testClassName, String testMethodName, Integer userId, Long testTimeoutMs, Long maxTimeToOutputMs, Long maxInstrumentationTimeoutMs, boolean checkResults, boolean isHiddenApiCheckDisabled, instrumentationArgs)

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 runner, String pkgName, String testClassName, String testMethodName, Integer userId, Long testTimeoutMs, Long maxTimeToOutputMs, Long maxInstrumentationTimeoutMs, boolean checkResults, boolean isHiddenApiCheckDisabled, boolean isTestApiCheckDisabled, boolean isIsolatedStorageDisabled, boolean isWindowAnimationDisabled, boolean isRestartDisabled, instrumentationArgs, extraListeners)

Method to run an installed instrumentation package.

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(ITestDevice device, String runner, String pkgName, String testClassName, String testMethodName, Integer userId, Long testTimeoutMs, Long maxTimeToOutputMs, Long maxInstrumentationTimeoutMs, boolean checkResults, boolean isHiddenApiCheckDisabled, boolean isTestApiCheckDisabled, boolean isIsolatedStorageDisabled, instrumentationArgs, extraListeners)

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 setTestInformation(TestInformation testInformation)
final String uninstallPackage(String pkgName)

Uninstalls a package on the device.

final String uninstallPackage(ITestDevice device, String pkgName)

Uninstalls a package on the device

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 ()

Returns
ITestDevice

getInvocationContext

public final IInvocationContext getInvocationContext ()

Returns
IInvocationContext

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  getListDevices ()

Returns

getTestInformation

public TestInformation getTestInformation ()

Returns
TestInformation

hasDeviceFeature

public boolean hasDeviceFeature (String feature)

Parameters
feature String

Returns
boolean

Throws
DeviceNotAvailableException

installPackage

public final void installPackage (DeviceTestRunOptions options)

Install an apk based on the DeviceTestRunOptions on the device. Apk will be auto-cleaned.

Parameters
options DeviceTestRunOptions: The options of the package installation.

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

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

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

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

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 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 runner, 
                String pkgName, 
                String testClassName, 
                String testMethodName, 
                Integer userId, 
                Long testTimeoutMs, 
                Long maxTimeToOutputMs, 
                Long maxInstrumentationTimeoutMs, 
                boolean checkResults, 
                boolean isHiddenApiCheckDisabled, 
                boolean isTestApiCheckDisabled, 
                boolean isIsolatedStorageDisabled, 
                boolean isWindowAnimationDisabled, 
                 instrumentationArgs, 
                 extraListeners)

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.

isTestApiCheckDisabled boolean: whether or not we should disable the test api check.

isIsolatedStorageDisabled boolean: whether or not we should disable isolated storage.

isWindowAnimationDisabled boolean: whether or not we should disable window animation.

instrumentationArgs : arguments to pass to the instrumentation.

extraListeners

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, 
                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 (ITestDevice device, 
                String runner, 
                String pkgName, 
                String testClassName, 
                String testMethodName, 
                Integer userId, 
                Long testTimeoutMs, 
                Long maxTimeToOutputMs, 
                Long maxInstrumentationTimeoutMs, 
                boolean checkResults, 
                boolean isHiddenApiCheckDisabled, 
                 instrumentationArgs)

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.

instrumentationArgs : arguments to pass to the instrumentation.

Returns
boolean True if it succeeded 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
com.android.tradefed.device.DeviceNotAvailableException
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, 
                boolean isTestApiCheckDisabled, 
                boolean isIsolatedStorageDisabled, 
                boolean isWindowAnimationDisabled, 
                boolean isRestartDisabled, 
                 instrumentationArgs, 
                 extraListeners)

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.

isTestApiCheckDisabled boolean: whether or not we should disable the test api check.

isIsolatedStorageDisabled boolean: whether or not we should disable isolated storage.

isWindowAnimationDisabled boolean: whether or not we should disable window animation.

isRestartDisabled boolean

instrumentationArgs : arguments to pass to the instrumentation.

extraListeners

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 (ITestDevice device, 
                String runner, 
                String pkgName, 
                String testClassName, 
                String testMethodName, 
                Integer userId, 
                Long testTimeoutMs, 
                Long maxTimeToOutputMs, 
                Long maxInstrumentationTimeoutMs, 
                boolean checkResults, 
                boolean isHiddenApiCheckDisabled, 
                boolean isTestApiCheckDisabled, 
                boolean isIsolatedStorageDisabled, 
                 instrumentationArgs, 
                 extraListeners)

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.

isTestApiCheckDisabled boolean: whether or not we should disable the test api check.

isIsolatedStorageDisabled boolean: whether or not we should disable isolated storage.

instrumentationArgs : arguments to pass to the instrumentation.

extraListeners

Returns
boolean True if it succeeded 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

setTestInformation

public final void setTestInformation (TestInformation testInformation)

Parameters
testInformation TestInformation

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