diff options
author | Sun Zhe <31067185+ZheSun88@users.noreply.github.com> | 2018-11-13 13:48:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-13 13:48:28 +0200 |
commit | dd0b0f44c4d9639fe6db60fe987eef0c4d9e36b7 (patch) | |
tree | b580007e921306c10ae2c25a9dfb06bf22aa92fd /uitest | |
parent | 26a2ffd8e467598c7ca388df4130f7532999cf9b (diff) | |
download | vaadin-framework-dd0b0f44c4d9639fe6db60fe987eef0c4d9e36b7.tar.gz vaadin-framework-dd0b0f44c4d9639fe6db60fe987eef0c4d9e36b7.zip |
Correct the logic for validation used in DateField (#11307)
Fixes #11276
Refactor the code and correct the logic for validation
Rename an internal variable, as it contains more than one type of messages
Diffstat (limited to 'uitest')
2 files changed, 143 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidation.java b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidation.java new file mode 100644 index 0000000000..21685f6523 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidation.java @@ -0,0 +1,103 @@ +package com.vaadin.tests.components.datefield; + +import java.time.LocalDate; +import java.util.Objects; + +import com.gargoylesoftware.htmlunit.javascript.host.html.FormField; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.data.Binder; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.ui.ErrorLevel; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.Button; +import com.vaadin.ui.DateField; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.TextField; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class DateFieldBinderCrossValidation extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + HorizontalLayout horizontalLayout = new HorizontalLayout(); + Label label = new Label(); + label.setId("status"); + + Binder<FromToModel> fromToModelBinder = new Binder<>(); + + DateField fromField = new DateField(); + fromField.setId("from-field"); + fromField.setDateFormat("yyyy/MM/dd"); + final Binder.Binding<FromToModel, LocalDate> fromBinding = fromToModelBinder + .forField(fromField).asRequired() + .bind(FromToModel::getFromDate, FromToModel::setFromDate); + DateField toField = new DateField(); + toField.setId("to-field"); + toField.setDateFormat("yyyy/MM/dd"); + final Binder.Binding<FromToModel, LocalDate> toBinding = fromToModelBinder + .forField(toField).asRequired() + .bind(FromToModel::getToDate, FromToModel::setToDate); + + fromField.addValueChangeListener(e -> { + toField.setRangeStart(e.getValue()); + if (toField.getValue() != null) { + toBinding.validate(); + } + label.setValue("from field is " + fromField.getErrorMessage() + + ". To field is " + toField.getErrorMessage()); + }); + toField.addValueChangeListener(e -> { + fromField.setRangeEnd(e.getValue()); + if (fromField.getValue() != null) { + fromBinding.validate(); + } + label.setValue("from field is " + fromField.getErrorMessage() + + ". To field is " + toField.getErrorMessage()); + }); + + horizontalLayout.addComponents(fromField, toField, label); + + addComponents(horizontalLayout); + } + + private static class FromToModel { + + private LocalDate fromDate; + private LocalDate toDate; + + public LocalDate getFromDate() { + return fromDate; + } + + public void setFromDate(LocalDate fromDate) { + this.fromDate = fromDate; + } + + public LocalDate getToDate() { + return toDate; + } + + public void setToDate(LocalDate toDate) { + this.toDate = toDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof FromToModel)) + return false; + FromToModel that = (FromToModel) o; + return Objects.equals(getFromDate(), that.getFromDate()) + && Objects.equals(getToDate(), that.getToDate()); + } + + @Override + public int hashCode() { + return Objects.hash(getFromDate(), getToDate()); + } + } +} 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 new file mode 100644 index 0000000000..77bf68d9a3 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldBinderCrossValidationTest.java @@ -0,0 +1,40 @@ +package com.vaadin.tests.components.datefield; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.DateFieldElement; +import com.vaadin.testbench.elements.LabelElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +import static org.junit.Assert.assertEquals; + +public class DateFieldBinderCrossValidationTest extends SingleBrowserTest { + + private final static String EXPECTED_ERROR = "from field is Date is out of allowed range. To field is Date is out of allowed range"; + private final static String EXPECTED_NULL_ERROR = "from field is null. To field is null"; + + @Test + public void makeBothFieldInvalidThenValid() { + openTestURL(); + + DateFieldElement fromField = $(DateFieldElement.class).id("from-field"); + WebElement fromFieldText = fromField.findElement(By.tagName("input")); + DateFieldElement toField = $(DateFieldElement.class).id("to-field"); + WebElement toFieldText = toField.findElement(By.tagName("input")); + LabelElement label = $(LabelElement.class).id("status"); + + fromFieldText.sendKeys("2019/01/01", Keys.ENTER); + toFieldText.sendKeys("2018/02/02", Keys.ENTER); + + assertEquals("Error message should contain the information", + EXPECTED_ERROR, label.getText()); + + fromFieldText.clear(); + fromFieldText.sendKeys("2018/01/01", Keys.ENTER); + assertEquals("Error message should be null", EXPECTED_NULL_ERROR, + label.getText()); + } +} |