CalendarAccessorPost40 implements CalendarAccessor { @Override public boolean addEntry(long calendarId, @NonNull Date when, int duration, @NonNull String title, @NonNull String description) { if (LOCAL_LOGV) { Log.v(TAG, String.format("addEntry(%d, %s, %d, %s, %s)", calendarId, when.toString(), duration, title, description)); } final ContentValues contentValues = new ContentValues(); contentValues.put(CalendarContract.Events.TITLE, title); contentValues.put(CalendarContract.Events.DESCRIPTION, description); contentValues.put(CalendarContract.Events.DTSTART, when.getTime()); contentValues.put(CalendarContract.Events.DTEND, when.getTime() + duration * 1000); contentValues.put(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_DEFAULT); contentValues.put(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CONFIRMED); contentValues.put(CalendarContract.Events.CALENDAR_ID, calendarId); contentValues.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); try { resolver.insert(CalendarContract.Events.CONTENT_URI, contentValues); return true; } catch (IllegalArgumentException e) { Log.e(TAG, "could not add calendar entry", e); return false; } catch (SQLiteException e) { Log.w(TAG, "could not add calendar entry", e); return false; } catch (SecurityException e) { Log.w(TAG, "could not add calendar entry (permission)", e); return false; } } 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 @Config public void shouldAddEntry() throws Exception { ArgumentCaptor<Uri> uri = ArgumentCaptor.forClass(Uri.class); ArgumentCaptor<ContentValues> values = ArgumentCaptor.forClass(ContentValues.class); Date when = new Date(); accessor.addEntry( 12, when, 100, "Title", "Desc"); verify(resolver).insert(uri.capture(), values.capture()); assertThat(uri.getValue().toString()).isEqualTo("content: ContentValues cv = values.getValue(); assertThat(cv.getAsString(Events.TITLE)).isEqualTo("Title"); assertThat(cv.getAsString(Events.DESCRIPTION)).isEqualTo("Desc"); assertThat(cv.getAsLong(Events.DTSTART)).isEqualTo(when.getTime()); assertThat(cv.getAsLong(Events.DTEND)).isGreaterThan(when.getTime()); assertThat(cv.getAsInteger(Events.ACCESS_LEVEL)).isEqualTo(CalendarContract.Events.ACCESS_DEFAULT); assertThat(cv.getAsInteger(Events.STATUS)).isEqualTo(CalendarContract.Events.STATUS_CONFIRMED); assertThat(cv.getAsLong(Events.CALENDAR_ID)).isEqualTo(12L); assertThat(cv.getAsString(Events.EVENT_TIMEZONE)).isEqualTo(Time.getCurrentTimezone()); }
@Test public void shouldIgnoreSQLiteException() { when(resolver.insert(eq(CONTENT_URI), any(ContentValues.class))).thenThrow(SQLiteException.class); boolean result = accessor.addEntry( 12, new Date(), 100, "Title", "Desc"); assertThat(result).isFalse(); }