ActivityUnitTestCasepublic abstract class ActivityUnitTestCase extends 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 {@link #setUp} and {@link #tearDown}.
If you prefer a functional test, see {@link 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:
- {@link android.app.Activity#createPendingResult(int, Intent, int)}
- {@link android.app.Activity#startActivityIfNeeded(Intent, int)}
- {@link android.app.Activity#startActivityFromChild(Activity, Intent, int)}
- {@link android.app.Activity#startNextMatchingActivity(Intent)}
- {@link android.app.Activity#getCallingActivity()}
- {@link android.app.Activity#getCallingPackage()}
- {@link android.app.Activity#createPendingResult(int, Intent, int)}
- {@link android.app.Activity#getTaskId()}
- {@link android.app.Activity#isTaskRoot()}
- {@link android.app.Activity#moveTaskToBack(boolean)}
The following methods may be called but will not do anything. For test purposes, you can use
the methods {@link #getStartedActivityIntent()} and {@link #getStartedActivityRequest()} to
inspect the parameters that they were called with.
- {@link android.app.Activity#startActivity(Intent)}
- {@link android.app.Activity#startActivityForResult(Intent, int)}
The following methods may be called but will not do anything. For test purposes, you can use
the methods {@link #isFinishCalled()} and {@link #getFinishedActivityRequest()} to inspect the
parameters that they were called with.
- {@link android.app.Activity#finish()}
- {@link android.app.Activity#finishFromChild(Activity child)}
- {@link android.app.Activity#finishActivity(int requestCode)}
|
Fields Summary |
---|
private static final String | TAG | private Class | mActivityClass | private android.content.Context | mActivityContext | private android.app.Application | mApplication | private MockParent | mMockParent | private boolean | mAttached | private boolean | mCreated |
Constructors Summary |
---|
public ActivityUnitTestCase(Class activityClass)
mActivityClass = activityClass;
|
Methods Summary |
---|
public T | getActivity()
return (T) super.getActivity();
| public int | getFinishedActivityRequest()This method will return the request code if the Activity under test called
{@link android.app.Activity#finishActivity(int)}.
if (mMockParent != null) {
return mMockParent.mFinishedActivityRequest;
}
return 0;
| public int | getRequestedOrientation()This method will return the value if your Activity under test calls
{@link android.app.Activity#setRequestedOrientation}.
if (mMockParent != null) {
return mMockParent.mRequestedOrientation;
}
return 0;
| public android.content.Intent | getStartedActivityIntent()This method will return the launch intent if your Activity under test calls
{@link android.app.Activity#startActivity(Intent)} or
{@link android.app.Activity#startActivityForResult(Intent, int)}.
if (mMockParent != null) {
return mMockParent.mStartedActivityIntent;
}
return null;
| public int | getStartedActivityRequest()This method will return the launch request code if your Activity under test calls
{@link android.app.Activity#startActivityForResult(Intent, int)}.
if (mMockParent != null) {
return mMockParent.mStartedActivityRequest;
}
return 0;
| public boolean | isFinishCalled()This method will notify you if the Activity under test called
{@link android.app.Activity#finish()},
{@link android.app.Activity#finishFromChild(Activity)}, or
{@link android.app.Activity#finishActivity(int)}.
if (mMockParent != null) {
return mMockParent.mFinished;
}
return false;
| public void | setActivityContext(android.content.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 {@link #startActivity}. If you wish to
obtain a real Context, as a building block, use getInstrumentation().getTargetContext().
mActivityContext = activityContext;
| public void | setApplication(android.app.Application application)Set the application for use during the test. You must call this function before calling
{@link #startActivity}. If your test does not call this method,
mApplication = application;
| protected void | setUp()
super.setUp();
// default value for target context, as a default
mActivityContext = getInstrumentation().getTargetContext();
| protected T | startActivity(android.content.Intent intent, android.os.Bundle savedInstanceState, java.lang.Object lastNonConfigurationInstance)Start the activity under test, in the same way as if it was started by
{@link android.content.Context#startActivity Context.startActivity()}, providing the
arguments it supplied. When you use this method to start the activity, it will automatically
be stopped by {@link #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.
assertFalse("Activity already created", mCreated);
if (!mAttached) {
assertNotNull(mActivityClass);
setActivity(null);
T newActivity = null;
try {
IBinder token = null;
if (mApplication == null) {
setApplication(new MockApplication());
}
ComponentName cn = new ComponentName(mActivityClass.getPackage().getName(),
mActivityClass.getName());
intent.setComponent(cn);
ActivityInfo info = new ActivityInfo();
CharSequence title = mActivityClass.getName();
mMockParent = new MockParent();
String id = null;
newActivity = (T) getInstrumentation().newActivity(mActivityClass, mActivityContext,
token, mApplication, intent, info, title, mMockParent, id,
lastNonConfigurationInstance);
} catch (Exception e) {
Log.w(TAG, "Catching exception", e);
assertNotNull(newActivity);
}
assertNotNull(newActivity);
setActivity(newActivity);
mAttached = true;
}
T result = getActivity();
if (result != null) {
getInstrumentation().callActivityOnCreate(getActivity(), savedInstanceState);
mCreated = true;
}
return result;
| protected void | tearDown()
setActivity(null);
// Scrub out members - protects against memory leaks in the case where someone
// creates a non-static inner class (thus referencing the test case) and gives it to
// someone else to hold onto
scrubClass(ActivityInstrumentationTestCase.class);
super.tearDown();
|
|