blob: 55be8d92029f14fe505d02fb08e427054c306ec1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +09002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
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
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090019import android.content.ContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070020import android.content.ContentProviderOperation;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090021import android.content.ContentProviderResult;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.EntityIterator;
25import android.content.IContentProvider;
Fred Quintana89437372009-05-15 15:10:40 -070026import android.content.OperationApplicationException;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090027import android.content.pm.PathPermission;
28import android.content.pm.ProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.AssetFileDescriptor;
30import android.database.Cursor;
31import android.database.CursorWindow;
32import android.database.IBulkCursor;
33import android.database.IContentObserver;
34import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.IBinder;
36import android.os.ParcelFileDescriptor;
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090037import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39import java.io.FileNotFoundException;
Fred Quintana03d94902009-05-22 14:23:31 -070040import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42/**
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090043 * 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 Project9066cfe2009-03-03 19:31:44 -080045 * implement behavior needed for tests.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 */
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090047public 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 Project9066cfe2009-03-03 19:31:44 -080052
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +090053 /**
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 Project9066cfe2009-03-03 19:31:44 -0800135 }
136
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900137 /**
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 Project9066cfe2009-03-03 19:31:44 -0800167 throw new UnsupportedOperationException("unimplemented mock method");
168 }
169
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900170 @Override
171 public String getType(Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 throw new UnsupportedOperationException("unimplemented mock method");
173 }
174
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900175 @Override
176 public Uri insert(Uri uri, ContentValues values) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 throw new UnsupportedOperationException("unimplemented mock method");
178 }
179
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900180 @Override
181 public boolean onCreate() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 throw new UnsupportedOperationException("unimplemented mock method");
183 }
184
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900185 @Override
186 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
187 String sortOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 throw new UnsupportedOperationException("unimplemented mock method");
189 }
190
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900191 @Override
192 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 throw new UnsupportedOperationException("unimplemented mock method");
194 }
Fred Quintana89437372009-05-15 15:10:40 -0700195
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900196 /**
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 Quintana89437372009-05-15 15:10:40 -0700201 throw new UnsupportedOperationException("unimplemented mock method");
202 }
203
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900204 @Override
205 public void attachInfo(Context context, ProviderInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 throw new UnsupportedOperationException("unimplemented mock method");
207 }
208
Daisuke Miyakawa8280c2b2009-10-22 08:36:42 +0900209 @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 Quintanaf99e2e02009-12-09 16:00:40 -0800225}