Added in API level 1
Deprecated in API level 24

ActivityUnitTestCase


abstract class ActivityUnitTestCase<T : Activity!> : ActivityTestCase

This class provides isolated testing of a single activity. The activity under test will be created with minimal connection to the system infrastructure, and you can inject mocked or wrappered versions of many of Activity's dependencies. Most of the work is handled automatically here by setUp and tearDown.

If you prefer a functional test, see android.test.ActivityInstrumentationTestCase.

It must be noted that, as a true unit test, your Activity will not be running in the normal system and will not participate in the normal interactions with other Activities. The following methods should not be called in this configuration - most of them will throw exceptions:

The following methods may be called but will not do anything. For test purposes, you can use the methods getStartedActivityIntent() and getStartedActivityRequest() to inspect the parameters that they were called with.

The following methods may be called but will not do anything. For test purposes, you can use the methods isFinishCalled() and getFinishedActivityRequest() to inspect the parameters that they were called with.

Summary

Public constructors
ActivityUnitTestCase(activityClass: Class<T>!)

Public methods
open T

open Int

This method will return the request code if the Activity under test called android.app.Activity#finishActivity(int).

open Int

This method will return the value if your Activity under test calls android.app.Activity#setRequestedOrientation.

open Intent!

This method will return the launch intent if your Activity under test calls android.

open Int

This method will return the launch request code if your Activity under test calls android.app.Activity#startActivityForResult(Intent, int).

open Boolean

This method will notify you if the Activity under test called android.app.Activity#finish(), android.app.Activity#finishFromChild(Activity), or android.app.Activity#finishActivity(int).

open Unit
setActivityContext(activityContext: Context!)

If you wish to inject a Mock, Isolated, or otherwise altered context, you can do so here.

open Unit
setApplication(application: Application!)

Set the application for use during the test.

Protected methods
open Unit

open T
startActivity(intent: Intent!, savedInstanceState: Bundle!, lastNonConfigurationInstance: Any!)

Start the activity under test, in the same way as if it was started by android.

open Unit

Inherited functions
Instrumentation! getInstrumentation()

Inheritors can access the instrumentation using this.

Unit injectInsrumentation(instrumentation: Instrumentation!)

Injects instrumentation into this test case. This method is called by the test runner during test setup.

Unit injectInstrumentation(instrumentation: Instrumentation!)

Injects instrumentation into this test case. This method is called by the test runner during test setup.

T launchActivity(pkg: String!, activityCls: Class<T>!, extras: Bundle!)

Utility method for launching an activity.

The Intent used to launch the Activity is: action = Intent.ACTION_MAIN extras = null, unless a custom bundle is provided here All other fields are null or empty.

NOTE: The parameter pkg must refer to the package identifier of the package hosting the activity to be launched, which is specified in the AndroidManifest.xml file. This is not necessarily the same as the java package name.

T launchActivityWithIntent(pkg: String!, activityCls: Class<T>!, intent: Intent!)

Utility method for launching an activity with a specific Intent.

NOTE: The parameter pkg must refer to the package identifier of the package hosting the activity to be launched, which is specified in the AndroidManifest.xml file. This is not necessarily the same as the java package name.

Unit runTest()

Runs the current unit test. If the unit test is annotated with android.test.UiThreadTest, the test is run on the UI thread.

Unit runTestOnUiThread(r: Runnable!)

Helper for running portions of a test on the UI thread. Note, in most cases it is simpler to annotate the test method with android.test.UiThreadTest, which will run the entire test method on the UI thread. Use this method if you need to switch in and out of the UI thread to perform your test.

Unit sendKeys(vararg keys: Int)

Sends a series of key events through instrumentation and waits for idle. For instance: sendKeys(KEYCODE_DPAD_LEFT, KEYCODE_DPAD_CENTER).

Unit sendKeys(keysSequence: String!)

Sends a series of key events through instrumentation and waits for idle. The sequence of keys is a string containing the key names as specified in KeyEvent, without the KEYCODE_ prefix. For instance: sendKeys("DPAD_LEFT A B C DPAD_CENTER"). Each key can be repeated by using the N* prefix. For instance, to send two KEYCODE_DPAD_LEFT, use the following: sendKeys("2*DPAD_LEFT").

Unit sendRepeatedKeys(vararg keys: Int)

Sends a series of key events through instrumentation and waits for idle. Each key code must be preceded by the number of times the key code must be sent. For instance: sendRepeatedKeys(1, KEYCODE_DPAD_CENTER, 2, KEYCODE_DPAD_LEFT).

Unit tearDown()

Make sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.

Unit scrubClass(testCaseClass: Class<*>!)

This function is called by various TestCase implementations, at tearDown() time, in order to scrub out any class variables. This protects against memory leaks in the case where a test case creates a non-static inner class (thus referencing the test case) and gives it to someone else to hold onto.

Unit setActivity(testActivity: Activity!)

Set the activity under test.

Public constructors

ActivityUnitTestCase

Added in API level 1
ActivityUnitTestCase(activityClass: Class<T>!)

Public methods

getActivity

Added in API level 1
open fun getActivity(): T

Deprecated: Deprecated in Java.

Return
T Returns the activity under test.

getFinishedActivityRequest

Added in API level 1
open fun getFinishedActivityRequest(): Int

Deprecated: Deprecated in Java.

This method will return the request code if the Activity under test called android.app.Activity#finishActivity(int).

Return
Int The request code provided in the start call, or -1 if no finish call was made.

getRequestedOrientation

Added in API level 1
open fun getRequestedOrientation(): Int

Deprecated: Deprecated in Java.

This method will return the value if your Activity under test calls android.app.Activity#setRequestedOrientation.

getStartedActivityIntent

Added in API level 1
open fun getStartedActivityIntent(): Intent!

Deprecated: Deprecated in Java.

This method will return the launch intent if your Activity under test calls android.app.Activity#startActivity(Intent) or android.app.Activity#startActivityForResult(Intent, int).

Return
Intent! The Intent provided in the start call, or null if no start call was made.

getStartedActivityRequest

Added in API level 1
open fun getStartedActivityRequest(): Int

Deprecated: Deprecated in Java.

This method will return the launch request code if your Activity under test calls android.app.Activity#startActivityForResult(Intent, int).

Return
Int The request code provided in the start call, or -1 if no start call was made.

isFinishCalled

Added in API level 1
open fun isFinishCalled(): Boolean

Deprecated: Deprecated in Java.

This method will notify you if the Activity under test called android.app.Activity#finish(), android.app.Activity#finishFromChild(Activity), or android.app.Activity#finishActivity(int).

Return
Boolean Returns true if one of the listed finish methods was called.

setActivityContext

Added in API level 1
open fun setActivityContext(activityContext: Context!): Unit

Deprecated: Deprecated in Java.

If you wish to inject a Mock, Isolated, or otherwise altered context, you can do so here. You must call this function before calling startActivity. If you wish to obtain a real Context, as a building block, use getInstrumentation().getTargetContext().

setApplication

Added in API level 1
open fun setApplication(application: Application!): Unit

Deprecated: Deprecated in Java.

Set the application for use during the test. You must call this function before calling startActivity. If your test does not call this method,

Parameters
application Application!: The Application object that will be injected into the Activity under test.

Protected methods

setUp

Added in API level 1
protected open fun setUp(): Unit

Deprecated: Deprecated in Java.

startActivity

Added in API level 1
protected open fun startActivity(
    intent: Intent!,
    savedInstanceState: Bundle!,
    lastNonConfigurationInstance: Any!
): T

Deprecated: Deprecated in Java.

Start the activity under test, in the same way as if it was started by android.content.Context#startActivity, providing the arguments it supplied. When you use this method to start the activity, it will automatically be stopped by tearDown.

This method will call onCreate(), but if you wish to further exercise Activity life cycle methods, you must call them yourself from your test case.

Do not call from your setUp() method. You must call this method from each of your test methods.

Parameters
intent Intent!: The Intent as if supplied to android.content.Context#startActivity.
savedInstanceState Bundle!: The instance state, if you are simulating this part of the life cycle. Typically null.
lastNonConfigurationInstance Any!: This Object will be available to the Activity if it calls android.app.Activity#getLastNonConfigurationInstance(). Typically null.
Return
T Returns the Activity that was created

tearDown

Added in API level 1
protected open fun tearDown(): Unit

Deprecated: Deprecated in Java.

Exceptions
java.lang.Exception