The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | package android.test; |
| 2 | |
| 3 | import android.content.ContentProvider; |
| 4 | import android.content.ContentResolver; |
| 5 | import android.content.Context; |
| 6 | import android.test.mock.MockContext; |
| 7 | import android.test.mock.MockContentResolver; |
| 8 | import android.database.DatabaseUtils; |
| 9 | |
| 10 | /** |
| 11 | * If you would like to test a single content provider with an |
| 12 | * {@link InstrumentationTestCase}, this provides some of the boiler plate in {@link #setUp} and |
| 13 | * {@link #tearDown}. |
| 14 | * |
| 15 | * @deprecated this class extends InstrumentationTestCase but should extend AndroidTestCase. Use |
| 16 | * ProviderTestCase2, which corrects this problem, instead. |
| 17 | */ |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 18 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | public abstract class ProviderTestCase<T extends ContentProvider> |
| 20 | extends InstrumentationTestCase { |
| 21 | |
| 22 | Class<T> mProviderClass; |
| 23 | String mProviderAuthority; |
| 24 | |
| 25 | private IsolatedContext mProviderContext; |
| 26 | private MockContentResolver mResolver; |
| 27 | |
| 28 | public ProviderTestCase(Class<T> providerClass, String providerAuthority) { |
| 29 | mProviderClass = providerClass; |
| 30 | mProviderAuthority = providerAuthority; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * The content provider that will be set up for use in each test method. |
| 35 | */ |
| 36 | private T mProvider; |
| 37 | |
| 38 | public T getProvider() { |
| 39 | return mProvider; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected void setUp() throws Exception { |
| 44 | super.setUp(); |
| 45 | |
| 46 | mResolver = new MockContentResolver(); |
| 47 | final String filenamePrefix = "test."; |
| 48 | RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( |
| 49 | new MockContext(), // The context that most methods are delegated to |
| 50 | getInstrumentation().getTargetContext(), // The context that file methods are delegated to |
| 51 | filenamePrefix); |
| 52 | mProviderContext = new IsolatedContext(mResolver, targetContextWrapper); |
| 53 | |
| 54 | mProvider = mProviderClass.newInstance(); |
| 55 | mProvider.attachInfo(mProviderContext, null); |
| 56 | assertNotNull(mProvider); |
| 57 | mResolver.addProvider(mProviderAuthority, getProvider()); |
| 58 | } |
| 59 | |
| 60 | public MockContentResolver getMockContentResolver() { |
| 61 | return mResolver; |
| 62 | } |
| 63 | |
| 64 | public IsolatedContext getMockContext() { |
| 65 | return mProviderContext; |
| 66 | } |
| 67 | |
| 68 | public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql( |
| 69 | Context targetContext, Class<T> providerClass, String authority, |
| 70 | String databaseName, int databaseVersion, String sql) |
| 71 | throws IllegalAccessException, InstantiationException { |
| 72 | final String filenamePrefix = "test."; |
| 73 | MockContentResolver resolver = new MockContentResolver(); |
| 74 | RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( |
| 75 | new MockContext(), // The context that most methods are delegated to |
| 76 | targetContext, // The context that file methods are delegated to |
| 77 | filenamePrefix); |
| 78 | Context context = new IsolatedContext( |
| 79 | resolver, targetContextWrapper); |
| 80 | DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql); |
| 81 | |
| 82 | T provider = providerClass.newInstance(); |
| 83 | provider.attachInfo(context, null); |
| 84 | resolver.addProvider(authority, provider); |
| 85 | |
| 86 | return resolver; |
| 87 | } |
| 88 | } |