ProviderTestCase2public abstract class ProviderTestCase2 extends AndroidTestCase This TestCase class provides a framework for isolated testing of a single
ContentProvider. It uses a {@link android.test.mock.MockContentResolver} to
access the provider, restricts the provider to an isolated area of the
filesystem (for safely creating & modifying databases & files), and injects
{@link android.test.IsolatedContext} to isolate the ContentProvider from the
rest of the running system.
This environment is created automatically by {@link #setUp} and {@link
#tearDown}. |
Fields Summary |
---|
Class | mProviderClass | String | mProviderAuthority | private IsolatedContext | mProviderContext | private android.test.mock.MockContentResolver | mResolver | private T | mProviderThe content provider that will be set up for use in each test method. |
Constructors Summary |
---|
public ProviderTestCase2(Class providerClass, String providerAuthority)
mProviderClass = providerClass;
mProviderAuthority = providerAuthority;
|
Methods Summary |
---|
public android.test.mock.MockContentResolver | getMockContentResolver()
return mResolver;
| public IsolatedContext | getMockContext()
return mProviderContext;
| public T | getProvider()
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)
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 = providerClass.newInstance();
provider.attachInfo(context, null);
resolver.addProvider(authority, provider);
return resolver;
| protected void | setUp()
super.setUp();
mResolver = new MockContentResolver();
final String filenamePrefix = "test.";
RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
new MockContext(), // The context that most methods are delegated to
getContext(), // The context that file methods are delegated to
filenamePrefix);
mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
mProvider = mProviderClass.newInstance();
mProvider.attachInfo(mProviderContext, null);
assertNotNull(mProvider);
mResolver.addProvider(mProviderAuthority, getProvider());
|
|