Browse Source

Fix AbstractDateField to correctly show week day names (#10188)

Fixes #9200
tags/8.2.0.alpha3
Ahmed Ashour 6 years ago
parent
commit
4148d910a5

+ 29
- 4
client/src/main/java/com/vaadin/client/DateTimeService.java View File

@@ -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;
}

+ 1
- 4
client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java View File

@@ -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>");

+ 0
- 2
uitest/src/main/java/com/vaadin/tests/components/datefield/AbstractDateFieldTest.java View File

@@ -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) {

+ 42
- 0
uitest/src/main/java/com/vaadin/tests/components/datefield/DateFieldWeekShortName.java View File

@@ -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);
}

}

+ 64
- 0
uitest/src/test/java/com/vaadin/tests/components/datefield/DateFieldWeekShortNameTest.java View File

@@ -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());
}
}

Loading…
Cancel
Save