ProviderTestCase2public abstract class ProviderTestCase2 extends AndroidTestCase This test case class provides a framework for testing a single
{@link 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:
-
An {@link android.test.IsolatedContext} that stubs out Context methods that might
affect the rest of the running system, while allowing tests to do real file and
database work.
-
A {@link android.test.mock.MockContentResolver} that provides the functionality of a
regular content resolver, but uses {@link IsolatedContext}. It stubs out
{@link ContentResolver#notifyChange(Uri, ContentObserver, boolean)} to
prevent the test from affecting the running system.
-
An instance of the provider under test, running in an {@link IsolatedContext}.
This framework is set up automatically by the base class' {@link #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
{@link #ProviderTestCase2(Class, String)} as its first operation.
For more information on content provider testing, please see
Content Provider Testing. |
Fields Summary |
---|
Class | mProviderClass | String | mProviderAuthority | private IsolatedContext | mProviderContext | private android.test.mock.MockContentResolver | mResolver | private T | mProvider |
Constructors Summary |
---|
public ProviderTestCase2(Class providerClass, String providerAuthority)Constructor.
mProviderClass = providerClass;
mProviderAuthority = providerAuthority;
|
Methods Summary |
---|
static T | createProviderForTest(android.content.Context context, java.lang.Class providerClass, java.lang.String authority)Creates and sets up a new instance of the provider.
T instance = providerClass.newInstance();
ProviderInfo providerInfo = new ProviderInfo();
providerInfo.authority = authority;
instance.attachInfoForTesting(context, providerInfo);
return instance;
| public android.test.mock.MockContentResolver | getMockContentResolver()Gets the {@link MockContentResolver} created by this class during initialization. You
must use the methods of this resolver to access the provider under test.
return mResolver;
| public IsolatedContext | getMockContext()Gets the {@link IsolatedContext} created by this class during initialization.
return mProviderContext;
| public T | getProvider()Returns the content provider created by this class in the {@link #setUp()} method.
return mProvider;
| public static android.content.ContentResolver | newResolverWithContentProviderFromSql(android.content.Context targetContext, java.lang.String filenamePrefix, java.lang.Class providerClass, java.lang.String authority, java.lang.String databaseName, int databaseVersion, java.lang.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 {@link MockContentResolver} and adds the provider to it.
Both the new provider and the new resolver are put into an {@link IsolatedContext}
that uses the targetContext parameter for file operations and a {@link 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.
MockContentResolver resolver = new MockContentResolver();
RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
new MockContext(), // The context that most methods are delegated to
targetContext, // The context that file methods are delegated to
filenamePrefix);
Context context = new IsolatedContext(resolver, targetContextWrapper);
DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
T provider = createProviderForTest(context, providerClass, authority);
resolver.addProvider(authority, provider);
return resolver;
| protected void | setUp()Sets up the environment for the test fixture.
Creates a new
{@link android.test.mock.MockContentResolver}, a new IsolatedContext
that isolates the provider's file operations, and a new instance of
the provider under test within the isolated environment.
super.setUp();
mResolver = new MockContentResolver();
final String filenamePrefix = "test.";
RenamingDelegatingContext targetContextWrapper = new
RenamingDelegatingContext(
new MockContext2(), // The context that most methods are
//delegated to
getContext(), // The context that file methods are delegated to
filenamePrefix);
mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
mProvider = createProviderForTest(mProviderContext, mProviderClass, mProviderAuthority);
mResolver.addProvider(mProviderAuthority, getProvider());
| protected void | tearDown()Tears down the environment for the test fixture.
Calls {@link android.content.ContentProvider#shutdown()} on the
{@link android.content.ContentProvider} represented by mProvider.
mProvider.shutdown();
super.tearDown();
|
|