MockContentProvider.java revision 12093976a4842a795491cfd2b1d3b71e18503f2d
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.EntityIterator; 25import android.content.IContentProvider; 26import android.content.OperationApplicationException; 27import android.content.pm.PathPermission; 28import android.content.pm.ProviderInfo; 29import android.content.res.AssetFileDescriptor; 30import android.database.Cursor; 31import android.database.CursorWindow; 32import android.database.IBulkCursor; 33import android.database.IContentObserver; 34import android.net.Uri; 35import android.os.IBinder; 36import android.os.ParcelFileDescriptor; 37import android.os.RemoteException; 38 39import java.io.FileNotFoundException; 40import java.util.ArrayList; 41 42/** 43 * Mock implementation of ContentProvider. All methods are non-functional and throw 44 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to 45 * implement behavior needed for tests. 46 */ 47public class MockContentProvider extends ContentProvider { 48 /* 49 * Note: if you add methods to ContentProvider, you must add similar methods to 50 * MockContentProvider. 51 */ 52 53 /** 54 * IContentProvider that directs all calls to this MockContentProvider. 55 */ 56 private class InversionIContentProvider implements IContentProvider { 57 @SuppressWarnings("unused") 58 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) 59 throws RemoteException, OperationApplicationException { 60 return MockContentProvider.this.applyBatch(operations); 61 } 62 63 @SuppressWarnings("unused") 64 public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException { 65 return MockContentProvider.this.bulkInsert(url, initialValues); 66 } 67 68 @SuppressWarnings("unused") 69 public IBulkCursor bulkQuery(Uri url, String[] projection, String selection, 70 String[] selectionArgs, String sortOrder, IContentObserver observer, 71 CursorWindow window) throws RemoteException { 72 throw new UnsupportedOperationException("Must not come here"); 73 } 74 75 @SuppressWarnings("unused") 76 public int delete(Uri url, String selection, String[] selectionArgs) 77 throws RemoteException { 78 return MockContentProvider.this.delete(url, selection, selectionArgs); 79 } 80 81 @SuppressWarnings("unused") 82 public String getType(Uri url) throws RemoteException { 83 return MockContentProvider.this.getType(url); 84 } 85 86 @SuppressWarnings("unused") 87 public Uri insert(Uri url, ContentValues initialValues) throws RemoteException { 88 return MockContentProvider.this.insert(url, initialValues); 89 } 90 91 @SuppressWarnings("unused") 92 public AssetFileDescriptor openAssetFile(Uri url, String mode) throws RemoteException, 93 FileNotFoundException { 94 return MockContentProvider.this.openAssetFile(url, mode); 95 } 96 97 @SuppressWarnings("unused") 98 public ParcelFileDescriptor openFile(Uri url, String mode) throws RemoteException, 99 FileNotFoundException { 100 return MockContentProvider.this.openFile(url, mode); 101 } 102 103 @SuppressWarnings("unused") 104 public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs, 105 String sortOrder) throws RemoteException { 106 return MockContentProvider.this.query(url, projection, selection, 107 selectionArgs, sortOrder); 108 } 109 110 @SuppressWarnings("unused") 111 public int update(Uri url, ContentValues values, String selection, String[] selectionArgs) 112 throws RemoteException { 113 return MockContentProvider.this.update(url, values, selection, selectionArgs); 114 } 115 116 public IBinder asBinder() { 117 throw new UnsupportedOperationException(); 118 } 119 120 } 121 private final InversionIContentProvider mIContentProvider = new InversionIContentProvider(); 122 123 /** 124 * A constructor using {@link MockContext} instance as a Context in it. 125 */ 126 protected MockContentProvider() { 127 super(new MockContext(), "", "", null); 128 } 129 130 /** 131 * A constructor accepting a Context instance, which is supposed to be the subclasss of 132 * {@link MockContext}. 133 */ 134 public MockContentProvider(Context context) { 135 super(context, "", "", null); 136 } 137 138 /** 139 * A constructor which initialize four member variables which 140 * {@link android.content.ContentProvider} have internally. 141 * 142 * @param context A Context object which should be some mock instance (like the 143 * instance of {@link android.test.mock.MockContext}). 144 * @param readPermission The read permision you want this instance should have in the 145 * test, which is available via {@link #getReadPermission()}. 146 * @param writePermission The write permission you want this instance should have 147 * in the test, which is available via {@link #getWritePermission()}. 148 * @param pathPermissions The PathPermissions you want this instance should have 149 * in the test, which is available via {@link #getPathPermissions()}. 150 */ 151 public MockContentProvider(Context context, 152 String readPermission, 153 String writePermission, 154 PathPermission[] pathPermissions) { 155 super(context, readPermission, writePermission, pathPermissions); 156 } 157 158 @Override 159 public int delete(Uri uri, String selection, String[] selectionArgs) { 160 throw new UnsupportedOperationException("unimplemented mock method"); 161 } 162 163 @Override 164 public String getType(Uri uri) { 165 throw new UnsupportedOperationException("unimplemented mock method"); 166 } 167 168 @Override 169 public Uri insert(Uri uri, ContentValues values) { 170 throw new UnsupportedOperationException("unimplemented mock method"); 171 } 172 173 @Override 174 public boolean onCreate() { 175 throw new UnsupportedOperationException("unimplemented mock method"); 176 } 177 178 @Override 179 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 180 String sortOrder) { 181 throw new UnsupportedOperationException("unimplemented mock method"); 182 } 183 184 @Override 185 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 186 throw new UnsupportedOperationException("unimplemented mock method"); 187 } 188 189 /** 190 * If you're reluctant to implement this manually, please just call super.bulkInsert(). 191 */ 192 @Override 193 public int bulkInsert(Uri uri, ContentValues[] values) { 194 throw new UnsupportedOperationException("unimplemented mock method"); 195 } 196 197 @Override 198 public void attachInfo(Context context, ProviderInfo info) { 199 throw new UnsupportedOperationException("unimplemented mock method"); 200 } 201 202 @Override 203 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { 204 throw new UnsupportedOperationException("unimplemented mock method"); 205 } 206 207 /** 208 * Returns IContentProvider which calls back same methods in this class. 209 * By overriding this class, we avoid the mechanism hidden behind ContentProvider 210 * (IPC, etc.) 211 * 212 * @hide 213 */ 214 @Override 215 public final IContentProvider getIContentProvider() { 216 return mIContentProvider; 217 } 218} 219