From: Sauli Tähkäpää Date: Sat, 13 Sep 2014 22:01:21 +0000 (+0300) Subject: Assign timezone before setting initial date in DateField. (#14653) X-Git-Tag: 7.3.3~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b6e66dd76ec744b5e779905060eeaa29783352d2;p=vaadin-framework.git Assign timezone before setting initial date in DateField. (#14653) Change-Id: I3b527084d23ee8bd49887bef3d018c1003f492e2 --- diff --git a/server/src/com/vaadin/ui/DateField.java b/server/src/com/vaadin/ui/DateField.java index e88d767bc9..030bd5f6c2 100644 --- a/server/src/com/vaadin/ui/DateField.java +++ b/server/src/com/vaadin/ui/DateField.java @@ -815,17 +815,15 @@ public class DateField extends AbstractField implements // Clone the instance final Calendar newCal = (Calendar) calendar.clone(); - // Assigns the current time tom calendar. - final Date currentDate = getValue(); - if (currentDate != null) { - newCal.setTime(currentDate); - } - final TimeZone currentTimeZone = getTimeZone(); if (currentTimeZone != null) { newCal.setTimeZone(currentTimeZone); } + final Date currentDate = getValue(); + if (currentDate != null) { + newCal.setTime(currentDate); + } return newCal; } diff --git a/uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffset.java b/uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffset.java new file mode 100644 index 0000000000..62db60be76 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffset.java @@ -0,0 +1,68 @@ +package com.vaadin.tests.components.datefield; + +import com.vaadin.data.Property; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.ui.datefield.Resolution; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.DateField; +import com.vaadin.ui.Label; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +public class DateFieldDayResolutionOffset extends AbstractTestUI { + + private final String initialDateString = "09/01/2014 00:00:00"; + + @Override + protected void setup(VaadinRequest request) { + final Label dateValue = new Label(initialDateString); + dateValue.setId("dateValue"); + + final TimeZone timezone = TimeZone.getTimeZone("GMT"); + final SimpleDateFormat dateformat = getDateFormat(timezone); + final DateField dateField = getDateField(timezone, dateformat); + + addComponent(dateValue); + addComponent(dateField); + + dateField.addValueChangeListener( new Property.ValueChangeListener(){ + @Override + public void valueChange(Property.ValueChangeEvent event) { + dateValue.setValue(dateformat.format(dateField.getValue())); + } + }); + } + + private DateField getDateField(TimeZone timezone, SimpleDateFormat dateformat) { + final DateField dateField = new DateField(); + try { + Date initialDate = dateformat.parse(initialDateString); + dateField.setResolution(Resolution.DAY); + dateField.setTimeZone(timezone); + dateField.setValue(initialDate); + } catch (ParseException e) { + e.printStackTrace(); + } + return dateField; + } + + private SimpleDateFormat getDateFormat(TimeZone timezone) { + final SimpleDateFormat dateformat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); + dateformat.setTimeZone(timezone); + return dateformat; + } + + @Override + protected String getTestDescription() { + return "The time should stay at 00:00:00 when selecting dates with Resolution.DAY selected."; + } + + @Override + protected Integer getTicketNumber() { + return 14653; + } +} diff --git a/uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffsetTest.java b/uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffsetTest.java new file mode 100644 index 0000000000..c3b3af9aa4 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffsetTest.java @@ -0,0 +1,43 @@ +package com.vaadin.tests.components.datefield; + +import com.vaadin.testbench.elements.DateFieldElement; +import com.vaadin.testbench.elements.LabelElement; +import com.vaadin.tests.tb3.AbstractTB3Test; +import com.vaadin.tests.tb3.MultiBrowserTest; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class DateFieldDayResolutionOffsetTest extends MultiBrowserTest { + + @Test + public void dateValueDoesNotHaveOffset() throws InterruptedException { + openTestURL(); + + openDatePicker(); + select2ndOfSeptember(); + + LabelElement dateValue = $(LabelElement.class).id("dateValue"); + assertThat(dateValue.getText(), is("09/02/2014 00:00:00")); + } + + private void select2ndOfSeptember() { + for(WebElement e : findElements(By.className("v-datefield-calendarpanel-day"))) { + if(e.getText().equals("2")) { + e.click(); + break; + } + } + } + + private void openDatePicker() { + DateFieldElement dateField = $(DateFieldElement.class).first(); + + dateField.findElement(By.tagName("button")) + .click(); + } + +} \ No newline at end of file