ProviderTestCase2


public abstract class ProviderTestCase2
extends AndroidTestCase

java.lang.Object
   ↳ android.test.AndroidTestCase
     ↳ android.test.ProviderTestCase2<T extends android.content.ContentProvider>


This test case class provides a framework for testing a single ContentProvider and for testing your app code with an isolated content provider. Instead of using the system map of providers that is based on the manifests of other applications, the test case creates its own internal map. It then uses this map to resolve providers given an authority. This allows you to inject test providers and to null out providers that you do not want to use.

This test case also sets up the following mock objects:

This framework is set up automatically by the base class' setUp() method. If you override this method, you must call the super method as the first statement in your override.

In order for their tests to be run, concrete subclasses must provide their own constructor with no arguments. This constructor must call ProviderTestCase2(java.lang.Class, java.lang.String) as its first operation.

For more information on content provider testing, please see Content Provider Testing.

Summary

Inherited fields

protected Context mContext

Public constructors

ProviderTestCase2(Class<T> providerClass, String providerAuthority)

Constructor.

Public methods

MockContentResolver getMockContentResolver()

Gets the MockContentResolver created by this class during initialization.

IsolatedContext getMockContext()

Gets the IsolatedContext created by this class during initialization.

T getProvider()

Returns the content provider created by this class in the setUp() method.

static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(Context targetContext, String filenamePrefix, Class<T> providerClass, String authority, String databaseName, int databaseVersion, String sql)

Creates a new content provider of the same type as that passed to the test case class, with an authority name set to the authority parameter, and using an SQLite database as the underlying data source.

Protected methods

void setUp()

Sets up the environment for the test fixture.

void tearDown()

Tears down the environment for the test fixture.

Inherited methods

void assertActivityRequiresPermission(String packageName, String className, String permission)

Asserts that launching a given activity is protected by a particular permission by attempting to start the activity and validating that a SecurityException is thrown that mentions the permission in its error message.

void assertReadingContentUriRequiresPermission(Uri uri, String permission)

Asserts that reading from the content uri requires a particular permission by querying the uri and ensuring a SecurityException is thrown mentioning the particular permission.

void assertWritingContentUriRequiresPermission(Uri uri, String permission)

Asserts that writing to the content uri requires a particular permission by inserting into the uri and ensuring a SecurityException is thrown mentioning the particular permission.

Context getContext()
Context getTestContext()

Returns the test context that was set via setTestContext(android.content.Context).

void scrubClass(Class<?> testCaseClass)

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

void setContext(Context context)
void setTestContext(Context context)

Test context can be used to access resources from the test's own package as opposed to the resources from the test target package.

void setUp()
void tearDown()
void testAndroidTestCaseSetupProperly()
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

ProviderTestCase2

Added in API level 3
public ProviderTestCase2 (Class<T> providerClass, 
                String providerAuthority)

Constructor.

Parameters
providerClass Class: The class name of the provider under test

providerAuthority String: The provider's authority string

Public methods

getMockContentResolver

Added in API level 3
public MockContentResolver getMockContentResolver ()

Gets the MockContentResolver created by this class during initialization. You must use the methods of this resolver to access the provider under test.

getMockContext

Added in API level 3
public IsolatedContext getMockContext ()

Gets the IsolatedContext created by this class during initialization.

Returns
IsolatedContext The IsolatedContext instance

getProvider

Added in API level 3
public T getProvider ()

Returns the content provider created by this class in the setUp() method.

Returns
T T An instance of the provider class given as a parameter to the test case class.

newResolverWithContentProviderFromSql

Added in API level 3
public static ContentResolver newResolverWithContentProviderFromSql (Context targetContext, 
                String filenamePrefix, 
                Class<T> providerClass, 
                String authority, 
                String databaseName, 
                int databaseVersion, 
                String sql)

Creates a new content provider of the same type as that passed to the test case class, with an authority name set to the authority parameter, and using an SQLite database as the underlying data source. The SQL statement parameter is used to create the database. This method also creates a new MockContentResolver and adds the provider to it.

Both the new provider and the new resolver are put into an IsolatedContext that uses the targetContext parameter for file operations and a MockContext for everything else. The IsolatedContext prepends the filenamePrefix parameter to file, database, and directory names.

This is a convenience method for creating a "mock" provider that can contain test data.

Parameters
targetContext Context: The context to use as the basis of the IsolatedContext

filenamePrefix String: A string that is prepended to file, database, and directory names

providerClass Class: The type of the provider being tested

authority String: The authority string to associated with the test provider

databaseName String: The name assigned to the database

databaseVersion int: The version assigned to the database

sql String: A string containing the SQL statements that are needed to create the desired database and its tables. The format is the same as that generated by the sqlite3 tool's .dump command.

Returns
ContentResolver ContentResolver A new MockContentResolver linked to the provider

Throws
java.lang.IllegalAccessException
java.lang.InstantiationException
IllegalAccessException
InstantiationException

Protected methods

setUp

Added in API level 3
protected void setUp ()

Sets up the environment for the test fixture.

Creates a new MockContentResolver, a new IsolatedContext that isolates the provider's file operations, and a new instance of the provider under test within the isolated environment.

Throws
java.lang.Exception
Exception

tearDown

Added in API level 3
protected void tearDown ()

Tears down the environment for the test fixture.

Calls ContentProvider.shutdown() on the ContentProvider represented by mProvider.

Throws
Exception