blob: 373f24a83f3e85ddb06efcc4759e2292244fe943 [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
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090019import android.content.ContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070020import android.content.ContentProviderOperation;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090021import android.content.ContentProviderResult;
22import android.content.ContentValues;
23import android.content.Context;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090024import android.content.IContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070025import android.content.OperationApplicationException;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090026import android.content.pm.PathPermission;
27import android.content.pm.ProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.res.AssetFileDescriptor;
29import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.Uri;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080031import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.IBinder;
Jeff Browna7771df2012-05-07 20:06:46 -070033import android.os.ICancellationSignal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.ParcelFileDescriptor;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090035import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37import java.io.FileNotFoundException;
Fred Quintana03d94902009-05-22 14:23:31 -070038import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40/**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090041 * Mock implementation of ContentProvider. All methods are non-functional and throw
42 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 * implement behavior needed for tests.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090045public class MockContentProvider extends ContentProvider {
46 /*
47 * Note: if you add methods to ContentProvider, you must add similar methods to
48 * MockContentProvider.
49 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090051 /**
52 * IContentProvider that directs all calls to this MockContentProvider.
53 */
54 private class InversionIContentProvider implements IContentProvider {
Jeff Brownd2183652011-10-09 12:39:53 -070055 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080056 public ContentProviderResult[] applyBatch(String callingPackage,
57 ArrayList<ContentProviderOperation> operations)
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090058 throws RemoteException, OperationApplicationException {
59 return MockContentProvider.this.applyBatch(operations);
60 }
61
Jeff Brownd2183652011-10-09 12:39:53 -070062 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080063 public int bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues)
64 throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090065 return MockContentProvider.this.bulkInsert(url, initialValues);
66 }
67
Jeff Brownd2183652011-10-09 12:39:53 -070068 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080069 public int delete(String callingPackage, Uri url, String selection, String[] selectionArgs)
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090070 throws RemoteException {
71 return MockContentProvider.this.delete(url, selection, selectionArgs);
72 }
73
Jeff Brownd2183652011-10-09 12:39:53 -070074 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090075 public String getType(Uri url) throws RemoteException {
76 return MockContentProvider.this.getType(url);
77 }
78
Jeff Brownd2183652011-10-09 12:39:53 -070079 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080080 public Uri insert(String callingPackage, Uri url, ContentValues initialValues)
81 throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090082 return MockContentProvider.this.insert(url, initialValues);
83 }
84
Jeff Brownd2183652011-10-09 12:39:53 -070085 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080086 public AssetFileDescriptor openAssetFile(String callingPackage, Uri url, String mode)
87 throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090088 return MockContentProvider.this.openAssetFile(url, mode);
89 }
90
Jeff Brownd2183652011-10-09 12:39:53 -070091 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080092 public ParcelFileDescriptor openFile(String callingPackage, Uri url, String mode)
93 throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090094 return MockContentProvider.this.openFile(url, mode);
95 }
96
Jeff Brownd2183652011-10-09 12:39:53 -070097 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -080098 public Cursor query(String callingPackage, Uri url, String[] projection, String selection,
99 String[] selectionArgs,
Jeff Brown4c1241d2012-02-02 17:05:00 -0800100 String sortOrder, ICancellationSignal cancellationSignal) throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900101 return MockContentProvider.this.query(url, projection, selection,
102 selectionArgs, sortOrder);
103 }
104
Jeff Brownd2183652011-10-09 12:39:53 -0700105 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800106 public int update(String callingPackage, Uri url, ContentValues values, String selection,
107 String[] selectionArgs) throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900108 return MockContentProvider.this.update(url, values, selection, selectionArgs);
109 }
110
Jeff Brownd2183652011-10-09 12:39:53 -0700111 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800112 public Bundle call(String callingPackage, String method, String request, Bundle args)
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800113 throws RemoteException {
114 return MockContentProvider.this.call(method, request, args);
115 }
116
Jeff Brownd2183652011-10-09 12:39:53 -0700117 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900118 public IBinder asBinder() {
119 throw new UnsupportedOperationException();
120 }
121
Jeff Brownd2183652011-10-09 12:39:53 -0700122 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700123 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
124 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
125 }
126
Jeff Brownd2183652011-10-09 12:39:53 -0700127 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800128 public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url,
129 String mimeType, Bundle opts)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700130 throws RemoteException, FileNotFoundException {
131 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
132 }
Jeff Brown75ea64f2012-01-25 19:37:13 -0800133
134 @Override
Jeff Brown4c1241d2012-02-02 17:05:00 -0800135 public ICancellationSignal createCancellationSignal() throws RemoteException {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800136 return null;
137 }
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900138 }
139 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider();
140
141 /**
142 * A constructor using {@link MockContext} instance as a Context in it.
143 */
144 protected MockContentProvider() {
145 super(new MockContext(), "", "", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900148 /**
149 * A constructor accepting a Context instance, which is supposed to be the subclasss of
150 * {@link MockContext}.
151 */
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 */
169 public MockContentProvider(Context context,
170 String readPermission,
171 String writePermission,
172 PathPermission[] pathPermissions) {
173 super(context, readPermission, writePermission, pathPermissions);
174 }
175
176 @Override
177 public int delete(Uri uri, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 throw new UnsupportedOperationException("unimplemented mock method");
179 }
180
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900181 @Override
182 public String getType(Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 throw new UnsupportedOperationException("unimplemented mock method");
184 }
185
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900186 @Override
187 public Uri insert(Uri uri, ContentValues values) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 throw new UnsupportedOperationException("unimplemented mock method");
189 }
190
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900191 @Override
192 public boolean onCreate() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 throw new UnsupportedOperationException("unimplemented mock method");
194 }
195
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900196 @Override
197 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
198 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 throw new UnsupportedOperationException("unimplemented mock method");
200 }
201
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900202 @Override
203 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 throw new UnsupportedOperationException("unimplemented mock method");
205 }
Fred Quintana89437372009-05-15 15:10:40 -0700206
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900207 /**
208 * If you're reluctant to implement this manually, please just call super.bulkInsert().
209 */
210 @Override
211 public int bulkInsert(Uri uri, ContentValues[] values) {
Fred Quintana89437372009-05-15 15:10:40 -0700212 throw new UnsupportedOperationException("unimplemented mock method");
213 }
214
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900215 @Override
216 public void attachInfo(Context context, ProviderInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 throw new UnsupportedOperationException("unimplemented mock method");
218 }
219
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900220 @Override
221 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
222 throw new UnsupportedOperationException("unimplemented mock method");
223 }
224
225 /**
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800226 * @hide
227 */
228 @Override
229 public Bundle call(String method, String request, Bundle args) {
230 throw new UnsupportedOperationException("unimplemented mock method call");
231 }
232
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700233 public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
234 throw new UnsupportedOperationException("unimplemented mock method call");
235 }
236
237 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
238 throw new UnsupportedOperationException("unimplemented mock method call");
239 }
240
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800241 /**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900242 * 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
249 public final IContentProvider getIContentProvider() {
250 return mIContentProvider;
251 }
Fred Quintanaf99e2e02009-12-09 16:00:40 -0800252}