RangeUnit extends CalendarUnit { public int getFirstWeek(@Nullable LocalDate firstDayOfMonth) { LocalDate from = getFrom(); LocalDate date; if (mMinDate != null && mMinDate.isAfter(from)) { date = mMinDate; } else { date = firstDayOfMonth; } return getWeekInMonth(date); } protected  RangeUnit(@NonNull LocalDate from, @NonNull LocalDate to, @NonNull LocalDate today,
            @Nullable LocalDate minDate, @Nullable LocalDate maxDate); @Nullable LocalDate getMinDate(); @Nullable LocalDate getMaxDate(); int getFirstWeek(@Nullable LocalDate firstDayOfMonth);  }
@Test public void testGetFirstWeekNull() throws Exception { LocalDate today = LocalDate.now(); RangeUnit unit = new Month(today, today, null, null); assertEquals(0, unit.getFirstWeek(null)); }
@Test public void testGetFirstWeekCurrentMonth() throws Exception { LocalDate today = LocalDate.now(); LocalDate firstMonday = today.withDayOfMonth(1).withDayOfWeek(DateTimeConstants.MONDAY); int week = Days.daysBetween(firstMonday, today).dividedBy(DateTimeConstants.DAYS_PER_WEEK).getDays(); assertEquals(week, mRangeUnit.getFirstWeek(today)); }
@Test public void testGetFirstWeekMinDate() throws Exception { LocalDate today = LocalDate.now(); LocalDate minDate = today.minusMonths(2); LocalDate date = minDate.minusWeeks(1); RangeUnit unit = new Week(date, today, minDate, today); LocalDate firstMonday = minDate.withDayOfMonth(1).withDayOfWeek(DateTimeConstants.MONDAY); int week = Days.daysBetween(firstMonday, minDate).dividedBy(DateTimeConstants.DAYS_PER_WEEK).getDays(); assertEquals(week, unit.getFirstWeek(date)); }
@Test public void testGetFirstWeekBetween() throws Exception { LocalDate date = LocalDate.parse("2014-08-01"); RangeUnit unit = new Month(date, date, date.plusDays(2), date.plusWeeks(2)); assertEquals(0, unit.getFirstWeek(date.plusDays(12))); }
@Test public void testGetFirstWeekOutOfView() throws Exception { LocalDate date = LocalDate.parse("2014-08-01"); RangeUnit unit = new Week(date, date, date, date.plusWeeks(2)); assertEquals(0, unit.getFirstWeek(date.plusDays(12))); }
@Test public void testGetFirstWeekDifferentMonth() throws Exception { LocalDate date = LocalDate.parse("2014-08-01"); RangeUnit unit = new Month(date, date, date.plusDays(2), date.plusMonths(3)); assertEquals(0, unit.getFirstWeek(date.plusMonths(1).plusWeeks(1))); }