]> source.dussan.org Git - vaadin-framework.git/commitdiff
Assign timezone before setting initial date in DateField. (#14653)
authorSauli Tähkäpää <sauli@vaadin.com>
Sat, 13 Sep 2014 22:01:21 +0000 (01:01 +0300)
committerMarkus Koivisto <markus@vaadin.com>
Tue, 14 Oct 2014 14:58:51 +0000 (17:58 +0300)
Change-Id: I3b527084d23ee8bd49887bef3d018c1003f492e2

server/src/com/vaadin/ui/DateField.java
uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffset.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/datefield/DateFieldDayResolutionOffsetTest.java [new file with mode: 0644]

index e88d767bc9d8e43bcbeeceb81d3a39ab567088ed..030bd5f6c29b36c194ef682d2c7bb054129d9ed3 100644 (file)
@@ -815,17 +815,15 @@ public class DateField extends AbstractField<Date> 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 (file)
index 0000000..62db60b
--- /dev/null
@@ -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 (file)
index 0000000..c3b3af9
--- /dev/null
@@ -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