diff options
author | Ahmed Ashour <asashour@yahoo.com> | 2017-10-16 17:21:23 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2017-10-16 18:21:23 +0300 |
commit | 4148d910a556c873477da62f1e31b0e285b8b4d9 (patch) | |
tree | 7caad300ff6431d64ca7b3ebd53ba996967013a1 | |
parent | 9b4bc1073527214437d41681741ce05f5fc60c56 (diff) | |
download | vaadin-framework-4148d910a556c873477da62f1e31b0e285b8b4d9.tar.gz vaadin-framework-4148d910a556c873477da62f1e31b0e285b8b4d9.zip |
Fix AbstractDateField to correctly show week day names (#10188)
Fixes #9200
5 files changed, 136 insertions, 10 deletions
diff --git a/client/src/main/java/com/vaadin/client/DateTimeService.java b/client/src/main/java/com/vaadin/client/DateTimeService.java index c1f6d70815..7367d686b6 100644 --- a/client/src/main/java/com/vaadin/client/DateTimeService.java +++ b/client/src/main/java/com/vaadin/client/DateTimeService.java @@ -96,6 +96,13 @@ public class DateTimeService { } } + /** + * Returns the localized short name of the specified day. + * + * @param day + * the day, {@code 0} is {@code SUNDAY} + * @return the localized short name + */ public String getShortDay(int day) { try { return LocaleService.getShortDayNames(locale)[day]; @@ -105,6 +112,11 @@ public class DateTimeService { } } + /** + * Returns the first day of the week, according to the used Locale. + * + * @return the localized first day of the week, {@code 0} is {@code SUNDAY} + */ public int getFirstDayOfWeek() { try { return LocaleService.getFirstDayOfWeek(locale); @@ -114,6 +126,12 @@ public class DateTimeService { } } + /** + * Returns whether the locale has twelve hour, or twenty four hour clock. + * + * @return {@code true} if the locale has twelve hour clock, {@code false} + * for twenty four clock + */ public boolean isTwelveHourClock() { try { return LocaleService.isTwelveHourClock(locale); @@ -145,9 +163,16 @@ public class DateTimeService { } } - public int getStartWeekDay(Date date) { - final Date dateForFirstOfThisMonth = new Date(date.getYear(), - date.getMonth(), 1); + /** + * Returns the first day of week of the specified {@code month}. + * + * @param month + * the month, not {@code null} + * @return the first day of week, + */ + public int getStartWeekDay(Date month) { + final Date dateForFirstOfThisMonth = new Date(month.getYear(), + month.getMonth(), 1); int firstDay; try { firstDay = LocaleService.getFirstDayOfWeek(locale); @@ -158,7 +183,7 @@ public class DateTimeService { } int start = dateForFirstOfThisMonth.getDay() - firstDay; if (start < 0) { - start = 6; + start += 7; } return start; } 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 7ac882f9e4..b250d1bb53 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java @@ -783,10 +783,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> // Print weekday names final int firstDay = getDateTimeService().getFirstDayOfWeek(); for (int i = 0; i < 7; i++) { - int day = i + firstDay; - if (day > 6) { - day = 0; - } + int day = (i + firstDay) % 7; if (isBelowMonth(getResolution())) { days.setHTML(headerRow, firstWeekdayColumn + i, "<strong>" + getDateTimeService().getShortDay(day) + "</strong>"); diff --git a/uitest/src/main/java/com/vaadin/tests/components/datefield/AbstractDateFieldTest.java b/uitest/src/main/java/com/vaadin/tests/components/datefield/AbstractDateFieldTest.java index 8ae2d48afd..9d7d75efb8 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/datefield/AbstractDateFieldTest.java +++ b/uitest/src/main/java/com/vaadin/tests/components/datefield/AbstractDateFieldTest.java @@ -31,7 +31,6 @@ public abstract class AbstractDateFieldTest<T extends AbstractLocalDateField> weekNumberCommand); createDateFormatSelectAction(CATEGORY_FEATURES); createSetValueAction(CATEGORY_FEATURES); - } private void createSetValueAction(String category) { @@ -68,7 +67,6 @@ public abstract class AbstractDateFieldTest<T extends AbstractLocalDateField> createSelectAction("Date format", category, options, "-", dateFormatCommand); - } private String getDatePattern(Locale locale, int dateStyle) { diff --git a/uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldWeekShortName.java b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldWeekShortName.java new file mode 100644 index 0000000000..a2148d4378 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldWeekShortName.java @@ -0,0 +1,42 @@ +package com.vaadin.tests.components.datefield; + +import java.time.LocalDate; +import java.util.Locale; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.DateField; + +public class DateFieldWeekShortName extends AbstractTestUI { + + @Override + protected String getTestDescription() { + return "DateField to correctly show week name for locales with different first week day"; + } + + @Override + protected Integer getTicketNumber() { + return 9200; + } + + @Override + protected void setup(VaadinRequest request) { + LocalDate localDate = LocalDate.of(2017, 10, 20); + + DateField ar = new DateField(); + ar.setValue(localDate); + ar.setLocale(new Locale("ar")); + addComponent(ar); + + DateField de = new DateField(); + de.setLocale(Locale.GERMAN); + de.setValue(localDate); + addComponent(de); + + DateField en = new DateField(); + en.setLocale(Locale.ENGLISH); + en.setValue(localDate); + addComponent(en); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldWeekShortNameTest.java b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldWeekShortNameTest.java new file mode 100644 index 0000000000..de30a53cd8 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldWeekShortNameTest.java @@ -0,0 +1,64 @@ +package com.vaadin.tests.components.datefield; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.DateFieldElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class DateFieldWeekShortNameTest extends MultiBrowserTest { + + @Test + public void ar() { + // Sat, Sun, Mon, Tue, Wed, Thu, Fri + String[] shortWeekDays = { "س", "ح", "ن", "ث", "ر", "خ", "ج" }; + test(0, 30, shortWeekDays); + } + + @Test + public void de() { + String[] shortWeekDays = { "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" }; + test(1, 25, shortWeekDays); + } + + @Test + public void en() { + String[] shortWeekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", + "Sat" }; + test(2, 1, shortWeekDays); + } + + /** + * @param index + * the index of the {@link DateFieldElement} to test + * @param firstWeekDay + * the day of month of the first day shown in the calendar + * @param shortWeekDays + * the names of the short week days + */ + private void test(int index, int firstWeekDay, String[] shortWeekDays) { + openTestURL(); + + DateFieldElement dateField = $(DateFieldElement.class).get(index); + dateField.openPopup(); + + WebElement weekDaysRow = getDriver().findElement( + By.className("v-datefield-calendarpanel-weekdays")); + List<WebElement> weekDays = weekDaysRow + .findElements(By.tagName("strong")); + + for (int i = 0; i < shortWeekDays.length; i++) { + assertEquals(shortWeekDays[i], weekDays.get(i + 1).getText()); + } + + WebElement firstWeekDayElement = getDriver().findElement( + By.className("v-datefield-calendarpanel-day-offmonth")); + assertEquals(String.valueOf(firstWeekDay), + firstWeekDayElement.getText()); + } +} |