@Test public void testOnDateConfiguredListener() { final Calendar testCal = Calendar.getInstance(locale); view.setDateSelectableFilter( new CalendarPickerView.DateSelectableFilter() { @Override public boolean isDateSelectable(Date date) { testCal.setTime(date); int dayOfWeek = testCal.get(DAY_OF_WEEK); return dayOfWeek > 1 && dayOfWeek < 7; } }); view.init(minDate, maxDate, locale) // .inMode(SINGLE) // .withSelectedDate(today.getTime()); Calendar jumpToCal = Calendar.getInstance(locale); jumpToCal.setTime(today.getTime()); jumpToCal.add(MONTH, 2); jumpToCal.set(DAY_OF_WEEK, 1); boolean wasAbleToSetDate = view.selectDate(jumpToCal.getTime()); assertThat(wasAbleToSetDate).isFalse(); jumpToCal.set(DAY_OF_WEEK, 2); wasAbleToSetDate = view.selectDate(jumpToCal.getTime()); assertThat(wasAbleToSetDate).isTrue(); }
@Test public void testMultiselectWithNoInitialSelections() throws Exception { view.init(minDate, maxDate, locale) // .inMode(MULTIPLE); assertThat(view.selectionMode).isEqualTo(MULTIPLE); assertThat(view.getSelectedDates()).isEmpty(); view.selectDate(minDate); assertThat(view.getSelectedDates()).hasSize(1); Calendar secondSelection = buildCal(2012, NOVEMBER, 17); view.selectDate(secondSelection.getTime()); assertThat(view.getSelectedDates()).hasSize(2); assertThat(view.getSelectedDates().get(1)).hasTime(secondSelection.getTimeInMillis()); }
@Test public void testRangeSelectionWithNoInitialSelection() throws Exception { view.init(minDate, maxDate, locale) // .inMode(RANGE); assertThat(view.selectedCals).hasSize(0); assertThat(view.selectedCells).hasSize(0); Calendar nov18 = buildCal(2012, NOVEMBER, 18); view.selectDate(nov18.getTime()); assertOneDateSelected(); Calendar nov24 = buildCal(2012, NOVEMBER, 24); view.selectDate(nov24.getTime()); assertRangeSelected(); assertRangeSelectionBehavior(); }
@Test public void selectDateReturnsTrueForDateInRange() { view.init(minDate, maxDate, locale) // .inMode(SINGLE) // .withSelectedDate(today.getTime()); Calendar inRange = buildCal(2013, FEBRUARY, 1); boolean wasAbleToSetDate = view.selectDate(inRange.getTime()); assertThat(wasAbleToSetDate).isTrue(); }
@Test public void selectDateDoesntSelectDisabledCell() { view.init(minDate, maxDate, locale) // .inMode(SINGLE) // .withSelectedDate(today.getTime()); Calendar jumpToCal = buildCal(2013, FEBRUARY, 1); boolean wasAbleToSetDate = view.selectDate(jumpToCal.getTime()); assertThat(wasAbleToSetDate).isTrue(); assertThat(view.selectedCells.get(0).isSelectable()).isTrue(); }
private void assertRangeSelectionBehavior() { // Start a new range in the middle of the current (Nov 18 - Nov 24) one. Calendar nov20 = buildCal(2012, NOVEMBER, 20); view.selectDate(nov20.getTime()); assertOneDateSelected(); // Finish that range. Calendar nov26 = buildCal(2012, NOVEMBER, 26); view.selectDate(nov26.getTime()); assertRangeSelected(); // Start a new range in the middle of the current (Nov 20 - Nov 26) one. Calendar nov24 = buildCal(2012, NOVEMBER, 24); view.selectDate(nov24.getTime()); assertOneDateSelected(); // Only Nov 24 is selected: going backward should start a new range. view.selectDate(nov20.getTime()); assertOneDateSelected(); }
@Test public void selectDateThrowsExceptionForDatesOutOfRange() { view.init(minDate, maxDate, locale) // .inMode(SINGLE) // .withSelectedDate(today.getTime()); Calendar outOfRange = buildCal(2015, FEBRUARY, 1); try { view.selectDate(outOfRange.getTime()); fail("selectDate should've blown up with an out of range date"); } catch (IllegalArgumentException expected) { } }
@Test public void testRangeStateOnDateSelections() { Calendar startCal = buildCal(2012, NOVEMBER, 17); Calendar endCal = buildCal(2012, NOVEMBER, 24); view.init(minDate, maxDate, locale) // .inMode(RANGE); boolean wasAbleToSetDate = view.selectDate(startCal.getTime()); assertThat(wasAbleToSetDate).isTrue(); wasAbleToSetDate = view.selectDate(endCal.getTime()); assertThat(wasAbleToSetDate).isTrue(); List<List<MonthCellDescriptor>> cells = getCells(NOVEMBER, 2012); assertCell(cells, 2, 6, 17, true, true, false, true, FIRST); assertCell(cells, 3, 0, 18, true, false, false, true, MIDDLE); assertCell(cells, 3, 1, 19, true, false, false, true, MIDDLE); assertCell(cells, 3, 2, 20, true, false, false, true, MIDDLE); assertCell(cells, 3, 3, 21, true, false, false, true, MIDDLE); assertCell(cells, 3, 4, 22, true, false, false, true, MIDDLE); assertCell(cells, 3, 5, 23, true, false, false, true, MIDDLE); assertCell(cells, 3, 6, 24, true, true, false, true, LAST); }
@Test public void testRangeWithOneInitialSelection() throws Exception { Calendar nov18 = buildCal(2012, NOVEMBER, 18); Calendar nov24 = buildCal(2012, NOVEMBER, 24); List<Date> selectedDates = Arrays.asList(nov18.getTime()); view.init(minDate, maxDate, locale) // .inMode(RANGE) // .withSelectedDates(selectedDates); assertOneDateSelected(); view.selectDate(nov24.getTime()); assertRangeSelected(); assertRangeSelectionBehavior(); }
private List<List<MonthCellDescriptor>> selectDateAndGetCells( int month, int year, Calendar selectedDate) { view.selectDate(selectedDate.getTime()); return getCells(month, year); }