The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 19 | import android.content.ContentProvider; |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 20 | import android.content.ContentProviderOperation; |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 21 | import android.content.ContentProviderResult; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.content.EntityIterator; |
| 25 | import android.content.IContentProvider; |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 26 | import android.content.OperationApplicationException; |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 27 | import android.content.pm.PathPermission; |
| 28 | import android.content.pm.ProviderInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.res.AssetFileDescriptor; |
| 30 | import android.database.Cursor; |
| 31 | import android.database.CursorWindow; |
| 32 | import android.database.IBulkCursor; |
| 33 | import android.database.IContentObserver; |
| 34 | import android.net.Uri; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 35 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.IBinder; |
| 37 | import android.os.ParcelFileDescriptor; |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 38 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | import java.io.FileNotFoundException; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 44 | * Mock implementation of ContentProvider. All methods are non-functional and throw |
| 45 | * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | * implement behavior needed for tests. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | */ |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 48 | public class MockContentProvider extends ContentProvider { |
| 49 | /* |
| 50 | * Note: if you add methods to ContentProvider, you must add similar methods to |
| 51 | * MockContentProvider. |
| 52 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 54 | /** |
| 55 | * IContentProvider that directs all calls to this MockContentProvider. |
| 56 | */ |
| 57 | private class InversionIContentProvider implements IContentProvider { |
| 58 | @SuppressWarnings("unused") |
| 59 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
| 60 | throws RemoteException, OperationApplicationException { |
| 61 | return MockContentProvider.this.applyBatch(operations); |
| 62 | } |
| 63 | |
| 64 | @SuppressWarnings("unused") |
| 65 | public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException { |
| 66 | return MockContentProvider.this.bulkInsert(url, initialValues); |
| 67 | } |
| 68 | |
| 69 | @SuppressWarnings("unused") |
| 70 | public IBulkCursor bulkQuery(Uri url, String[] projection, String selection, |
| 71 | String[] selectionArgs, String sortOrder, IContentObserver observer, |
| 72 | CursorWindow window) throws RemoteException { |
| 73 | throw new UnsupportedOperationException("Must not come here"); |
| 74 | } |
| 75 | |
| 76 | @SuppressWarnings("unused") |
| 77 | public int delete(Uri url, String selection, String[] selectionArgs) |
| 78 | throws RemoteException { |
| 79 | return MockContentProvider.this.delete(url, selection, selectionArgs); |
| 80 | } |
| 81 | |
| 82 | @SuppressWarnings("unused") |
| 83 | public String getType(Uri url) throws RemoteException { |
| 84 | return MockContentProvider.this.getType(url); |
| 85 | } |
| 86 | |
| 87 | @SuppressWarnings("unused") |
| 88 | public Uri insert(Uri url, ContentValues initialValues) throws RemoteException { |
| 89 | return MockContentProvider.this.insert(url, initialValues); |
| 90 | } |
| 91 | |
| 92 | @SuppressWarnings("unused") |
| 93 | public AssetFileDescriptor openAssetFile(Uri url, String mode) throws RemoteException, |
| 94 | FileNotFoundException { |
| 95 | return MockContentProvider.this.openAssetFile(url, mode); |
| 96 | } |
| 97 | |
| 98 | @SuppressWarnings("unused") |
| 99 | public ParcelFileDescriptor openFile(Uri url, String mode) throws RemoteException, |
| 100 | FileNotFoundException { |
| 101 | return MockContentProvider.this.openFile(url, mode); |
| 102 | } |
| 103 | |
| 104 | @SuppressWarnings("unused") |
| 105 | public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs, |
| 106 | String sortOrder) throws RemoteException { |
| 107 | return MockContentProvider.this.query(url, projection, selection, |
| 108 | selectionArgs, sortOrder); |
| 109 | } |
| 110 | |
| 111 | @SuppressWarnings("unused") |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 112 | public int update(Uri url, ContentValues values, String selection, String[] selectionArgs) |
| 113 | throws RemoteException { |
| 114 | return MockContentProvider.this.update(url, values, selection, selectionArgs); |
| 115 | } |
| 116 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 117 | /** |
| 118 | * @hide |
| 119 | */ |
| 120 | @SuppressWarnings("unused") |
| 121 | public Bundle call(String method, String request, Bundle args) |
| 122 | throws RemoteException { |
| 123 | return MockContentProvider.this.call(method, request, args); |
| 124 | } |
| 125 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 126 | public IBinder asBinder() { |
| 127 | throw new UnsupportedOperationException(); |
| 128 | } |
| 129 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 130 | @SuppressWarnings("unused") |
| 131 | public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { |
| 132 | return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); |
| 133 | } |
| 134 | |
| 135 | @SuppressWarnings("unused") |
| 136 | public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) |
| 137 | throws RemoteException, FileNotFoundException { |
| 138 | return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); |
| 139 | } |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 140 | } |
| 141 | private final InversionIContentProvider mIContentProvider = new InversionIContentProvider(); |
| 142 | |
| 143 | /** |
| 144 | * A constructor using {@link MockContext} instance as a Context in it. |
| 145 | */ |
| 146 | protected MockContentProvider() { |
| 147 | super(new MockContext(), "", "", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 150 | /** |
| 151 | * A constructor accepting a Context instance, which is supposed to be the subclasss of |
| 152 | * {@link MockContext}. |
| 153 | */ |
| 154 | public MockContentProvider(Context context) { |
| 155 | super(context, "", "", null); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * A constructor which initialize four member variables which |
| 160 | * {@link android.content.ContentProvider} have internally. |
| 161 | * |
| 162 | * @param context A Context object which should be some mock instance (like the |
| 163 | * instance of {@link android.test.mock.MockContext}). |
| 164 | * @param readPermission The read permision you want this instance should have in the |
| 165 | * test, which is available via {@link #getReadPermission()}. |
| 166 | * @param writePermission The write permission you want this instance should have |
| 167 | * in the test, which is available via {@link #getWritePermission()}. |
| 168 | * @param pathPermissions The PathPermissions you want this instance should have |
| 169 | * in the test, which is available via {@link #getPathPermissions()}. |
| 170 | */ |
| 171 | public MockContentProvider(Context context, |
| 172 | String readPermission, |
| 173 | String writePermission, |
| 174 | PathPermission[] pathPermissions) { |
| 175 | super(context, readPermission, writePermission, pathPermissions); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 181 | } |
| 182 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 183 | @Override |
| 184 | public String getType(Uri uri) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 186 | } |
| 187 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 188 | @Override |
| 189 | public Uri insert(Uri uri, ContentValues values) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 191 | } |
| 192 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 193 | @Override |
| 194 | public boolean onCreate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 196 | } |
| 197 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 198 | @Override |
| 199 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 200 | String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 202 | } |
| 203 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 204 | @Override |
| 205 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 207 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 208 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 209 | /** |
| 210 | * If you're reluctant to implement this manually, please just call super.bulkInsert(). |
| 211 | */ |
| 212 | @Override |
| 213 | public int bulkInsert(Uri uri, ContentValues[] values) { |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 214 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 215 | } |
| 216 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 217 | @Override |
| 218 | public void attachInfo(Context context, ProviderInfo info) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 220 | } |
| 221 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 222 | @Override |
| 223 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { |
| 224 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 225 | } |
| 226 | |
| 227 | /** |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 228 | * @hide |
| 229 | */ |
| 230 | @Override |
| 231 | public Bundle call(String method, String request, Bundle args) { |
| 232 | throw new UnsupportedOperationException("unimplemented mock method call"); |
| 233 | } |
| 234 | |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 235 | public String[] getStreamTypes(Uri url, String mimeTypeFilter) { |
| 236 | throw new UnsupportedOperationException("unimplemented mock method call"); |
| 237 | } |
| 238 | |
| 239 | public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) { |
| 240 | throw new UnsupportedOperationException("unimplemented mock method call"); |
| 241 | } |
| 242 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 243 | /** |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 244 | * Returns IContentProvider which calls back same methods in this class. |
| 245 | * By overriding this class, we avoid the mechanism hidden behind ContentProvider |
| 246 | * (IPC, etc.) |
| 247 | * |
| 248 | * @hide |
| 249 | */ |
| 250 | @Override |
| 251 | public final IContentProvider getIContentProvider() { |
| 252 | return mIContentProvider; |
| 253 | } |
Fred Quintana | f99e2e0 | 2009-12-09 16:00:40 -0800 | [diff] [blame] | 254 | } |