aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/shadows/ShadowContentResolver.java
blob: 6f685a18bf80f1e911775a2e1d53b45af0804943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package com.xtremelabs.robolectric.shadows;

import android.accounts.Account;
import android.content.*;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.tester.android.database.TestCursor;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

@Implements(ContentResolver.class)
public class ShadowContentResolver {
    private int nextDatabaseIdForInserts;
    private int nextDatabaseIdForUpdates;

    private TestCursor cursor;
    private final List<InsertStatement> insertStatements = new ArrayList<InsertStatement>();
    private final List<UpdateStatement> updateStatements = new ArrayList<UpdateStatement>();
    private final List<DeleteStatement> deleteStatements = new ArrayList<DeleteStatement>();
    private List<NotifiedUri> notifiedUris = new ArrayList<NotifiedUri>();
    private HashMap<Uri, TestCursor> uriCursorMap = new HashMap<Uri, TestCursor>();
    private final Map<String, ArrayList<ContentProviderOperation>> contentProviderOperations = new HashMap<String, ArrayList<ContentProviderOperation>>();
    private ContentProviderResult[] contentProviderResults;

    private static final Map<String, Map<Account, Status>>  syncableAccounts =
            new HashMap<String, Map<Account, Status>>();
    private static final Map<String, ContentProvider> providers = new HashMap<String, ContentProvider>();
    private static boolean masterSyncAutomatically;

    public static void reset() {
        syncableAccounts.clear();
        providers.clear();
        masterSyncAutomatically = false;
    }

    public static class NotifiedUri {
        public final Uri uri;
        public final boolean syncToNetwork;
        public final ContentObserver observer;

        public NotifiedUri(Uri uri, ContentObserver observer, boolean syncToNetwork) {
            this.uri = uri;
            this.syncToNetwork = syncToNetwork;
            this.observer = observer;
        }
    }

    public static class Status {
        public int syncRequests;
        public int state = -1;
        public boolean syncAutomatically;
        public Bundle syncExtras;
        public List<PeriodicSync> syncs = new ArrayList<PeriodicSync>();
    }

    @Implementation
    public final InputStream openInputStream(final Uri uri) {

        if (uri != null && ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())) {
            String path = uri.getPath();
            // check that path is a numerical resource id
            if (path != null && path.matches("/[0-9]+")) {
                int resourceId = Integer.parseInt(path.substring(1));
                return Robolectric.application.getResources().openRawResource(resourceId);
            }
        }

        return new InputStream() {
            @Override
            public int read() throws IOException {
                throw new UnsupportedOperationException();
            }

            @Override
            public String toString() {
                return "stream for " + uri;
            }
        };
    }
    
    @Implementation
    public final OutputStream openOutputStream(final Uri uri) {
        return new OutputStream() {
            
            @Override
            public void write(int arg0) throws IOException {                
            }

            @Override
            public String toString() {
                return "outputstream for " + uri;
            }
        };
    }

    @Implementation
    public final Uri insert(Uri url, ContentValues values) {
        ContentProvider provider = getProvider(url);
        if (provider != null) {
            return provider.insert(url, values);
        } else {
            InsertStatement insertStatement = new InsertStatement(url, new ContentValues(values));
            insertStatements.add(insertStatement);
            return Uri.parse(url.toString() + "/" + nextDatabaseIdForInserts++);
        }
    }

    @Implementation
    public int update(Uri uri, ContentValues values, String where, String[] selectionArgs) {
        ContentProvider provider = getProvider(uri);
        if (provider != null) {
            return provider.update(uri, values, where, selectionArgs);
        } else {
            UpdateStatement updateStatement = new UpdateStatement(uri, new ContentValues(values), where, selectionArgs);
            updateStatements.add(updateStatement);
            return nextDatabaseIdForUpdates++;
        }
    }

    @Implementation
    public final Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        ContentProvider provider = getProvider(uri);
        if (provider != null) {
            return provider.query(uri, projection, selection, selectionArgs, sortOrder);
        } else {
            TestCursor returnCursor = getCursor(uri);
            if (returnCursor == null) {
                return null;
            }

            returnCursor.setQuery(uri, projection, selection, selectionArgs,
                    sortOrder);
            return returnCursor;
        }
    }

    @Implementation
    public final int delete(Uri url, String where, String[] selectionArgs) {
        ContentProvider provider = getProvider(url);
        if (provider != null) {
            return provider.delete(url, where, selectionArgs);
        } else {
            DeleteStatement deleteStatement = new DeleteStatement(url, where, selectionArgs);
            deleteStatements.add(deleteStatement);
            return 1;
        }
    }

    @Implementation
    public final int bulkInsert(Uri url, ContentValues[] values) {
        ContentProvider provider = getProvider(url);
        if (provider != null) {
            return provider.bulkInsert(url, values);
        } else {
            return 0;
        }
    }

    @Implementation
    public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) {
       notifiedUris.add(new NotifiedUri(uri, observer, syncToNetwork));
    }

    @Implementation
    public void notifyChange(Uri uri, ContentObserver observer) {
        notifyChange(uri, observer, false);
    }

    @Implementation
    public ContentProviderResult[] applyBatch(String authority, ArrayList<ContentProviderOperation> operations) {
        contentProviderOperations.put(authority, operations);
        return contentProviderResults;
    }

    @Implementation
    public static void requestSync(Account account, String authority, Bundle extras) {
        validateSyncExtrasBundle(extras);
        Status status = getStatus(account, authority, true);
        status.syncRequests++;
        status.syncExtras = extras;
    }

    @Implementation
    public static void setIsSyncable(Account account, String authority, int syncable) {
        getStatus(account, authority, true).state = syncable;
    }

    @Implementation
    public static int getIsSyncable(Account account, String authority) {
        return getStatus(account, authority, true).state;
    }

    @Implementation
    public static boolean getSyncAutomatically(Account account, String authority) {
        return getStatus(account, authority, true).syncAutomatically;
    }

    @Implementation
    public static void setSyncAutomatically(Account account, String authority, boolean sync) {
        getStatus(account, authority, true).syncAutomatically = sync;
    }

    @Implementation
    public static void addPeriodicSync(Account account, String authority, Bundle extras,
                                       long pollFrequency) {

        validateSyncExtrasBundle(extras);
        getStatus(account, authority, true).syncs.add(new PeriodicSync(account, authority, extras, pollFrequency));
    }

    @Implementation
    public static void removePeriodicSync(Account account, String authority, Bundle extras) {
        validateSyncExtrasBundle(extras);
        Status status = getStatus(account, authority);
        if (status != null) status.syncs.clear();
    }

    @Implementation
    public static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) {
        return getStatus(account, authority, true).syncs;
    }

    @Implementation
    public static void validateSyncExtrasBundle(Bundle extras) {
        for (String key : extras.keySet()) {
            Object value = extras.get(key);
            if (value == null) continue;
            if (value instanceof Long) continue;
            if (value instanceof Integer) continue;
            if (value instanceof Boolean) continue;
            if (value instanceof Float) continue;
            if (value instanceof Double) continue;
            if (value instanceof String) continue;
            if (value instanceof Account) continue;
            throw new IllegalArgumentException("unexpected value type: "
                    + value.getClass().getName());
        }
    }

    @Implementation
    public static void setMasterSyncAutomatically(boolean sync) {
        masterSyncAutomatically = sync;

    }

    @Implementation
    public static boolean getMasterSyncAutomatically() {
        return masterSyncAutomatically;
    }


    public static ContentProvider getProvider(Uri uri) {
        if (uri == null) {
            return null;
        } else if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
            return null;
        } else {
            return providers.get(uri.getAuthority());
        }
    }

    public static void registerProvider(String authority, ContentProvider provider) {
        providers.put(authority, provider);
    }

    public static Status getStatus(Account account, String authority) {
        return getStatus(account, authority, false);
    }

    public static Status getStatus(Account account, String authority, boolean create) {
        Map<Account, Status> map = syncableAccounts.get(authority);
        if (map == null) {
            map = new HashMap<Account, Status>();
            syncableAccounts.put(authority, map);
        }
        Status status = map.get(account);
        if (status == null && create) {
            status = new Status();
            map.put(account, status);
        }
        return status;
    }

    public void setCursor(TestCursor cursor) {
        this.cursor = cursor;
    }

    public void setCursor(Uri uri, TestCursor cursorForUri) {
        this.uriCursorMap.put(uri, cursorForUri);
    }

    public void setNextDatabaseIdForInserts(int nextId) {
        nextDatabaseIdForInserts = nextId;
    }

    public void setNextDatabaseIdForUpdates(int nextId) {
        nextDatabaseIdForUpdates = nextId;
    }

    public List<InsertStatement> getInsertStatements() {
        return insertStatements;
    }

    public List<UpdateStatement> getUpdateStatements() {
        return updateStatements;
    }

    public List<Uri> getDeletedUris() {
        List<Uri> uris = new ArrayList<Uri>();
        for (DeleteStatement deleteStatement : deleteStatements) {
            uris.add(deleteStatement.getUri());
        }
        return uris;
    }

    public List<DeleteStatement> getDeleteStatements() {
        return deleteStatements;
    }

    public List<NotifiedUri> getNotifiedUris() {
        return notifiedUris;
    }

    public ArrayList<ContentProviderOperation> getContentProviderOperations(String authority) {
        ArrayList<ContentProviderOperation> operations = contentProviderOperations.get(authority);
        if (operations == null)
            return new ArrayList<ContentProviderOperation>();
        return operations;
    }


    public void setContentProviderResult(ContentProviderResult[] contentProviderResults) {
        this.contentProviderResults = contentProviderResults;
    }

    private TestCursor getCursor(Uri uri) {
        if (uriCursorMap.get(uri) != null) {
            return uriCursorMap.get(uri);
        } else if (cursor != null) {
            return cursor;
        } else {
            return null;
        }
    }

    public static class InsertStatement {
        private final Uri uri;
        private final ContentValues contentValues;

        public InsertStatement(Uri uri, ContentValues contentValues) {
            this.uri = uri;
            this.contentValues = contentValues;
        }

        public Uri getUri() {
            return uri;
        }

        public ContentValues getContentValues() {
            return contentValues;
        }
    }

    public static class UpdateStatement {
        private final Uri uri;
        private final ContentValues values;
        private final String where;
        private final String[] selectionArgs;

        public UpdateStatement(Uri uri, ContentValues values, String where, String[] selectionArgs) {
            this.uri = uri;
            this.values = values;
            this.where = where;
            this.selectionArgs = selectionArgs;
        }

        public Uri getUri() {
            return uri;
        }

        public ContentValues getContentValues() {
            return values;
        }

        public String getWhere() {
            return where;
        }

        public String[] getSelectionArgs() {
            return selectionArgs;
        }
    }

    public static class DeleteStatement {
        private final Uri uri;
        private final String where;
        private final String[] selectionArgs;

        public DeleteStatement(Uri uri, String where, String[] selectionArgs) {
            this.uri = uri;
            this.where = where;
            this.selectionArgs = selectionArgs;
        }

        public Uri getUri() {
            return uri;
        }

        public String getWhere() {
            return where;
        }

        public String[] getSelectionArgs() {
            return selectionArgs;
        }
    }
}