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 17package android.test.mock; 18 19import android.content.ContentProvider; 20import android.content.ContentProviderOperation; 21import android.content.ContentProviderResult; 22import android.content.ContentValues; 23import android.content.Context; 24import android.content.IContentProvider; 25import android.content.OperationApplicationException; 26import android.content.pm.PathPermission; 27import android.content.pm.ProviderInfo; 28import android.content.res.AssetFileDescriptor; 29import android.database.Cursor; 30import android.net.Uri; 31import android.os.Bundle; 32import android.os.IBinder; 33import android.os.ICancellationSignal; 34import android.os.ParcelFileDescriptor; 35import android.os.RemoteException; 36 37import java.io.FileNotFoundException; 38import 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 */ 45public 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(ArrayList<ContentProviderOperation> operations) 57 throws RemoteException, OperationApplicationException { 58 return MockContentProvider.this.applyBatch(operations); 59 } 60 61 @Override 62 public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException { 63 return MockContentProvider.this.bulkInsert(url, initialValues); 64 } 65 66 @Override 67 public int delete(Uri url, String selection, String[] selectionArgs) 68 throws RemoteException { 69 return MockContentProvider.this.delete(url, selection, selectionArgs); 70 } 71 72 @Override 73 public String getType(Uri url) throws RemoteException { 74 return MockContentProvider.this.getType(url); 75 } 76 77 @Override 78 public Uri insert(Uri url, ContentValues initialValues) throws RemoteException { 79 return MockContentProvider.this.insert(url, initialValues); 80 } 81 82 @Override 83 public AssetFileDescriptor openAssetFile(Uri url, String mode) throws RemoteException, 84 FileNotFoundException { 85 return MockContentProvider.this.openAssetFile(url, mode); 86 } 87 88 @Override 89 public ParcelFileDescriptor openFile(Uri url, String mode) throws RemoteException, 90 FileNotFoundException { 91 return MockContentProvider.this.openFile(url, mode); 92 } 93 94 @Override 95 public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs, 96 String sortOrder, ICancellationSignal cancellationSignal) throws RemoteException { 97 return MockContentProvider.this.query(url, projection, selection, 98 selectionArgs, sortOrder); 99 } 100 101 @Override 102 public int update(Uri url, ContentValues values, String selection, String[] selectionArgs) 103 throws RemoteException { 104 return MockContentProvider.this.update(url, values, selection, selectionArgs); 105 } 106 107 @Override 108 public Bundle call(String method, String request, Bundle args) 109 throws RemoteException { 110 return MockContentProvider.this.call(method, request, args); 111 } 112 113 @Override 114 public IBinder asBinder() { 115 throw new UnsupportedOperationException(); 116 } 117 118 @Override 119 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { 120 return MockContentProvider.this.getStreamTypes(url, mimeTypeFilter); 121 } 122 123 @Override 124 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) 125 throws RemoteException, FileNotFoundException { 126 return MockContentProvider.this.openTypedAssetFile(url, mimeType, opts); 127 } 128 129 @Override 130 public ICancellationSignal createCancellationSignal() throws RemoteException { 131 return null; 132 } 133 } 134 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider(); 135 136 /** 137 * A constructor using {@link MockContext} instance as a Context in it. 138 */ 139 protected MockContentProvider() { 140 super(new MockContext(), "", "", null); 141 } 142 143 /** 144 * A constructor accepting a Context instance, which is supposed to be the subclasss of 145 * {@link MockContext}. 146 */ 147 public MockContentProvider(Context context) { 148 super(context, "", "", null); 149 } 150 151 /** 152 * A constructor which initialize four member variables which 153 * {@link android.content.ContentProvider} have internally. 154 * 155 * @param context A Context object which should be some mock instance (like the 156 * instance of {@link android.test.mock.MockContext}). 157 * @param readPermission The read permision you want this instance should have in the 158 * test, which is available via {@link #getReadPermission()}. 159 * @param writePermission The write permission you want this instance should have 160 * in the test, which is available via {@link #getWritePermission()}. 161 * @param pathPermissions The PathPermissions you want this instance should have 162 * in the test, which is available via {@link #getPathPermissions()}. 163 */ 164 public MockContentProvider(Context context, 165 String readPermission, 166 String writePermission, 167 PathPermission[] pathPermissions) { 168 super(context, readPermission, writePermission, pathPermissions); 169 } 170 171 @Override 172 public int delete(Uri uri, String selection, String[] selectionArgs) { 173 throw new UnsupportedOperationException("unimplemented mock method"); 174 } 175 176 @Override 177 public String getType(Uri uri) { 178 throw new UnsupportedOperationException("unimplemented mock method"); 179 } 180 181 @Override 182 public Uri insert(Uri uri, ContentValues values) { 183 throw new UnsupportedOperationException("unimplemented mock method"); 184 } 185 186 @Override 187 public boolean onCreate() { 188 throw new UnsupportedOperationException("unimplemented mock method"); 189 } 190 191 @Override 192 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 193 String sortOrder) { 194 throw new UnsupportedOperationException("unimplemented mock method"); 195 } 196 197 @Override 198 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 199 throw new UnsupportedOperationException("unimplemented mock method"); 200 } 201 202 /** 203 * If you're reluctant to implement this manually, please just call super.bulkInsert(). 204 */ 205 @Override 206 public int bulkInsert(Uri uri, ContentValues[] values) { 207 throw new UnsupportedOperationException("unimplemented mock method"); 208 } 209 210 @Override 211 public void attachInfo(Context context, ProviderInfo info) { 212 throw new UnsupportedOperationException("unimplemented mock method"); 213 } 214 215 @Override 216 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { 217 throw new UnsupportedOperationException("unimplemented mock method"); 218 } 219 220 /** 221 * @hide 222 */ 223 @Override 224 public Bundle call(String method, String request, Bundle args) { 225 throw new UnsupportedOperationException("unimplemented mock method call"); 226 } 227 228 public String[] getStreamTypes(Uri url, String mimeTypeFilter) { 229 throw new UnsupportedOperationException("unimplemented mock method call"); 230 } 231 232 public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts) { 233 throw new UnsupportedOperationException("unimplemented mock method call"); 234 } 235 236 /** 237 * Returns IContentProvider which calls back same methods in this class. 238 * By overriding this class, we avoid the mechanism hidden behind ContentProvider 239 * (IPC, etc.) 240 * 241 * @hide 242 */ 243 @Override 244 public final IContentProvider getIContentProvider() { 245 return mIContentProvider; 246 } 247} 248