blob: 0ac35bc2628cfc90f50ace45d62c335925648d9c [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
Steve McKayea93fe72016-12-02 11:35:35 -080019import android.annotation.Nullable;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090020import android.content.ContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070021import android.content.ContentProviderOperation;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090022import android.content.ContentProviderResult;
23import android.content.ContentValues;
24import android.content.Context;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090025import android.content.IContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070026import android.content.OperationApplicationException;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090027import android.content.pm.PathPermission;
28import android.content.pm.ProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.AssetFileDescriptor;
30import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.Uri;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080032import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070034import android.os.ICancellationSignal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.ParcelFileDescriptor;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090036import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38import java.io.FileNotFoundException;
Fred Quintana03d94902009-05-22 14:23:31 -070039import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41/**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090042 * Mock implementation of ContentProvider. All methods are non-functional and throw
43 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 * implement behavior needed for tests.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090046public class MockContentProvider extends ContentProvider {
47 /*
48 * Note: if you add methods to ContentProvider, you must add similar methods to
49 * MockContentProvider.
50 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090052 /**
53 * IContentProvider that directs all calls to this MockContentProvider.
54 */
55 private class InversionIContentProvider implements IContentProvider {
Jeff Brownd2183652011-10-09 12:39:53 -070056 @Override
Jeff Sharkey633a13e2018-12-07 12:00:45 -070057 public ContentProviderResult[] applyBatch(String callingPackage, String authority,
Dianne Hackborn35654b62013-01-14 17:38:02 -080058 ArrayList<ContentProviderOperation> operations)
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090059 throws RemoteException, OperationApplicationException {
Jeff Sharkey633a13e2018-12-07 12:00:45 -070060 return MockContentProvider.this.applyBatch(authority, operations);
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090061 }
62
Jeff Brownd2183652011-10-09 12:39:53 -070063 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080064 public int bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues)
65 throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090066 return MockContentProvider.this.bulkInsert(url, initialValues);
67 }
68
Jeff Brownd2183652011-10-09 12:39:53 -070069 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080070 public int delete(String callingPackage, Uri url, String selection, String[] selectionArgs)
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090071 throws RemoteException {
72 return MockContentProvider.this.delete(url, selection, selectionArgs);
73 }
74
Jeff Brownd2183652011-10-09 12:39:53 -070075 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090076 public String getType(Uri url) throws RemoteException {
77 return MockContentProvider.this.getType(url);
78 }
79
Jeff Brownd2183652011-10-09 12:39:53 -070080 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080081 public Uri insert(String callingPackage, Uri url, ContentValues initialValues)
82 throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090083 return MockContentProvider.this.insert(url, initialValues);
84 }
85
Jeff Brownd2183652011-10-09 12:39:53 -070086 @Override
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070087 public AssetFileDescriptor openAssetFile(
88 String callingPackage, Uri url, String mode, ICancellationSignal signal)
Dianne Hackborn35654b62013-01-14 17:38:02 -080089 throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090090 return MockContentProvider.this.openAssetFile(url, mode);
91 }
92
Jeff Brownd2183652011-10-09 12:39:53 -070093 @Override
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070094 public ParcelFileDescriptor openFile(
Dianne Hackbornff170242014-11-19 10:59:01 -080095 String callingPackage, Uri url, String mode, ICancellationSignal signal,
96 IBinder callerToken) throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090097 return MockContentProvider.this.openFile(url, mode);
98 }
99
Jeff Brownd2183652011-10-09 12:39:53 -0700100 @Override
Steve McKayea93fe72016-12-02 11:35:35 -0800101 public Cursor query(String callingPackage, Uri url, @Nullable String[] projection,
102 @Nullable Bundle queryArgs,
103 @Nullable ICancellationSignal cancellationSignal)
104 throws RemoteException {
105 return MockContentProvider.this.query(url, projection, queryArgs, null);
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900106 }
107
Jeff Brownd2183652011-10-09 12:39:53 -0700108 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800109 public int update(String callingPackage, Uri url, ContentValues values, String selection,
110 String[] selectionArgs) throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900111 return MockContentProvider.this.update(url, values, selection, selectionArgs);
112 }
113
Jeff Brownd2183652011-10-09 12:39:53 -0700114 @Override
Jeff Sharkey633a13e2018-12-07 12:00:45 -0700115 public Bundle call(String callingPackage, String authority, String method, String request,
116 Bundle args) throws RemoteException {
117 return MockContentProvider.this.call(authority, method, request, args);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800118 }
119
Jeff Brownd2183652011-10-09 12:39:53 -0700120 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900121 public IBinder asBinder() {
122 throw new UnsupportedOperationException();
123 }
124
Jeff Brownd2183652011-10-09 12:39:53 -0700125 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700126 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
127 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
128 }
129
Jeff Brownd2183652011-10-09 12:39:53 -0700130 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800131 public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url,
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700132 String mimeType, Bundle opts, ICancellationSignal signal)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700133 throws RemoteException, FileNotFoundException {
134 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
135 }
Jeff Brown75ea64f2012-01-25 19:37:13 -0800136
137 @Override
Jeff Brown4c1241d2012-02-02 17:05:00 -0800138 public ICancellationSignal createCancellationSignal() throws RemoteException {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800139 return null;
140 }
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700141
142 @Override
143 public Uri canonicalize(String callingPkg, Uri uri) throws RemoteException {
144 return MockContentProvider.this.canonicalize(uri);
145 }
146
147 @Override
148 public Uri uncanonicalize(String callingPkg, Uri uri) throws RemoteException {
149 return MockContentProvider.this.uncanonicalize(uri);
150 }
Ben Lin1cf454f2016-11-10 13:50:54 -0800151
152 @Override
153 public boolean refresh(String callingPkg, Uri url, Bundle args,
154 ICancellationSignal cancellationSignal) throws RemoteException {
155 return MockContentProvider.this.refresh(url, args);
156 }
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900157 }
158 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider();
159
160 /**
161 * A constructor using {@link MockContext} instance as a Context in it.
162 */
163 protected MockContentProvider() {
164 super(new MockContext(), "", "", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
166
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900167 /**
168 * A constructor accepting a Context instance, which is supposed to be the subclasss of
169 * {@link MockContext}.
170 */
171 public MockContentProvider(Context context) {
172 super(context, "", "", null);
173 }
174
175 /**
176 * A constructor which initialize four member variables which
177 * {@link android.content.ContentProvider} have internally.
178 *
179 * @param context A Context object which should be some mock instance (like the
180 * instance of {@link android.test.mock.MockContext}).
181 * @param readPermission The read permision you want this instance should have in the
182 * test, which is available via {@link #getReadPermission()}.
183 * @param writePermission The write permission you want this instance should have
184 * in the test, which is available via {@link #getWritePermission()}.
185 * @param pathPermissions The PathPermissions you want this instance should have
186 * in the test, which is available via {@link #getPathPermissions()}.
187 */
188 public MockContentProvider(Context context,
189 String readPermission,
190 String writePermission,
191 PathPermission[] pathPermissions) {
192 super(context, readPermission, writePermission, pathPermissions);
193 }
194
195 @Override
196 public int delete(Uri uri, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 throw new UnsupportedOperationException("unimplemented mock method");
198 }
199
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900200 @Override
201 public String getType(Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 throw new UnsupportedOperationException("unimplemented mock method");
203 }
204
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900205 @Override
206 public Uri insert(Uri uri, ContentValues values) {
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 boolean onCreate() {
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 Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
217 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 throw new UnsupportedOperationException("unimplemented mock method");
219 }
220
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900221 @Override
222 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 throw new UnsupportedOperationException("unimplemented mock method");
224 }
Fred Quintana89437372009-05-15 15:10:40 -0700225
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900226 /**
227 * If you're reluctant to implement this manually, please just call super.bulkInsert().
228 */
229 @Override
230 public int bulkInsert(Uri uri, ContentValues[] values) {
Fred Quintana89437372009-05-15 15:10:40 -0700231 throw new UnsupportedOperationException("unimplemented mock method");
232 }
233
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900234 @Override
235 public void attachInfo(Context context, ProviderInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 throw new UnsupportedOperationException("unimplemented mock method");
237 }
238
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900239 @Override
240 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
241 throw new UnsupportedOperationException("unimplemented mock method");
242 }
243
244 /**
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800245 * @hide
246 */
247 @Override
248 public Bundle call(String method, String request, Bundle args) {
249 throw new UnsupportedOperationException("unimplemented mock method call");
250 }
251
Steve McKayea93fe72016-12-02 11:35:35 -0800252 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700253 public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
254 throw new UnsupportedOperationException("unimplemented mock method call");
255 }
256
Steve McKayea93fe72016-12-02 11:35:35 -0800257 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700258 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
259 throw new UnsupportedOperationException("unimplemented mock method call");
260 }
261
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800262 /**
Ben Lin1cf454f2016-11-10 13:50:54 -0800263 * @hide
264 */
265 public boolean refresh(Uri url, Bundle args) {
266 throw new UnsupportedOperationException("unimplemented mock method call");
267 }
268
269 /**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900270 * Returns IContentProvider which calls back same methods in this class.
271 * By overriding this class, we avoid the mechanism hidden behind ContentProvider
272 * (IPC, etc.)
273 *
274 * @hide
275 */
276 @Override
277 public final IContentProvider getIContentProvider() {
278 return mIContentProvider;
279 }
Paul Duffin772b6922017-12-22 16:13:15 +0000280
281 /**
282 * Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use
283 * when directly instantiating the provider for testing.
284 *
285 * <p>Provided for use by {@code android.test.ProviderTestCase2} and
286 * {@code android.test.RenamingDelegatingContext}.
287 *
288 * @deprecated Use a mocking framework like <a href="https://github.com/mockito/mockito">Mockito</a>.
289 * New tests should be written using the
290 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
291 */
292 @Deprecated
293 public static void attachInfoForTesting(
294 ContentProvider provider, Context context, ProviderInfo providerInfo) {
295 provider.attachInfoForTesting(context, providerInfo);
296 }
Fred Quintanaf99e2e02009-12-09 16:00:40 -0800297}