--- /dev/null
+package com.vaadin.tests.components.datefield;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.data.Binder;
+import com.vaadin.data.Result;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.DateField;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Notification;
+import com.vaadin.ui.VerticalLayout;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.time.format.FormatStyle;
+import java.util.Locale;
+
+@Widgetset("com.vaadin.DefaultWidgetSet")
+public class DateTextHandling extends AbstractTestUI {
+
+ public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.UK);
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ final VerticalLayout layout = new VerticalLayout();
+
+ DateField dateField = new DateField("Date") {
+ @Override
+ protected Result<LocalDate> handleUnparsableDateString(String dateString) {
+ if (dateString.equalsIgnoreCase("Y2K")) {
+ return Result.ok(LocalDate.of(2000, 1, 1));
+ } else {
+ return super.handleUnparsableDateString(dateString);
+ }
+ }
+
+ ;
+ };
+ dateField.setParseErrorMessage("Parse error");
+ dateField.setDateOutOfRangeMessage("Out of range");
+ layout.addComponent(dateField);
+ Label errorLabel = new Label();
+ errorLabel.setId("errorLabel");
+ layout.addComponent(errorLabel);
+
+ Binder<Void> binder = new Binder<>();
+ binder.forField(dateField).withStatusLabel(errorLabel).bind(o -> dateField.getEmptyValue(), null);
+
+ Button button = new Button("Validate!");
+ button.addClickListener(event1 -> {
+ binder.validate();
+ if (dateField.getValue() == null) {
+ Notification.show("NULL");
+ } else {
+ Notification.show(DATE_TIME_FORMATTER.format(dateField.getValue()));
+ }
+
+ });
+ layout.addComponent(button);
+
+ addComponent(layout);
+ }
+}
+++ /dev/null
-package com.vaadin.tests.data;
-
-import com.vaadin.annotations.Widgetset;
-import com.vaadin.data.Binder;
-import com.vaadin.data.Result;
-import com.vaadin.server.VaadinRequest;
-import com.vaadin.tests.components.AbstractTestUI;
-import com.vaadin.ui.Button;
-import com.vaadin.ui.DateField;
-import com.vaadin.ui.Label;
-import com.vaadin.ui.UI;
-import com.vaadin.ui.VerticalLayout;
-
-import java.time.LocalDate;
-
-/**
- * Created by elmot on 7/11/2017.
- */
-@Widgetset("com.vaadin.DefaultWidgetSet")
-public class DateValidationUI extends AbstractTestUI {
- @Override
- protected void setup(VaadinRequest request) {
- final VerticalLayout layout = new VerticalLayout();
-
- DateField dateField = new DateField("Date") {
- @Override
- protected Result<LocalDate> handleUnparsableDateString(String dateString) {
- if (dateString.equalsIgnoreCase("Y2K")) {
- return Result.ok(LocalDate.of(2000,1,1));
- } else {
- return super.handleUnparsableDateString(dateString);
- }
- };
- };
- dateField.setParseErrorMessage("Parse error");
- dateField.setDateOutOfRangeMessage("Out of range");
- layout.addComponent(dateField);
- dateField.addValueChangeListener(event -> {
- System.out.println(dateField.getValue());
- });
- Label errorLabel = new Label();
- layout.addComponent(errorLabel);
-
- Binder<Void> binder = new Binder<>();
- binder.forField(dateField).withStatusLabel(errorLabel).bind(o -> dateField.getEmptyValue(), null);
-
- Button button = new Button("Validate!");
- button.addClickListener(event1 -> {
- if (binder.validate().isOk()) {
- System.out.println("Correct");
- } else {
- System.out.println(dateField.isEmpty() + "Error!");
- }
- });
- layout.addComponent(button);
-
- addComponent(layout);
- }
-}
--- /dev/null
+package com.vaadin.tests.components.datefield;
+
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.DateFieldElement;
+import com.vaadin.testbench.elements.LabelElement;
+import com.vaadin.testbench.elements.NotificationElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.Keys;
+import org.openqa.selenium.WebElement;
+
+import static org.junit.Assert.assertEquals;
+
+public class DateTextHandlingTest extends SingleBrowserTest {
+ @Test
+ public void testSpecialValue() throws InterruptedException {
+ openTestURL();
+ DateFieldElement dateFieldElement = $(DateFieldElement.class).first();
+ ButtonElement validate = $(ButtonElement.class).first();
+ LabelElement validateResult = $(LabelElement.class).first();
+ WebElement dateTextbox = dateFieldElement
+ .findElement(com.vaadin.testbench.By.className("v-textfield"));
+
+ dateTextbox.sendKeys("Y2K",Keys.TAB);
+ validate.click();
+ assertNotification("Y2K Sould be converted to 1-JAN-2000", "01-Jan-2000");
+
+ dateTextbox.clear();
+ validate.click();
+ assertNotification("Null for empty string","NULL");
+ }
+
+ protected void assertNotification(String message, String expected) {
+ NotificationElement notification = $(NotificationElement.class).first();
+ assertEquals(message, expected, notification.getCaption());
+ notification.close();
+ }
+
+}