ContactAccessor { public Map<Integer, Group> getGroups(ContentResolver resolver, Resources resources) { final Map<Integer, Group> map = new LinkedHashMap<Integer, Group>(); map.put(EVERYBODY_ID, new Group(EVERYBODY_ID, resources.getString(R.string.everybody), 0)); final Cursor c = resolver.query( Groups.CONTENT_SUMMARY_URI, new String[]{Groups._ID, Groups.TITLE, Groups.SUMMARY_COUNT}, null, null, Groups.TITLE + " ASC"); while (c != null && c.moveToNext()) { map.put(c.getInt(0), new Group(c.getInt(0), c.getString(1), c.getInt(2))); } if (c != null) c.close(); return map; }  Map<Integer, Group> getGroups(ContentResolver resolver, Resources resources); ContactGroupIds getGroupContactIds(ContentResolver resolver, ContactGroup group);  }
@Test public void shouldAccessContactsWithEverybody() throws Exception { Map<Integer,Group> groups = accessor.getGroups(resolver, RuntimeEnvironment.application.getResources()); assertThat(groups).hasSize(1); Group everybody = groups.get(EVERYBODY_ID); assertThat(everybody.title).isEqualTo("Everybody"); assertThat(everybody._id).isEqualTo(EVERYBODY_ID); assertThat(everybody.count).isEqualTo(0); verify(resolver).query(eq(ContactsContract.Groups.CONTENT_SUMMARY_URI), any(String[].class), any(String.class), any(String[].class), eq(ContactsContract.Groups.TITLE + " ASC")); }
@Test public void shouldGetGroupsFromResolver() throws Exception { MatrixCursor cursor = new MatrixCursor( new String[]{ContactsContract.Groups._ID, ContactsContract.Groups.TITLE, ContactsContract.Groups.SUMMARY_COUNT} ); cursor.addRow(new Object[] { 23, "Testing", 42 }); when(resolver.query(eq(ContactsContract.Groups.CONTENT_SUMMARY_URI), any(String[].class), any(String.class), any(String[].class), eq(ContactsContract.Groups.TITLE + " ASC"))).thenReturn( cursor ); Map<Integer,Group> groups = accessor.getGroups(resolver, RuntimeEnvironment.application.getResources()); assertThat(groups).hasSize(2); Group everybody = groups.get(23); assertThat(everybody.title).isEqualTo("Testing"); assertThat(everybody._id).isEqualTo(23); assertThat(everybody.count).isEqualTo(42); }