| 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; |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 24 | import android.content.IContentProvider; |
| Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 25 | import android.content.OperationApplicationException; |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 26 | import android.content.pm.PathPermission; |
| 27 | import android.content.pm.ProviderInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.res.AssetFileDescriptor; |
| 29 | import android.database.Cursor; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.net.Uri; |
| Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 31 | import android.os.Bundle; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.IBinder; |
| Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 33 | import android.os.ICancellationSignal; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.ParcelFileDescriptor; |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 35 | import android.os.RemoteException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | import java.io.FileNotFoundException; |
| Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 41 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | * implement behavior needed for tests. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | */ |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 45 | public 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 51 | /** |
| 52 | * IContentProvider that directs all calls to this MockContentProvider. |
| 53 | */ |
| 54 | private class InversionIContentProvider implements IContentProvider { |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 55 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 56 | public ContentProviderResult[] applyBatch(String callingPackage, |
| 57 | ArrayList<ContentProviderOperation> operations) |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 58 | throws RemoteException, OperationApplicationException { |
| 59 | return MockContentProvider.this.applyBatch(operations); |
| 60 | } |
| 61 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 62 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 63 | public int bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues) |
| 64 | throws RemoteException { |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 65 | return MockContentProvider.this.bulkInsert(url, initialValues); |
| 66 | } |
| 67 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 68 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 69 | public int delete(String callingPackage, Uri url, String selection, String[] selectionArgs) |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 70 | throws RemoteException { |
| 71 | return MockContentProvider.this.delete(url, selection, selectionArgs); |
| 72 | } |
| 73 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 74 | @Override |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 75 | public String getType(Uri url) throws RemoteException { |
| 76 | return MockContentProvider.this.getType(url); |
| 77 | } |
| 78 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 79 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 80 | public Uri insert(String callingPackage, Uri url, ContentValues initialValues) |
| 81 | throws RemoteException { |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 82 | return MockContentProvider.this.insert(url, initialValues); |
| 83 | } |
| 84 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 85 | @Override |
| Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 86 | public AssetFileDescriptor openAssetFile( |
| 87 | String callingPackage, Uri url, String mode, ICancellationSignal signal) |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 88 | throws RemoteException, FileNotFoundException { |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 89 | return MockContentProvider.this.openAssetFile(url, mode); |
| 90 | } |
| 91 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 92 | @Override |
| Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 93 | public ParcelFileDescriptor openFile( |
| Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 94 | String callingPackage, Uri url, String mode, ICancellationSignal signal, |
| 95 | IBinder callerToken) throws RemoteException, FileNotFoundException { |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 96 | return MockContentProvider.this.openFile(url, mode); |
| 97 | } |
| 98 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 99 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 100 | public Cursor query(String callingPackage, Uri url, String[] projection, String selection, |
| 101 | String[] selectionArgs, |
| Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 102 | String sortOrder, ICancellationSignal cancellationSignal) throws RemoteException { |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 103 | return MockContentProvider.this.query(url, projection, selection, |
| 104 | selectionArgs, sortOrder); |
| 105 | } |
| 106 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 107 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 108 | public int update(String callingPackage, Uri url, ContentValues values, String selection, |
| 109 | String[] selectionArgs) throws RemoteException { |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 110 | return MockContentProvider.this.update(url, values, selection, selectionArgs); |
| 111 | } |
| 112 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 113 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 114 | public Bundle call(String callingPackage, String method, String request, Bundle args) |
| Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 115 | throws RemoteException { |
| 116 | return MockContentProvider.this.call(method, request, args); |
| 117 | } |
| 118 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 119 | @Override |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 120 | public IBinder asBinder() { |
| 121 | throw new UnsupportedOperationException(); |
| 122 | } |
| 123 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 124 | @Override |
| Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 125 | public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { |
| 126 | return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); |
| 127 | } |
| 128 | |
| Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 129 | @Override |
| Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 130 | public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url, |
| Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 131 | String mimeType, Bundle opts, ICancellationSignal signal) |
| Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 132 | throws RemoteException, FileNotFoundException { |
| 133 | return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); |
| 134 | } |
| Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 135 | |
| 136 | @Override |
| Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 137 | public ICancellationSignal createCancellationSignal() throws RemoteException { |
| Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 138 | return null; |
| 139 | } |
| Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 140 | |
| 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 Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 150 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 160 | /** |
| 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 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 String getType(Uri uri) { |
| 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 Uri insert(Uri uri, ContentValues values) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 201 | } |
| 202 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 203 | @Override |
| 204 | public boolean onCreate() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 206 | } |
| 207 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 208 | @Override |
| 209 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 210 | String sortOrder) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 212 | } |
| 213 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 214 | @Override |
| 215 | 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] | 216 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 217 | } |
| Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 218 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 219 | /** |
| 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 Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 224 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 225 | } |
| 226 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 227 | @Override |
| 228 | public void attachInfo(Context context, ProviderInfo info) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 230 | } |
| 231 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 232 | @Override |
| 233 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { |
| 234 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 235 | } |
| 236 | |
| 237 | /** |
| Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 238 | * @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 Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 245 | 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 Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 253 | /** |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 254 | * 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 Quintana | f99e2e0 | 2009-12-09 16:00:40 -0800 | [diff] [blame] | 264 | } |