blob: e0ce3228c75bceb7fc05ae40e69ffb70e68a0035 [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;
33import android.os.ParcelFileDescriptor;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090034import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import java.io.FileNotFoundException;
Fred Quintana03d94902009-05-22 14:23:31 -070037import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39/**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090040 * Mock implementation of ContentProvider. All methods are non-functional and throw
41 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 * implement behavior needed for tests.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090044public class MockContentProvider extends ContentProvider {
45 /*
46 * Note: if you add methods to ContentProvider, you must add similar methods to
47 * MockContentProvider.
48 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090050 /**
51 * IContentProvider that directs all calls to this MockContentProvider.
52 */
53 private class InversionIContentProvider implements IContentProvider {
Jeff Brownd2183652011-10-09 12:39:53 -070054 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090055 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
56 throws RemoteException, OperationApplicationException {
57 return MockContentProvider.this.applyBatch(operations);
58 }
59
Jeff Brownd2183652011-10-09 12:39:53 -070060 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090061 public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException {
62 return MockContentProvider.this.bulkInsert(url, initialValues);
63 }
64
Jeff Brownd2183652011-10-09 12:39:53 -070065 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090066 public int delete(Uri url, String selection, String[] selectionArgs)
67 throws RemoteException {
68 return MockContentProvider.this.delete(url, selection, selectionArgs);
69 }
70
Jeff Brownd2183652011-10-09 12:39:53 -070071 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090072 public String getType(Uri url) throws RemoteException {
73 return MockContentProvider.this.getType(url);
74 }
75
Jeff Brownd2183652011-10-09 12:39:53 -070076 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090077 public Uri insert(Uri url, ContentValues initialValues) throws RemoteException {
78 return MockContentProvider.this.insert(url, initialValues);
79 }
80
Jeff Brownd2183652011-10-09 12:39:53 -070081 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090082 public AssetFileDescriptor openAssetFile(Uri url, String mode) throws RemoteException,
83 FileNotFoundException {
84 return MockContentProvider.this.openAssetFile(url, mode);
85 }
86
Jeff Brownd2183652011-10-09 12:39:53 -070087 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090088 public ParcelFileDescriptor openFile(Uri url, String mode) throws RemoteException,
89 FileNotFoundException {
90 return MockContentProvider.this.openFile(url, mode);
91 }
92
Jeff Brownd2183652011-10-09 12:39:53 -070093 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090094 public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs,
95 String sortOrder) throws RemoteException {
96 return MockContentProvider.this.query(url, projection, selection,
97 selectionArgs, sortOrder);
98 }
99
Jeff Brownd2183652011-10-09 12:39:53 -0700100 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900101 public int update(Uri url, ContentValues values, String selection, String[] selectionArgs)
102 throws RemoteException {
103 return MockContentProvider.this.update(url, values, selection, selectionArgs);
104 }
105
Jeff Brownd2183652011-10-09 12:39:53 -0700106 @Override
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800107 public Bundle call(String method, String request, Bundle args)
108 throws RemoteException {
109 return MockContentProvider.this.call(method, request, args);
110 }
111
Jeff Brownd2183652011-10-09 12:39:53 -0700112 @Override
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900113 public IBinder asBinder() {
114 throw new UnsupportedOperationException();
115 }
116
Jeff Brownd2183652011-10-09 12:39:53 -0700117 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700118 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
119 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter);
120 }
121
Jeff Brownd2183652011-10-09 12:39:53 -0700122 @Override
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700123 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts)
124 throws RemoteException, FileNotFoundException {
125 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts);
126 }
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900127 }
128 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider();
129
130 /**
131 * A constructor using {@link MockContext} instance as a Context in it.
132 */
133 protected MockContentProvider() {
134 super(new MockContext(), "", "", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900137 /**
138 * A constructor accepting a Context instance, which is supposed to be the subclasss of
139 * {@link MockContext}.
140 */
141 public MockContentProvider(Context context) {
142 super(context, "", "", null);
143 }
144
145 /**
146 * A constructor which initialize four member variables which
147 * {@link android.content.ContentProvider} have internally.
148 *
149 * @param context A Context object which should be some mock instance (like the
150 * instance of {@link android.test.mock.MockContext}).
151 * @param readPermission The read permision you want this instance should have in the
152 * test, which is available via {@link #getReadPermission()}.
153 * @param writePermission The write permission you want this instance should have
154 * in the test, which is available via {@link #getWritePermission()}.
155 * @param pathPermissions The PathPermissions you want this instance should have
156 * in the test, which is available via {@link #getPathPermissions()}.
157 */
158 public MockContentProvider(Context context,
159 String readPermission,
160 String writePermission,
161 PathPermission[] pathPermissions) {
162 super(context, readPermission, writePermission, pathPermissions);
163 }
164
165 @Override
166 public int delete(Uri uri, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 throw new UnsupportedOperationException("unimplemented mock method");
168 }
169
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900170 @Override
171 public String getType(Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 throw new UnsupportedOperationException("unimplemented mock method");
173 }
174
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900175 @Override
176 public Uri insert(Uri uri, ContentValues values) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 throw new UnsupportedOperationException("unimplemented mock method");
178 }
179
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900180 @Override
181 public boolean onCreate() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 throw new UnsupportedOperationException("unimplemented mock method");
183 }
184
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900185 @Override
186 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
187 String sortOrder) {
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 int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 throw new UnsupportedOperationException("unimplemented mock method");
194 }
Fred Quintana89437372009-05-15 15:10:40 -0700195
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900196 /**
197 * If you're reluctant to implement this manually, please just call super.bulkInsert().
198 */
199 @Override
200 public int bulkInsert(Uri uri, ContentValues[] values) {
Fred Quintana89437372009-05-15 15:10:40 -0700201 throw new UnsupportedOperationException("unimplemented mock method");
202 }
203
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900204 @Override
205 public void attachInfo(Context context, ProviderInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 throw new UnsupportedOperationException("unimplemented mock method");
207 }
208
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900209 @Override
210 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) {
211 throw new UnsupportedOperationException("unimplemented mock method");
212 }
213
214 /**
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800215 * @hide
216 */
217 @Override
218 public Bundle call(String method, String request, Bundle args) {
219 throw new UnsupportedOperationException("unimplemented mock method call");
220 }
221
Dianne Hackborn23fdaf62010-08-06 12:16:55 -0700222 public String[] getStreamTypes(Uri url, String mimeTypeFilter) {
223 throw new UnsupportedOperationException("unimplemented mock method call");
224 }
225
226 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) {
227 throw new UnsupportedOperationException("unimplemented mock method call");
228 }
229
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800230 /**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900231 * Returns IContentProvider which calls back same methods in this class.
232 * By overriding this class, we avoid the mechanism hidden behind ContentProvider
233 * (IPC, etc.)
234 *
235 * @hide
236 */
237 @Override
238 public final IContentProvider getIContentProvider() {
239 return mIContentProvider;
240 }
Fred Quintanaf99e2e02009-12-09 16:00:40 -0800241}