diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NumericDatePicker.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NumericDatePicker.java index 8ff47a6e2..d8c1df1a9 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NumericDatePicker.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/customviews/NumericDatePicker.java @@ -657,7 +657,19 @@ private int getMaxDayForSelectedDate() { private int getPreviousDay(int prevDay, int minDay) { if (prevDay > 0) { - return getMonth() + 1 == 2 ? Math.min(prevDay, (NumericDatePickerHelper.isLeapYear(getYear()) ? 29 : 28)) : prevDay; + int selectedMonth = getMonth() + 1; + + switch (selectedMonth) { + case 2: + return Math.min(prevDay, (NumericDatePickerHelper.isLeapYear(getYear()) ? 29 : 28)); + case 4: + case 6: + case 9: + case 11: + return Math.min(prevDay, 30); + default: + return Math.min(prevDay, 31); + } } else { return minDay; } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/customviews/NumericDatePickerTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/customviews/NumericDatePickerTest.java index 8f77ca81e..c99208e00 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/customviews/NumericDatePickerTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/customviews/NumericDatePickerTest.java @@ -1,5 +1,7 @@ package com.vijay.jsonwizard.customviews; +import static org.mockito.Mockito.mock; + import android.util.AttributeSet; import android.widget.DatePicker; import android.widget.NumberPicker; @@ -12,7 +14,9 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.powermock.reflect.Whitebox; import org.robolectric.Robolectric; import org.robolectric.RuntimeEnvironment; import org.robolectric.util.ReflectionHelpers; @@ -442,4 +446,70 @@ public void testResetDatePickerCreatesValidDateWhenMinConstraintsViolatedAfterUp Assert.assertEquals(Calendar.OCTOBER, datePicker.getMonth()); } + + @Test + public void testGetPreviousDayReturns1WhenDateSelectedIsLowerThanZero() throws Exception { + NumericDatePicker datePicker1 = mock(NumericDatePicker.class); + Mockito.when(datePicker1.getMonth()).thenReturn(10); // November + Mockito.when(datePicker1.getYear()).thenReturn(2022); + + int prevDay = -2; + int minDay = 1; + Assert.assertEquals(1, Whitebox.invokeMethod(datePicker1, "getPreviousDay", prevDay, minDay)); + } + + @Test + public void testGetPreviousDayReturnsMinDayWhenDateSelectedIsLowerThanLastDayOfTheMonth() throws Exception { + NumericDatePicker datePicker1 = mock(NumericDatePicker.class); + Mockito.when(datePicker1.getMonth()).thenReturn(10); // November + Mockito.when(datePicker1.getYear()).thenReturn(2022); + + int prevDay = 10; + int minDay = 30; + Assert.assertEquals(10, Whitebox.invokeMethod(datePicker1, "getPreviousDay", prevDay, minDay)); + } + + @Test + public void testGetPreviousDayReturns30WhenMonthIs30DaysLongAndDateSelectedIsHigher() throws Exception { + NumericDatePicker datePicker1 = mock(NumericDatePicker.class); + Mockito.when(datePicker1.getMonth()).thenReturn(3); // April + Mockito.when(datePicker1.getYear()).thenReturn(2022); + + int prevDay = 31; + int minDay = 30; + Assert.assertEquals(30, Whitebox.invokeMethod(datePicker1, "getPreviousDay", prevDay, minDay)); + } + + @Test + public void testGetPreviousDayReturns31WhenMonthIs31DaysLongAndDateSelectedIsHigher() throws Exception { + NumericDatePicker datePicker1 = mock(NumericDatePicker.class); + Mockito.when(datePicker1.getMonth()).thenReturn(0); // January + Mockito.when(datePicker1.getYear()).thenReturn(2022); + + int prevDay = 35; + int minDay = 31; + Assert.assertEquals(31, Whitebox.invokeMethod(datePicker1, "getPreviousDay", prevDay, minDay)); + } + + @Test + public void testGetPreviousDayReturns28WhenMonthIsFebruaryAndYearIsNotALeapYearAndDateSelectedIsHigher() throws Exception { + NumericDatePicker datePicker1 = mock(NumericDatePicker.class); + Mockito.when(datePicker1.getMonth()).thenReturn(1); + Mockito.when(datePicker1.getYear()).thenReturn(2022); + + int prevDay = 31; + int minDay = 28; + Assert.assertEquals(28, Whitebox.invokeMethod(datePicker1, "getPreviousDay", prevDay, minDay)); + } + + @Test + public void testGetPreviousDayReturns29WhenMonthIsFebruaryAndYearIsALeapYearAndDateSelectedIsHigher() throws Exception { + NumericDatePicker datePicker1 = mock(NumericDatePicker.class); + Mockito.when(datePicker1.getMonth()).thenReturn(1); + Mockito.when(datePicker1.getYear()).thenReturn(2020); + + int prevDay = 31; + int minDay = 29; + Assert.assertEquals(29, Whitebox.invokeMethod(datePicker1, "getPreviousDay", prevDay, minDay)); + } } \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java index f24e6e118..f27b7b2b6 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java @@ -144,6 +144,13 @@ public void testGetDurationWithDays() { Assert.assertEquals("0d", duration); } + @Test + public void testGetDurationWithMonths() { + String duration = Utils.getDuration("10-01-2021", "01-12-2021"); + Assert.assertNotNull(duration); + Assert.assertEquals("10m 3w", duration); + } + @Test public void testBuildRulesWithUniqueIdShouldUpdateRelevanceRulesEngineObjectAccordingly() throws JSONException, IOException { String ruleType = "relevance"; diff --git a/gradle.properties b/gradle.properties index 014527c99..31f720489 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=2.1.15-SNAPSHOT +VERSION_NAME=2.1.16-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard