Skip to content

Commit

Permalink
Merge pull request #624 from opensrp/fix-numeric-datepicker-crash
Browse files Browse the repository at this point in the history
Fix Numeric Datepicker Crash
  • Loading branch information
qiarie authored Mar 23, 2022
2 parents 0307657 + 1d8ae00 commit f908cd6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f908cd6

Please sign in to comment.