ActivityUnitTestCase


public abstract class ActivityUnitTestCase
extends ActivityTestCase

java.lang.Object
   ↳ android.test.InstrumentationTestCase
     ↳ android.test.ActivityTestCase
       ↳ android.test.ActivityUnitTestCase<T extends android.app.Activity>


This class was deprecated in API level 24.
Write Local Unit Tests instead.

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 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(Class<T> activityClass)

Public methods

T getActivity()
int getFinishedActivityRequest()

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

int getRequestedOrientation()

This method will return the value if your Activity under test calls Activity.setRequestedOrientation(int).

Intent getStartedActivityIntent()

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

int getStartedActivityRequest()

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

boolean isFinishCalled()

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

void setActivityContext(Context activityContext)

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

void setApplication(Application application)

Set the application for use during the test.

Protected methods

void setUp()
T startActivity(Intent intent, Bundle savedInstanceState, Object lastNonConfigurationInstance)

Start the activity under test, in the same way as if it was started by Context.startActivity(), providing the arguments it supplied.

void tearDown()

Make sure all resources are cleaned up and garbage collected before moving on to the next test.

Inherited methods

Activity getActivity()
void scrubClass(Class<?> testCaseClass)

This function is called by various TestCase implementations, at tearDown() time, in order to scrub out any class variables.

void setActivity(Activity testActivity)

Set the activity under test.

Instrumentation getInstrumentation()

Inheritors can access the instrumentation using this.

void injectInsrumentation(Instrumentation instrumentation)

This method was deprecated in API level 15. Incorrect spelling, use injectInstrumentation(android.app.Instrumentation) instead.

void injectInstrumentation(Instrumentation instrumentation)

Injects instrumentation into this test case.

final <T extends Activity> T launchActivity(String pkg, Class<T> activityCls, Bundle extras)

Utility method for launching an activity.

final <T extends Activity> T launchActivityWithIntent(String pkg, Class<T> activityCls, Intent intent)

Utility method for launching an activity with a specific Intent.

void runTest()

Runs the current unit test.

void runTestOnUiThread(Runnable r)

Helper for running portions of a test on the UI thread.

void sendKeys(int... keys)

Sends a series of key events through instrumentation and waits for idle.

void sendKeys(String keysSequence)

Sends a series of key events through instrumentation and waits for idle.

void sendRepeatedKeys(int... keys)

Sends a series of key events through instrumentation and waits for idle.

void tearDown()

Make sure all resources are cleaned up and garbage collected before moving on to the next test.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

ActivityUnitTestCase

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

Parameters
activityClass Class

Public methods

getActivity

Added in API level 1
public T getActivity ()

Returns
T Returns the activity under test.

getFinishedActivityRequest

Added in API level 1
public int getFinishedActivityRequest ()

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

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

getRequestedOrientation

Added in API level 1
public int getRequestedOrientation ()

This method will return the value if your Activity under test calls Activity.setRequestedOrientation(int).

Returns
int

getStartedActivityIntent

Added in API level 1
public Intent getStartedActivityIntent ()

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

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

getStartedActivityRequest

Added in API level 1
public int getStartedActivityRequest ()

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

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

isFinishCalled

Added in API level 1
public boolean isFinishCalled ()

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

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

setActivityContext

Added in API level 1
public void setActivityContext (Context activityContext)

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(Intent, Bundle, Object). If you wish to obtain a real Context, as a building block, use getInstrumentation().getTargetContext().

Parameters
activityContext Context

setApplication

Added in API level 1
public void setApplication (Application application)

Set the application for use during the test. You must call this function before calling startActivity(Intent, Bundle, Object). 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 void setUp ()

Throws
Exception

startActivity

Added in API level 1
protected T startActivity (Intent intent, 
                Bundle savedInstanceState, 
                Object lastNonConfigurationInstance)

Start the activity under test, in the same way as if it was started by 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 Context.startActivity(Intent).

savedInstanceState Bundle: The instance state, if you are simulating this part of the life cycle. Typically null.

lastNonConfigurationInstance Object: This Object will be available to the Activity if it calls Activity.getLastNonConfigurationInstance(). Typically null.

Returns
T Returns the Activity that was created

tearDown

Added in API level 1
protected void 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.

Throws
Exception