1515 */
1616package com .google .android .material .datepicker ;
1717
18+ import static com .google .common .truth .Truth .assertThat ;
1819import static org .junit .Assert .assertEquals ;
1920
2021import com .google .android .material .internal .ParcelableTestUtils ;
2425import org .junit .rules .ExpectedException ;
2526import org .junit .runner .RunWith ;
2627import org .robolectric .RobolectricTestRunner ;
27- import org .robolectric .annotation .internal .DoNotInstrument ;
2828
29+ /** Tests for {@link CalendarConstraints} */
2930@ RunWith (RobolectricTestRunner .class )
30- @ DoNotInstrument
3131public class CalendarConstraintsTest {
3232
3333 private static final long FEB_2016 = Month .create (2016 , Calendar .FEBRUARY ).timeInMillis ;
@@ -45,6 +45,52 @@ public void equalAfterParceling() {
4545 assertEquals (originalBounds , constructedBounds );
4646 }
4747
48+ @ Test
49+ public void clampMonth_when_InsideBounds () {
50+ Month today = Month .today ();
51+ Month yearBefore = today .monthsLater (-12 );
52+ Month yearAfter = today .monthsLater (12 );
53+
54+ long start = yearBefore .timeInMillis ;
55+ long end = yearAfter .timeInMillis ;
56+ CalendarConstraints calendarConstraints =
57+ new CalendarConstraints .Builder ().setStart (start ).setEnd (end ).build ();
58+
59+ Month monthWithinBounds = yearAfter .monthsLater (-5 );
60+ assertThat (calendarConstraints .clamp (monthWithinBounds ))
61+ .isEqualTo (monthWithinBounds );
62+ }
63+
64+ @ Test
65+ public void clampMonth_when_beforeLowerBound () {
66+ Month today = Month .today ();
67+ Month yearBefore = today .monthsLater (-12 );
68+ Month yearAfter = today .monthsLater (12 );
69+
70+ long start = yearBefore .timeInMillis ;
71+ long end = yearAfter .timeInMillis ;
72+ CalendarConstraints calendarConstraints =
73+ new CalendarConstraints .Builder ().setStart (start ).setEnd (end ).build ();
74+
75+ assertThat (calendarConstraints .clamp (yearBefore .monthsLater (-5 )))
76+ .isEqualTo (yearBefore );
77+ }
78+
79+ @ Test
80+ public void clampMonth_when_AfterUpperBound () {
81+ Month today = Month .today ();
82+ Month yearBefore = today .monthsLater (-12 );
83+ Month yearAfter = today .monthsLater (12 );
84+
85+ long start = yearBefore .timeInMillis ;
86+ long end = yearAfter .timeInMillis ;
87+ CalendarConstraints calendarConstraints =
88+ new CalendarConstraints .Builder ().setStart (start ).setEnd (end ).build ();
89+
90+ assertThat (calendarConstraints .clamp (yearAfter .monthsLater (5 )))
91+ .isEqualTo (yearAfter );
92+ }
93+
4894 @ Test
4995 public void currentDefaultsToTodayIfWithinBounds () {
5096 Month today = Month .today ();
0 commit comments