Home | History | Annotate | Download | only in mock
      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.Nullable;
     20 import android.content.ContentProvider;
     21 import android.content.ContentProviderOperation;
     22 import android.content.ContentProviderResult;
     23 import android.content.ContentValues;
     24 import android.content.Context;
     25 import android.content.IContentProvider;
     26 import android.content.OperationApplicationException;
     27 import android.content.pm.PathPermission;
     28 import android.content.pm.ProviderInfo;
     29 import android.content.res.AssetFileDescriptor;
     30 import android.database.Cursor;
     31 import android.net.Uri;
     32 import android.os.Bundle;
     33 import android.os.IBinder;
     34 import android.os.ICancellationSignal;
     35 import android.os.ParcelFileDescriptor;
     36 import android.os.RemoteException;
     37 
     38 import java.io.FileNotFoundException;
     39 import java.util.ArrayList;
     40 
     41 /**
     42  * Mock implementation of ContentProvider.  All methods are non-functional and throw
     43  * {@link java.lang.UnsupportedOperationException}.  Tests can extend this class to
     44  * implement behavior needed for tests.
     45  */
     46 public class MockContentProvider extends ContentProvider {
     47     /*
     48      * Note: if you add methods to ContentProvider, you must add similar methods to
     49      *       MockContentProvider.
     50      */
     51 
     52     /**
     53      * IContentProvider that directs all calls to this MockContentProvider.
     54      */
     55     private class InversionIContentProvider implements IContentProvider {
     56         @Override
     57         public ContentProviderResult[] applyBatch(String callingPackage,
     58                 ArrayList<ContentProviderOperation> operations)
     59                 throws RemoteException, OperationApplicationException {
     60             return MockContentProvider.this.applyBatch(operations);
     61         }
     62 
     63         @Override
     64         public int bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues)
     65                 throws RemoteException {
     66             return MockContentProvider.this.bulkInsert(url, initialValues);
     67         }
     68 
     69         @Override
     70         public int delete(String callingPackage, Uri url, String selection, String[] selectionArgs)
     71                 throws RemoteException {
     72             return MockContentProvider.this.delete(url, selection, selectionArgs);
     73         }
     74 
     75         @Override
     76         public String getType(Uri url) throws RemoteException {
     77             return MockContentProvider.this.getType(url);
     78         }
     79 
     80         @Override
     81         public Uri insert(String callingPackage, Uri url, ContentValues initialValues)
     82                 throws RemoteException {
     83             return MockContentProvider.this.insert(url, initialValues);
     84         }
     85 
     86         @Override
     87         public AssetFileDescriptor openAssetFile(
     88                 String callingPackage, Uri url, String mode, ICancellationSignal signal)
     89                 throws RemoteException, FileNotFoundException {
     90             return MockContentProvider.this.openAssetFile(url, mode);
     91         }
     92 
     93         @Override
     94         public ParcelFileDescriptor openFile(
     95                 String callingPackage, Uri url, String mode, ICancellationSignal signal,
     96                 IBinder callerToken) throws RemoteException, FileNotFoundException {
     97             return MockContentProvider.this.openFile(url, mode);
     98         }
     99 
    100         @Override
    101         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);
    106         }
    107 
    108         @Override
    109         public int update(String callingPackage, Uri url, ContentValues values, String selection,
    110                 String[] selectionArgs) throws RemoteException {
    111             return MockContentProvider.this.update(url, values, selection, selectionArgs);
    112         }
    113 
    114         @Override
    115         public Bundle call(String callingPackage, String method, String request, Bundle args)
    116                 throws RemoteException {
    117             return MockContentProvider.this.call(method, request, args);
    118         }
    119 
    120         @Override
    121         public IBinder asBinder() {
    122             throw new UnsupportedOperationException();
    123         }
    124 
    125         @Override
    126         public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
    127             return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
    128         }
    129 
    130         @Override
    131         public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url,
    132                 String mimeType, Bundle opts, ICancellationSignal signal)
    133                 throws RemoteException, FileNotFoundException {
    134             return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
    135         }
    136 
    137         @Override
    138         public ICancellationSignal createCancellationSignal() throws RemoteException {
    139             return null;
    140         }
    141 
    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         }
    151 
    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         }
    157     }
    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);
    165     }
    166 
    167     /**
    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) {
    197         throw new UnsupportedOperationException("unimplemented mock method");
    198     }
    199 
    200     @Override
    201     public String getType(Uri uri) {
    202         throw new UnsupportedOperationException("unimplemented mock method");
    203     }
    204 
    205     @Override
    206     public Uri insert(Uri uri, ContentValues values) {
    207         throw new UnsupportedOperationException("unimplemented mock method");
    208     }
    209 
    210     @Override
    211     public boolean onCreate() {
    212         throw new UnsupportedOperationException("unimplemented mock method");
    213     }
    214 
    215     @Override
    216     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
    217             String sortOrder) {
    218         throw new UnsupportedOperationException("unimplemented mock method");
    219     }
    220 
    221     @Override
    222     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    223         throw new UnsupportedOperationException("unimplemented mock method");
    224     }
    225 
    226     /**
    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) {
    231         throw new UnsupportedOperationException("unimplemented mock method");
    232     }
    233 
    234     @Override
    235     public void attachInfo(Context context, ProviderInfo info) {
    236         throw new UnsupportedOperationException("unimplemented mock method");
    237     }
    238 
    239     @Override
    240     public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
    241         throw new UnsupportedOperationException("unimplemented mock method");
    242     }
    243 
    244     /**
    245      * @hide
    246      */
    247     @Override
    248     public Bundle call(String method, String request, Bundle args) {
    249         throw new UnsupportedOperationException("unimplemented mock method call");
    250     }
    251 
    252     @Override
    253     public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
    254         throw new UnsupportedOperationException("unimplemented mock method call");
    255     }
    256 
    257     @Override
    258     public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
    259         throw new UnsupportedOperationException("unimplemented mock method call");
    260     }
    261 
    262     /**
    263      * @hide
    264      */
    265     public boolean refresh(Uri url, Bundle args) {
    266         throw new UnsupportedOperationException("unimplemented mock method call");
    267     }
    268 
    269     /**
    270      * 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     }
    280 }
    281