aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowContentResolverTest.java
blob: 94a19debbeaac1c292af0ac457f665ce1e5e3cf1 (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
package org.robolectric.shadows;

import static android.content.ContentResolver.QUERY_ARG_SQL_SELECTION;
import static android.content.ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS;
import static android.content.ContentResolver.QUERY_ARG_SQL_SORT_ORDER;
import static android.os.Build.VERSION_CODES.O;
import static android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.annotation.Config.NONE;

import android.accounts.Account;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.content.PeriodicSync;
import android.content.SyncAdapterType;
import android.content.SyncInfo;
import android.content.UriPermission;
import android.content.pm.ProviderInfo;
import android.content.res.AssetFileDescriptor;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.collect.Iterables;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.robolectric.R;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.fakes.BaseCursor;
import org.robolectric.util.NamedStream;

@RunWith(AndroidJUnit4.class)
public class ShadowContentResolverTest {
  @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();

  private static final String AUTHORITY = "org.robolectric";

  private ContentResolver contentResolver;
  private ShadowContentResolver shadowContentResolver;
  private Uri uri21;
  private Uri uri22;
  private Account a, b;

  @Before
  public void setUp() {
    contentResolver = ApplicationProvider.getApplicationContext().getContentResolver();
    shadowContentResolver = shadowOf(contentResolver);
    uri21 = Uri.parse(EXTERNAL_CONTENT_URI.toString() + "/21");
    uri22 = Uri.parse(EXTERNAL_CONTENT_URI.toString() + "/22");

    a = new Account("a", "type");
    b = new Account("b", "type");
  }

  @Test
  public void insert_shouldReturnIncreasingUris() {
    shadowContentResolver.setNextDatabaseIdForInserts(20);

    assertThat(contentResolver.insert(EXTERNAL_CONTENT_URI, new ContentValues())).isEqualTo(uri21);
    assertThat(contentResolver.insert(EXTERNAL_CONTENT_URI, new ContentValues())).isEqualTo(uri22);
  }

  @Test
  public void getType_shouldDefaultToNull() {
    assertThat(contentResolver.getType(uri21)).isNull();
  }

  @Test
  public void getType_shouldReturnProviderValue() {
    ShadowContentResolver.registerProviderInternal(
        AUTHORITY,
        new ContentProvider() {
          @Override
          public boolean onCreate() {
            return false;
          }

          @Override
          public Cursor query(
              Uri uri,
              String[] projection,
              String selection,
              String[] selectionArgs,
              String sortOrder) {
            return new BaseCursor();
          }

          @Override
          public Uri insert(Uri uri, ContentValues values) {
            return null;
          }

          @Override
          public int delete(Uri uri, String selection, String[] selectionArgs) {
            return -1;
          }

          @Override
          public int update(
              Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            return -1;
          }

          @Override
          public String getType(Uri uri) {
            return "mytype";
          }
        });
    final Uri uri = Uri.parse("content://" + AUTHORITY + "/some/path");
    assertThat(contentResolver.getType(uri)).isEqualTo("mytype");
  }

  @Test
  public void insert_shouldTrackInsertStatements() {
    ContentValues contentValues = new ContentValues();
    contentValues.put("foo", "bar");
    contentResolver.insert(EXTERNAL_CONTENT_URI, contentValues);
    assertThat(shadowContentResolver.getInsertStatements().size()).isEqualTo(1);
    assertThat(shadowContentResolver.getInsertStatements().get(0).getUri())
        .isEqualTo(EXTERNAL_CONTENT_URI);
    assertThat(
            shadowContentResolver
                .getInsertStatements()
                .get(0)
                .getContentValues()
                .getAsString("foo"))
        .isEqualTo("bar");

    contentValues = new ContentValues();
    contentValues.put("hello", "world");
    contentResolver.insert(EXTERNAL_CONTENT_URI, contentValues);
    assertThat(shadowContentResolver.getInsertStatements().size()).isEqualTo(2);
    assertThat(
            shadowContentResolver
                .getInsertStatements()
                .get(1)
                .getContentValues()
                .getAsString("hello"))
        .isEqualTo("world");
  }

  @Test
  public void insert_shouldTrackUpdateStatements() {
    ContentValues contentValues = new ContentValues();
    contentValues.put("foo", "bar");
    contentResolver.update(
        EXTERNAL_CONTENT_URI, contentValues, "robolectric", new String[] {"awesome"});
    assertThat(shadowContentResolver.getUpdateStatements().size()).isEqualTo(1);
    assertThat(shadowContentResolver.getUpdateStatements().get(0).getUri())
        .isEqualTo(EXTERNAL_CONTENT_URI);
    assertThat(
            shadowContentResolver
                .getUpdateStatements()
                .get(0)
                .getContentValues()
                .getAsString("foo"))
        .isEqualTo("bar");
    assertThat(shadowContentResolver.getUpdateStatements().get(0).getWhere())
        .isEqualTo("robolectric");
    assertThat(shadowContentResolver.getUpdateStatements().get(0).getSelectionArgs())
        .isEqualTo(new String[] {"awesome"});

    contentValues = new ContentValues();
    contentValues.put("hello", "world");
    contentResolver.update(EXTERNAL_CONTENT_URI, contentValues, null, null);
    assertThat(shadowContentResolver.getUpdateStatements().size()).isEqualTo(2);
    assertThat(shadowContentResolver.getUpdateStatements().get(1).getUri())
        .isEqualTo(EXTERNAL_CONTENT_URI);
    assertThat(
            shadowContentResolver
                .getUpdateStatements()
                .get(1)
                .getContentValues()
                .getAsString("hello"))
        .isEqualTo("world");
    assertThat(shadowContentResolver.getUpdateStatements().get(1).getWhere()).isNull();
    assertThat(shadowContentResolver.getUpdateStatements().get(1).getSelectionArgs()).isNull();
  }

  @Test
  public void insert_supportsNullContentValues() {
    contentResolver.insert(EXTERNAL_CONTENT_URI, null);
    assertThat(shadowContentResolver.getInsertStatements().get(0).getContentValues()).isNull();
  }

  @Test
  public void update_supportsNullContentValues() {
    contentResolver.update(EXTERNAL_CONTENT_URI, null, null, null);
    assertThat(shadowContentResolver.getUpdateStatements().get(0).getContentValues()).isNull();
  }

  @Test
  public void delete_shouldTrackDeletedUris() {
    assertThat(shadowContentResolver.getDeletedUris().size()).isEqualTo(0);

    assertThat(contentResolver.delete(uri21, null, null)).isEqualTo(1);
    assertThat(shadowContentResolver.getDeletedUris()).contains(uri21);
    assertThat(shadowContentResolver.getDeletedUris().size()).isEqualTo(1);

    assertThat(contentResolver.delete(uri22, null, null)).isEqualTo(1);
    assertThat(shadowContentResolver.getDeletedUris()).contains(uri22);
    assertThat(shadowContentResolver.getDeletedUris().size()).isEqualTo(2);
  }

  @Test
  public void delete_shouldTrackDeletedStatements() {
    assertThat(shadowContentResolver.getDeleteStatements().size()).isEqualTo(0);

    assertThat(contentResolver.delete(uri21, "id", new String[] {"5"})).isEqualTo(1);
    assertThat(shadowContentResolver.getDeleteStatements().size()).isEqualTo(1);
    assertThat(shadowContentResolver.getDeleteStatements().get(0).getUri()).isEqualTo(uri21);
    assertThat(shadowContentResolver.getDeleteStatements().get(0).getContentProvider()).isNull();
    assertThat(shadowContentResolver.getDeleteStatements().get(0).getWhere()).isEqualTo("id");
    assertThat(shadowContentResolver.getDeleteStatements().get(0).getSelectionArgs()[0])
        .isEqualTo("5");

    assertThat(contentResolver.delete(uri21, "foo", new String[] {"bar"})).isEqualTo(1);
    assertThat(shadowContentResolver.getDeleteStatements().size()).isEqualTo(2);
    assertThat(shadowContentResolver.getDeleteStatements().get(1).getUri()).isEqualTo(uri21);
    assertThat(shadowContentResolver.getDeleteStatements().get(1).getWhere()).isEqualTo("foo");
    assertThat(shadowContentResolver.getDeleteStatements().get(1).getSelectionArgs()[0])
        .isEqualTo("bar");
  }

  @Test
  public void whenCursorHasBeenSet_query_shouldReturnTheCursor() {
    assertThat(shadowContentResolver.query(null, null, null, null, null)).isNull();
    BaseCursor cursor = new BaseCursor();
    shadowContentResolver.setCursor(cursor);
    assertThat((BaseCursor) shadowContentResolver.query(null, null, null, null, null))
        .isSameInstanceAs(cursor);
  }

  @Test
  public void whenCursorHasBeenSet_queryWithCancellationSignal_shouldReturnTheCursor() {
    assertThat(shadowContentResolver.query(null, null, null, null, null, new CancellationSignal()))
        .isNull();
    BaseCursor cursor = new BaseCursor();
    shadowContentResolver.setCursor(cursor);
    assertThat(
            (BaseCursor)
                shadowContentResolver.query(null, null, null, null, null, new CancellationSignal()))
        .isSameInstanceAs(cursor);
  }

  @Test
  public void query_shouldReturnSpecificCursorsForSpecificUris() {
    assertThat(shadowContentResolver.query(uri21, null, null, null, null)).isNull();
    assertThat(shadowContentResolver.query(uri22, null, null, null, null)).isNull();

    BaseCursor cursor21 = new BaseCursor();
    BaseCursor cursor22 = new BaseCursor();
    shadowContentResolver.setCursor(uri21, cursor21);
    shadowContentResolver.setCursor(uri22, cursor22);

    assertThat((BaseCursor) shadowContentResolver.query(uri21, null, null, null, null))
        .isSameInstanceAs(cursor21);
    assertThat((BaseCursor) shadowContentResolver.query(uri22, null, null, null, null))
        .isSameInstanceAs(cursor22);
  }

  @Test
  public void query_shouldKnowWhatItsParamsWere() {
    String[] projection = {};
    String selection = "select";
    String[] selectionArgs = {};
    String sortOrder = "order";

    QueryParamTrackingCursor testCursor = new QueryParamTrackingCursor();

    shadowContentResolver.setCursor(testCursor);
    Cursor cursor =
        shadowContentResolver.query(uri21, projection, selection, selectionArgs, sortOrder);
    assertThat((QueryParamTrackingCursor) cursor).isEqualTo(testCursor);
    assertThat(testCursor.uri).isEqualTo(uri21);
    assertThat(testCursor.projection).isEqualTo(projection);
    assertThat(testCursor.selection).isEqualTo(selection);
    assertThat(testCursor.selectionArgs).isEqualTo(selectionArgs);
    assertThat(testCursor.sortOrder).isEqualTo(sortOrder);
  }

  @Test
  @Config(minSdk = O)
  public void query_shouldKnowWhatIsInBundle() {
    String[] projection = {};
    String selection = "select";
    String[] selectionArgs = {};
    String sortOrder = "order";
    Bundle queryArgs = new Bundle();
    queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
    queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
    queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);

    QueryParamTrackingCursor testCursor = new QueryParamTrackingCursor();
    shadowContentResolver.setCursor(testCursor);
    Cursor cursor = shadowContentResolver.query(uri21, projection, queryArgs, null);
    assertThat((QueryParamTrackingCursor) cursor).isEqualTo(testCursor);
    assertThat(testCursor.uri).isEqualTo(uri21);
    assertThat(testCursor.projection).isEqualTo(projection);
    assertThat(testCursor.selection).isEqualTo(selection);
    assertThat(testCursor.selectionArgs).isEqualTo(selectionArgs);
    assertThat(testCursor.sortOrder).isEqualTo(sortOrder);
  }

  @Test
  public void acquireUnstableProvider_shouldDefaultToNull() {
    assertThat(contentResolver.acquireUnstableProvider(uri21)).isNull();
  }

  @Test
  public void acquireUnstableProvider_shouldReturnWithUri() {
    ContentProvider cp = mock(ContentProvider.class);
    ShadowContentResolver.registerProviderInternal(AUTHORITY, cp);
    final Uri uri = Uri.parse("content://" + AUTHORITY);
    assertThat(contentResolver.acquireUnstableProvider(uri))
        .isSameInstanceAs(cp.getIContentProvider());
  }

  @Test
  public void acquireUnstableProvider_shouldReturnWithString() {
    ContentProvider cp = mock(ContentProvider.class);
    ShadowContentResolver.registerProviderInternal(AUTHORITY, cp);
    assertThat(contentResolver.acquireUnstableProvider(AUTHORITY))
        .isSameInstanceAs(cp.getIContentProvider());
  }

  @Test
  public void call_shouldCallProvider() {
    final String METHOD = "method";
    final String ARG = "arg";
    final Bundle EXTRAS = new Bundle();
    final Uri uri = Uri.parse("content://" + AUTHORITY);

    ContentProvider provider = mock(ContentProvider.class);
    doReturn(null).when(provider).call(METHOD, ARG, EXTRAS);
    ShadowContentResolver.registerProviderInternal(AUTHORITY, provider);

    contentResolver.call(uri, METHOD, ARG, EXTRAS);
    verify(provider).call(METHOD, ARG, EXTRAS);
  }

  @Test
  public void registerProvider_shouldAttachProviderInfo() {
    ContentProvider mock = mock(ContentProvider.class);

    ProviderInfo providerInfo0 = new ProviderInfo();
    providerInfo0.authority = "the-authority"; // todo: support multiple authorities
    providerInfo0.grantUriPermissions = true;
    mock.attachInfo(ApplicationProvider.getApplicationContext(), providerInfo0);
    mock.onCreate();

    ArgumentCaptor<ProviderInfo> captor = ArgumentCaptor.forClass(ProviderInfo.class);
    verify(mock)
        .attachInfo(
            same((Application) ApplicationProvider.getApplicationContext()), captor.capture());
    ProviderInfo providerInfo = captor.getValue();

    assertThat(providerInfo.authority).isEqualTo("the-authority");
    assertThat(providerInfo.grantUriPermissions).isEqualTo(true);
  }

  @Test(expected = UnsupportedOperationException.class)
  public void openInputStream_shouldReturnAnInputStreamThatExceptionsOnRead() throws Exception {
    InputStream inputStream = contentResolver.openInputStream(uri21);
    inputStream.read();
  }

  @Test
  public void openInputStream_returnsPreRegisteredStream() throws Exception {
    shadowContentResolver.registerInputStream(
        uri21, new ByteArrayInputStream("ourStream".getBytes(UTF_8)));
    InputStream inputStream = contentResolver.openInputStream(uri21);
    byte[] data = new byte[9];
    inputStream.read(data);
    assertThat(new String(data, UTF_8)).isEqualTo("ourStream");
  }

  @Test
  public void openInputStream_returnsNewStreamEachTimeFromRegisteredSupplier() throws Exception {
    shadowContentResolver.registerInputStreamSupplier(
        uri21, () -> new ByteArrayInputStream("ourStream".getBytes(UTF_8)));
    InputStream inputStream1 = contentResolver.openInputStream(uri21);
    byte[] data1 = new byte[9];
    inputStream1.read(data1);
    inputStream1.close();
    InputStream inputStream2 = contentResolver.openInputStream(uri21);
    byte[] data2 = new byte[9];
    inputStream2.read(data2);
    inputStream2.close();
    assertThat(new String(data1, UTF_8)).isEqualTo("ourStream");
    assertThat(new String(data2, UTF_8)).isEqualTo("ourStream");
  }

  @Test
  public void openInputStream_returnsResourceUriStream() throws Exception {
    InputStream inputStream =
        contentResolver.openInputStream(
            new Uri.Builder()
                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                .authority(ApplicationProvider.getApplicationContext().getPackageName())
                .appendPath(String.valueOf(R.drawable.an_image))
                .build());
    assertThat(inputStream).isNotNull();
    inputStream.read();
  }

  @SuppressLint("NewApi")
  @Test
  public void openInputStream_returnsFileUriStream() throws Exception {
    File file = temporaryFolder.newFile();
    try (FileOutputStream out = new FileOutputStream(file)) {
      out.write("foo".getBytes(UTF_8));
    }

    InputStream inputStream = contentResolver.openInputStream(Uri.fromFile(file));

    assertThat(inputStream).isNotNull();
    assertThat(new String(inputStream.readAllBytes(), UTF_8)).isEqualTo("foo");
  }

  @Test
  public void openInputStream_returnsProviderInputStream() throws Exception {
    ProviderInfo info = new ProviderInfo();
    info.authority = AUTHORITY;
    ContentProvider myContentProvider = new MyContentProvider();
    myContentProvider.attachInfo(ApplicationProvider.getApplicationContext(), info);
    ShadowContentResolver.registerProviderInternal(AUTHORITY, myContentProvider);

    Uri uri = Uri.parse("content://" + AUTHORITY + "/some/path");
    InputStream actualInputStream = contentResolver.openInputStream(uri);
    // Registered provider does not return named stream
    assertThat(actualInputStream).isNotInstanceOf(NamedStream.class);

    Uri otherUri = Uri.parse("content://otherAuthority/some/path");
    InputStream secondInputStream = contentResolver.openInputStream(otherUri);
    // No registered provider results in named stream
    assertThat(secondInputStream).isInstanceOf(NamedStream.class);

    shadowContentResolver.registerInputStreamSupplier(
        uri, () -> new ByteArrayInputStream("ourStream".getBytes(UTF_8)));
    InputStream registeredInputStream = contentResolver.openInputStream(uri);
    byte[] byteArray = new byte[registeredInputStream.available()];
    registeredInputStream.read(byteArray);
    // Explicitly registered stream takes precedence
    assertThat(byteArray).isEqualTo("ourStream".getBytes(UTF_8));
  }

  @Test
  public void openOutputStream_withNoRealOrRegisteredProvider_doesNotThrow() throws Exception {
    Uri uri = Uri.parse("content://invalidauthority/test/1");
    assertThat(contentResolver.openOutputStream(uri)).isNotNull();
  }

  @Test
  public void openOutputStream_withRealContentProvider_canReadBytesWrittenToOutputStream()
      throws IOException, RemoteException {
    Robolectric.setupContentProvider(MyContentProvider.class, AUTHORITY);
    Uri uri = Uri.parse("content://" + AUTHORITY + "/test/1");

    // Write content through given outputstream
    try (OutputStream outputStream = contentResolver.openOutputStream(uri)) {
      outputStream.write("foo".getBytes(UTF_8));
    }

    // Verify written content can be read back
    InputStream inputStream = contentResolver.openInputStream(uri);
    assertThat(new String(inputStream.readAllBytes(), UTF_8)).isEqualTo("foo");
  }

  @Test
  public void openOutputStream_shouldReturnRegisteredStream() throws Exception {
    final Uri uri = Uri.parse("content://registeredProvider/path");

    AtomicInteger callCount = new AtomicInteger();
    OutputStream outputStream =
        new OutputStream() {

          @Override
          public void write(int arg0) throws IOException {
            callCount.incrementAndGet();
          }

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

    shadowOf(contentResolver).registerOutputStream(uri, outputStream);

    assertThat(callCount.get()).isEqualTo(0);
    contentResolver.openOutputStream(uri).write(5);
    assertThat(callCount.get()).isEqualTo(1);

    contentResolver.openOutputStream(uri21).write(5);
    assertThat(callCount.get()).isEqualTo(1);
  }

  @Test
  public void openOutputStream_shouldReturnNewStreamFromRegisteredSupplier() throws Exception {
    final Uri uri = Uri.parse("content://registeredProvider/path");

    AtomicInteger streamCreateCount = new AtomicInteger();
    shadowOf(contentResolver)
        .registerOutputStreamSupplier(
            uri,
            () -> {
              streamCreateCount.incrementAndGet();
              AtomicBoolean isClosed = new AtomicBoolean();
              isClosed.set(false);
              OutputStream outputStream =
                  new OutputStream() {
                    @Override
                    public void close() {
                      isClosed.set(true);
                    }

                    @Override
                    public void write(int arg0) throws IOException {
                      if (isClosed.get()) {
                        throw new IOException();
                      }
                    }

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

    assertThat(streamCreateCount.get()).isEqualTo(0);
    OutputStream outputStream1 = contentResolver.openOutputStream(uri);
    outputStream1.close();
    assertThat(streamCreateCount.get()).isEqualTo(1);

    contentResolver.openOutputStream(uri).write(5);
    assertThat(streamCreateCount.get()).isEqualTo(2);
  }

  @Test
  public void openOutputStream_withModeWithNoRealOrRegisteredProvider_throws() {
    Uri uri = Uri.parse("content://invalidauthority/test/1");
    assertThrows(FileNotFoundException.class, () -> contentResolver.openOutputStream(uri, "wt"));
  }

  @Test
  public void openOutputStream_withModeWithRealContentProvider_canReadBytesWrittenToOutputStream()
      throws IOException, RemoteException {
    Robolectric.setupContentProvider(MyContentProvider.class, AUTHORITY);
    Uri uri = Uri.parse("content://" + AUTHORITY + "/test/1");

    // Write content through given outputstream
    try (OutputStream outputStream = contentResolver.openOutputStream(uri, "wt")) {
      outputStream.write("foo".getBytes(UTF_8));
    }

    // Verify written content can be read back
    InputStream inputStream = contentResolver.openInputStream(uri);
    assertThat(new String(inputStream.readAllBytes(), UTF_8)).isEqualTo("foo");
  }

  @Test
  public void openOutputStream_withModeShouldReturnRegisteredStream() throws Exception {
    final Uri uri = Uri.parse("content://registeredProvider/path");

    AtomicInteger callCount = new AtomicInteger();
    OutputStream outputStream =
        new OutputStream() {

          @Override
          public void write(int arg0) throws IOException {
            callCount.incrementAndGet();
          }

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

    shadowOf(contentResolver).registerOutputStream(uri, outputStream);

    assertThat(callCount.get()).isEqualTo(0);
    contentResolver.openOutputStream(uri, "wt").write(5);
    assertThat(callCount.get()).isEqualTo(1);
  }

  @Test
  public void openOutputStream_withModeShouldReturnNewStreamFromRegisteredSupplier()
      throws Exception {
    final Uri uri = Uri.parse("content://registeredProvider/path");

    AtomicInteger streamCreateCount = new AtomicInteger();
    shadowOf(contentResolver)
        .registerOutputStreamSupplier(
            uri,
            () -> {
              streamCreateCount.incrementAndGet();
              AtomicBoolean isClosed = new AtomicBoolean();
              isClosed.set(false);
              OutputStream outputStream =
                  new OutputStream() {
                    @Override
                    public void close() {
                      isClosed.set(true);
                    }

                    @Override
                    public void write(int arg0) throws IOException {
                      if (isClosed.get()) {
                        throw new IOException();
                      }
                    }

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

    assertThat(streamCreateCount.get()).isEqualTo(0);
    OutputStream outputStream1 = contentResolver.openOutputStream(uri, "wt");
    outputStream1.close();
    assertThat(streamCreateCount.get()).isEqualTo(1);

    contentResolver.openOutputStream(uri, "wt").write(5);
    assertThat(streamCreateCount.get()).isEqualTo(2);
  }

  @Test
  public void shouldTrackNotifiedUris() {
    contentResolver.notifyChange(Uri.parse("foo"), null, true);
    contentResolver.notifyChange(Uri.parse("bar"), null);

    assertThat(shadowContentResolver.getNotifiedUris().size()).isEqualTo(2);
    ShadowContentResolver.NotifiedUri uri = shadowContentResolver.getNotifiedUris().get(0);

    assertThat(uri.uri.toString()).isEqualTo("foo");
    assertThat(uri.syncToNetwork).isTrue();
    assertThat(uri.observer).isNull();

    uri = shadowContentResolver.getNotifiedUris().get(1);

    assertThat(uri.uri.toString()).isEqualTo("bar");
    assertThat(uri.syncToNetwork).isFalse();
    assertThat(uri.observer).isNull();
  }

  @SuppressWarnings("serial")
  @Test
  public void applyBatchForRegisteredProvider()
      throws RemoteException, OperationApplicationException {
    final List<String> operations = new ArrayList<>();
    ShadowContentResolver.registerProviderInternal(
        "registeredProvider",
        new ContentProvider() {
          @Override
          public boolean onCreate() {
            return true;
          }

          @Override
          public Cursor query(
              Uri uri,
              String[] projection,
              String selection,
              String[] selectionArgs,
              String sortOrder) {
            operations.add("query");
            MatrixCursor cursor = new MatrixCursor(new String[] {"a"});
            cursor.addRow(new Object[] {"b"});
            return cursor;
          }

          @Override
          public String getType(Uri uri) {
            return null;
          }

          @Override
          public Uri insert(Uri uri, ContentValues values) {
            operations.add("insert");
            return ContentUris.withAppendedId(uri, 1);
          }

          @Override
          public int delete(Uri uri, String selection, String[] selectionArgs) {
            operations.add("delete");
            return 0;
          }

          @Override
          public int update(
              Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            operations.add("update");
            return 0;
          }
        });

    final Uri uri = Uri.parse("content://registeredProvider/path");
    List<ContentProviderOperation> contentProviderOperations =
        Arrays.asList(
            ContentProviderOperation.newInsert(uri).withValue("a", "b").build(),
            ContentProviderOperation.newUpdate(uri).withValue("a", "b").build(),
            ContentProviderOperation.newDelete(uri).build(),
            ContentProviderOperation.newAssertQuery(uri).withValue("a", "b").build());
    contentResolver.applyBatch("registeredProvider", new ArrayList<>(contentProviderOperations));

    assertThat(operations).containsExactly("insert", "update", "delete", "query");
  }

  @Test
  public void applyBatchForUnregisteredProvider()
      throws RemoteException, OperationApplicationException {
    List<ContentProviderOperation> resultOperations =
        shadowContentResolver.getContentProviderOperations(AUTHORITY);
    assertThat(resultOperations).isNotNull();
    assertThat(resultOperations.size()).isEqualTo(0);

    Uri uri = Uri.parse("content://org.robolectric");
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();
    operations.add(
        ContentProviderOperation.newInsert(uri)
            .withValue("column1", "foo")
            .withValue("column2", 5)
            .build());
    operations.add(
        ContentProviderOperation.newUpdate(uri)
            .withSelection("id_column", new String[] {"99"})
            .withValue("column1", "bar")
            .build());
    operations.add(
        ContentProviderOperation.newDelete(uri)
            .withSelection("id_column", new String[] {"11"})
            .build());
    ContentProviderResult[] result = contentResolver.applyBatch(AUTHORITY, operations);

    resultOperations = shadowContentResolver.getContentProviderOperations(AUTHORITY);
    assertThat(resultOperations).isEqualTo(operations);
    assertThat(result).isNotNull();
  }

  @Test
  public void shouldKeepTrackOfSyncRequests() {
    ShadowContentResolver.Status status = ShadowContentResolver.getStatus(a, AUTHORITY, true);
    assertThat(status).isNotNull();
    assertThat(status.syncRequests).isEqualTo(0);
    ContentResolver.requestSync(a, AUTHORITY, new Bundle());
    assertThat(status.syncRequests).isEqualTo(1);
    assertThat(status.syncExtras).isNotNull();
  }

  @Test
  public void shouldKnowIfSyncIsActive() {
    assertThat(ContentResolver.isSyncActive(a, AUTHORITY)).isFalse();
    ContentResolver.requestSync(a, AUTHORITY, new Bundle());
    assertThat(ContentResolver.isSyncActive(a, AUTHORITY)).isTrue();
  }

  @Test
  public void shouldGetCurrentSyncs() {
    ContentResolver.requestSync(a, AUTHORITY, new Bundle());
    ContentResolver.requestSync(b, AUTHORITY, new Bundle());

    List<SyncInfo> syncs = ContentResolver.getCurrentSyncs();
    assertThat(syncs.size()).isEqualTo(2);

    SyncInfo syncA = Iterables.find(syncs, s -> s.account.equals(a));
    assertThat(syncA.account).isEqualTo(a);
    assertThat(syncA.authority).isEqualTo(AUTHORITY);

    SyncInfo syncB = Iterables.find(syncs, s -> s.account.equals(b));
    assertThat(syncB.account).isEqualTo(b);
    assertThat(syncB.authority).isEqualTo(AUTHORITY);

    ContentResolver.cancelSync(a, AUTHORITY);
    List<SyncInfo> syncsAgain = ContentResolver.getCurrentSyncs();
    assertThat(syncsAgain.size()).isEqualTo(1);

    SyncInfo firstAgain = syncsAgain.get(0);
    assertThat(firstAgain.account).isEqualTo(b);
    assertThat(firstAgain.authority).isEqualTo(AUTHORITY);

    ContentResolver.cancelSync(b, AUTHORITY);
    List<SyncInfo> s = ContentResolver.getCurrentSyncs();
    assertThat(s.size()).isEqualTo(0);
  }

  @Test
  public void shouldCancelSync() {
    ContentResolver.requestSync(a, AUTHORITY, new Bundle());
    ContentResolver.requestSync(b, AUTHORITY, new Bundle());
    assertThat(ContentResolver.isSyncActive(a, AUTHORITY)).isTrue();
    assertThat(ContentResolver.isSyncActive(b, AUTHORITY)).isTrue();

    ContentResolver.cancelSync(a, AUTHORITY);
    assertThat(ContentResolver.isSyncActive(a, AUTHORITY)).isFalse();
    assertThat(ContentResolver.isSyncActive(b, AUTHORITY)).isTrue();
  }

  @Test
  public void shouldSetIsSyncable() {
    assertThat(ContentResolver.getIsSyncable(a, AUTHORITY)).isEqualTo(-1);
    assertThat(ContentResolver.getIsSyncable(b, AUTHORITY)).isEqualTo(-1);
    ContentResolver.setIsSyncable(a, AUTHORITY, 1);
    ContentResolver.setIsSyncable(b, AUTHORITY, 2);
    assertThat(ContentResolver.getIsSyncable(a, AUTHORITY)).isEqualTo(1);
    assertThat(ContentResolver.getIsSyncable(b, AUTHORITY)).isEqualTo(2);
  }

  @Test
  public void shouldSetSyncAutomatically() {
    assertThat(ContentResolver.getSyncAutomatically(a, AUTHORITY)).isFalse();
    ContentResolver.setSyncAutomatically(a, AUTHORITY, true);
    assertThat(ContentResolver.getSyncAutomatically(a, AUTHORITY)).isTrue();
  }

  @Test
  public void shouldAddPeriodicSync() {
    Bundle fooBar = new Bundle();
    fooBar.putString("foo", "bar");
    Bundle fooBaz = new Bundle();
    fooBaz.putString("foo", "baz");

    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBaz, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 6000L);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY))
        .containsExactly(
            new PeriodicSync(a, AUTHORITY, fooBar, 6000L),
            new PeriodicSync(a, AUTHORITY, fooBaz, 6000L));
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY))
        .containsExactly(
            new PeriodicSync(b, AUTHORITY, fooBar, 6000L),
            new PeriodicSync(b, AUTHORITY, fooBaz, 6000L));

    // If same extras, but different time, simply update the time.
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 42L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 42L);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY))
        .containsExactly(
            new PeriodicSync(a, AUTHORITY, fooBar, 42L),
            new PeriodicSync(a, AUTHORITY, fooBaz, 6000L));
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY))
        .containsExactly(
            new PeriodicSync(b, AUTHORITY, fooBar, 6000L),
            new PeriodicSync(b, AUTHORITY, fooBaz, 42L));
  }

  @Test
  public void shouldRemovePeriodSync() {
    Bundle fooBar = new Bundle();
    fooBar.putString("foo", "bar");
    Bundle fooBaz = new Bundle();
    fooBaz.putString("foo", "baz");
    Bundle foo42 = new Bundle();
    foo42.putInt("foo", 42);
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY)).isEmpty();
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).isEmpty();

    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBaz, 6000L);
    ContentResolver.addPeriodicSync(a, AUTHORITY, foo42, 6000L);

    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, foo42, 6000L);

    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY))
        .containsExactly(
            new PeriodicSync(a, AUTHORITY, fooBar, 6000L),
            new PeriodicSync(a, AUTHORITY, fooBaz, 6000L),
            new PeriodicSync(a, AUTHORITY, foo42, 6000L));

    ContentResolver.removePeriodicSync(a, AUTHORITY, fooBar);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY))
        .containsExactly(
            new PeriodicSync(a, AUTHORITY, fooBaz, 6000L),
            new PeriodicSync(a, AUTHORITY, foo42, 6000L));

    ContentResolver.removePeriodicSync(a, AUTHORITY, fooBaz);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY))
        .containsExactly(new PeriodicSync(a, AUTHORITY, foo42, 6000L));

    ContentResolver.removePeriodicSync(a, AUTHORITY, foo42);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).isEmpty();
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY))
        .containsExactly(
            new PeriodicSync(b, AUTHORITY, fooBar, 6000L),
            new PeriodicSync(b, AUTHORITY, fooBaz, 6000L),
            new PeriodicSync(b, AUTHORITY, foo42, 6000L));
  }

  @Test
  public void shouldGetPeriodSyncs() {
    assertThat(ContentResolver.getPeriodicSyncs(a, AUTHORITY).size()).isEqualTo(0);
    ContentResolver.addPeriodicSync(a, AUTHORITY, new Bundle(), 6000L);

    List<PeriodicSync> syncs = ContentResolver.getPeriodicSyncs(a, AUTHORITY);
    assertThat(syncs.size()).isEqualTo(1);

    PeriodicSync first = syncs.get(0);
    assertThat(first.account).isEqualTo(a);
    assertThat(first.authority).isEqualTo(AUTHORITY);
    assertThat(first.period).isEqualTo(6000L);
    assertThat(first.extras).isNotNull();
  }

  @Test
  public void shouldValidateSyncExtras() {
    Bundle bundle = new Bundle();
    bundle.putString("foo", "strings");
    bundle.putLong("long", 10L);
    bundle.putDouble("double", 10.0d);
    bundle.putFloat("float", 10.0f);
    bundle.putInt("int", 10);
    bundle.putParcelable("account", a);
    ContentResolver.validateSyncExtrasBundle(bundle);
  }

  @Test(expected = IllegalArgumentException.class)
  public void shouldValidateSyncExtrasAndThrow() {
    Bundle bundle = new Bundle();
    bundle.putParcelable("intent", new Intent());
    ContentResolver.validateSyncExtrasBundle(bundle);
  }

  @Test
  public void shouldSetMasterSyncAutomatically() {
    assertThat(ContentResolver.getMasterSyncAutomatically()).isFalse();
    ContentResolver.setMasterSyncAutomatically(true);
    assertThat(ContentResolver.getMasterSyncAutomatically()).isTrue();
  }

  @Test
  public void shouldDelegateCallsToRegisteredProvider() {
    ShadowContentResolver.registerProviderInternal(
        AUTHORITY,
        new ContentProvider() {
          @Override
          public boolean onCreate() {
            return false;
          }

          @Override
          public Cursor query(
              Uri uri,
              String[] projection,
              String selection,
              String[] selectionArgs,
              String sortOrder) {
            return new BaseCursor();
          }

          @Override
          public Uri insert(Uri uri, ContentValues values) {
            return null;
          }

          @Override
          public int delete(Uri uri, String selection, String[] selectionArgs) {
            return -1;
          }

          @Override
          public int update(
              Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            return -1;
          }

          @Override
          public String getType(Uri uri) {
            return null;
          }
        });
    final Uri uri = Uri.parse("content://" + AUTHORITY + "/some/path");
    final Uri unrelated = Uri.parse("content://unrelated/some/path");

    assertThat(contentResolver.query(uri, null, null, null, null)).isNotNull();
    assertThat(contentResolver.insert(uri, new ContentValues())).isNull();

    assertThat(contentResolver.delete(uri, null, null)).isEqualTo(-1);
    assertThat(contentResolver.update(uri, new ContentValues(), null, null)).isEqualTo(-1);

    assertThat(contentResolver.query(unrelated, null, null, null, null)).isNull();
    assertThat(contentResolver.insert(unrelated, new ContentValues())).isNotNull();
    assertThat(contentResolver.delete(unrelated, null, null)).isEqualTo(1);
    assertThat(contentResolver.update(unrelated, new ContentValues(), null, null)).isEqualTo(1);
  }

  @Test
  public void shouldThrowConfiguredExceptionWhenRegisteringContentObservers() {
    ShadowContentResolver scr = shadowOf(contentResolver);
    scr.setRegisterContentProviderException(EXTERNAL_CONTENT_URI, new SecurityException());
    try {
      contentResolver.registerContentObserver(
          EXTERNAL_CONTENT_URI, true, new TestContentObserver(null));
      fail();
    } catch (SecurityException expected) {
    }
  }

  @Test
  public void shouldClearConfiguredExceptionForRegisteringContentObservers() {
    ShadowContentResolver scr = shadowOf(contentResolver);
    scr.setRegisterContentProviderException(EXTERNAL_CONTENT_URI, new SecurityException());
    scr.clearRegisterContentProviderException(EXTERNAL_CONTENT_URI);
    // Should not throw the SecurityException.
    contentResolver.registerContentObserver(
        EXTERNAL_CONTENT_URI, true, new TestContentObserver(null));
  }

  @Test
  public void shouldRegisterContentObservers() {
    TestContentObserver co = new TestContentObserver(null);
    ShadowContentResolver scr = shadowOf(contentResolver);

    assertThat(scr.getContentObservers(EXTERNAL_CONTENT_URI)).isEmpty();

    contentResolver.registerContentObserver(EXTERNAL_CONTENT_URI, true, co);

    assertThat(scr.getContentObservers(EXTERNAL_CONTENT_URI)).containsExactly((ContentObserver) co);

    assertThat(co.changed).isFalse();
    contentResolver.notifyChange(EXTERNAL_CONTENT_URI, null);
    assertThat(co.changed).isTrue();

    contentResolver.unregisterContentObserver(co);
    assertThat(scr.getContentObservers(EXTERNAL_CONTENT_URI)).isEmpty();
  }

  @Test
  public void shouldUnregisterContentObservers() {
    TestContentObserver co = new TestContentObserver(null);
    ShadowContentResolver scr = shadowOf(contentResolver);
    contentResolver.registerContentObserver(EXTERNAL_CONTENT_URI, true, co);
    assertThat(scr.getContentObservers(EXTERNAL_CONTENT_URI)).contains(co);

    contentResolver.unregisterContentObserver(co);
    assertThat(scr.getContentObservers(EXTERNAL_CONTENT_URI)).isEmpty();

    assertThat(co.changed).isFalse();
    contentResolver.notifyChange(EXTERNAL_CONTENT_URI, null);
    assertThat(co.changed).isFalse();
  }

  @Test
  public void shouldNotifyChildContentObservers() throws Exception {
    TestContentObserver co1 = new TestContentObserver(null);
    TestContentObserver co2 = new TestContentObserver(null);

    Uri childUri = EXTERNAL_CONTENT_URI.buildUpon().appendPath("path").build();

    contentResolver.registerContentObserver(EXTERNAL_CONTENT_URI, true, co1);
    contentResolver.registerContentObserver(childUri, false, co2);

    co1.changed = co2.changed = false;
    contentResolver.notifyChange(childUri, null);
    assertThat(co1.changed).isTrue();
    assertThat(co2.changed).isTrue();

    co1.changed = co2.changed = false;
    contentResolver.notifyChange(EXTERNAL_CONTENT_URI, null);
    assertThat(co1.changed).isTrue();
    assertThat(co2.changed).isFalse();

    co1.changed = co2.changed = false;
    contentResolver.notifyChange(childUri.buildUpon().appendPath("extra").build(), null);
    assertThat(co1.changed).isTrue();
    assertThat(co2.changed).isFalse();
  }

  @Test
  public void getProvider_shouldCreateProviderFromManifest() throws Exception {
    Uri uri = Uri.parse("content://org.robolectric.authority1/shadows");
    ContentProvider provider = ShadowContentResolver.getProvider(uri);
    assertThat(provider).isNotNull();
    assertThat(provider.getReadPermission()).isEqualTo("READ_PERMISSION");
    assertThat(provider.getWritePermission()).isEqualTo("WRITE_PERMISSION");
    assertThat(provider.getPathPermissions()).asList().hasSize(1);

    // unfortunately, there is no direct way of testing if authority is set or not
    // however, it's checked in ContentProvider.Transport method calls (validateIncomingUri), so
    // it's the closest we can test against
    if (RuntimeEnvironment.getApiLevel() <= 28) {
      provider.getIContentProvider().getType(uri); // should not throw
    } else {
      // just call validateIncomingUri directly
      provider.validateIncomingUri(uri);
    }
  }

  @Test
  @Config(manifest = NONE)
  @SuppressWarnings("RobolectricSystemContext") // preexisting when check was enabled
  public void getProvider_shouldNotReturnAnyProviderWhenManifestIsNull() {
    Application application = new Application();
    shadowOf(application).callAttach(RuntimeEnvironment.systemContext);
    assertThat(ShadowContentResolver.getProvider(Uri.parse("content://"))).isNull();
  }

  @Test
  public void openTypedAssetFileDescriptor_shouldOpenDescriptor()
      throws IOException, RemoteException {
    Robolectric.setupContentProvider(MyContentProvider.class, AUTHORITY);

    try (AssetFileDescriptor afd =
        contentResolver.openTypedAssetFileDescriptor(
            Uri.parse("content://" + AUTHORITY + "/whatever"), "*/*", null)) {

      FileDescriptor descriptor = afd.getFileDescriptor();
      assertThat(descriptor).isNotNull();
    }
  }

  @Test
  public void takeAndReleasePersistableUriPermissions() {
    List<UriPermission> permissions = contentResolver.getPersistedUriPermissions();
    assertThat(permissions).isEmpty();

    // Take the read permission for the uri.
    Uri uri = Uri.parse("content://" + AUTHORITY + "/whatever");
    contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    assertThat(permissions).hasSize(1);
    assertThat(permissions.get(0).getUri()).isSameInstanceAs(uri);
    assertThat(permissions.get(0).isReadPermission()).isTrue();
    assertThat(permissions.get(0).isWritePermission()).isFalse();

    // Take the write permission for the uri.
    contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    assertThat(permissions).hasSize(1);
    assertThat(permissions.get(0).getUri()).isSameInstanceAs(uri);
    assertThat(permissions.get(0).isReadPermission()).isTrue();
    assertThat(permissions.get(0).isWritePermission()).isTrue();

    // Release the read permission for the uri.
    contentResolver.releasePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    assertThat(permissions).hasSize(1);
    assertThat(permissions.get(0).getUri()).isSameInstanceAs(uri);
    assertThat(permissions.get(0).isReadPermission()).isFalse();
    assertThat(permissions.get(0).isWritePermission()).isTrue();

    // Release the write permission for the uri.
    contentResolver.releasePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    assertThat(permissions).isEmpty();
  }

  @Test
  public void getSyncAdapterTypes() {
    SyncAdapterType[] syncAdapterTypes =
        new SyncAdapterType[] {
          new SyncAdapterType(
              "authority1", "accountType1", /* userVisible=*/ false, /* supportsUploading=*/ false),
          new SyncAdapterType(
              "authority2", "accountType2", /* userVisible=*/ true, /* supportsUploading=*/ false),
          new SyncAdapterType(
              "authority3", "accountType3", /* userVisible=*/ true, /* supportsUploading=*/ true)
        };

    ShadowContentResolver.setSyncAdapterTypes(syncAdapterTypes);
    assertThat(ContentResolver.getSyncAdapterTypes()).isEqualTo(syncAdapterTypes);
  }

  private static class QueryParamTrackingCursor extends BaseCursor {
    public Uri uri;
    public String[] projection;
    public String selection;
    public String[] selectionArgs;
    public String sortOrder;

    @Override
    public void setQuery(
        Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
      this.uri = uri;
      this.projection = projection;
      this.selection = selection;
      this.selectionArgs = selectionArgs;
      this.sortOrder = sortOrder;
    }
  }

  private static class TestContentObserver extends ContentObserver {
    public TestContentObserver(Handler handler) {
      super(handler);
    }

    public boolean changed = false;

    @Override
    public void onChange(boolean selfChange) {
      changed = true;
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
      changed = true;
    }
  }

  /** Provider that opens a temporary file. */
  public static class MyContentProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
      return true;
    }

    @Override
    public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
      return null;
    }

    @Override
    public String getType(Uri uri) {
      return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
      return null;
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
      return 0;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
      return 0;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
      final File file =
          new File(ApplicationProvider.getApplicationContext().getFilesDir(), "test_file");
      try {
        file.createNewFile();
      } catch (IOException e) {
        throw new RuntimeException("error creating new file", e);
      }
      return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
    }
  }
}