• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.ContentProvider;
20 import android.content.ContentProviderOperation;
21 import android.content.ContentProviderResult;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.IContentProvider;
25 import android.content.OperationApplicationException;
26 import android.content.pm.PathPermission;
27 import android.content.pm.ProviderInfo;
28 import android.content.res.AssetFileDescriptor;
29 import android.database.Cursor;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.os.IBinder;
33 import android.os.ICancellationSignal;
34 import android.os.ParcelFileDescriptor;
35 import android.os.RemoteException;
36 
37 import java.io.FileNotFoundException;
38 import java.util.ArrayList;
39 
40 /**
41  * Mock implementation of ContentProvider.  All methods are non-functional and throw
42  * {@link java.lang.UnsupportedOperationException}.  Tests can extend this class to
43  * implement behavior needed for tests.
44  */
45 public class MockContentProvider extends ContentProvider {
46     /*
47      * Note: if you add methods to ContentProvider, you must add similar methods to
48      *       MockContentProvider.
49      */
50 
51     /**
52      * IContentProvider that directs all calls to this MockContentProvider.
53      */
54     private class InversionIContentProvider implements IContentProvider {
55         @Override
applyBatch(String callingPackage, ArrayList<ContentProviderOperation> operations)56         public ContentProviderResult[] applyBatch(String callingPackage,
57                 ArrayList<ContentProviderOperation> operations)
58                 throws RemoteException, OperationApplicationException {
59             return MockContentProvider.this.applyBatch(operations);
60         }
61 
62         @Override
bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues)63         public int bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues)
64                 throws RemoteException {
65             return MockContentProvider.this.bulkInsert(url, initialValues);
66         }
67 
68         @Override
delete(String callingPackage, Uri url, String selection, String[] selectionArgs)69         public int delete(String callingPackage, Uri url, String selection, String[] selectionArgs)
70                 throws RemoteException {
71             return MockContentProvider.this.delete(url, selection, selectionArgs);
72         }
73 
74         @Override
getType(Uri url)75         public String getType(Uri url) throws RemoteException {
76             return MockContentProvider.this.getType(url);
77         }
78 
79         @Override
insert(String callingPackage, Uri url, ContentValues initialValues)80         public Uri insert(String callingPackage, Uri url, ContentValues initialValues)
81                 throws RemoteException {
82             return MockContentProvider.this.insert(url, initialValues);
83         }
84 
85         @Override
openAssetFile(String callingPackage, Uri url, String mode)86         public AssetFileDescriptor openAssetFile(String callingPackage, Uri url, String mode)
87                 throws RemoteException, FileNotFoundException {
88             return MockContentProvider.this.openAssetFile(url, mode);
89         }
90 
91         @Override
openFile(String callingPackage, Uri url, String mode)92         public ParcelFileDescriptor openFile(String callingPackage, Uri url, String mode)
93                 throws RemoteException, FileNotFoundException {
94             return MockContentProvider.this.openFile(url, mode);
95         }
96 
97         @Override
query(String callingPackage, Uri url, String[] projection, String selection, String[] selectionArgs, String sortOrder, ICancellationSignal cancellationSignal)98         public Cursor query(String callingPackage, Uri url, String[] projection, String selection,
99                 String[] selectionArgs,
100                 String sortOrder, ICancellationSignal cancellationSignal) throws RemoteException {
101             return MockContentProvider.this.query(url, projection, selection,
102                     selectionArgs, sortOrder);
103         }
104 
105         @Override
update(String callingPackage, Uri url, ContentValues values, String selection, String[] selectionArgs)106         public int update(String callingPackage, Uri url, ContentValues values, String selection,
107                 String[] selectionArgs) throws RemoteException {
108             return MockContentProvider.this.update(url, values, selection, selectionArgs);
109         }
110 
111         @Override
call(String callingPackage, String method, String request, Bundle args)112         public Bundle call(String callingPackage, String method, String request, Bundle args)
113                 throws RemoteException {
114             return MockContentProvider.this.call(method, request, args);
115         }
116 
117         @Override
asBinder()118         public IBinder asBinder() {
119             throw new UnsupportedOperationException();
120         }
121 
122         @Override
getStreamTypes(Uri url, String mimeTypeFilter)123         public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
124             return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
125         }
126 
127         @Override
openTypedAssetFile(String callingPackage, Uri url, String mimeType, Bundle opts)128         public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url,
129                 String mimeType, Bundle opts)
130                 throws RemoteException, FileNotFoundException {
131             return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
132         }
133 
134         @Override
createCancellationSignal()135         public ICancellationSignal createCancellationSignal() throws RemoteException {
136             return null;
137         }
138     }
139     private final InversionIContentProvider mIContentProvider = new InversionIContentProvider();
140 
141     /**
142      * A constructor using {@link MockContext} instance as a Context in it.
143      */
MockContentProvider()144     protected MockContentProvider() {
145         super(new MockContext(), "", "", null);
146     }
147 
148     /**
149      * A constructor accepting a Context instance, which is supposed to be the subclasss of
150      * {@link MockContext}.
151      */
MockContentProvider(Context context)152     public MockContentProvider(Context context) {
153         super(context, "", "", null);
154     }
155 
156     /**
157      * A constructor which initialize four member variables which
158      * {@link android.content.ContentProvider} have internally.
159      *
160      * @param context A Context object which should be some mock instance (like the
161      * instance of {@link android.test.mock.MockContext}).
162      * @param readPermission The read permision you want this instance should have in the
163      * test, which is available via {@link #getReadPermission()}.
164      * @param writePermission The write permission you want this instance should have
165      * in the test, which is available via {@link #getWritePermission()}.
166      * @param pathPermissions The PathPermissions you want this instance should have
167      * in the test, which is available via {@link #getPathPermissions()}.
168      */
MockContentProvider(Context context, String readPermission, String writePermission, PathPermission[] pathPermissions)169     public MockContentProvider(Context context,
170             String readPermission,
171             String writePermission,
172             PathPermission[] pathPermissions) {
173         super(context, readPermission, writePermission, pathPermissions);
174     }
175 
176     @Override
delete(Uri uri, String selection, String[] selectionArgs)177     public int delete(Uri uri, String selection, String[] selectionArgs) {
178         throw new UnsupportedOperationException("unimplemented mock method");
179     }
180 
181     @Override
getType(Uri uri)182     public String getType(Uri uri) {
183         throw new UnsupportedOperationException("unimplemented mock method");
184     }
185 
186     @Override
insert(Uri uri, ContentValues values)187     public Uri insert(Uri uri, ContentValues values) {
188         throw new UnsupportedOperationException("unimplemented mock method");
189     }
190 
191     @Override
onCreate()192     public boolean onCreate() {
193         throw new UnsupportedOperationException("unimplemented mock method");
194     }
195 
196     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)197     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
198             String sortOrder) {
199         throw new UnsupportedOperationException("unimplemented mock method");
200     }
201 
202     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)203     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
204         throw new UnsupportedOperationException("unimplemented mock method");
205     }
206 
207     /**
208      * If you're reluctant to implement this manually, please just call super.bulkInsert().
209      */
210     @Override
bulkInsert(Uri uri, ContentValues[] values)211     public int bulkInsert(Uri uri, ContentValues[] values) {
212         throw new UnsupportedOperationException("unimplemented mock method");
213     }
214 
215     @Override
attachInfo(Context context, ProviderInfo info)216     public void attachInfo(Context context, ProviderInfo info) {
217         throw new UnsupportedOperationException("unimplemented mock method");
218     }
219 
220     @Override
applyBatch(ArrayList<ContentProviderOperation> operations)221     public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
222         throw new UnsupportedOperationException("unimplemented mock method");
223     }
224 
225     /**
226      * @hide
227      */
228     @Override
call(String method, String request, Bundle args)229     public Bundle call(String method, String request, Bundle args) {
230         throw new UnsupportedOperationException("unimplemented mock method call");
231     }
232 
getStreamTypes(Uri url, String mimeTypeFilter)233     public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
234         throw new UnsupportedOperationException("unimplemented mock method call");
235     }
236 
openTypedAssetFile(Uri url, String mimeType, Bundle opts)237     public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
238         throw new UnsupportedOperationException("unimplemented mock method call");
239     }
240 
241     /**
242      * Returns IContentProvider which calls back same methods in this class.
243      * By overriding this class, we avoid the mechanism hidden behind ContentProvider
244      * (IPC, etc.)
245      *
246      * @hide
247      */
248     @Override
getIContentProvider()249     public final IContentProvider getIContentProvider() {
250         return mIContentProvider;
251     }
252 }
253