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 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 20 | import android.content.ContentProvider; |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 21 | import android.content.ContentProviderOperation; |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 22 | import android.content.ContentProviderResult; |
| 23 | import android.content.ContentValues; |
| 24 | import android.content.Context; |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.net.Uri; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 32 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.os.IBinder; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 34 | import android.os.ICancellationSignal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.ParcelFileDescriptor; |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 36 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | import java.io.FileNotFoundException; |
Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 42 | * Mock implementation of ContentProvider. All methods are non-functional and throw |
| 43 | * {@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] | 44 | * implement behavior needed for tests. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | */ |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 46 | public class MockContentProvider extends ContentProvider { |
| 47 | /* |
| 48 | * Note: if you add methods to ContentProvider, you must add similar methods to |
| 49 | * MockContentProvider. |
| 50 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 52 | /** |
| 53 | * IContentProvider that directs all calls to this MockContentProvider. |
| 54 | */ |
| 55 | private class InversionIContentProvider implements IContentProvider { |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 56 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 57 | public ContentProviderResult[] applyBatch(String callingPackage, |
| 58 | ArrayList<ContentProviderOperation> operations) |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 59 | throws RemoteException, OperationApplicationException { |
| 60 | return MockContentProvider.this.applyBatch(operations); |
| 61 | } |
| 62 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 63 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 64 | public int bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues) |
| 65 | throws RemoteException { |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 66 | return MockContentProvider.this.bulkInsert(url, initialValues); |
| 67 | } |
| 68 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 69 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 70 | public int delete(String callingPackage, Uri url, String selection, String[] selectionArgs) |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 71 | throws RemoteException { |
| 72 | return MockContentProvider.this.delete(url, selection, selectionArgs); |
| 73 | } |
| 74 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 75 | @Override |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 76 | public String getType(Uri url) throws RemoteException { |
| 77 | return MockContentProvider.this.getType(url); |
| 78 | } |
| 79 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 80 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 81 | public Uri insert(String callingPackage, Uri url, ContentValues initialValues) |
| 82 | throws RemoteException { |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 83 | return MockContentProvider.this.insert(url, initialValues); |
| 84 | } |
| 85 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 86 | @Override |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 87 | public AssetFileDescriptor openAssetFile( |
| 88 | String callingPackage, Uri url, String mode, ICancellationSignal signal) |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 89 | throws RemoteException, FileNotFoundException { |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 90 | return MockContentProvider.this.openAssetFile(url, mode); |
| 91 | } |
| 92 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 93 | @Override |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 94 | public ParcelFileDescriptor openFile( |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 95 | String callingPackage, Uri url, String mode, ICancellationSignal signal, |
| 96 | IBinder callerToken) throws RemoteException, FileNotFoundException { |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 97 | return MockContentProvider.this.openFile(url, mode); |
| 98 | } |
| 99 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 100 | @Override |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 101 | public Cursor query(String callingPackage, Uri url, @Nullable String[] projection, |
| 102 | @Nullable Bundle queryArgs, |
| 103 | @Nullable ICancellationSignal cancellationSignal) |
| 104 | throws RemoteException { |
| 105 | return MockContentProvider.this.query(url, projection, queryArgs, null); |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 108 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 109 | public int update(String callingPackage, Uri url, ContentValues values, String selection, |
| 110 | String[] selectionArgs) throws RemoteException { |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 111 | return MockContentProvider.this.update(url, values, selection, selectionArgs); |
| 112 | } |
| 113 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 114 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 115 | public Bundle call(String callingPackage, String method, String request, Bundle args) |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 116 | throws RemoteException { |
| 117 | return MockContentProvider.this.call(method, request, args); |
| 118 | } |
| 119 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 120 | @Override |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 121 | public IBinder asBinder() { |
| 122 | throw new UnsupportedOperationException(); |
| 123 | } |
| 124 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 125 | @Override |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 126 | public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { |
| 127 | return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); |
| 128 | } |
| 129 | |
Jeff Brown | d218365 | 2011-10-09 12:39:53 -0700 | [diff] [blame] | 130 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 131 | public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url, |
Jeff Sharkey | bd3b902 | 2013-08-20 15:20:04 -0700 | [diff] [blame] | 132 | String mimeType, Bundle opts, ICancellationSignal signal) |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 133 | throws RemoteException, FileNotFoundException { |
| 134 | return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); |
| 135 | } |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 136 | |
| 137 | @Override |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 138 | public ICancellationSignal createCancellationSignal() throws RemoteException { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 139 | return null; |
| 140 | } |
Dianne Hackborn | 38ed2a4 | 2013-09-06 16:17:22 -0700 | [diff] [blame] | 141 | |
| 142 | @Override |
| 143 | public Uri canonicalize(String callingPkg, Uri uri) throws RemoteException { |
| 144 | return MockContentProvider.this.canonicalize(uri); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Uri uncanonicalize(String callingPkg, Uri uri) throws RemoteException { |
| 149 | return MockContentProvider.this.uncanonicalize(uri); |
| 150 | } |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 151 | |
| 152 | @Override |
| 153 | public boolean refresh(String callingPkg, Uri url, Bundle args, |
| 154 | ICancellationSignal cancellationSignal) throws RemoteException { |
| 155 | return MockContentProvider.this.refresh(url, args); |
| 156 | } |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 157 | } |
| 158 | private final InversionIContentProvider mIContentProvider = new InversionIContentProvider(); |
| 159 | |
| 160 | /** |
| 161 | * A constructor using {@link MockContext} instance as a Context in it. |
| 162 | */ |
| 163 | protected MockContentProvider() { |
| 164 | super(new MockContext(), "", "", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 167 | /** |
| 168 | * A constructor accepting a Context instance, which is supposed to be the subclasss of |
| 169 | * {@link MockContext}. |
| 170 | */ |
| 171 | public MockContentProvider(Context context) { |
| 172 | super(context, "", "", null); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * A constructor which initialize four member variables which |
| 177 | * {@link android.content.ContentProvider} have internally. |
| 178 | * |
| 179 | * @param context A Context object which should be some mock instance (like the |
| 180 | * instance of {@link android.test.mock.MockContext}). |
| 181 | * @param readPermission The read permision you want this instance should have in the |
| 182 | * test, which is available via {@link #getReadPermission()}. |
| 183 | * @param writePermission The write permission you want this instance should have |
| 184 | * in the test, which is available via {@link #getWritePermission()}. |
| 185 | * @param pathPermissions The PathPermissions you want this instance should have |
| 186 | * in the test, which is available via {@link #getPathPermissions()}. |
| 187 | */ |
| 188 | public MockContentProvider(Context context, |
| 189 | String readPermission, |
| 190 | String writePermission, |
| 191 | PathPermission[] pathPermissions) { |
| 192 | super(context, readPermission, writePermission, pathPermissions); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 198 | } |
| 199 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 200 | @Override |
| 201 | public String getType(Uri uri) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 203 | } |
| 204 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 205 | @Override |
| 206 | public Uri insert(Uri uri, ContentValues values) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 208 | } |
| 209 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 210 | @Override |
| 211 | public boolean onCreate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 213 | } |
| 214 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 215 | @Override |
| 216 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 217 | String sortOrder) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 219 | } |
| 220 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 221 | @Override |
| 222 | 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] | 223 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 224 | } |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 225 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 226 | /** |
| 227 | * If you're reluctant to implement this manually, please just call super.bulkInsert(). |
| 228 | */ |
| 229 | @Override |
| 230 | public int bulkInsert(Uri uri, ContentValues[] values) { |
Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 231 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 232 | } |
| 233 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 234 | @Override |
| 235 | public void attachInfo(Context context, ProviderInfo info) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 237 | } |
| 238 | |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 239 | @Override |
| 240 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { |
| 241 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 242 | } |
| 243 | |
| 244 | /** |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 245 | * @hide |
| 246 | */ |
| 247 | @Override |
| 248 | public Bundle call(String method, String request, Bundle args) { |
| 249 | throw new UnsupportedOperationException("unimplemented mock method call"); |
| 250 | } |
| 251 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 252 | @Override |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 253 | public String[] getStreamTypes(Uri url, String mimeTypeFilter) { |
| 254 | throw new UnsupportedOperationException("unimplemented mock method call"); |
| 255 | } |
| 256 | |
Steve McKay | ea93fe7 | 2016-12-02 11:35:35 -0800 | [diff] [blame] | 257 | @Override |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 258 | public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) { |
| 259 | throw new UnsupportedOperationException("unimplemented mock method call"); |
| 260 | } |
| 261 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 262 | /** |
Ben Lin | 1cf454f | 2016-11-10 13:50:54 -0800 | [diff] [blame] | 263 | * @hide |
| 264 | */ |
| 265 | public boolean refresh(Uri url, Bundle args) { |
| 266 | throw new UnsupportedOperationException("unimplemented mock method call"); |
| 267 | } |
| 268 | |
| 269 | /** |
Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 270 | * Returns IContentProvider which calls back same methods in this class. |
| 271 | * By overriding this class, we avoid the mechanism hidden behind ContentProvider |
| 272 | * (IPC, etc.) |
| 273 | * |
| 274 | * @hide |
| 275 | */ |
| 276 | @Override |
| 277 | public final IContentProvider getIContentProvider() { |
| 278 | return mIContentProvider; |
| 279 | } |
Paul Duffin | 772b692 | 2017-12-22 16:13:15 +0000 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use |
| 283 | * when directly instantiating the provider for testing. |
| 284 | * |
| 285 | * <p>Provided for use by {@code android.test.ProviderTestCase2} and |
| 286 | * {@code android.test.RenamingDelegatingContext}. |
| 287 | * |
| 288 | * @deprecated Use a mocking framework like <a href="https://github.com/mockito/mockito">Mockito</a>. |
| 289 | * New tests should be written using the |
| 290 | * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>. |
| 291 | */ |
| 292 | @Deprecated |
| 293 | public static void attachInfoForTesting( |
| 294 | ContentProvider provider, Context context, ProviderInfo providerInfo) { |
| 295 | provider.attachInfoForTesting(context, providerInfo); |
| 296 | } |
Fred Quintana | f99e2e0 | 2009-12-09 16:00:40 -0800 | [diff] [blame] | 297 | } |