1 /* 2 * Copyright (C) 2009 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 17 package android.test.mock; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.AttributionSource; 22 import android.content.ContentProvider; 23 import android.content.ContentProviderOperation; 24 import android.content.ContentProviderResult; 25 import android.content.ContentResolver; 26 import android.content.ContentValues; 27 import android.content.Context; 28 import android.content.IContentProvider; 29 import android.content.Intent; 30 import android.content.OperationApplicationException; 31 import android.content.pm.PathPermission; 32 import android.content.pm.ProviderInfo; 33 import android.content.res.AssetFileDescriptor; 34 import android.database.Cursor; 35 import android.net.Uri; 36 import android.os.AsyncTask; 37 import android.os.Binder; 38 import android.os.Bundle; 39 import android.os.IBinder; 40 import android.os.ICancellationSignal; 41 import android.os.ParcelFileDescriptor; 42 import android.os.RemoteCallback; 43 import android.os.RemoteException; 44 45 import java.io.FileNotFoundException; 46 import java.util.ArrayList; 47 48 /** 49 * Mock implementation of ContentProvider. All methods are non-functional and throw 50 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to 51 * implement behavior needed for tests. 52 */ 53 public class MockContentProvider extends ContentProvider { 54 /* 55 * Note: if you add methods to ContentProvider, you must add similar methods to 56 * MockContentProvider. 57 */ 58 59 /** 60 * IContentProvider that directs all calls to this MockContentProvider. 61 */ 62 private class InversionIContentProvider implements IContentProvider { 63 @Override applyBatch(@onNull AttributionSource attributionSource, String authority, ArrayList<ContentProviderOperation> operations)64 public ContentProviderResult[] applyBatch(@NonNull AttributionSource attributionSource, 65 String authority, ArrayList<ContentProviderOperation> operations) 66 throws RemoteException, OperationApplicationException { 67 return MockContentProvider.this.applyBatch(authority, operations); 68 } 69 70 @Override bulkInsert(@onNull AttributionSource attributionSource, Uri url, ContentValues[] initialValues)71 public int bulkInsert(@NonNull AttributionSource attributionSource, Uri url, 72 ContentValues[] initialValues) throws RemoteException { 73 return MockContentProvider.this.bulkInsert(url, initialValues); 74 } 75 76 @Override delete(@onNull AttributionSource attributionSource, Uri url, Bundle extras)77 public int delete(@NonNull AttributionSource attributionSource, Uri url, 78 Bundle extras) throws RemoteException { 79 return MockContentProvider.this.delete(url, extras); 80 } 81 82 @Override getType(Uri url)83 public String getType(Uri url) throws RemoteException { 84 return MockContentProvider.this.getType(url); 85 } 86 87 @Override getTypeAsync(Uri uri, RemoteCallback callback)88 public void getTypeAsync(Uri uri, RemoteCallback callback) throws RemoteException { 89 MockContentProvider.this.getTypeAsync(uri, callback); 90 } 91 92 @Override insert(@onNull AttributionSource attributionSource, Uri url, ContentValues initialValues, Bundle extras)93 public Uri insert(@NonNull AttributionSource attributionSource, Uri url, 94 ContentValues initialValues, Bundle extras) throws RemoteException { 95 return MockContentProvider.this.insert(url, initialValues, extras); 96 } 97 98 @Override openAssetFile(@onNull AttributionSource attributionSource, Uri url, String mode, ICancellationSignal signal)99 public AssetFileDescriptor openAssetFile(@NonNull AttributionSource attributionSource, 100 Uri url, String mode, ICancellationSignal signal) 101 throws RemoteException, FileNotFoundException { 102 return MockContentProvider.this.openAssetFile(url, mode); 103 } 104 105 @Override openFile(@onNull AttributionSource attributionSource, Uri url, String mode, ICancellationSignal signal)106 public ParcelFileDescriptor openFile(@NonNull AttributionSource attributionSource, 107 Uri url, String mode, ICancellationSignal signal) 108 throws RemoteException, FileNotFoundException { 109 return MockContentProvider.this.openFile(url, mode); 110 } 111 112 @Override query(@onNull AttributionSource attributionSource, Uri url, @Nullable String[] projection, @Nullable Bundle queryArgs, @Nullable ICancellationSignal cancellationSignal)113 public Cursor query(@NonNull AttributionSource attributionSource, Uri url, 114 @Nullable String[] projection, @Nullable Bundle queryArgs, 115 @Nullable ICancellationSignal cancellationSignal) throws RemoteException { 116 return MockContentProvider.this.query(url, projection, queryArgs, null); 117 } 118 119 @Override update(@onNull AttributionSource attributionSource, Uri url, ContentValues values, Bundle extras)120 public int update(@NonNull AttributionSource attributionSource, Uri url, 121 ContentValues values, Bundle extras) throws RemoteException { 122 return MockContentProvider.this.update(url, values, extras); 123 } 124 125 @Override call(@onNull AttributionSource attributionSource, String authority, String method, String request, Bundle args)126 public Bundle call(@NonNull AttributionSource attributionSource, String authority, 127 String method, String request, Bundle args) throws RemoteException { 128 return MockContentProvider.this.call(authority, method, request, args); 129 } 130 131 @Override asBinder()132 public IBinder asBinder() { 133 return MockContentProvider.this.getIContentProviderBinder(); 134 } 135 136 @Override getStreamTypes(Uri url, String mimeTypeFilter)137 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { 138 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); 139 } 140 141 @Override openTypedAssetFile( @onNull AttributionSource attributionSource, Uri url, String mimeType, Bundle opts, ICancellationSignal signal)142 public AssetFileDescriptor openTypedAssetFile( 143 @NonNull AttributionSource attributionSource, Uri url, String mimeType, 144 Bundle opts, ICancellationSignal signal) 145 throws RemoteException, FileNotFoundException { 146 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); 147 } 148 149 @Override createCancellationSignal()150 public ICancellationSignal createCancellationSignal() throws RemoteException { 151 return null; 152 } 153 154 @Override canonicalize(@onNull AttributionSource attributionSource, Uri uri)155 public Uri canonicalize(@NonNull AttributionSource attributionSource, Uri uri) 156 throws RemoteException { 157 return MockContentProvider.this.canonicalize(uri); 158 } 159 160 @Override canonicalizeAsync(@onNull AttributionSource attributionSource, Uri uri, RemoteCallback callback)161 public void canonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, 162 RemoteCallback callback) { 163 MockContentProvider.this.canonicalizeAsync(uri, callback); 164 } 165 166 @Override uncanonicalize(@onNull AttributionSource attributionSource, Uri uri)167 public Uri uncanonicalize(@NonNull AttributionSource attributionSource, Uri uri) 168 throws RemoteException { 169 return MockContentProvider.this.uncanonicalize(uri); 170 } 171 172 @Override uncanonicalizeAsync(@onNull AttributionSource attributionSource, Uri uri, RemoteCallback callback)173 public void uncanonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, 174 RemoteCallback callback) { 175 MockContentProvider.this.uncanonicalizeAsync(uri, callback); 176 } 177 178 @Override refresh(@onNull AttributionSource attributionSource, Uri url, Bundle args, ICancellationSignal cancellationSignal)179 public boolean refresh(@NonNull AttributionSource attributionSource, Uri url, 180 Bundle args, ICancellationSignal cancellationSignal) throws RemoteException { 181 return MockContentProvider.this.refresh(url, args); 182 } 183 184 @Override checkUriPermission(@onNull AttributionSource attributionSource, Uri uri, int uid, int modeFlags)185 public int checkUriPermission(@NonNull AttributionSource attributionSource, Uri uri, 186 int uid, int modeFlags) { 187 return MockContentProvider.this.checkUriPermission(uri, uid, modeFlags); 188 } 189 } 190 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider(); 191 192 /** 193 * A constructor using {@link MockContext} instance as a Context in it. 194 */ MockContentProvider()195 protected MockContentProvider() { 196 super(new MockContext(), "", "", null); 197 } 198 199 /** 200 * A constructor accepting a Context instance, which is supposed to be the subclasss of 201 * {@link MockContext}. 202 */ MockContentProvider(Context context)203 public MockContentProvider(Context context) { 204 super(context, "", "", null); 205 } 206 207 /** 208 * A constructor which initialize four member variables which 209 * {@link android.content.ContentProvider} have internally. 210 * 211 * @param context A Context object which should be some mock instance (like the 212 * instance of {@link android.test.mock.MockContext}). 213 * @param readPermission The read permision you want this instance should have in the 214 * test, which is available via {@link #getReadPermission()}. 215 * @param writePermission The write permission you want this instance should have 216 * in the test, which is available via {@link #getWritePermission()}. 217 * @param pathPermissions The PathPermissions you want this instance should have 218 * in the test, which is available via {@link #getPathPermissions()}. 219 */ MockContentProvider(Context context, String readPermission, String writePermission, PathPermission[] pathPermissions)220 public MockContentProvider(Context context, 221 String readPermission, 222 String writePermission, 223 PathPermission[] pathPermissions) { 224 super(context, readPermission, writePermission, pathPermissions); 225 } 226 227 @Override delete(Uri uri, String selection, String[] selectionArgs)228 public int delete(Uri uri, String selection, String[] selectionArgs) { 229 throw new UnsupportedOperationException("unimplemented mock method"); 230 } 231 232 @Override getType(Uri uri)233 public String getType(Uri uri) { 234 throw new UnsupportedOperationException("unimplemented mock method"); 235 } 236 237 /** 238 * @hide 239 */ 240 @SuppressWarnings("deprecation") getTypeAsync(Uri uri, RemoteCallback remoteCallback)241 public void getTypeAsync(Uri uri, RemoteCallback remoteCallback) { 242 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 243 final Bundle bundle = new Bundle(); 244 bundle.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri)); 245 remoteCallback.sendResult(bundle); 246 }); 247 } 248 249 @Override insert(Uri uri, ContentValues values)250 public Uri insert(Uri uri, ContentValues values) { 251 throw new UnsupportedOperationException("unimplemented mock method"); 252 } 253 254 @Override onCreate()255 public boolean onCreate() { 256 throw new UnsupportedOperationException("unimplemented mock method"); 257 } 258 259 @Override query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)260 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 261 String sortOrder) { 262 throw new UnsupportedOperationException("unimplemented mock method"); 263 } 264 265 @Override update(Uri uri, ContentValues values, String selection, String[] selectionArgs)266 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 267 throw new UnsupportedOperationException("unimplemented mock method"); 268 } 269 270 /** 271 * If you're reluctant to implement this manually, please just call super.bulkInsert(). 272 */ 273 @Override bulkInsert(Uri uri, ContentValues[] values)274 public int bulkInsert(Uri uri, ContentValues[] values) { 275 throw new UnsupportedOperationException("unimplemented mock method"); 276 } 277 278 @Override attachInfo(Context context, ProviderInfo info)279 public void attachInfo(Context context, ProviderInfo info) { 280 throw new UnsupportedOperationException("unimplemented mock method"); 281 } 282 283 @Override applyBatch(ArrayList<ContentProviderOperation> operations)284 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { 285 throw new UnsupportedOperationException("unimplemented mock method"); 286 } 287 288 /** 289 * @hide 290 */ 291 @Override call(String method, String request, Bundle args)292 public Bundle call(String method, String request, Bundle args) { 293 throw new UnsupportedOperationException("unimplemented mock method call"); 294 } 295 296 @Override getStreamTypes(Uri url, String mimeTypeFilter)297 public String[] getStreamTypes(Uri url, String mimeTypeFilter) { 298 throw new UnsupportedOperationException("unimplemented mock method call"); 299 } 300 301 @Override openTypedAssetFile(Uri url, String mimeType, Bundle opts)302 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) { 303 throw new UnsupportedOperationException("unimplemented mock method call"); 304 } 305 306 /** 307 * @hide 308 */ 309 @SuppressWarnings("deprecation") canonicalizeAsync(Uri uri, RemoteCallback callback)310 public void canonicalizeAsync(Uri uri, RemoteCallback callback) { 311 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 312 final Bundle bundle = new Bundle(); 313 bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, canonicalize(uri)); 314 callback.sendResult(bundle); 315 }); 316 } 317 318 /** 319 * @hide 320 */ 321 @SuppressWarnings("deprecation") uncanonicalizeAsync(Uri uri, RemoteCallback callback)322 public void uncanonicalizeAsync(Uri uri, RemoteCallback callback) { 323 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 324 final Bundle bundle = new Bundle(); 325 bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, uncanonicalize(uri)); 326 callback.sendResult(bundle); 327 }); 328 } 329 330 /** 331 * @hide 332 */ refresh(Uri url, Bundle args)333 public boolean refresh(Uri url, Bundle args) { 334 throw new UnsupportedOperationException("unimplemented mock method call"); 335 } 336 337 /** {@hide} */ 338 @Override checkUriPermission(@onNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags)339 public int checkUriPermission(@NonNull Uri uri, int uid, @Intent.AccessUriMode int modeFlags) { 340 throw new UnsupportedOperationException("unimplemented mock method call"); 341 } 342 343 /** 344 * Returns IContentProvider which calls back same methods in this class. 345 * By overriding this class, we avoid the mechanism hidden behind ContentProvider 346 * (IPC, etc.) 347 * 348 * @hide 349 */ 350 @Override getIContentProvider()351 public final IContentProvider getIContentProvider() { 352 return mIContentProvider; 353 } 354 355 /** 356 * @hide 357 */ getIContentProviderBinder()358 public IBinder getIContentProviderBinder() { 359 return new Binder(); 360 } 361 362 /** 363 * Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use 364 * when directly instantiating the provider for testing. 365 * 366 * <p>Provided for use by {@code android.test.ProviderTestCase2} and 367 * {@code android.test.RenamingDelegatingContext}. 368 * 369 * @deprecated Use a mocking framework like <a href="https://github.com/mockito/mockito">Mockito</a>. 370 * New tests should be written using the 371 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>. 372 */ 373 @Deprecated attachInfoForTesting( ContentProvider provider, Context context, ProviderInfo providerInfo)374 public static void attachInfoForTesting( 375 ContentProvider provider, Context context, ProviderInfo providerInfo) { 376 provider.attachInfoForTesting(context, providerInfo); 377 } 378 } 379