diff options
author | Anna Miroshnik <anna.miroshnik@arcadia.spb.ru> | 2014-09-24 11:33:01 +0400 |
---|---|---|
committer | Sauli Tähkäpää <sauli@vaadin.com> | 2014-11-10 12:59:04 +0200 |
commit | 59e024b4a4231fa3e0c21d7adbffc5237774cb01 (patch) | |
tree | 1cb16f51ccbd2007109d17c03b04e8d946034c63 | |
parent | d9cbbe89d35d7b8110376b7bf9adce0a4e1e39c6 (diff) | |
download | vaadin-framework-59e024b4a4231fa3e0c21d7adbffc5237774cb01.tar.gz vaadin-framework-59e024b4a4231fa3e0c21d7adbffc5237774cb01.zip |
Fix: Navigation to invisible days of week in VAADIN calendar (#12243)
Some changes in BasicBackwardHandler and BasicForwardHandler.
Also test was added (CalendarBackwardForwardTest).
Changes after review.
Change-Id: Ibe0283534b784e3c18134619e1843440ca74e65c
4 files changed, 263 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/components/calendar/handler/BasicBackwardHandler.java b/server/src/com/vaadin/ui/components/calendar/handler/BasicBackwardHandler.java index f4d47f89d4..0e99b26856 100644 --- a/server/src/com/vaadin/ui/components/calendar/handler/BasicBackwardHandler.java +++ b/server/src/com/vaadin/ui/components/calendar/handler/BasicBackwardHandler.java @@ -47,6 +47,7 @@ public class BasicBackwardHandler implements BackwardHandler { // calculate amount to move back int durationInDays = (int) (((end.getTime()) - start.getTime()) / DateConstants.DAYINMILLIS); durationInDays++; + // for week view durationInDays = -7, for day view durationInDays = -1 durationInDays = -durationInDays; // set new start and end times @@ -59,6 +60,21 @@ public class BasicBackwardHandler implements BackwardHandler { javaCalendar.add(java.util.Calendar.DATE, durationInDays); Date newEnd = javaCalendar.getTime(); + if (start.equals(end)) { // day view + int firstDay = event.getComponent().getFirstVisibleDayOfWeek(); + int lastDay = event.getComponent().getLastVisibleDayOfWeek(); + int dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK); + + // we suppose that 7 >= lastDay >= firstDay >= 1 + while (!(firstDay <= dayOfWeek && dayOfWeek <= lastDay)) { + javaCalendar.add(java.util.Calendar.DATE, -1); + dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK); + } + + newStart = javaCalendar.getTime(); + newEnd = javaCalendar.getTime(); + } + setDates(event, newStart, newEnd); } diff --git a/server/src/com/vaadin/ui/components/calendar/handler/BasicForwardHandler.java b/server/src/com/vaadin/ui/components/calendar/handler/BasicForwardHandler.java index 96c3c097dc..45b44cb673 100644 --- a/server/src/com/vaadin/ui/components/calendar/handler/BasicForwardHandler.java +++ b/server/src/com/vaadin/ui/components/calendar/handler/BasicForwardHandler.java @@ -45,6 +45,7 @@ public class BasicForwardHandler implements ForwardHandler { // calculate amount to move forward int durationInDays = (int) (((end.getTime()) - start.getTime()) / DateConstants.DAYINMILLIS); + // for week view durationInDays = 7, for day view durationInDays = 1 durationInDays++; // set new start and end times @@ -57,6 +58,21 @@ public class BasicForwardHandler implements ForwardHandler { javaCalendar.add(java.util.Calendar.DATE, durationInDays); Date newEnd = javaCalendar.getTime(); + if (start.equals(end)) { // day view + int firstDay = event.getComponent().getFirstVisibleDayOfWeek(); + int lastDay = event.getComponent().getLastVisibleDayOfWeek(); + int dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK); + + // we suppose that 7 >= lastDay >= firstDay >= 1 + while (!(firstDay <= dayOfWeek && dayOfWeek <= lastDay)) { + javaCalendar.add(java.util.Calendar.DATE, 1); + dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK); + } + + newStart = javaCalendar.getTime(); + newEnd = javaCalendar.getTime(); + } + setDates(event, newStart, newEnd); } diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarBackwardForward.java b/uitest/src/com/vaadin/tests/components/calendar/CalendarBackwardForward.java new file mode 100644 index 0000000000..5a21353d7d --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/calendar/CalendarBackwardForward.java @@ -0,0 +1,132 @@ +/* + * 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 java.util.Locale; + +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; + +/** + * Test: Vaadin Calendar: Navigation to invisible days of week (#12243) + * + * @author Vaadin Ltd + */ +@Theme("tests-calendar") +public class CalendarBackwardForward 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); + + // in english locale first day of week - sunday + calendar.setLocale(Locale.ENGLISH); + // show only working days: 2 - monday, 6 - friday + calendar.setFirstVisibleDayOfWeek(2); + calendar.setLastVisibleDayOfWeek(6); + + 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 "If one uses the feature setVisibleDaysOfWeek of Calendar, the invisible days should be skipped in single-day-mode."; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber() + */ + @Override + protected Integer getTicketNumber() { + return 12243; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarBackwardForwardTest.java b/uitest/src/com/vaadin/tests/components/calendar/CalendarBackwardForwardTest.java new file mode 100644 index 0000000000..98773f475b --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/calendar/CalendarBackwardForwardTest.java @@ -0,0 +1,99 @@ +/* + * 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 static org.hamcrest.MatcherAssert.assertThat; + +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: Vaadin Calendar: Navigation to invisible days of week (#12243) + * + * @author Vaadin Ltd + */ +public class CalendarBackwardForwardTest extends MultiBrowserTest { + + @Test + public void testCalendar() throws InterruptedException, IOException { + openTestURL(); + + openWeekView(); + openDayView(); + clickCalendarNext(); + + WebElement headerDayElement = getDriver().findElement( + By.className("v-calendar-header-day")); + + assertThat("This day should be Monday 9/9/13", headerDayElement + .getText().equals("Monday 9/9/13")); + + for (int i = 0; i < 6; i++) { + clickCalendarBack(); + } + + headerDayElement = getDriver().findElement( + By.className("v-calendar-header-day")); + + assertThat("This day should be Friday 8/30/13", headerDayElement + .getText().equals("Friday 8/30/13")); + } + + 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("Friday 9/6/13")) { + webElement.click(); + break; + } + } + } + + private void clickCalendarNext() { + List<WebElement> elements = getDriver().findElements( + By.className("v-calendar-next")); + + elements.get(0).click(); + } + + private void clickCalendarBack() { + List<WebElement> elements = getDriver().findElements( + By.className("v-calendar-back")); + + elements.get(0).click(); + } + +} |