]> source.dussan.org Git - vaadin-framework.git/commitdiff
calendar visible hours shows invalid dates(week/day view) (#12521)
authorAnna Miroshnik <anna.miroshnik@arcadia.spb.ru>
Thu, 4 Sep 2014 09:32:35 +0000 (13:32 +0400)
committerSauli Tähkäpää <sauli@vaadin.com>
Fri, 12 Sep 2014 13:27:40 +0000 (16:27 +0300)
Fix: test fails on phantomJs.

Change-Id: I70179c249a180a795e14683e4185068d5395762b

client/src/com/vaadin/client/ui/calendar/schedule/CalendarEvent.java
client/src/com/vaadin/client/ui/calendar/schedule/DateCell.java
client/src/com/vaadin/client/ui/calendar/schedule/WeekGrid.java
shared/src/com/vaadin/shared/ui/calendar/DateConstants.java
uitest/src/com/vaadin/tests/components/calendar/CalendarVisibleHours.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/calendar/CalendarVisibleHoursTest.java [new file with mode: 0644]

index d3a5e3f16edf8a82981aded5cd20e3c4761fbe27..5630db2415d87c38b913fcc9eca9a981ddbfb3fc 100644 (file)
@@ -274,21 +274,25 @@ public class CalendarEvent {
      * @return
      */
     public long getRangeInMinutesForDay(Date targetDay) {
+        long rangeInMinutesForDay = 0;
+        // we must take into account that here can be not only 1 and 2 days, but
+        // 1, 2, 3, 4... days first and last days - special cases all another
+        // days between first and last - have range "ALL DAY"
         if (isTimeOnDifferentDays()) {
-            // Time range is on different days. Calculate the second day's
-            // range.
-            long range = (getEndTime().getTime() - getEnd().getTime())
-                    / DateConstants.MINUTEINMILLIS;
-
-            if (getEnd().compareTo(targetDay) != 0) {
-                // Calculate first day's range.
-                return getRangeInMinutes() - range;
+            if (targetDay.compareTo(getStart()) == 0) { // for first day
+                rangeInMinutesForDay = DateConstants.DAYINMINUTES
+                        - (getStartTime().getTime() - getStart().getTime())
+                        / DateConstants.MINUTEINMILLIS;
+            } else if (targetDay.compareTo(getEnd()) == 0) { // for last day
+                rangeInMinutesForDay = (getEndTime().getTime() - getEnd()
+                        .getTime()) / DateConstants.MINUTEINMILLIS;
+            } else { // for in-between days
+                rangeInMinutesForDay = DateConstants.DAYINMINUTES;
             }
-
-            return range;
-        } else {
-            return getRangeInMinutes();
+        } else { // simple case - period is in one day
+            rangeInMinutesForDay = getRangeInMinutes();
         }
+        return rangeInMinutesForDay;
     }
 
     /**
@@ -298,16 +302,18 @@ public class CalendarEvent {
      */
     @SuppressWarnings("deprecation")
     public boolean isTimeOnDifferentDays() {
-        if (getEndTime().getTime() - getStart().getTime() > DateConstants.DAYINMILLIS) {
-            return true;
-        }
-
-        if (getStart().compareTo(getEnd()) != 0) {
-            if (getEndTime().getHours() == 0 && getEndTime().getMinutes() == 0) {
-                return false;
+        boolean isSeveralDays = false;
+
+        // if difference between start and end times is more than day - of
+        // course it is not one day, but several days
+        if (getEndTime().getTime() - getStartTime().getTime() > DateConstants.DAYINMILLIS) {
+            isSeveralDays = true;
+        } else { // if difference <= day -> there can be different cases
+            if (getStart().compareTo(getEnd()) != 0
+                    && getEndTime().compareTo(getEnd()) != 0) {
+                isSeveralDays = true;
             }
-            return true;
         }
-        return false;
+        return isSeveralDays;
     }
 }
index ffa5f780719d90b67468e047de924d50112295d7..9e110b6d7aa2f19cfc2b73f493fc79081b8ae39d 100644 (file)
@@ -510,8 +510,8 @@ public class DateCell extends FocusableComplexPanel implements
     @SuppressWarnings("deprecation")
     private void updatePositionFor(DateCellDayEvent dayEvent, Date targetDay,
             CalendarEvent calendarEvent) {
-        if (canDisplay(calendarEvent)) {
 
+        if (shouldDisplay(calendarEvent)) {
             dayEvent.getElement().getStyle().clearDisplay();
 
             Date fromDt = calendarEvent.getStartTime();
@@ -522,9 +522,8 @@ public class DateCell extends FocusableComplexPanel implements
             boolean onDifferentDays = calendarEvent.isTimeOnDifferentDays();
             if (onDifferentDays) {
                 if (calendarEvent.getStart().compareTo(targetDay) != 0) {
-                    // Current day slot is for the end date. Lets fix also
-                    // the
-                    // start & end times.
+                    // Current day slot is for the end date and all in-between
+                    // days. Lets fix also the start & end times.
                     h = 0;
                     m = 0;
                 }
@@ -532,7 +531,6 @@ public class DateCell extends FocusableComplexPanel implements
 
             int startFromMinutes = (h * 60) + m;
             dayEvent.updatePosition(startFromMinutes, range);
-
         } else {
             dayEvent.getElement().getStyle().setDisplay(Display.NONE);
         }
@@ -571,17 +569,28 @@ public class DateCell extends FocusableComplexPanel implements
      * 
      * @param event
      * @return
+     * 
+     *         This method is not necessary in the long run.. Or here can be
+     *         various types of implementations..
      */
     // Date methods not deprecated in GWT
     @SuppressWarnings("deprecation")
-    private boolean canDisplay(CalendarEvent event) {
-        Date eventStart = event.getStartTime();
-        Date eventEnd = event.getEndTime();
-
-        int eventStartHours = eventStart.getHours();
-        int eventEndHours = eventEnd.getHours();
-
-        return (eventStartHours <= lastHour) && (eventEndHours >= firstHour);
+    private boolean shouldDisplay(CalendarEvent event) {
+        boolean display = true;
+        if (event.isTimeOnDifferentDays()) {
+            display = true;
+        } else { // only in case of one-day event we are able not to display
+                 // event
+                 // which is placed in unpublished parts on calendar
+            Date eventStart = event.getStartTime();
+            Date eventEnd = event.getEndTime();
+
+            int eventStartHours = eventStart.getHours();
+            int eventEndHours = eventEnd.getHours();
+
+            display = !(eventEndHours < firstHour || eventStartHours > lastHour);
+        }
+        return display;
     }
 
     @Override
index d1d99e6cf22af599df11f233a0fd9e63b8a61ea4..545ddadc52fa6b20f943817f87ffd0e491c3821e 100644 (file)
@@ -33,6 +33,7 @@ import com.google.gwt.user.client.ui.Widget;
 import com.vaadin.client.DateTimeService;
 import com.vaadin.client.Util;
 import com.vaadin.client.ui.VCalendar;
+import com.vaadin.shared.ui.calendar.DateConstants;
 
 /**
  * 
@@ -307,14 +308,23 @@ public class WeekGrid extends SimplePanel {
         int pixelLength = 0;
         int currentSlot = 0;
 
-        int firstHourInMinutes = firstHour * 60;
+        int firstHourInMinutes = firstHour * DateConstants.HOURINMINUTES;
+        int endHourInMinutes = lastHour * DateConstants.HOURINMINUTES;
 
         if (firstHourInMinutes > startFromMinutes) {
+            durationInMinutes = durationInMinutes
+                    - (firstHourInMinutes - startFromMinutes);
             startFromMinutes = 0;
         } else {
             startFromMinutes -= firstHourInMinutes;
         }
 
+        int shownHeightInMinutes = endHourInMinutes - firstHourInMinutes
+                + DateConstants.HOURINMINUTES;
+
+        durationInMinutes = Math.min(durationInMinutes, shownHeightInMinutes
+                - startFromMinutes);
+
         // calculate full slots to event
         int slotsTillEvent = startFromMinutes / slotInMinutes;
         int startOverFlowTime = slotInMinutes
index 75b3d6cf136d5739b0fb51a2fb50eb14369dd910..48f42eebbe391bbbd7f9abbde68f5cca594dddcb 100644 (file)
@@ -32,4 +32,7 @@ public class DateConstants implements Serializable {
     public static final long DAYINMILLIS = 24 * HOURINMILLIS;
     public static final long WEEKINMILLIS = 7 * DAYINMILLIS;
 
+    public static final int DAYINMINUTES = 24 * 60;
+    public static final int HOURINMINUTES = 60;
+
 }
diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarVisibleHours.java b/uitest/src/com/vaadin/tests/components/calendar/CalendarVisibleHours.java
new file mode 100644 (file)
index 0000000..65e5502
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2000-2014 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.calendar;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+
+import com.vaadin.annotations.Theme;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Calendar;
+import com.vaadin.ui.Calendar.TimeFormat;
+import com.vaadin.ui.components.calendar.CalendarComponentEvents.EventResizeHandler;
+import com.vaadin.ui.components.calendar.event.BasicEvent;
+
+/**
+ * 
+ * @since
+ * @author Vaadin Ltd
+ * 
+ *         test for defect: calendar visible hours of day invalid shows invalid
+ *         dates(week/day view) (#12521)
+ */
+@Theme("tests-calendar")
+public class CalendarVisibleHours extends AbstractTestUI {
+
+    private static final long serialVersionUID = 1L;
+    private Calendar calendar;
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server.
+     * VaadinRequest)
+     */
+    @Override
+    protected void setup(VaadinRequest request) {
+        calendar = new Calendar();
+
+        try {
+
+            BasicEvent event = new BasicEvent("EVENT NAME 1",
+                    "EVENT TOOLTIP 1",
+                    new SimpleDateFormat("yyyy-MM-dd HH:mm")
+                            .parse("2013-09-05 15:30"), new SimpleDateFormat(
+                            "yyyy-MM-dd HH:mm").parse("2013-09-07 22:20"));
+            event.setStyleName("color1");
+            calendar.addEvent(event);
+
+            event = new BasicEvent("EVENT NAME 2", "EVENT TOOLTIP 2",
+                    new SimpleDateFormat("yyyy-MM-dd HH:mm")
+                            .parse("2013-09-05 12:10"), new SimpleDateFormat(
+                            "yyyy-MM-dd HH:mm").parse("2013-09-05 13:20"));
+            event.setStyleName("color2");
+            calendar.addEvent(event);
+
+            event = new BasicEvent("EVENT NAME 3", "EVENT TOOLTIP 3",
+                    new SimpleDateFormat("yyyy-MM-dd HH:mm")
+                            .parse("2013-09-01 11:30"), new SimpleDateFormat(
+                            "yyyy-MM-dd HH:mm").parse("2013-09-29 15:20"));
+            event.setStyleName("color3");
+            calendar.addEvent(event);
+
+            event = new BasicEvent("EVENT NAME 4", "EVENT TOOLTIP 4",
+                    new SimpleDateFormat("yyyy-MM-dd HH:mm")
+                            .parse("2013-09-01 11:30"), new SimpleDateFormat(
+                            "yyyy-MM-dd HH:mm").parse("2013-09-01 15:20"));
+            event.setStyleName("color4");
+            event.setAllDay(true);
+            calendar.addEvent(event);
+        } catch (ParseException e1) { // Nothing to do
+            e1.printStackTrace();
+        }
+
+        try {
+            calendar.setStartDate(new SimpleDateFormat("yyyy-MM-dd")
+                    .parse("2013-09-01"));
+            calendar.setEndDate(new SimpleDateFormat("yyyy-MM-dd")
+                    .parse("2013-09-30"));
+        } catch (ParseException e) { // Nothing to do
+
+        }
+
+        calendar.setImmediate(true);
+
+        calendar.setFirstVisibleHourOfDay(8);
+        calendar.setLastVisibleHourOfDay(16);
+
+        calendar.setTimeFormat(TimeFormat.Format24H);
+        calendar.setHandler((EventResizeHandler) null);
+        setEnabled(true);
+
+        addComponent(calendar);
+        calendar.setSizeFull();
+        setSizeFull();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.tests.components.AbstractTestUI#getTestDescription()
+     */
+    @Override
+    protected String getTestDescription() {
+        return "Validate fix of defect 'Calendar visible hours of day invalid shows invalid dates(week/day view)'";
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber()
+     */
+    @Override
+    protected Integer getTicketNumber() {
+        return 12521;
+    }
+
+}
diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarVisibleHoursTest.java b/uitest/src/com/vaadin/tests/components/calendar/CalendarVisibleHoursTest.java
new file mode 100644 (file)
index 0000000..5306ee3
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2000-2014 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.calendar;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+/**
+ * Tests calendar via prepared screenshots calendar visible hours of day invalid
+ * shows invalid dates(week/day view) (#12521)
+ */
+public class CalendarVisibleHoursTest extends MultiBrowserTest {
+
+    @Test
+    public void testCalendar() throws InterruptedException, IOException {
+        openTestURL();
+
+        openWeekView();
+        compareScreen("weekview");
+
+        openDayView();
+
+        compareScreen("dayview");
+    }
+
+    private void openWeekView() {
+        List<WebElement> elements = getDriver().findElements(
+                By.className("v-calendar-week-number"));
+
+        for (WebElement webElement : elements) {
+            if (webElement.getText().equals("36")) {
+                webElement.click();
+                break;
+            }
+        }
+    }
+
+    private void openDayView() {
+        List<WebElement> elements = getDriver().findElements(
+                By.className("v-calendar-header-day"));
+
+        for (WebElement webElement : elements) {
+            if (webElement.getText().contains("Thursday 9/5/13")) {
+                webElement.click();
+                break;
+            }
+        }
+    }
+}