FileDocCategorySizeDatePackage
ProviderTestCase2.javaAPI DocAndroid 1.5 API3360Wed May 06 22:42:02 BST 2009android.test

ProviderTestCase2

public 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
mProvider
The 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.MockContentResolvergetMockContentResolver()

        return mResolver;
    
public IsolatedContextgetMockContext()

        return mProviderContext;
    
public TgetProvider()

        return mProvider;
    
public static android.content.ContentResolvernewResolverWithContentProviderFromSql(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 voidsetUp()

        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());