blob: be18b53059f3860a9436d69fd09343d0a8c1aa01 [file] [log] [blame]
Kenny Root10362ab2010-03-12 11:13:50 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.test;
18
19import android.content.ContentProvider;
20import android.content.ContentResolver;
21import android.content.Context;
Akos Ludanyi9189c4c2014-09-04 14:08:49 +020022import android.content.pm.ProviderInfo;
Ken Shirriff04cc0e12009-07-28 16:15:38 -070023import android.content.res.Resources;
Paul Duffin4ea70a22018-01-05 13:52:17 +000024import android.test.mock.MockContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.test.mock.MockContext;
26import android.test.mock.MockContentResolver;
27import android.database.DatabaseUtils;
28
Paul Westbrook10481082010-02-11 10:33:12 -080029import java.io.File;
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031/**
Joe Malin7d433aa2010-05-31 14:37:28 -070032 * This test case class provides a framework for testing a single
33 * {@link ContentProvider} and for testing your app code with an
34 * isolated content provider. Instead of using the system map of
35 * providers that is based on the manifests of other applications, the test
36 * case creates its own internal map. It then uses this map to resolve providers
37 * given an authority. This allows you to inject test providers and to null out
38 * providers that you do not want to use.
39 * <p>
40 * This test case also sets up the following mock objects:
41 * </p>
42 * <ul>
43 * <li>
44 * An {@link android.test.IsolatedContext} that stubs out Context methods that might
45 * affect the rest of the running system, while allowing tests to do real file and
46 * database work.
47 * </li>
48 * <li>
49 * A {@link android.test.mock.MockContentResolver} that provides the functionality of a
50 * regular content resolver, but uses {@link IsolatedContext}. It stubs out
51 * {@link ContentResolver#notifyChange(Uri, ContentObserver, boolean)} to
52 * prevent the test from affecting the running system.
53 * </li>
54 * <li>
55 * An instance of the provider under test, running in an {@link IsolatedContext}.
56 * </li>
57 * </ul>
58 * <p>
59 * This framework is set up automatically by the base class' {@link #setUp()} method. If you
60 * override this method, you must call the super method as the first statement in
61 * your override.
62 * </p>
63 * <p>
64 * In order for their tests to be run, concrete subclasses must provide their own
65 * constructor with no arguments. This constructor must call
66 * {@link #ProviderTestCase2(Class, String)} as its first operation.
67 * </p>
68 * For more information on content provider testing, please see
Scott Mainfe3b1cb22012-07-24 18:13:11 -070069 * <a href="{@docRoot}tools/testing/contentprovider_testing.html">Content Provider Testing</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 */
71public abstract class ProviderTestCase2<T extends ContentProvider> extends AndroidTestCase {
72
73 Class<T> mProviderClass;
74 String mProviderAuthority;
75
76 private IsolatedContext mProviderContext;
77 private MockContentResolver mResolver;
78
Makoto Onuki8e342032010-09-07 14:27:25 -070079 private class MockContext2 extends MockContext {
Ken Shirriff04cc0e12009-07-28 16:15:38 -070080
81 @Override
82 public Resources getResources() {
83 return getContext().getResources();
84 }
Paul Westbrook10481082010-02-11 10:33:12 -080085
86 @Override
87 public File getDir(String name, int mode) {
Joe Malin7d433aa2010-05-31 14:37:28 -070088 // name the directory so the directory will be separated from
Paul Westbrook10481082010-02-11 10:33:12 -080089 // one created through the regular Context
90 return getContext().getDir("mockcontext2_" + name, mode);
91 }
Makoto Onuki8e342032010-09-07 14:27:25 -070092
93 @Override
94 public Context getApplicationContext() {
95 return this;
96 }
Ken Shirriff04cc0e12009-07-28 16:15:38 -070097 }
Joe Malin7d433aa2010-05-31 14:37:28 -070098 /**
99 * Constructor.
100 *
101 * @param providerClass The class name of the provider under test
102 * @param providerAuthority The provider's authority string
103 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 public ProviderTestCase2(Class<T> providerClass, String providerAuthority) {
105 mProviderClass = providerClass;
106 mProviderAuthority = providerAuthority;
107 }
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 private T mProvider;
110
Joe Malin7d433aa2010-05-31 14:37:28 -0700111 /**
112 * Returns the content provider created by this class in the {@link #setUp()} method.
113 * @return T An instance of the provider class given as a parameter to the test case class.
114 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public T getProvider() {
116 return mProvider;
117 }
118
Joe Malin7d433aa2010-05-31 14:37:28 -0700119 /**
120 * Sets up the environment for the test fixture.
121 * <p>
122 * Creates a new
123 * {@link android.test.mock.MockContentResolver}, a new IsolatedContext
124 * that isolates the provider's file operations, and a new instance of
125 * the provider under test within the isolated environment.
126 * </p>
127 *
128 * @throws Exception
129 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 @Override
131 protected void setUp() throws Exception {
132 super.setUp();
133
134 mResolver = new MockContentResolver();
135 final String filenamePrefix = "test.";
Joe Malin7d433aa2010-05-31 14:37:28 -0700136 RenamingDelegatingContext targetContextWrapper = new
137 RenamingDelegatingContext(
138 new MockContext2(), // The context that most methods are
139 //delegated to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 getContext(), // The context that file methods are delegated to
141 filenamePrefix);
142 mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
Akos Ludanyi9189c4c2014-09-04 14:08:49 +0200143 mProvider = createProviderForTest(mProviderContext, mProviderClass, mProviderAuthority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 mResolver.addProvider(mProviderAuthority, getProvider());
145 }
146
Joe Malin7d433aa2010-05-31 14:37:28 -0700147 /**
Akos Ludanyi9189c4c2014-09-04 14:08:49 +0200148 * Creates and sets up a new instance of the provider.
149 */
150 static <T extends ContentProvider> T createProviderForTest(
151 Context context, Class<T> providerClass, String authority)
152 throws IllegalAccessException, InstantiationException {
153 T instance = providerClass.newInstance();
154 ProviderInfo providerInfo = new ProviderInfo();
155 providerInfo.authority = authority;
Paul Duffin4ea70a22018-01-05 13:52:17 +0000156 MockContentProvider.attachInfoForTesting(instance, context, providerInfo);
Akos Ludanyi9189c4c2014-09-04 14:08:49 +0200157 return instance;
158 }
159
160 /**
Vasu Nori0c9e14a2010-08-04 13:31:48 -0700161 * Tears down the environment for the test fixture.
162 * <p>
163 * Calls {@link android.content.ContentProvider#shutdown()} on the
Vasu Norib6d14372010-08-04 18:05:00 -0700164 * {@link android.content.ContentProvider} represented by mProvider.
Vasu Nori0c9e14a2010-08-04 13:31:48 -0700165 */
166 @Override
167 protected void tearDown() throws Exception {
168 mProvider.shutdown();
169 super.tearDown();
170 }
171
172 /**
Joe Malin7d433aa2010-05-31 14:37:28 -0700173 * Gets the {@link MockContentResolver} created by this class during initialization. You
174 * must use the methods of this resolver to access the provider under test.
175 *
176 * @return A {@link MockContentResolver} instance.
177 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public MockContentResolver getMockContentResolver() {
179 return mResolver;
180 }
181
Joe Malin7d433aa2010-05-31 14:37:28 -0700182 /**
183 * Gets the {@link IsolatedContext} created by this class during initialization.
184 * @return The {@link IsolatedContext} instance
185 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public IsolatedContext getMockContext() {
187 return mProviderContext;
188 }
189
Joe Malin7d433aa2010-05-31 14:37:28 -0700190 /**
191 * <p>
192 * Creates a new content provider of the same type as that passed to the test case class,
193 * with an authority name set to the authority parameter, and using an SQLite database as
194 * the underlying data source. The SQL statement parameter is used to create the database.
195 * This method also creates a new {@link MockContentResolver} and adds the provider to it.
196 * </p>
197 * <p>
198 * Both the new provider and the new resolver are put into an {@link IsolatedContext}
199 * that uses the targetContext parameter for file operations and a {@link MockContext}
200 * for everything else. The IsolatedContext prepends the filenamePrefix parameter to
201 * file, database, and directory names.
202 * </p>
203 * <p>
204 * This is a convenience method for creating a "mock" provider that can contain test data.
205 * </p>
206 *
207 * @param targetContext The context to use as the basis of the IsolatedContext
208 * @param filenamePrefix A string that is prepended to file, database, and directory names
209 * @param providerClass The type of the provider being tested
210 * @param authority The authority string to associated with the test provider
211 * @param databaseName The name assigned to the database
212 * @param databaseVersion The version assigned to the database
213 * @param sql A string containing the SQL statements that are needed to create the desired
214 * database and its tables. The format is the same as that generated by the
215 * <a href="http://www.sqlite.org/sqlite.html">sqlite3</a> tool's <code>.dump</code> command.
216 * @return ContentResolver A new {@link MockContentResolver} linked to the provider
217 *
218 * @throws IllegalAccessException
219 * @throws InstantiationException
220 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(
222 Context targetContext, String filenamePrefix, Class<T> providerClass, String authority,
223 String databaseName, int databaseVersion, String sql)
224 throws IllegalAccessException, InstantiationException {
225 MockContentResolver resolver = new MockContentResolver();
226 RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
227 new MockContext(), // The context that most methods are delegated to
228 targetContext, // The context that file methods are delegated to
229 filenamePrefix);
230 Context context = new IsolatedContext(resolver, targetContextWrapper);
231 DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
232
Akos Ludanyi9189c4c2014-09-04 14:08:49 +0200233 T provider = createProviderForTest(context, providerClass, authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 resolver.addProvider(authority, provider);
235
236 return resolver;
237 }
Dianne Hackborn935ae462009-04-13 16:11:55 -0700238}