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.ParcelFileDescriptor; 34 import android.os.RemoteException; 35 36 import java.io.FileNotFoundException; 37 import java.util.ArrayList; 38 39 /** 40 * Mock implementation of ContentProvider. All methods are non-functional and throw 41 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to 42 * implement behavior needed for tests. 43 */ 44 public class MockContentProvider extends ContentProvider { 45 /* 46 * Note: if you add methods to ContentProvider, you must add similar methods to 47 * MockContentProvider. 48 */ 49 50 /** 51 * IContentProvider that directs all calls to this MockContentProvider. 52 */ 53 private class InversionIContentProvider implements IContentProvider { 54 @Override 55 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) 56 throws RemoteException, OperationApplicationException { 57 return MockContentProvider.this.applyBatch(operations); 58 } 59 60 @Override 61 public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException { 62 return MockContentProvider.this.bulkInsert(url, initialValues); 63 } 64 65 @Override 66 public int delete(Uri url, String selection, String[] selectionArgs) 67 throws RemoteException { 68 return MockContentProvider.this.delete(url, selection, selectionArgs); 69 } 70 71 @Override 72 public String getType(Uri url) throws RemoteException { 73 return MockContentProvider.this.getType(url); 74 } 75 76 @Override 77 public Uri insert(Uri url, ContentValues initialValues) throws RemoteException { 78 return MockContentProvider.this.insert(url, initialValues); 79 } 80 81 @Override 82 public AssetFileDescriptor openAssetFile(Uri url, String mode) throws RemoteException, 83 FileNotFoundException { 84 return MockContentProvider.this.openAssetFile(url, mode); 85 } 86 87 @Override 88 public ParcelFileDescriptor openFile(Uri url, String mode) throws RemoteException, 89 FileNotFoundException { 90 return MockContentProvider.this.openFile(url, mode); 91 } 92 93 @Override 94 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 100 @Override 101 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 106 @Override 107 public Bundle call(String method, String request, Bundle args) 108 throws RemoteException { 109 return MockContentProvider.this.call(method, request, args); 110 } 111 112 @Override 113 public IBinder asBinder() { 114 throw new UnsupportedOperationException(); 115 } 116 117 @Override 118 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { 119 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); 120 } 121 122 @Override 123 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) 124 throws RemoteException, FileNotFoundException { 125 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); 126 } 127 } 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); 135 } 136 137 /** 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) { 167 throw new UnsupportedOperationException("unimplemented mock method"); 168 } 169 170 @Override 171 public String getType(Uri uri) { 172 throw new UnsupportedOperationException("unimplemented mock method"); 173 } 174 175 @Override 176 public Uri insert(Uri uri, ContentValues values) { 177 throw new UnsupportedOperationException("unimplemented mock method"); 178 } 179 180 @Override 181 public boolean onCreate() { 182 throw new UnsupportedOperationException("unimplemented mock method"); 183 } 184 185 @Override 186 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 187 String sortOrder) { 188 throw new UnsupportedOperationException("unimplemented mock method"); 189 } 190 191 @Override 192 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 193 throw new UnsupportedOperationException("unimplemented mock method"); 194 } 195 196 /** 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) { 201 throw new UnsupportedOperationException("unimplemented mock method"); 202 } 203 204 @Override 205 public void attachInfo(Context context, ProviderInfo info) { 206 throw new UnsupportedOperationException("unimplemented mock method"); 207 } 208 209 @Override 210 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { 211 throw new UnsupportedOperationException("unimplemented mock method"); 212 } 213 214 /** 215 * @hide 216 */ 217 @Override 218 public Bundle call(String method, String request, Bundle args) { 219 throw new UnsupportedOperationException("unimplemented mock method call"); 220 } 221 222 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 230 /** 231 * 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 } 241 } 242