1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 19 import android.content.ContentProvider; 20 import android.content.ContentProviderOperation; 21 import android.content.ContentProviderResult; 22 import android.content.ContentValues; 23 import android.content.Context; 24 import android.content.IContentProvider; 25 import android.content.OperationApplicationException; 26 import android.content.pm.PathPermission; 27 import android.content.pm.ProviderInfo; 28 import android.content.res.AssetFileDescriptor; 29 import android.database.Cursor; 30 import android.net.Uri; 31 import android.os.Bundle; 32 import android.os.IBinder; 33 import android.os.ICancellationSignal; 34 import android.os.ParcelFileDescriptor; 35 import android.os.RemoteException; 36 37 import java.io.FileNotFoundException; 38 import java.util.ArrayList; 39 40 /** 41 * Mock implementation of ContentProvider. All methods are non-functional and throw 42 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to 43 * implement behavior needed for tests. 44 */ 45 public class MockContentProvider extends ContentProvider { 46 /* 47 * Note: if you add methods to ContentProvider, you must add similar methods to 48 * MockContentProvider. 49 */ 50 51 /** 52 * IContentProvider that directs all calls to this MockContentProvider. 53 */ 54 private class InversionIContentProvider implements IContentProvider { 55 @Override 56 public ContentProviderResult[] applyBatch(String callingPackage, 57 ArrayList<ContentProviderOperation> operations) 58 throws RemoteException, OperationApplicationException { 59 return MockContentProvider.this.applyBatch(operations); 60 } 61 62 @Override 63 public int bulkInsert(String callingPackage, Uri url, ContentValues[] initialValues) 64 throws RemoteException { 65 return MockContentProvider.this.bulkInsert(url, initialValues); 66 } 67 68 @Override 69 public int delete(String callingPackage, Uri url, String selection, String[] selectionArgs) 70 throws RemoteException { 71 return MockContentProvider.this.delete(url, selection, selectionArgs); 72 } 73 74 @Override 75 public String getType(Uri url) throws RemoteException { 76 return MockContentProvider.this.getType(url); 77 } 78 79 @Override 80 public Uri insert(String callingPackage, Uri url, ContentValues initialValues) 81 throws RemoteException { 82 return MockContentProvider.this.insert(url, initialValues); 83 } 84 85 @Override 86 public AssetFileDescriptor openAssetFile( 87 String callingPackage, Uri url, String mode, ICancellationSignal signal) 88 throws RemoteException, FileNotFoundException { 89 return MockContentProvider.this.openAssetFile(url, mode); 90 } 91 92 @Override 93 public ParcelFileDescriptor openFile( 94 String callingPackage, Uri url, String mode, ICancellationSignal signal, 95 IBinder callerToken) throws RemoteException, FileNotFoundException { 96 return MockContentProvider.this.openFile(url, mode); 97 } 98 99 @Override 100 public Cursor query(String callingPackage, Uri url, String[] projection, String selection, 101 String[] selectionArgs, 102 String sortOrder, ICancellationSignal cancellationSignal) throws RemoteException { 103 return MockContentProvider.this.query(url, projection, selection, 104 selectionArgs, sortOrder); 105 } 106 107 @Override 108 public int update(String callingPackage, Uri url, ContentValues values, String selection, 109 String[] selectionArgs) throws RemoteException { 110 return MockContentProvider.this.update(url, values, selection, selectionArgs); 111 } 112 113 @Override 114 public Bundle call(String callingPackage, String method, String request, Bundle args) 115 throws RemoteException { 116 return MockContentProvider.this.call(method, request, args); 117 } 118 119 @Override 120 public IBinder asBinder() { 121 throw new UnsupportedOperationException(); 122 } 123 124 @Override 125 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { 126 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); 127 } 128 129 @Override 130 public AssetFileDescriptor openTypedAssetFile(String callingPackage, Uri url, 131 String mimeType, Bundle opts, ICancellationSignal signal) 132 throws RemoteException, FileNotFoundException { 133 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); 134 } 135 136 @Override 137 public ICancellationSignal createCancellationSignal() throws RemoteException { 138 return null; 139 } 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 } 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); 158 } 159 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) { 190 throw new UnsupportedOperationException("unimplemented mock method"); 191 } 192 193 @Override 194 public String getType(Uri uri) { 195 throw new UnsupportedOperationException("unimplemented mock method"); 196 } 197 198 @Override 199 public Uri insert(Uri uri, ContentValues values) { 200 throw new UnsupportedOperationException("unimplemented mock method"); 201 } 202 203 @Override 204 public boolean onCreate() { 205 throw new UnsupportedOperationException("unimplemented mock method"); 206 } 207 208 @Override 209 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 210 String sortOrder) { 211 throw new UnsupportedOperationException("unimplemented mock method"); 212 } 213 214 @Override 215 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 216 throw new UnsupportedOperationException("unimplemented mock method"); 217 } 218 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) { 224 throw new UnsupportedOperationException("unimplemented mock method"); 225 } 226 227 @Override 228 public void attachInfo(Context context, ProviderInfo info) { 229 throw new UnsupportedOperationException("unimplemented mock method"); 230 } 231 232 @Override 233 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { 234 throw new UnsupportedOperationException("unimplemented mock method"); 235 } 236 237 /** 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 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 253 /** 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 } 264 } 265