Build 1.0_r1(from source)

android.test
Class ActivityUnitTestCase<T extends Activity>

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by android.test.InstrumentationTestCase
              extended by android.test.ActivityTestCase
                  extended by android.test.ActivityUnitTestCase<T>
All Implemented Interfaces:
Test

public abstract class ActivityUnitTestCase<T extends Activity>
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 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.


Constructor Summary
ActivityUnitTestCase(Class<T> activityClass)
           
 
Method Summary
 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  void setUp()
          Sets up the fixture, for example, open a network connection.
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.
protected  void tearDown()
          Make sure all resources are cleaned up and garbage collected before moving on to the next test.
 
Methods inherited from class android.test.ActivityTestCase
scrubClass, setActivity
 
Methods inherited from class android.test.InstrumentationTestCase
getInstrumentation, injectInsrumentation, launchActivity, runTest, sendKeys, sendKeys, sendRepeatedKeys
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ActivityUnitTestCase

public ActivityUnitTestCase(Class<T> activityClass)
Method Detail

getActivity

public T getActivity()
Overrides:
getActivity in class ActivityTestCase
Returns:
Returns the activity under test.

setUp

protected void setUp()
              throws Exception
Description copied from class: TestCase
Sets up the fixture, for example, open a network connection. This method is called before a test is executed.

Overrides:
setUp in class TestCase
Throws:
Exception

startActivity

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 - The Intent as if supplied to Context.startActivity(android.content.Intent).
savedInstanceState - The instance state, if you are simulating this part of the life cycle. Typically null.
lastNonConfigurationInstance - This Object will be available to the Activity if it calls Activity.getLastNonConfigurationInstance(). Typically null.
Returns:
Returns the Activity that was created

tearDown

protected void tearDown()
                 throws Exception
Description copied from class: InstrumentationTestCase
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.

Overrides:
tearDown in class InstrumentationTestCase
Throws:
Exception

setApplication

public void setApplication(Application application)
Set the application for use during the test. You must call this function before calling startActivity(android.content.Intent, android.os.Bundle, java.lang.Object). If your test does not call this method,

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

setActivityContext

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


getRequestedOrientation

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


getStartedActivityIntent

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:
The Intent provided in the start call, or null if no start call was made.

getStartedActivityRequest

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

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

isFinishCalled

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

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

getFinishedActivityRequest

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

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

Build 1.0_r1(from source)

Please submit a feedback, bug or feature