blob: a8c388e33053abd4d833cea0549afe26fbcbb30e [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;
Jeff Brown4c1241d2012-02-02 17:05:00 -080024import android.content.ICancellationSignal;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090025import android.content.IContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070026import android.content.OperationApplicationException;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090027import android.content.pm.PathPermission;
28import android.content.pm.ProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.AssetFileDescriptor;
30import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.Uri;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080032import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.IBinder;
34import 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
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090056 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
57 throws RemoteException, OperationApplicationException {
58 return MockContentProvider.this.applyBatch(operations);
59 }
60
Jeff Brownd2183652011-10-09 12:39:53 -070061 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090062 public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException {
63 return MockContentProvider.this.bulkInsert(url, initialValues);
64 }
65
Jeff Brownd2183652011-10-09 12:39:53 -070066 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090067 public int delete(Uri url, String selection, String[] selectionArgs)
68 throws RemoteException {
69 return MockContentProvider.this.delete(url, selection, selectionArgs);
70 }
71
Jeff Brownd2183652011-10-09 12:39:53 -070072 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090073 public String getType(Uri url) throws RemoteException {
74 return MockContentProvider.this.getType(url);
75 }
76
Jeff Brownd2183652011-10-09 12:39:53 -070077 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090078 public Uri insert(Uri url, ContentValues initialValues) throws RemoteException {
79 return MockContentProvider.this.insert(url, initialValues);
80 }
81
Jeff Brownd2183652011-10-09 12:39:53 -070082 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090083 public AssetFileDescriptor openAssetFile(Uri url, String mode) throws RemoteException,
84 FileNotFoundException {
85 return MockContentProvider.this.openAssetFile(url, mode);
86 }
87
Jeff Brownd2183652011-10-09 12:39:53 -070088 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090089 public ParcelFileDescriptor openFile(Uri url, String mode) throws RemoteException,
90 FileNotFoundException {
91 return MockContentProvider.this.openFile(url, mode);
92 }
93
Jeff Brownd2183652011-10-09 12:39:53 -070094 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090095 public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs,
Jeff Brown4c1241d2012-02-02 17:05:00 -080096 String sortOrder, ICancellationSignal cancellationSignal) throws RemoteException {
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090097 return MockContentProvider.this.query(url, projection, selection,
98 selectionArgs, sortOrder);
99 }
100
Jeff Brownd2183652011-10-09 12:39:53 -0700101 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900102 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
Jeff Brownd2183652011-10-09 12:39:53 -0700107 @Override
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800108 public Bundle call(String method, String request, Bundle args)
109 throws RemoteException {
110 return MockContentProvider.this.call(method, request, args);
111 }
112
Jeff Brownd2183652011-10-09 12:39:53 -0700113 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900114 public IBinder asBinder() {
115 throw new UnsupportedOperationException();
116 }
117
Jeff Brownd2183652011-10-09 12:39:53 -0700118 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700119 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
120 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
121 }
122
Jeff Brownd2183652011-10-09 12:39:53 -0700123 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700124 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts)
125 throws RemoteException, FileNotFoundException {
126 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
127 }
Jeff Brown75ea64f2012-01-25 19:37:13 -0800128
129 @Override
Jeff Brown4c1241d2012-02-02 17:05:00 -0800130 public ICancellationSignal createCancellationSignal() throws RemoteException {
Jeff Brown75ea64f2012-01-25 19:37:13 -0800131 return null;
132 }
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900133 }
134 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider();
135
136 /**
137 * A constructor using {@link MockContext} instance as a Context in it.
138 */
139 protected MockContentProvider() {
140 super(new MockContext(), "", "", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
142
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900143 /**
144 * A constructor accepting a Context instance, which is supposed to be the subclasss of
145 * {@link MockContext}.
146 */
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 */
164 public MockContentProvider(Context context,
165 String readPermission,
166 String writePermission,
167 PathPermission[] pathPermissions) {
168 super(context, readPermission, writePermission, pathPermissions);
169 }
170
171 @Override
172 public int delete(Uri uri, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 throw new UnsupportedOperationException("unimplemented mock method");
174 }
175
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900176 @Override
177 public String getType(Uri uri) {
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 Uri insert(Uri uri, ContentValues values) {
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 boolean onCreate() {
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 Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
193 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 throw new UnsupportedOperationException("unimplemented mock method");
195 }
196
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900197 @Override
198 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 throw new UnsupportedOperationException("unimplemented mock method");
200 }
Fred Quintana89437372009-05-15 15:10:40 -0700201
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900202 /**
203 * If you're reluctant to implement this manually, please just call super.bulkInsert().
204 */
205 @Override
206 public int bulkInsert(Uri uri, ContentValues[] values) {
Fred Quintana89437372009-05-15 15:10:40 -0700207 throw new UnsupportedOperationException("unimplemented mock method");
208 }
209
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900210 @Override
211 public void attachInfo(Context context, ProviderInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 throw new UnsupportedOperationException("unimplemented mock method");
213 }
214
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900215 @Override
216 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
217 throw new UnsupportedOperationException("unimplemented mock method");
218 }
219
220 /**
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800221 * @hide
222 */
223 @Override
224 public Bundle call(String method, String request, Bundle args) {
225 throw new UnsupportedOperationException("unimplemented mock method call");
226 }
227
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700228 public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
229 throw new UnsupportedOperationException("unimplemented mock method call");
230 }
231
232 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
233 throw new UnsupportedOperationException("unimplemented mock method call");
234 }
235
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800236 /**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900237 * 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
244 public final IContentProvider getIContentProvider() {
245 return mIContentProvider;
246 }
Fred Quintanaf99e2e02009-12-09 16:00:40 -0800247}