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