blob: 5ef71df341bfc5c931c4958a0d730f32f941ae02 [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
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070086 public AssetFileDescriptor openAssetFile(
87 String callingPackage, Uri url, String mode, ICancellationSignal signal)
Dianne Hackborn35654b62013-01-14 17:38:02 -080088 throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090089 return MockContentProvider.this.openAssetFile(url, mode);
90 }
91
Jeff Brownd2183652011-10-09 12:39:53 -070092 @Override
Jeff Sharkeybd3b9022013-08-20 15:20:04 -070093 public ParcelFileDescriptor openFile(
Dianne Hackbornff170242014-11-19 10:59:01 -080094 String callingPackage, Uri url, String mode, ICancellationSignal signal,
95 IBinder callerToken) throws RemoteException, FileNotFoundException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090096 return MockContentProvider.this.openFile(url, mode);
97 }
98
Jeff Brownd2183652011-10-09 12:39:53 -070099 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800100 public Cursor query(String callingPackage, Uri url, String[] projection, String selection,
101 String[] selectionArgs,
Jeff Brown4c1241d2012-02-02 17:05:00 -0800102 String sortOrder, ICancellationSignal cancellationSignal) throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900103 return MockContentProvider.this.query(url, projection, selection,
104 selectionArgs, sortOrder);
105 }
106
Jeff Brownd2183652011-10-09 12:39:53 -0700107 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800108 public int update(String callingPackage, Uri url, ContentValues values, String selection,
109 String[] selectionArgs) throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900110 return MockContentProvider.this.update(url, values, selection, selectionArgs);
111 }
112
Jeff Brownd2183652011-10-09 12:39:53 -0700113 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800114 public Bundle call(String callingPackage, String method, String request, Bundle args)
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800115 throws RemoteException {
116 return MockContentProvider.this.call(method, request, args);
117 }
118
Jeff Brownd2183652011-10-09 12:39:53 -0700119 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900120 public IBinder asBinder() {
121 throw new UnsupportedOperationException();
122 }
123
Jeff Brownd2183652011-10-09 12:39:53 -0700124 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700125 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
126 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
127 }
128
Jeff Brownd2183652011-10-09 12:39:53 -0700129 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800130 public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url,
Jeff Sharkeybd3b9022013-08-20 15:20:04 -0700131 String mimeType, Bundle opts, ICancellationSignal signal)
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700132 throws RemoteException, FileNotFoundException {
133 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
134 }
Jeff Brown75ea64f2012-01-25 19:37:13 -0800135
136 @Override
Jeff Brown4c1241d2012-02-02 17:05:00 -0800137 public ICancellationSignal createCancellationSignal() throws RemoteException {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800138 return null;
139 }
Dianne Hackborn38ed2a42013-09-06 16:17:22 -0700140
141 @Override
142 public Uri canonicalize(String callingPkg, Uri uri) throws RemoteException {
143 return MockContentProvider.this.canonicalize(uri);
144 }
145
146 @Override
147 public Uri uncanonicalize(String callingPkg, Uri uri) throws RemoteException {
148 return MockContentProvider.this.uncanonicalize(uri);
149 }
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900150 }
151 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider();
152
153 /**
154 * A constructor using {@link MockContext} instance as a Context in it.
155 */
156 protected MockContentProvider() {
157 super(new MockContext(), "", "", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 }
159
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900160 /**
161 * A constructor accepting a Context instance, which is supposed to be the subclasss of
162 * {@link MockContext}.
163 */
164 public MockContentProvider(Context context) {
165 super(context, "", "", null);
166 }
167
168 /**
169 * A constructor which initialize four member variables which
170 * {@link android.content.ContentProvider} have internally.
171 *
172 * @param context A Context object which should be some mock instance (like the
173 * instance of {@link android.test.mock.MockContext}).
174 * @param readPermission The read permision you want this instance should have in the
175 * test, which is available via {@link #getReadPermission()}.
176 * @param writePermission The write permission you want this instance should have
177 * in the test, which is available via {@link #getWritePermission()}.
178 * @param pathPermissions The PathPermissions you want this instance should have
179 * in the test, which is available via {@link #getPathPermissions()}.
180 */
181 public MockContentProvider(Context context,
182 String readPermission,
183 String writePermission,
184 PathPermission[] pathPermissions) {
185 super(context, readPermission, writePermission, pathPermissions);
186 }
187
188 @Override
189 public int delete(Uri uri, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 throw new UnsupportedOperationException("unimplemented mock method");
191 }
192
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900193 @Override
194 public String getType(Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 throw new UnsupportedOperationException("unimplemented mock method");
196 }
197
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900198 @Override
199 public Uri insert(Uri uri, ContentValues values) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 throw new UnsupportedOperationException("unimplemented mock method");
201 }
202
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900203 @Override
204 public boolean onCreate() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 throw new UnsupportedOperationException("unimplemented mock method");
206 }
207
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900208 @Override
209 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
210 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 throw new UnsupportedOperationException("unimplemented mock method");
212 }
213
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900214 @Override
215 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 throw new UnsupportedOperationException("unimplemented mock method");
217 }
Fred Quintana89437372009-05-15 15:10:40 -0700218
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900219 /**
220 * If you're reluctant to implement this manually, please just call super.bulkInsert().
221 */
222 @Override
223 public int bulkInsert(Uri uri, ContentValues[] values) {
Fred Quintana89437372009-05-15 15:10:40 -0700224 throw new UnsupportedOperationException("unimplemented mock method");
225 }
226
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900227 @Override
228 public void attachInfo(Context context, ProviderInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 throw new UnsupportedOperationException("unimplemented mock method");
230 }
231
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900232 @Override
233 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
234 throw new UnsupportedOperationException("unimplemented mock method");
235 }
236
237 /**
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800238 * @hide
239 */
240 @Override
241 public Bundle call(String method, String request, Bundle args) {
242 throw new UnsupportedOperationException("unimplemented mock method call");
243 }
244
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700245 public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
246 throw new UnsupportedOperationException("unimplemented mock method call");
247 }
248
249 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
250 throw new UnsupportedOperationException("unimplemented mock method call");
251 }
252
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800253 /**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900254 * Returns IContentProvider which calls back same methods in this class.
255 * By overriding this class, we avoid the mechanism hidden behind ContentProvider
256 * (IPC, etc.)
257 *
258 * @hide
259 */
260 @Override
261 public final IContentProvider getIContentProvider() {
262 return mIContentProvider;
263 }
Fred Quintanaf99e2e02009-12-09 16:00:40 -0800264}