/src/test/java/com/xtremelabs/robolectric/shadows/ContentResolverTest.java

https://github.com/Macarse/robolectric · Java · 127 lines · 106 code · 21 blank · 0 comment · 0 complexity · d8c0d74f156b70706dec58fa148d0d50 MD5 · raw file

  1. package com.xtremelabs.robolectric.shadows;
  2. import android.app.Activity;
  3. import android.content.ContentResolver;
  4. import android.content.ContentValues;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import com.xtremelabs.robolectric.WithTestDefaultsRunner;
  8. import com.xtremelabs.robolectric.tester.android.database.TestCursor;
  9. import org.hamcrest.CoreMatchers;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13. import java.io.InputStream;
  14. import static android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  15. import static com.xtremelabs.robolectric.Robolectric.shadowOf;
  16. import static org.hamcrest.CoreMatchers.equalTo;
  17. import static org.hamcrest.CoreMatchers.hasItem;
  18. import static org.hamcrest.CoreMatchers.sameInstance;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertThat;
  21. @RunWith(WithTestDefaultsRunner.class)
  22. public class ContentResolverTest {
  23. private ContentResolver contentResolver;
  24. private ShadowContentResolver shadowContentResolver;
  25. private Uri uri21;
  26. private Uri uri22;
  27. @Before
  28. public void setUp() throws Exception {
  29. contentResolver = new Activity().getContentResolver();
  30. shadowContentResolver = shadowOf(contentResolver);
  31. uri21 = Uri.parse(EXTERNAL_CONTENT_URI.toString() + "/21");
  32. uri22 = Uri.parse(EXTERNAL_CONTENT_URI.toString() + "/22");
  33. }
  34. @Test
  35. public void insert_shouldReturnIncreasingUris() throws Exception {
  36. shadowContentResolver.setNextDatabaseIdForInserts(21);
  37. assertThat(contentResolver.insert(EXTERNAL_CONTENT_URI, new ContentValues()), equalTo(uri21));
  38. assertThat(contentResolver.insert(EXTERNAL_CONTENT_URI, new ContentValues()), equalTo(uri22));
  39. }
  40. @Test
  41. public void delete_shouldTrackDeletedUris() throws Exception {
  42. assertThat(shadowContentResolver.getDeletedUris().size(), equalTo(0));
  43. assertThat(contentResolver.delete(uri21, null, null), equalTo(1));
  44. assertThat(shadowContentResolver.getDeletedUris(), hasItem(uri21));
  45. assertThat(shadowContentResolver.getDeletedUris().size(), equalTo(1));
  46. assertThat(contentResolver.delete(uri22, null, null), equalTo(1));
  47. assertThat(shadowContentResolver.getDeletedUris(), hasItem(uri22));
  48. assertThat(shadowContentResolver.getDeletedUris().size(), equalTo(2));
  49. }
  50. @Test
  51. public void query_shouldReturnTheCursorThatWasSet() throws Exception {
  52. assertNull(shadowContentResolver.query(null, null, null, null, null));
  53. TestCursor cursor = new TestCursor();
  54. shadowContentResolver.setCursor(cursor);
  55. assertThat((TestCursor) shadowContentResolver.query(null, null, null, null, null),
  56. sameInstance(cursor));
  57. }
  58. @Test
  59. public void query__shouldReturnSpecificCursorsForSpecificUris() throws Exception {
  60. assertNull(shadowContentResolver.query(uri21, null, null, null, null));
  61. assertNull(shadowContentResolver.query(uri22, null, null, null, null));
  62. TestCursor cursor21 = new TestCursor();
  63. TestCursor cursor22 = new TestCursor();
  64. shadowContentResolver.setCursor(uri21, cursor21);
  65. shadowContentResolver.setCursor(uri22, cursor22);
  66. assertThat((TestCursor) shadowContentResolver.query(uri21, null, null, null, null),
  67. sameInstance(cursor21));
  68. assertThat((TestCursor) shadowContentResolver.query(uri22, null, null, null, null),
  69. sameInstance(cursor22));
  70. }
  71. @Test
  72. public void query__shouldKnowWhatItsParamsWere() throws Exception {
  73. String[] projection = {};
  74. String selection = "select";
  75. String[] selectionArgs = {};
  76. String sortOrder = "order";
  77. QueryParamTrackingTestCursor testCursor = new QueryParamTrackingTestCursor();
  78. shadowContentResolver.setCursor(testCursor);
  79. Cursor cursor = shadowContentResolver.query(uri21, projection, selection, selectionArgs, sortOrder);
  80. assertThat((QueryParamTrackingTestCursor)cursor, equalTo(testCursor));
  81. assertThat(testCursor.uri, equalTo(uri21));
  82. assertThat(testCursor.projection, equalTo(projection));
  83. assertThat(testCursor.selection, equalTo(selection));
  84. assertThat(testCursor.selectionArgs, equalTo(selectionArgs));
  85. assertThat(testCursor.sortOrder, equalTo(sortOrder));
  86. }
  87. @Test
  88. public void openInputStream_shouldReturnAnInputStream() throws Exception {
  89. assertThat(contentResolver.openInputStream(uri21), CoreMatchers.instanceOf(InputStream.class));
  90. }
  91. class QueryParamTrackingTestCursor extends TestCursor {
  92. public Uri uri;
  93. public String[] projection;
  94. public String selection;
  95. public String[] selectionArgs;
  96. public String sortOrder;
  97. @Override
  98. public void setQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  99. this.uri = uri;
  100. this.projection = projection;
  101. this.selection = selection;
  102. this.selectionArgs = selectionArgs;
  103. this.sortOrder = sortOrder;
  104. }
  105. }
  106. }