CalendarAccessorPost40 implements CalendarAccessor { @Override @NonNull public Map<String, String> getCalendars() { final Map<String, String> map = new LinkedHashMap<String, String>(); Cursor cursor = null; try { cursor = resolver.query(CalendarContract.Calendars.CONTENT_URI, new String[] { CalendarContract.Calendars._ID, CalendarContract.Calendars.NAME, CalendarContract.Calendars.SYNC_EVENTS }, null, null, CalendarContract.Calendars.NAME + " ASC"); while (cursor != null && cursor.moveToNext()) { String id = cursor.getString(0); String name = cursor.getString(1); boolean isSynced = cursor.getInt(2) == 1; if (LOCAL_LOGV) Log.d(TAG, "id:"+id+", name:"+name+", synced:"+isSynced); map.put(id, name); } } catch (IllegalArgumentException e) { Log.e(TAG, "calendars not available", e); } catch (SecurityException e) { Log.e(TAG, "calendar permission missing", e); } finally { if (cursor != null) { cursor.close(); } } return map; } CalendarAccessorPost40(ContentResolver resolver); @Override boolean enableSync(long calendarId); @Override boolean addEntry(long calendarId, @NonNull Date when, int duration, @NonNull String title,
                            @NonNull String description); @Override @NonNull Map<String, String> getCalendars();  }
@Test public void shouldGetCalendars() throws Exception { MatrixCursor cursor = new MatrixCursor(new String[] { "_id", "name", "sync_events" } ); cursor.addRow(new Object[] { "12", "Testing", 1 }); when(resolver.query(eq(CalendarContract.Calendars.CONTENT_URI), any(String[].class), any(String.class), any(String[].class), eq(CalendarContract.Calendars.NAME + " ASC"))).thenReturn( cursor ); Map<String, String> calendars = accessor.getCalendars(); assertThat(calendars).hasSize(1); assertThat(calendars).containsEntry("12", "Testing"); }