blob: 85e5916a63df90b4b9ba8030936a861b4028d8cc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +09002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
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
17package android.test.mock;
18
Jeff Sharkey9edef252019-05-20 14:00:17 -060019import android.annotation.NonNull;
Steve McKayea93fe72016-12-02 11:35:35 -080020import android.annotation.Nullable;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090021import android.content.ContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070022import android.content.ContentProviderOperation;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090023import android.content.ContentProviderResult;
24import android.content.ContentValues;
25import android.content.Context;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090026import android.content.IContentProvider;
Jeff Sharkey9edef252019-05-20 14:00:17 -060027import android.content.Intent;
Fred Quintana89437372009-05-15 15:10:40 -070028import android.content.OperationApplicationException;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090029import android.content.pm.PathPermission;
30import android.content.pm.ProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.res.AssetFileDescriptor;
32import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.net.Uri;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080034import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070036import android.os.ICancellationSignal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.ParcelFileDescriptor;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090038import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import java.io.FileNotFoundException;
Fred Quintana03d94902009-05-22 14:23:31 -070041import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43/**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090044 * Mock implementation of ContentProvider. All methods are non-functional and throw
45 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 * implement behavior needed for tests.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090048public class MockContentProvider extends ContentProvider {
49 /*
50 * Note: if you add methods to ContentProvider, you must add similar methods to
51 * MockContentProvider.
52 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090054 /**
55 * IContentProvider that directs all calls to this MockContentProvider.
56 */
57 private class InversionIContentProvider implements IContentProvider {
Jeff Brownd2183652011-10-09 12:39:53 -070058 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -070059 public ContentProviderResult[] applyBatch(String callingPackage,
60 @Nullable String featureId, String authority,
Dianne Hackborn35654b62013-01-14 17:38:02 -080061 ArrayList<ContentProviderOperation> operations)
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090062 throws RemoteException, OperationApplicationException {
Jeff Sharkey633a13e2018-12-07 12:00:45 -070063 return MockContentProvider.this.applyBatch(authority, operations);
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090064 }
65
Jeff Brownd2183652011-10-09 12:39:53 -070066 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -070067 public int bulkInsert(String callingPackage, @Nullable String featureId, Uri url,
68 ContentValues[] initialValues) throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090069 return MockContentProvider.this.bulkInsert(url, initialValues);
70 }
71
Jeff Brownd2183652011-10-09 12:39:53 -070072 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -070073 public int delete(String callingPackage, @Nullable String featureId, Uri url,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -070074 Bundle extras) throws RemoteException {
75 return MockContentProvider.this.delete(url, extras);
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090076 }
77
Jeff Brownd2183652011-10-09 12:39:53 -070078 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090079 public String getType(Uri url) throws RemoteException {
80 return MockContentProvider.this.getType(url);
81 }
82
Jeff Brownd2183652011-10-09 12:39:53 -070083 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -070084 public Uri insert(String callingPackage, @Nullable String featureId, Uri url,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -070085 ContentValues initialValues, Bundle extras) throws RemoteException {
86 return MockContentProvider.this.insert(url, initialValues, extras);
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090087 }
88
Jeff Brownd2183652011-10-09 12:39:53 -070089 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -070090 public AssetFileDescriptor openAssetFile(String callingPackage,
91 @Nullable String featureId, Uri url, String mode, ICancellationSignal signal)
Dianne Hackborn35654b62013-01-14 17:38:02 -080092 throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090093 return MockContentProvider.this.openAssetFile(url, mode);
94 }
95
Jeff Brownd2183652011-10-09 12:39:53 -070096 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -070097 public ParcelFileDescriptor openFile(String callingPackage, @Nullable String featureId,
98 Uri url, String mode, ICancellationSignal signal, IBinder callerToken)
99 throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900100 return MockContentProvider.this.openFile(url, mode);
101 }
102
Jeff Brownd2183652011-10-09 12:39:53 -0700103 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700104 public Cursor query(String callingPackage, @Nullable String featureId, Uri url,
105 @Nullable String[] projection, @Nullable Bundle queryArgs,
106 @Nullable ICancellationSignal cancellationSignal) throws RemoteException {
Steve McKayea93fe72016-12-02 11:35:35 -0800107 return MockContentProvider.this.query(url, projection, queryArgs, null);
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900108 }
109
Jeff Brownd2183652011-10-09 12:39:53 -0700110 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700111 public int update(String callingPackage, @Nullable String featureId, Uri url,
Jeff Sharkeye9fe1522019-11-15 12:45:15 -0700112 ContentValues values, Bundle extras) throws RemoteException {
113 return MockContentProvider.this.update(url, values, extras);
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900114 }
115
Jeff Brownd2183652011-10-09 12:39:53 -0700116 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700117 public Bundle call(String callingPackage, @Nullable String featureId, String authority,
118 String method, String request, Bundle args) throws RemoteException {
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700119 return MockContentProvider.this.call(authority, method, request, args);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800120 }
121
Jeff Brownd2183652011-10-09 12:39:53 -0700122 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900123 public IBinder asBinder() {
Sudheer Shankafe7668a2018-12-16 15:52:33 -0800124 return MockContentProvider.this.getIContentProviderBinder();
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900125 }
126
Jeff Brownd2183652011-10-09 12:39:53 -0700127 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700128 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
129 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
130 }
131
Jeff Brownd2183652011-10-09 12:39:53 -0700132 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700133 public AssetFileDescriptor openTypedAssetFile(String callingPackage,
134 @Nullable String featureId, Uri url, String mimeType, Bundle opts,
135 ICancellationSignal signal) throws RemoteException, FileNotFoundException {
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700136 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
137 }
Jeff Brown75ea64f2012-01-25 19:37:13 -0800138
139 @Override
Jeff Brown4c1241d2012-02-02 17:05:00 -0800140 public ICancellationSignal createCancellationSignal() throws RemoteException {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800141 return null;
142 }
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700143
144 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700145 public Uri canonicalize(String callingPkg, @Nullable String featureId, Uri uri)
146 throws RemoteException {
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700147 return MockContentProvider.this.canonicalize(uri);
148 }
149
150 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700151 public Uri uncanonicalize(String callingPkg, @Nullable String featureId, Uri uri)
152 throws RemoteException {
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700153 return MockContentProvider.this.uncanonicalize(uri);
154 }
Ben Lin1cf454f2016-11-10 13:50:54 -0800155
156 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700157 public boolean refresh(String callingPkg, @Nullable String featureId, Uri url,
158 Bundle args, ICancellationSignal cancellationSignal) throws RemoteException {
Ben Lin1cf454f2016-11-10 13:50:54 -0800159 return MockContentProvider.this.refresh(url, args);
160 }
Jeff Sharkey9edef252019-05-20 14:00:17 -0600161
162 @Override
Philip P. Moltmann128b7032019-09-27 08:44:12 -0700163 public int checkUriPermission(String callingPkg, @Nullable String featureId, Uri uri,
164 int uid, int modeFlags) {
Jeff Sharkey9edef252019-05-20 14:00:17 -0600165 return MockContentProvider.this.checkUriPermission(uri, uid, modeFlags);
166 }
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900167 }
168 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider();
169
170 /**
171 * A constructor using {@link MockContext} instance as a Context in it.
172 */
173 protected MockContentProvider() {
174 super(new MockContext(), "", "", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 }
176
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900177 /**
178 * A constructor accepting a Context instance, which is supposed to be the subclasss of
179 * {@link MockContext}.
180 */
181 public MockContentProvider(Context context) {
182 super(context, "", "", null);
183 }
184
185 /**
186 * A constructor which initialize four member variables which
187 * {@link android.content.ContentProvider} have internally.
188 *
189 * @param context A Context object which should be some mock instance (like the
190 * instance of {@link android.test.mock.MockContext}).
191 * @param readPermission The read permision you want this instance should have in the
192 * test, which is available via {@link #getReadPermission()}.
193 * @param writePermission The write permission you want this instance should have
194 * in the test, which is available via {@link #getWritePermission()}.
195 * @param pathPermissions The PathPermissions you want this instance should have
196 * in the test, which is available via {@link #getPathPermissions()}.
197 */
198 public MockContentProvider(Context context,
199 String readPermission,
200 String writePermission,
201 PathPermission[] pathPermissions) {
202 super(context, readPermission, writePermission, pathPermissions);
203 }
204
205 @Override
206 public int delete(Uri uri, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 throw new UnsupportedOperationException("unimplemented mock method");
208 }
209
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900210 @Override
211 public String getType(Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 throw new UnsupportedOperationException("unimplemented mock method");
213 }
214
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900215 @Override
216 public Uri insert(Uri uri, ContentValues values) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 throw new UnsupportedOperationException("unimplemented mock method");
218 }
219
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900220 @Override
221 public boolean onCreate() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 throw new UnsupportedOperationException("unimplemented mock method");
223 }
224
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900225 @Override
226 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
227 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 throw new UnsupportedOperationException("unimplemented mock method");
229 }
230
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900231 @Override
232 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 throw new UnsupportedOperationException("unimplemented mock method");
234 }
Fred Quintana89437372009-05-15 15:10:40 -0700235
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900236 /**
237 * If you're reluctant to implement this manually, please just call super.bulkInsert().
238 */
239 @Override
240 public int bulkInsert(Uri uri, ContentValues[] values) {
Fred Quintana89437372009-05-15 15:10:40 -0700241 throw new UnsupportedOperationException("unimplemented mock method");
242 }
243
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900244 @Override
245 public void attachInfo(Context context, ProviderInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 throw new UnsupportedOperationException("unimplemented mock method");
247 }
248
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900249 @Override
250 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
251 throw new UnsupportedOperationException("unimplemented mock method");
252 }
253
254 /**
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800255 * @hide
256 */
257 @Override
258 public Bundle call(String method, String request, Bundle args) {
259 throw new UnsupportedOperationException("unimplemented mock method call");
260 }
261
Steve McKayea93fe72016-12-02 11:35:35 -0800262 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700263 public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
264 throw new UnsupportedOperationException("unimplemented mock method call");
265 }
266
Steve McKayea93fe72016-12-02 11:35:35 -0800267 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700268 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
269 throw new UnsupportedOperationException("unimplemented mock method call");
270 }
271
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800272 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800273 * @hide
274 */
275 public boolean refresh(Uri url, Bundle args) {
276 throw new UnsupportedOperationException("unimplemented mock method call");
277 }
278
Jeff Sharkey9edef252019-05-20 14:00:17 -0600279 /** {@hide} */
280 @Override
281 public int checkUriPermission(@NonNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags) {
282 throw new UnsupportedOperationException("unimplemented mock method call");
283 }
284
Ben Lin1cf454f2016-11-10 13:50:54 -0800285 /**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900286 * Returns IContentProvider which calls back same methods in this class.
287 * By overriding this class, we avoid the mechanism hidden behind ContentProvider
288 * (IPC, etc.)
289 *
290 * @hide
291 */
292 @Override
293 public final IContentProvider getIContentProvider() {
294 return mIContentProvider;
295 }
Paul Duffin772b6922017-12-22 16:13:15 +0000296
297 /**
Sudheer Shankafe7668a2018-12-16 15:52:33 -0800298 * @hide
299 */
300 public IBinder getIContentProviderBinder() {
301 throw new UnsupportedOperationException("unimplemented mock method");
302 }
303
304 /**
Paul Duffin772b6922017-12-22 16:13:15 +0000305 * Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use
306 * when directly instantiating the provider for testing.
307 *
308 * <p>Provided for use by {@code android.test.ProviderTestCase2} and
309 * {@code android.test.RenamingDelegatingContext}.
310 *
311 * @deprecated Use a mocking framework like <a href="https://github.com/mockito/mockito">Mockito</a>.
312 * New tests should be written using the
313 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
314 */
315 @Deprecated
316 public static void attachInfoForTesting(
317 ContentProvider provider, Context context, ProviderInfo providerInfo) {
318 provider.attachInfoForTesting(context, providerInfo);
319 }
Fred Quintanaf99e2e02009-12-09 16:00:40 -0800320}