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; |
Ken Shirriff | 04cc0e1 | 2009-07-28 16:15:38 -0700 | [diff] [blame] | 6 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | import android.test.mock.MockContext; |
| 8 | import android.test.mock.MockContentResolver; |
| 9 | import android.database.DatabaseUtils; |
| 10 | |
| 11 | /** |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame] | 12 | * This TestCase class provides a framework for isolated testing of a single |
| 13 | * ContentProvider. It uses a {@link android.test.mock.MockContentResolver} to |
| 14 | * access the provider, restricts the provider to an isolated area of the |
| 15 | * filesystem (for safely creating & modifying databases & files), and injects |
| 16 | * {@link android.test.IsolatedContext} to isolate the ContentProvider from the |
| 17 | * rest of the running system. |
| 18 | * |
| 19 | * <p>This environment is created automatically by {@link #setUp} and {@link |
| 20 | * #tearDown}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | */ |
| 22 | public abstract class ProviderTestCase2<T extends ContentProvider> extends AndroidTestCase { |
| 23 | |
| 24 | Class<T> mProviderClass; |
| 25 | String mProviderAuthority; |
| 26 | |
| 27 | private IsolatedContext mProviderContext; |
| 28 | private MockContentResolver mResolver; |
| 29 | |
Ken Shirriff | 04cc0e1 | 2009-07-28 16:15:38 -0700 | [diff] [blame] | 30 | private class MockContext2 extends MockContext { |
| 31 | |
| 32 | @Override |
| 33 | public Resources getResources() { |
| 34 | return getContext().getResources(); |
| 35 | } |
| 36 | } |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | public ProviderTestCase2(Class<T> providerClass, String providerAuthority) { |
| 39 | mProviderClass = providerClass; |
| 40 | mProviderAuthority = providerAuthority; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * The content provider that will be set up for use in each test method. |
| 45 | */ |
| 46 | private T mProvider; |
| 47 | |
| 48 | public T getProvider() { |
| 49 | return mProvider; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected void setUp() throws Exception { |
| 54 | super.setUp(); |
| 55 | |
| 56 | mResolver = new MockContentResolver(); |
| 57 | final String filenamePrefix = "test."; |
| 58 | RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( |
Ken Shirriff | 04cc0e1 | 2009-07-28 16:15:38 -0700 | [diff] [blame] | 59 | new MockContext2(), // The context that most methods are delegated to |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | getContext(), // The context that file methods are delegated to |
| 61 | filenamePrefix); |
| 62 | mProviderContext = new IsolatedContext(mResolver, targetContextWrapper); |
| 63 | |
| 64 | mProvider = mProviderClass.newInstance(); |
| 65 | mProvider.attachInfo(mProviderContext, null); |
| 66 | assertNotNull(mProvider); |
| 67 | mResolver.addProvider(mProviderAuthority, getProvider()); |
| 68 | } |
| 69 | |
| 70 | public MockContentResolver getMockContentResolver() { |
| 71 | return mResolver; |
| 72 | } |
| 73 | |
| 74 | public IsolatedContext getMockContext() { |
| 75 | return mProviderContext; |
| 76 | } |
| 77 | |
| 78 | public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql( |
| 79 | Context targetContext, String filenamePrefix, Class<T> providerClass, String authority, |
| 80 | String databaseName, int databaseVersion, String sql) |
| 81 | throws IllegalAccessException, InstantiationException { |
| 82 | MockContentResolver resolver = new MockContentResolver(); |
| 83 | RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( |
| 84 | new MockContext(), // The context that most methods are delegated to |
| 85 | targetContext, // The context that file methods are delegated to |
| 86 | filenamePrefix); |
| 87 | Context context = new IsolatedContext(resolver, targetContextWrapper); |
| 88 | DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql); |
| 89 | |
| 90 | T provider = providerClass.newInstance(); |
| 91 | provider.attachInfo(context, null); |
| 92 | resolver.addProvider(authority, provider); |
| 93 | |
| 94 | return resolver; |
| 95 | } |
Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame] | 96 | } |