diff options
author | Ahmed Ashour <asashour@yahoo.com> | 2017-09-28 11:37:32 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-09-28 12:37:32 +0300 |
commit | c520767bf156c54a9d1a9f69f0aa78bc3b835b3f (patch) | |
tree | 208c6d8679708a6cc9e8b425d86fe10f2abeedc9 /uitest | |
parent | 131601de3693655387313e47e887f593c32fa625 (diff) | |
download | vaadin-framework-c520767bf156c54a9d1a9f69f0aa78bc3b835b3f.tar.gz vaadin-framework-c520767bf156c54a9d1a9f69f0aa78bc3b835b3f.zip |
Handle 'z' (timezone) in AbstractDateField.setDateFormat() (#8844)
Diffstat (limited to 'uitest')
-rw-r--r-- | uitest/src/main/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneId.java | 78 | ||||
-rw-r--r-- | uitest/src/test/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneIdTest.java | 204 |
2 files changed, 282 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneId.java b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneId.java new file mode 100644 index 0000000000..cbdbc23ec1 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneId.java @@ -0,0 +1,78 @@ +package com.vaadin.tests.components.datefield; + +import java.time.LocalDateTime; +import java.time.Month; +import java.time.ZoneId; +import java.util.Locale; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Stream; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.ComboBox; +import com.vaadin.ui.DateTimeField; +import com.vaadin.ui.TextField; + +public class DateTimeFieldZoneId extends AbstractTestUI { + + static final String ZONE_ID = "zoneId"; + static final String LOCALE_ID = "localeId"; + static final String PATTERN_ID = "patternId"; + + static final LocalDateTime INITIAL_DATE_TIME = LocalDateTime.of(2017, + Month.JANUARY, 1, 0, 0); + private static final String FORMAT_PATTERN = "dd MMM yyyy - hh:mm:ss a z"; + + @Override + protected String getTestDescription() { + return "DateTimeField to correctly show time zone name"; + } + + @Override + protected Integer getTicketNumber() { + return 8844; + } + + @Override + protected void setup(VaadinRequest request) { + final ComboBox<String> zoneIdComboBox = new ComboBox<>(); + zoneIdComboBox.setId(ZONE_ID); + Set<String> zoneIdSet = new TreeSet<>(ZoneId.getAvailableZoneIds()); + zoneIdComboBox.setItems(zoneIdSet); + addComponent(zoneIdComboBox); + + final ComboBox<Locale> localeIdComboBox = new ComboBox<>(); + localeIdComboBox.setId(LOCALE_ID); + Stream<Locale> localeStream = Stream.of(Locale.getAvailableLocales()) + .sorted((l1, l2) -> l1.toString().compareTo(l2.toString())); + localeIdComboBox.setItems(localeStream); + addComponent(localeIdComboBox); + + final TextField patternTextField = new TextField(); + patternTextField.setId(PATTERN_ID); + patternTextField.setValue(FORMAT_PATTERN); + addComponent(patternTextField); + + final DateTimeField dateTimeField = new DateTimeField(); + dateTimeField.setValue(INITIAL_DATE_TIME); + dateTimeField.setDateFormat(FORMAT_PATTERN); + addComponent(dateTimeField); + + zoneIdComboBox.addValueChangeListener(event -> { + String value = event.getValue(); + if (value == null) { + dateTimeField.setZoneId(null); + } else { + dateTimeField.setZoneId(ZoneId.of(value)); + } + }); + + localeIdComboBox.addValueChangeListener( + event -> dateTimeField.setLocale(event.getValue())); + + patternTextField.addValueChangeListener( + event -> dateTimeField.setDateFormat(event.getValue())); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneIdTest.java b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneIdTest.java new file mode 100644 index 0000000000..d86307868d --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateTimeFieldZoneIdTest.java @@ -0,0 +1,204 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.datefield; + +import static com.vaadin.tests.components.datefield.DateTimeFieldZoneId.INITIAL_DATE_TIME; +import static com.vaadin.tests.components.datefield.DateTimeFieldZoneId.LOCALE_ID; +import static com.vaadin.tests.components.datefield.DateTimeFieldZoneId.PATTERN_ID; +import static com.vaadin.tests.components.datefield.DateTimeFieldZoneId.ZONE_ID; +import static java.time.temporal.ChronoUnit.MONTHS; +import static org.junit.Assert.assertTrue; + +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.TimeZone; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.vaadin.testbench.elements.ComboBoxElement; +import com.vaadin.testbench.elements.DateTimeFieldElement; +import com.vaadin.testbench.elements.TextFieldElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class DateTimeFieldZoneIdTest extends MultiBrowserTest { + + private static TimeZone defaultTimeZone; + private static LocalDateTime THIRTY_OF_JULY = INITIAL_DATE_TIME + .plus(6, MONTHS).withDayOfMonth(30); + + @BeforeClass + public static void init() { + defaultTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("Brazil/Acre")); + } + + @AfterClass + public static void cleanup() { + TimeZone.setDefault(defaultTimeZone); + } + + @Test + public void defaultDisplayName() { + openTestURL(); + + DateTimeFieldElement dateField = $(DateTimeFieldElement.class).first(); + dateField.openPopup(); + + LocalDate initialDate = INITIAL_DATE_TIME.toLocalDate(); + assertEndsWith(dateField, getUTCString(initialDate)); + + dateField.setDateTime(THIRTY_OF_JULY); + + assertEndsWith(dateField, getUTCString(THIRTY_OF_JULY.toLocalDate())); + } + + @Test + public void zoneIdTokyo() { + openTestURL(); + + DateTimeFieldElement dateField = $(DateTimeFieldElement.class).first(); + + setZoneId("Asia/Tokyo"); + + dateField.openPopup(); + + assertEndsWith(dateField, "JST"); + + dateField.setDateTime(THIRTY_OF_JULY); + + assertEndsWith(dateField, "JST"); + } + + @Test + public void zoneIdBerlin() { + openTestURL(); + + DateTimeFieldElement dateField = $(DateTimeFieldElement.class).first(); + + setZoneId("Europe/Berlin"); + + dateField.openPopup(); + + assertEndsWith(dateField, "CET"); + + dateField.setDateTime(THIRTY_OF_JULY); + + assertEndsWith(dateField, "CEST"); + } + + @Test + public void defaultDisplayNameLocaleGerman() { + openTestURL(); + + setLocale("de"); + + DateTimeFieldElement dateField = $(DateTimeFieldElement.class).first(); + dateField.openPopup(); + + assertEndsWith(dateField, + getUTCString(INITIAL_DATE_TIME.toLocalDate())); + + dateField.setDateTime(THIRTY_OF_JULY); + + assertEndsWith(dateField, getUTCString(THIRTY_OF_JULY.toLocalDate())); + } + + @Test + public void zoneIdBeirutLocaleGerman() { + openTestURL(); + + DateTimeFieldElement dateField = $(DateTimeFieldElement.class).first(); + + setZoneId("Asia/Beirut"); + setLocale("de"); + + dateField.openPopup(); + + assertEndsWith(dateField, "OEZ"); + + dateField.setDateTime(THIRTY_OF_JULY); + + assertEndsWith(dateField, "OESZ"); + } + + @Test + public void zInQuotes() { + openTestURL(); + + DateTimeFieldElement dateField = $(DateTimeFieldElement.class).first(); + + setZoneId("Asia/Tokyo"); + + TextFieldElement patternField = $(TextFieldElement.class) + .id(PATTERN_ID); + patternField.setValue("dd MMM yyyy - hh:mm:ss a 'z' z"); + + dateField.openPopup(); + + assertEndsWith(dateField, "z JST"); + + dateField.setDateTime(THIRTY_OF_JULY); + + assertEndsWith(dateField, "z JST"); + } + + private void assertEndsWith(DateTimeFieldElement element, String suffix) { + String text = element.getValue(); + assertTrue(text + " should end with " + suffix, text.endsWith(suffix)); + } + + /** + * Returns the timezone name formatted as returned by + * {@link com.google.gwt.i18n.client.DateTimeFormat}, which supports only + * standard GMT and RFC format. + * + * The {@link ZoneId} used is the operating system default + */ + private static String getUTCString(LocalDate localDate) { + Instant instant = localDate.atStartOfDay() + .atZone(defaultTimeZone.toZoneId()).toInstant(); + Duration duration = Duration + .ofMillis(defaultTimeZone.getOffset(instant.toEpochMilli())); + + String suffix; + if (duration.toMinutes() == 0) { + suffix = ""; + } else { + long minutes = duration.toMinutes() + % Duration.ofHours(1).toMinutes(); + long hours = duration.toHours(); + suffix = (hours >= 0 ? "+" : "") + hours + + (minutes != 0 ? ":" + minutes : ""); + } + + return "UTC" + suffix; + } + + private void setZoneId(String zoneId) { + ComboBoxElement zoneIdComboBox = $(ComboBoxElement.class).id(ZONE_ID); + zoneIdComboBox.selectByText(zoneId); + } + + private void setLocale(String locale) { + ComboBoxElement zoneIdComboBox = $(ComboBoxElement.class).id(LOCALE_ID); + zoneIdComboBox.selectByText(locale); + } +} |