FileDocCategorySizeDatePackage
ActivityUnitTestCase.javaAPI DocAndroid 1.5 API13265Wed May 06 22:42:02 BST 2009android.test

ActivityUnitTestCase

public 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)}
  • {@link android.app.Activity#setPersistent(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 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 TgetActivity()

        return (T) super.getActivity();
    
public intgetFinishedActivityRequest()
This method will return the request code if the Activity under test called {@link android.app.Activity#finishActivity(int)}.

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

        if (mMockParent != null) {
            return mMockParent.mFinishedActivityRequest;
        }
        return 0;
    
public intgetRequestedOrientation()
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.IntentgetStartedActivityIntent()
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)}.

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

        if (mMockParent != null) {
            return mMockParent.mStartedActivityIntent;
        }
        return null;
    
public intgetStartedActivityRequest()
This method will return the launch request code if your Activity under test calls {@link android.app.Activity#startActivityForResult(Intent, int)}.

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

        if (mMockParent != null) {
            return mMockParent.mStartedActivityRequest;
        }
        return 0;
    
public booleanisFinishCalled()
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)}.

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

        if (mMockParent != null) {
            return mMockParent.mFinished;
        }
        return false;
    
public voidsetActivityContext(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 voidsetApplication(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,

param
application The Application object that will be injected into the Activity under test.

        mApplication = application;
    
protected voidsetUp()

        super.setUp();

        // default value for target context, as a default
      mActivityContext = getInstrumentation().getTargetContext();
    
protected TstartActivity(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.

param
intent The Intent as if supplied to {@link android.content.Context#startActivity}.
param
savedInstanceState The instance state, if you are simulating this part of the life cycle. Typically null.
param
lastNonConfigurationInstance This Object will be available to the Activity if it calls {@link android.app.Activity#getLastNonConfigurationInstance()}. Typically null.
return
Returns the Activity that was created

        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) {
                assertNotNull(newActivity);
            }
            
            assertNotNull(newActivity);
            setActivity(newActivity);
            
            mAttached = true;
        }
        
        T result = getActivity();
        if (result != null) {
            getInstrumentation().callActivityOnCreate(getActivity(), savedInstanceState);
            mCreated = true;
        }
        return result;
    
protected voidtearDown()

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