ShadowContentResolver.java revision 84cf4dda334a3221374f15f52e2c172e5723afbf
1package com.xtremelabs.robolectric.shadows;
2
3import android.content.ContentResolver;
4import android.content.ContentValues;
5import android.database.ContentObserver;
6import android.database.Cursor;
7import android.net.Uri;
8import com.xtremelabs.robolectric.internal.Implementation;
9import com.xtremelabs.robolectric.internal.Implements;
10import com.xtremelabs.robolectric.tester.android.database.TestCursor;
11
12import java.io.IOException;
13import java.io.InputStream;
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.List;
17
18@Implements(ContentResolver.class)
19public class ShadowContentResolver {
20    private int nextDatabaseIdForInserts;
21    private int nextDatabaseIdForUpdates;
22
23    private TestCursor cursor;
24    private final List<InsertStatement> insertStatements = new ArrayList<InsertStatement>();
25    private final List<UpdateStatement> updateStatements = new ArrayList<UpdateStatement>();
26    private final List<DeleteStatement> deleteStatements = new ArrayList<DeleteStatement>();
27    private List<NotifiedUri> notifiedUris = new ArrayList<NotifiedUri>();
28    private HashMap<Uri, TestCursor> uriCursorMap = new HashMap<Uri, TestCursor>();
29
30    public static class NotifiedUri {
31        public final Uri uri;
32        public final boolean syncToNetwork;
33        public final ContentObserver observer;
34
35        public NotifiedUri(Uri uri, ContentObserver observer, boolean syncToNetwork) {
36            this.uri = uri;
37            this.syncToNetwork = syncToNetwork;
38            this.observer = observer;
39        }
40    }
41
42    @Implementation
43    public final InputStream openInputStream(final Uri uri) {
44        return new InputStream() {
45            @Override
46            public int read() throws IOException {
47                throw new UnsupportedOperationException();
48            }
49
50            @Override
51            public String toString() {
52                return "stream for " + uri;
53            }
54        };
55    }
56
57    @Implementation
58    public final Uri insert(Uri url, ContentValues values) {
59        InsertStatement insertStatement = new InsertStatement(url, new ContentValues(values));
60        insertStatements.add(insertStatement);
61        return Uri.parse(url.toString() + "/" + nextDatabaseIdForInserts++);
62    }
63
64    @Implementation
65    public int update(Uri uri, ContentValues values, String where, String[] selectionArgs) {
66        UpdateStatement updateStatement = new UpdateStatement(uri, new ContentValues(values), where, selectionArgs);
67        updateStatements.add(updateStatement);
68        return nextDatabaseIdForUpdates++;
69    }
70
71    @Implementation
72    public final Cursor query(Uri uri, String[] projection, String selection,
73            String[] selectionArgs, String sortOrder) {
74
75        TestCursor returnCursor = getCursor(uri);
76        if (returnCursor == null) {
77            return null;
78        }
79
80        returnCursor.setQuery(uri, projection, selection, selectionArgs,
81                sortOrder);
82        return returnCursor;
83    }
84
85    @Implementation
86    public final int delete(Uri url, String where, String[] selectionArgs) {
87        DeleteStatement deleteStatement = new DeleteStatement(url, where, selectionArgs);
88        deleteStatements.add(deleteStatement);
89        return 1;
90    }
91
92    @Implementation
93    public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) {
94       notifiedUris.add(new NotifiedUri(uri, observer, syncToNetwork));
95    }
96
97    @Implementation
98    public void notifyChange(Uri uri, ContentObserver observer) {
99        notifyChange(uri, observer, false);
100    }
101
102    public void setCursor(TestCursor cursor) {
103        this.cursor = cursor;
104    }
105
106    public void setCursor(Uri uri, TestCursor cursorForUri) {
107        this.uriCursorMap.put(uri, cursorForUri);
108    }
109
110    public void setNextDatabaseIdForInserts(int nextId) {
111        nextDatabaseIdForInserts = nextId;
112    }
113
114    public void setNextDatabaseIdForUpdates(int nextId) {
115        nextDatabaseIdForUpdates = nextId;
116    }
117
118    public List<InsertStatement> getInsertStatements() {
119        return insertStatements;
120    }
121
122    public List<UpdateStatement> getUpdateStatements() {
123        return updateStatements;
124    }
125
126    public List<Uri> getDeletedUris() {
127        List<Uri> uris = new ArrayList<Uri>();
128        for (DeleteStatement deleteStatement : deleteStatements) {
129            uris.add(deleteStatement.getUri());
130        }
131        return uris;
132    }
133
134    public List<DeleteStatement> getDeleteStatements() {
135        return deleteStatements;
136    }
137
138    public List<NotifiedUri> getNotifiedUris() {
139        return notifiedUris;
140    }
141
142    private TestCursor getCursor(Uri uri) {
143        if (uriCursorMap.get(uri) != null) {
144            return uriCursorMap.get(uri);
145        } else if (cursor != null) {
146            return cursor;
147        } else {
148            return null;
149        }
150    }
151
152    public class InsertStatement {
153        private final Uri uri;
154        private final ContentValues contentValues;
155
156        public InsertStatement(Uri uri, ContentValues contentValues) {
157            this.uri = uri;
158            this.contentValues = contentValues;
159        }
160
161        public Uri getUri() {
162            return uri;
163        }
164
165        public ContentValues getContentValues() {
166            return contentValues;
167        }
168    }
169
170    public class UpdateStatement {
171        private final Uri uri;
172        private final ContentValues values;
173        private final String where;
174        private final String[] selectionArgs;
175
176        public UpdateStatement(Uri uri, ContentValues values, String where, String[] selectionArgs) {
177            this.uri = uri;
178            this.values = values;
179            this.where = where;
180            this.selectionArgs = selectionArgs;
181        }
182
183        public Uri getUri() {
184            return uri;
185        }
186
187        public ContentValues getContentValues() {
188            return values;
189        }
190
191        public String getWhere() {
192            return where;
193        }
194
195        public String[] getSelectionArgs() {
196            return selectionArgs;
197        }
198    }
199
200    public class DeleteStatement {
201        private final Uri uri;
202        private final String where;
203        private final String[] selectionArgs;
204
205        public DeleteStatement(Uri uri, String where, String[] selectionArgs) {
206            this.uri = uri;
207            this.where = where;
208            this.selectionArgs = selectionArgs;
209        }
210
211        public Uri getUri() {
212            return uri;
213        }
214
215        public String getWhere() {
216            return where;
217        }
218
219        public String[] getSelectionArgs() {
220            return selectionArgs;
221        }
222    }
223}
224