public abstract class ProviderTestCase2<T extends ContentProvider> extends AndroidTestCase
ContentProviderand 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:
IsolatedContextthat stubs out Context methods that might affect the rest of the running system, while allowing tests to do real file and database work.
MockContentResolverthat provides the functionality of a regular content resolver, but uses
IsolatedContext. It stubs out
ContentResolver.notifyChange(Uri, ContentObserver, boolean)to prevent the test from affecting the running system.
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
In order for their tests to be run, concrete subclasses must provide their own
constructor with no arguments. This constructor must call
ProviderTestCase2(Class, String) as its first operation.
|Constructor and Description|
|Modifier and Type||Method and Description|
Returns the content provider created by this class in the
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.
Sets up the environment for the test fixture.
Tears down the environment for the test fixture.
assertActivityRequiresPermission, assertReadingContentUriRequiresPermission, assertWritingContentUriRequiresPermission, getContext, getTestContext, scrubClass, setContext, setTestContext, testAndroidTestCaseSetupProperly
public T getProvider()
protected void setUp() throws Exception
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.
protected void tearDown() throws Exception
public MockContentResolver getMockContentResolver()
MockContentResolvercreated by this class during initialization. You must use the methods of this resolver to access the provider under test.
public IsolatedContext getMockContext()
IsolatedContextcreated by this class during initialization.
public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(Context targetContext, String filenamePrefix, Class<T> providerClass, String authority, String databaseName, int databaseVersion, String sql) throws IllegalAccessException, InstantiationException
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
that uses the targetContext parameter for file operations and a
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.
targetContext- The context to use as the basis of the IsolatedContext
filenamePrefix- A string that is prepended to file, database, and directory names
providerClass- The type of the provider being tested
authority- The authority string to associated with the test provider
databaseName- The name assigned to the database
databaseVersion- The version assigned to the database
sql- 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
MockContentResolverlinked to the provider