diff options
author | Tatu Lund <tatu@vaadin.com> | 2020-02-13 09:40:12 +0200 |
---|---|---|
committer | Zhe Sun <31067185+ZheSun88@users.noreply.github.com> | 2020-02-20 16:23:38 +0200 |
commit | 2b0c501a1fe486ed3e1ee64423223dfb9d2f562e (patch) | |
tree | 6a1ab467e3e731e4736efc0d19d13acc10491a9e | |
parent | aec6dcd3824369ddab95af9c0d7245c82a10665c (diff) | |
download | vaadin-framework-2b0c501a1fe486ed3e1ee64423223dfb9d2f562e.tar.gz vaadin-framework-2b0c501a1fe486ed3e1ee64423223dfb9d2f562e.zip |
Fix logic of lexicographical comparison of DateField range end (#11885)
* Fix logic of lexicographical comparison of DateField range end
Fix logic of lexicographical comparison of DateField range end with large year numbers ( > 9999)
Fixes #11881
* Added tests
Purpose of dateFieldRangeYearDigitsIncrease() is to test that rangeEnd works correctly on 4 to 5 digits change boundary
Purpose of dateFieldRangeYearBigNumbersPopupOpens() is to test that popup opens also when there is more than 4 digits in year
* Fixing typo
* Add error message
* Set the date of the field in test
* Fixing
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java | 20 | ||||
-rw-r--r-- | uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidationTest.java | 46 |
2 files changed, 64 insertions, 2 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java index 8fde0264e4..765df8867d 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java @@ -762,8 +762,18 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> return true; } + // If dateStrResolution has more year digits than rangeEnd, we need + // to pad it in order to be lexicographically compatible String dateStrResolution = dateStrResolution(date, minResolution); - return rangeEnd.substring(0, dateStrResolution.length()) + String paddedEnd = rangeEnd.substring(0); + int yearDigits = dateStrResolution.indexOf("-"); + if (yearDigits == -1) { + yearDigits = dateStrResolution.length(); + } + while (paddedEnd.indexOf("-") < yearDigits) { + paddedEnd = "0" + paddedEnd; + } + return paddedEnd.substring(0, dateStrResolution.length()) .compareTo(dateStrResolution) >= 0; } @@ -2126,7 +2136,13 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> */ public void setRangeEnd(String newRangeEnd) { if (!SharedUtil.equals(rangeEnd, newRangeEnd)) { - rangeEnd = newRangeEnd; + // Dates with year 10000 or more has + prefix, which is not compatible + // with format returned by dateStrResolution method + if (newRangeEnd.startsWith("+")) { + rangeEnd = newRangeEnd.substring(1); + } else { + rangeEnd = newRangeEnd; + } if (initialRenderDone) { // Dynamic updates to the range needs to render the calendar to // update the element stylenames diff --git a/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidationTest.java b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidationTest.java index 77bf68d9a3..efbbf3f999 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidationTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidationTest.java @@ -10,6 +10,7 @@ import com.vaadin.testbench.elements.LabelElement; import com.vaadin.tests.tb3.SingleBrowserTest; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class DateFieldBinderCrossValidationTest extends SingleBrowserTest { @@ -37,4 +38,49 @@ public class DateFieldBinderCrossValidationTest extends SingleBrowserTest { assertEquals("Error message should be null", EXPECTED_NULL_ERROR, label.getText()); } + + @Test + public void dateFieldRangeYearDigitsIncrease() { + openTestURL(); + + DateFieldElement toField = $(DateFieldElement.class).id("to-field"); + // This will set the rangeEnd of the fromField + WebElement toFieldText = toField.findElement(By.tagName("input")); + toFieldText.sendKeys("9999/12/31", Keys.ENTER); + + DateFieldElement fromField = $(DateFieldElement.class).id("from-field"); + WebElement fromFieldText = fromField.findElement(By.tagName("input")); + // Set year to 9999, next year and next month will be on 10000 + fromFieldText.sendKeys("9999/12/01", Keys.ENTER); + fromField.openPopup(); + waitForElementPresent(By.className("v-datefield-popup")); + + WebElement monthYearLabel = findElement(By.className("v-datefield-calendarpanel-month")); + + // The next month button should be disabled + findElement(By.className("v-button-nextmonth")).click(); + // Test that year has not changed + assertTrue("Month label should contain 9999, contains: "+monthYearLabel.getText(),monthYearLabel.getText().contains("9999")); + + // The next year button should be disabled + findElement(By.className("v-button-nextyear")).click(); + // Test that year has not changed + assertTrue("Month label should contain 9999, contains: "+monthYearLabel.getText(),monthYearLabel.getText().contains("9999")); + } + + @Test + public void dateFieldRangeYearBigNumbersPopupOpens() { + openTestURL(); + + DateFieldElement toField = $(DateFieldElement.class).id("to-field"); + // This will set the rangeEnd of the fromField + WebElement toFieldText = toField.findElement(By.tagName("input")); + toFieldText.sendKeys("10000/12/31", Keys.ENTER); + DateFieldElement fromField = $(DateFieldElement.class).id("from-field"); + + // Test that popup opens + fromField.openPopup(); + waitForElementPresent(By.className("v-datefield-popup")); + assertElementPresent(By.className("v-datefield-popup")); + } } |