| 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 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 19 | import android.content.ContentProvider; |
| Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 20 | import android.content.ContentProviderOperation; |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 21 | import android.content.ContentProviderResult; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.content.EntityIterator; |
| 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; |
| 31 | import android.database.CursorWindow; |
| 32 | import android.database.IBulkCursor; |
| 33 | import android.database.IContentObserver; |
| 34 | import android.net.Uri; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.IBinder; |
| 36 | import android.os.ParcelFileDescriptor; |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 37 | import android.os.RemoteException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | import java.io.FileNotFoundException; |
| Fred Quintana | 03d9490 | 2009-05-22 14:23:31 -0700 | [diff] [blame] | 40 | import java.util.ArrayList; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 43 | * Mock implementation of ContentProvider. All methods are non-functional and throw |
| 44 | * {@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] | 45 | * implement behavior needed for tests. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | */ |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 47 | public class MockContentProvider extends ContentProvider { |
| 48 | /* |
| 49 | * Note: if you add methods to ContentProvider, you must add similar methods to |
| 50 | * MockContentProvider. |
| 51 | */ |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 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 EntityIterator queryEntities(Uri url, String selection, String[] selectionArgs, |
| 112 | String sortOrder) throws RemoteException { |
| 113 | return MockContentProvider.this.queryEntities( |
| 114 | url, selection, selectionArgs, sortOrder); |
| 115 | } |
| 116 | |
| 117 | @SuppressWarnings("unused") |
| 118 | public int update(Uri url, ContentValues values, String selection, String[] selectionArgs) |
| 119 | throws RemoteException { |
| 120 | return MockContentProvider.this.update(url, values, selection, selectionArgs); |
| 121 | } |
| 122 | |
| 123 | public IBinder asBinder() { |
| 124 | throw new UnsupportedOperationException(); |
| 125 | } |
| 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); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 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) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 168 | } |
| 169 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 170 | @Override |
| 171 | public String getType(Uri uri) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 173 | } |
| 174 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 175 | @Override |
| 176 | public Uri insert(Uri uri, ContentValues values) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 178 | } |
| 179 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 180 | @Override |
| 181 | public boolean onCreate() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 183 | } |
| 184 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 185 | @Override |
| 186 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 187 | String sortOrder) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 189 | } |
| 190 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 191 | @Override |
| 192 | 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] | 193 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 194 | } |
| Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 195 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 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) { |
| Fred Quintana | 8943737 | 2009-05-15 15:10:40 -0700 | [diff] [blame] | 201 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 202 | } |
| 203 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 204 | @Override |
| 205 | public void attachInfo(Context context, ProviderInfo info) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 207 | } |
| 208 | |
| Daisuke Miyakawa | 8280c2b | 2009-10-22 08:36:42 +0900 | [diff] [blame] | 209 | @Override |
| 210 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) { |
| 211 | throw new UnsupportedOperationException("unimplemented mock method"); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns IContentProvider which calls back same methods in this class. |
| 216 | * By overriding this class, we avoid the mechanism hidden behind ContentProvider |
| 217 | * (IPC, etc.) |
| 218 | * |
| 219 | * @hide |
| 220 | */ |
| 221 | @Override |
| 222 | public final IContentProvider getIContentProvider() { |
| 223 | return mIContentProvider; |
| 224 | } |
| Fred Quintana | f99e2e0 | 2009-12-09 16:00:40 -0800 | [diff] [blame^] | 225 | } |