PageRenderTime 825ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/roberttaylor426/robolectric
Java | 365 lines | 312 code | 53 blank | 0 comment | 0 complexity | 35895c8d09f15746be59b1480a4cc907 MD5 | raw file
  1. package com.xtremelabs.robolectric.shadows;
  2. import static android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  3. import static com.xtremelabs.robolectric.Robolectric.shadowOf;
  4. import static org.hamcrest.CoreMatchers.equalTo;
  5. import static org.hamcrest.CoreMatchers.hasItem;
  6. import static org.hamcrest.CoreMatchers.is;
  7. import static org.hamcrest.CoreMatchers.notNullValue;
  8. import static org.hamcrest.CoreMatchers.nullValue;
  9. import static org.hamcrest.CoreMatchers.sameInstance;
  10. import static org.junit.Assert.*;
  11. import java.io.InputStream;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import android.accounts.Account;
  15. import android.content.*;
  16. import android.os.Bundle;
  17. import org.hamcrest.CoreMatchers;
  18. import org.junit.Before;
  19. import org.junit.Test;
  20. import org.junit.runner.RunWith;
  21. import android.app.Activity;
  22. import android.database.Cursor;
  23. import android.net.Uri;
  24. import android.os.RemoteException;
  25. import com.xtremelabs.robolectric.WithTestDefaultsRunner;
  26. import com.xtremelabs.robolectric.tester.android.database.TestCursor;
  27. @RunWith(WithTestDefaultsRunner.class)
  28. public class ContentResolverTest {
  29. static final String AUTHORITY = "com.xtremelabs.robolectric";
  30. private ContentResolver contentResolver;
  31. private ShadowContentResolver shadowContentResolver;
  32. private Uri uri21;
  33. private Uri uri22;
  34. private Account a, b;
  35. @Before
  36. public void setUp() throws Exception {
  37. contentResolver = new Activity().getContentResolver();
  38. shadowContentResolver = shadowOf(contentResolver);
  39. uri21 = Uri.parse(EXTERNAL_CONTENT_URI.toString() + "/21");
  40. uri22 = Uri.parse(EXTERNAL_CONTENT_URI.toString() + "/22");
  41. a = new Account("a", "type");
  42. b = new Account("b", "type");
  43. }
  44. @Test
  45. public void insert_shouldReturnIncreasingUris() throws Exception {
  46. shadowContentResolver.setNextDatabaseIdForInserts(21);
  47. assertThat(contentResolver.insert(EXTERNAL_CONTENT_URI, new ContentValues()), equalTo(uri21));
  48. assertThat(contentResolver.insert(EXTERNAL_CONTENT_URI, new ContentValues()), equalTo(uri22));
  49. }
  50. @Test
  51. public void insert_shouldTrackInsertStatements() throws Exception {
  52. ContentValues contentValues = new ContentValues();
  53. contentValues.put("foo", "bar");
  54. contentResolver.insert(EXTERNAL_CONTENT_URI, contentValues);
  55. assertThat(shadowContentResolver.getInsertStatements().size(), is(1));
  56. assertThat(shadowContentResolver.getInsertStatements().get(0).getUri(), equalTo(EXTERNAL_CONTENT_URI));
  57. assertThat(shadowContentResolver.getInsertStatements().get(0).getContentValues().getAsString("foo"), equalTo("bar"));
  58. contentValues = new ContentValues();
  59. contentValues.put("hello", "world");
  60. contentResolver.insert(EXTERNAL_CONTENT_URI, contentValues);
  61. assertThat(shadowContentResolver.getInsertStatements().size(), is(2));
  62. assertThat(shadowContentResolver.getInsertStatements().get(1).getContentValues().getAsString("hello"), equalTo("world"));
  63. }
  64. @Test
  65. public void insert_shouldTrackUpdateStatements() throws Exception {
  66. ContentValues contentValues = new ContentValues();
  67. contentValues.put("foo", "bar");
  68. contentResolver.update(EXTERNAL_CONTENT_URI, contentValues, "robolectric", new String[] { "awesome" });
  69. assertThat(shadowContentResolver.getUpdateStatements().size(), is(1));
  70. assertThat(shadowContentResolver.getUpdateStatements().get(0).getUri(), equalTo(EXTERNAL_CONTENT_URI));
  71. assertThat(shadowContentResolver.getUpdateStatements().get(0).getContentValues().getAsString("foo"), equalTo("bar"));
  72. assertThat(shadowContentResolver.getUpdateStatements().get(0).getWhere(), equalTo("robolectric"));
  73. assertThat(shadowContentResolver.getUpdateStatements().get(0).getSelectionArgs(), equalTo(new String[] { "awesome" }));
  74. contentValues = new ContentValues();
  75. contentValues.put("hello", "world");
  76. contentResolver.update(EXTERNAL_CONTENT_URI, contentValues, null, null);
  77. assertThat(shadowContentResolver.getUpdateStatements().size(), is(2));
  78. assertThat(shadowContentResolver.getUpdateStatements().get(1).getUri(), equalTo(EXTERNAL_CONTENT_URI));
  79. assertThat(shadowContentResolver.getUpdateStatements().get(1).getContentValues().getAsString("hello"), equalTo("world"));
  80. assertThat(shadowContentResolver.getUpdateStatements().get(1).getWhere(), nullValue());
  81. assertThat(shadowContentResolver.getUpdateStatements().get(1).getSelectionArgs(), nullValue());
  82. }
  83. @Test
  84. public void delete_shouldTrackDeletedUris() throws Exception {
  85. assertThat(shadowContentResolver.getDeletedUris().size(), equalTo(0));
  86. assertThat(contentResolver.delete(uri21, null, null), equalTo(1));
  87. assertThat(shadowContentResolver.getDeletedUris(), hasItem(uri21));
  88. assertThat(shadowContentResolver.getDeletedUris().size(), equalTo(1));
  89. assertThat(contentResolver.delete(uri22, null, null), equalTo(1));
  90. assertThat(shadowContentResolver.getDeletedUris(), hasItem(uri22));
  91. assertThat(shadowContentResolver.getDeletedUris().size(), equalTo(2));
  92. }
  93. @Test
  94. public void delete_shouldTrackDeletedStatements() {
  95. assertThat(shadowContentResolver.getDeleteStatements().size(), equalTo(0));
  96. assertThat(contentResolver.delete(uri21, "id", new String[] { "5" }), equalTo(1));
  97. assertThat(shadowContentResolver.getDeleteStatements().size(), equalTo(1));
  98. assertThat(shadowContentResolver.getDeleteStatements().get(0).getUri(), equalTo(uri21));
  99. assertThat(shadowContentResolver.getDeleteStatements().get(0).getWhere(), equalTo("id"));
  100. assertThat(shadowContentResolver.getDeleteStatements().get(0).getSelectionArgs()[0], equalTo("5"));
  101. assertThat(contentResolver.delete(uri21, "foo", new String[] { "bar" }), equalTo(1));
  102. assertThat(shadowContentResolver.getDeleteStatements().size(), equalTo(2));
  103. assertThat(shadowContentResolver.getDeleteStatements().get(1).getUri(), equalTo(uri21));
  104. assertThat(shadowContentResolver.getDeleteStatements().get(1).getWhere(), equalTo("foo"));
  105. assertThat(shadowContentResolver.getDeleteStatements().get(1).getSelectionArgs()[0], equalTo("bar"));
  106. }
  107. @Test
  108. public void query_shouldReturnTheCursorThatWasSet() throws Exception {
  109. assertNull(shadowContentResolver.query(null, null, null, null, null));
  110. TestCursor cursor = new TestCursor();
  111. shadowContentResolver.setCursor(cursor);
  112. assertThat((TestCursor) shadowContentResolver.query(null, null, null, null, null),
  113. sameInstance(cursor));
  114. }
  115. @Test
  116. public void query__shouldReturnSpecificCursorsForSpecificUris() throws Exception {
  117. assertNull(shadowContentResolver.query(uri21, null, null, null, null));
  118. assertNull(shadowContentResolver.query(uri22, null, null, null, null));
  119. TestCursor cursor21 = new TestCursor();
  120. TestCursor cursor22 = new TestCursor();
  121. shadowContentResolver.setCursor(uri21, cursor21);
  122. shadowContentResolver.setCursor(uri22, cursor22);
  123. assertThat((TestCursor) shadowContentResolver.query(uri21, null, null, null, null),
  124. sameInstance(cursor21));
  125. assertThat((TestCursor) shadowContentResolver.query(uri22, null, null, null, null),
  126. sameInstance(cursor22));
  127. }
  128. @Test
  129. public void query__shouldKnowWhatItsParamsWere() throws Exception {
  130. String[] projection = {};
  131. String selection = "select";
  132. String[] selectionArgs = {};
  133. String sortOrder = "order";
  134. QueryParamTrackingTestCursor testCursor = new QueryParamTrackingTestCursor();
  135. shadowContentResolver.setCursor(testCursor);
  136. Cursor cursor = shadowContentResolver.query(uri21, projection, selection, selectionArgs, sortOrder);
  137. assertThat((QueryParamTrackingTestCursor)cursor, equalTo(testCursor));
  138. assertThat(testCursor.uri, equalTo(uri21));
  139. assertThat(testCursor.projection, equalTo(projection));
  140. assertThat(testCursor.selection, equalTo(selection));
  141. assertThat(testCursor.selectionArgs, equalTo(selectionArgs));
  142. assertThat(testCursor.sortOrder, equalTo(sortOrder));
  143. }
  144. @Test
  145. public void openInputStream_shouldReturnAnInputStream() throws Exception {
  146. assertThat(contentResolver.openInputStream(uri21), CoreMatchers.instanceOf(InputStream.class));
  147. }
  148. @Test
  149. public void shouldTrackNotifiedUris() throws Exception {
  150. contentResolver.notifyChange(Uri.parse("foo"), null, true);
  151. contentResolver.notifyChange(Uri.parse("bar"), null);
  152. assertThat(shadowContentResolver.getNotifiedUris().size(), equalTo(2));
  153. ShadowContentResolver.NotifiedUri uri = shadowContentResolver.getNotifiedUris().get(0);
  154. assertThat(uri.uri.toString(), equalTo("foo"));
  155. assertTrue(uri.syncToNetwork);
  156. assertNull(uri.observer);
  157. uri = shadowContentResolver.getNotifiedUris().get(1);
  158. assertThat(uri.uri.toString(), equalTo("bar"));
  159. assertFalse(uri.syncToNetwork);
  160. assertNull(uri.observer);
  161. }
  162. @Test
  163. public void applyBatch() throws RemoteException, OperationApplicationException {
  164. ArrayList<ContentProviderOperation> resultOperations = shadowContentResolver.getContentProviderOperations(AUTHORITY);
  165. assertThat(resultOperations, notNullValue());
  166. assertThat(resultOperations.size(), is(0));
  167. ContentProviderResult[] contentProviderResults = new ContentProviderResult[] {
  168. new ContentProviderResult(1),
  169. new ContentProviderResult(1),
  170. };
  171. shadowContentResolver.setContentProviderResult(contentProviderResults);
  172. Uri uri = Uri.parse("content://com.xtremelabs.robolectric");
  173. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  174. operations.add(ContentProviderOperation.newInsert(uri)
  175. .withValue("column1", "foo")
  176. .withValue("column2", 5)
  177. .build());
  178. operations.add(ContentProviderOperation.newUpdate(uri)
  179. .withSelection("id_column", new String[] { "99" })
  180. .withValue("column1", "bar")
  181. .build());
  182. operations.add(ContentProviderOperation.newDelete(uri)
  183. .withSelection("id_column", new String[] { "11" })
  184. .build());
  185. ContentProviderResult[] result = contentResolver.applyBatch(AUTHORITY, operations);
  186. resultOperations = shadowContentResolver.getContentProviderOperations(AUTHORITY);
  187. assertThat(resultOperations, equalTo(operations));
  188. assertThat(result, equalTo(contentProviderResults));
  189. }
  190. @Test
  191. public void shouldKeepTrackOfSyncRequests() throws Exception {
  192. ShadowContentResolver.Status status = ShadowContentResolver.getStatus(a, AUTHORITY, true);
  193. assertNotNull(status);
  194. assertThat(status.syncRequests, equalTo(0));
  195. ContentResolver.requestSync(a, AUTHORITY, new Bundle());
  196. assertThat(status.syncRequests, equalTo(1));
  197. assertNotNull(status.syncExtras);
  198. }
  199. @Test
  200. public void shouldSetIsSyncable() throws Exception {
  201. assertThat(ContentResolver.getIsSyncable(a, AUTHORITY), equalTo(-1));
  202. assertThat(ContentResolver.getIsSyncable(b, AUTHORITY), equalTo(-1));
  203. ContentResolver.setIsSyncable(a, AUTHORITY, 1);
  204. ContentResolver.setIsSyncable(b, AUTHORITY, 2);
  205. assertThat(ContentResolver.getIsSyncable(a, AUTHORITY), equalTo(1));
  206. assertThat(ContentResolver.getIsSyncable(b, AUTHORITY), equalTo(2));
  207. }
  208. @Test
  209. public void shouldSetSyncAutomatically() throws Exception {
  210. assertFalse(ContentResolver.getSyncAutomatically(a, AUTHORITY));
  211. ContentResolver.setSyncAutomatically(a, AUTHORITY, true);
  212. assertTrue(ContentResolver.getSyncAutomatically(a, AUTHORITY));
  213. }
  214. @Test
  215. public void shouldAddPeriodicSync() throws Exception {
  216. ContentResolver.addPeriodicSync(a, AUTHORITY, new Bundle(), 6000l);
  217. ShadowContentResolver.Status status = ShadowContentResolver.getStatus(a, AUTHORITY);
  218. assertNotNull(status);
  219. assertThat(status.syncs.size(), is(1));
  220. assertThat(status.syncs.get(0).period, is(6000l));
  221. assertNotNull(status.syncs.get(0).extras);
  222. }
  223. @Test
  224. public void shouldRemovePeriodSync() throws Exception {
  225. ContentResolver.addPeriodicSync(a, AUTHORITY, new Bundle(), 6000l);
  226. ContentResolver.removePeriodicSync(a, AUTHORITY, new Bundle());
  227. assertThat(ShadowContentResolver.getStatus(a, AUTHORITY).syncs.size(), is(0));
  228. }
  229. @Test
  230. public void shouldGetPeriodSyncs() throws Exception {
  231. assertThat(ContentResolver.getPeriodicSyncs(a, AUTHORITY).size(), is(0));
  232. ContentResolver.addPeriodicSync(a, AUTHORITY, new Bundle(), 6000l);
  233. List<PeriodicSync> syncs = ContentResolver.getPeriodicSyncs(a, AUTHORITY);
  234. assertThat(syncs.size(), is(1));
  235. PeriodicSync first = syncs.get(0);
  236. assertThat(first.account, equalTo(a));
  237. assertThat(first.authority, equalTo(AUTHORITY));
  238. assertThat(first.period, equalTo(6000l));
  239. assertNotNull(first.extras);
  240. }
  241. @Test
  242. public void shouldValidateSyncExtras() throws Exception {
  243. Bundle bundle = new Bundle();
  244. bundle.putString("foo", "strings");
  245. bundle.putLong("long", 10l);
  246. bundle.putDouble("double", 10.0d);
  247. bundle.putFloat("float", 10.0f);
  248. bundle.putInt("int", 10);
  249. bundle.putParcelable("account", a);
  250. ContentResolver.validateSyncExtrasBundle(bundle);
  251. }
  252. @Test(expected = IllegalArgumentException.class)
  253. public void shouldValidateSyncExtrasAndThrow() throws Exception {
  254. Bundle bundle = new Bundle();
  255. bundle.putParcelable("intent", new Intent());
  256. ContentResolver.validateSyncExtrasBundle(bundle);
  257. }
  258. @Test
  259. public void shouldSetMasterSyncAutomatically() throws Exception {
  260. assertFalse(ContentResolver.getMasterSyncAutomatically());
  261. ContentResolver.setMasterSyncAutomatically(true);
  262. assertTrue(ContentResolver.getMasterSyncAutomatically());
  263. }
  264. @Test
  265. public void shouldDelegateCallsToRegisteredProvider() throws Exception {
  266. ShadowContentResolver.registerProvider(AUTHORITY, new ContentProvider() {
  267. @Override public boolean onCreate() {
  268. return false;
  269. }
  270. @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  271. return new TestCursor();
  272. }
  273. @Override public Uri insert(Uri uri, ContentValues values) {
  274. return null;
  275. }
  276. @Override public int delete(Uri uri, String selection, String[] selectionArgs) {
  277. return -1;
  278. }
  279. @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  280. return -1;
  281. }
  282. @Override public String getType(Uri uri) {
  283. return null;
  284. }
  285. });
  286. final Uri uri = Uri.parse("content://"+AUTHORITY+"/some/path");
  287. final Uri unrelated = Uri.parse("content://unrelated/some/path");
  288. assertNotNull(contentResolver.query(uri, null, null, null, null));
  289. assertNull(contentResolver.insert(uri, new ContentValues()));
  290. assertThat(contentResolver.delete(uri, null, null), is(-1));
  291. assertThat(contentResolver.update(uri, new ContentValues(), null, null), is(-1));
  292. assertNull(contentResolver.query(unrelated, null, null, null, null));
  293. assertNotNull(contentResolver.insert(unrelated, new ContentValues()));
  294. assertThat(contentResolver.delete(unrelated, null, null), is(1));
  295. assertThat(contentResolver.update(unrelated, new ContentValues(), null, null), is(0));
  296. }
  297. static class QueryParamTrackingTestCursor extends TestCursor {
  298. public Uri uri;
  299. public String[] projection;
  300. public String selection;
  301. public String[] selectionArgs;
  302. public String sortOrder;
  303. @Override
  304. public void setQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  305. this.uri = uri;
  306. this.projection = projection;
  307. this.selection = selection;
  308. this.selectionArgs = selectionArgs;
  309. this.sortOrder = sortOrder;
  310. }
  311. }
  312. }