From 70ba487b622ab13aa39f45cb53507065c180dc56 Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Fri, 20 Oct 2017 08:54:49 +0200 Subject: AbstractDateField: set value after being invalid (#10187) Fixes #9763 Fixes #9978 --- .../main/java/com/vaadin/ui/AbstractDateField.java | 39 ++++++----- .../datefield/DateFieldSetAfterInvalid.java | 36 ++++++++++ .../components/datefield/DisabledParentLayout.java | 11 +--- .../datefield/DateFieldSetAfterInvalidTest.java | 76 ++++++++++++++++++++++ 4 files changed, 133 insertions(+), 29 deletions(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldSetAfterInvalid.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldSetAfterInvalidTest.java diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index bfe3119f26..67067bd070 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -48,6 +48,7 @@ import com.vaadin.event.FieldEvents.BlurNotifier; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.event.FieldEvents.FocusNotifier; +import com.vaadin.server.ErrorMessage; import com.vaadin.server.UserError; import com.vaadin.shared.Registration; import com.vaadin.shared.ui.datefield.AbstractDateFieldServerRpc; @@ -95,10 +96,6 @@ public abstract class AbstractDateField parsedDate = handleUnparsableDateString( dateString); - parsedDate.ifOk(v -> { - getState().parsable = true; - currentParseErrorMessage = null; - setValue(v, true); - }); + parsedDate.ifOk(v -> setValue(v, true)); if (parsedDate.isError()) { dateString = null; - getState().parsable = false; currentParseErrorMessage = parsedDate .getMessage().orElse("Parsing error"); - setComponentError( - new UserError(getParseErrorMessage())); - setValue(null, true); + + if (!isDifferentValue(null)) { + doSetValue(null); + } else { + setValue(null, true); + } } } else { - getState().parsable = true; - currentParseErrorMessage = null; setValue(newDate, true); } } @@ -526,6 +517,7 @@ public abstract class AbstractDateField dateField.setValue(LocalDate.now())); + addComponent(nowButton); + + Button clearButton = new Button("Clear"); + clearButton.addClickListener(e -> dateField.clear()); + addComponent(clearButton); + } + +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/datefield/DisabledParentLayout.java b/uitest/src/main/java/com/vaadin/tests/components/datefield/DisabledParentLayout.java index 847a25174a..914c7b490f 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/datefield/DisabledParentLayout.java +++ b/uitest/src/main/java/com/vaadin/tests/components/datefield/DisabledParentLayout.java @@ -30,22 +30,15 @@ public class DisabledParentLayout extends AbstractReindeerTestUI { @Override protected void setup(VaadinRequest request) { - VerticalLayout content = new VerticalLayout(); - - content.setSpacing(true); - content.setMargin(true); final VerticalLayout pane = new VerticalLayout(); AbstractLocalDateField dateField = new TestDateField(); pane.addComponent(dateField); - content.addComponent(pane); - Button button = new Button("Test"); button.addClickListener(event -> pane.setEnabled(!pane.isEnabled())); - content.addComponent(button); - addComponent(content); + addComponents(pane, button); } @Override @@ -55,7 +48,7 @@ public class DisabledParentLayout extends AbstractReindeerTestUI { @Override protected Integer getTicketNumber() { - return 13124; + return 4773; } } diff --git a/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldSetAfterInvalidTest.java b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldSetAfterInvalidTest.java new file mode 100644 index 0000000000..5e25c55cc6 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldSetAfterInvalidTest.java @@ -0,0 +1,76 @@ +package com.vaadin.tests.components.datefield; + +import static java.time.temporal.ChronoUnit.DAYS; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.time.LocalDate; + +import org.junit.Test; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.DateFieldElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class DateFieldSetAfterInvalidTest extends MultiBrowserTest { + + private static final org.openqa.selenium.By ERROR_INDICATOR_BY = By + .className("v-errorindicator"); + + private static String INVALID_TEXT = "abc"; + + @Test + public void setValueAfterBeingInvalid() { + openTestURL(); + + DateFieldElement dateField = $(DateFieldElement.class).first(); + dateField.setDate(LocalDate.now().minus(5, DAYS)); + assertNoErrorIndicator(); + + dateField.setValue(dateField.getValue() + INVALID_TEXT); + assertErrorIndicator(); + + $(ButtonElement.class).caption("Today").first().click(); + + assertFalse(dateField.getValue().endsWith(INVALID_TEXT)); + assertNoErrorIndicator(); + } + + @Test + public void clearAfterBeingInvalid() { + openTestURL(); + + DateFieldElement dateField = $(DateFieldElement.class).first(); + dateField.setDate(LocalDate.now().minus(5, DAYS)); + assertNoErrorIndicator(); + + dateField.setValue(dateField.getValue() + INVALID_TEXT); + assertErrorIndicator(); + + $(ButtonElement.class).caption("Clear").first().click(); + + assertTrue(dateField.getValue().isEmpty()); + assertNoErrorIndicator(); + + dateField.setValue(INVALID_TEXT); + assertErrorIndicator(); + } + + @Test + public void invalidTypedText() { + openTestURL(); + + DateFieldElement dateField = $(DateFieldElement.class).first(); + dateField.setValue(INVALID_TEXT); + assertErrorIndicator(); + } + + private void assertErrorIndicator() { + assertElementPresent(ERROR_INDICATOR_BY); + } + + private void assertNoErrorIndicator() { + assertElementNotPresent(ERROR_INDICATOR_BY); + } +} -- cgit v1.2.3