blob: a923d2ab659360beec1744dac38e76c046903bea [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001package android.test;
2
3import android.content.ContentProvider;
4import android.content.ContentResolver;
5import android.content.Context;
Ken Shirriff04cc0e12009-07-28 16:15:38 -07006import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007import android.test.mock.MockContext;
8import android.test.mock.MockContentResolver;
9import android.database.DatabaseUtils;
10
11/**
Dianne Hackborn935ae462009-04-13 16:11:55 -070012 * 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 Project9066cfe2009-03-03 19:31:44 -080021 */
22public 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 Shirriff04cc0e12009-07-28 16:15:38 -070030 private class MockContext2 extends MockContext {
31
32 @Override
33 public Resources getResources() {
34 return getContext().getResources();
35 }
36 }
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 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 Shirriff04cc0e12009-07-28 16:15:38 -070059 new MockContext2(), // The context that most methods are delegated to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 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 Hackborn935ae462009-04-13 16:11:55 -070096}