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