2 * Copyright 2000-2016 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.tests.server.component.calendar;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
22 import java.util.Date;
23 import java.util.GregorianCalendar;
24 import java.util.Locale;
25 import java.util.TimeZone;
27 import org.junit.Assert;
28 import org.junit.Test;
30 import com.vaadin.v7.ui.Calendar;
31 import com.vaadin.v7.ui.Calendar.TimeFormat;
32 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.BackwardEvent;
33 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.DateClickEvent;
34 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.EventResize;
35 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.ForwardEvent;
36 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.MoveEvent;
37 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.WeekClick;
38 import com.vaadin.v7.ui.components.calendar.event.BasicEventProvider;
39 import com.vaadin.v7.ui.components.calendar.event.CalendarEventProvider;
42 * Basic API tests for the calendar
44 public class CalendarBasicsTest {
47 public void testEmptyConstructorInitialization() {
49 Calendar calendar = new Calendar();
51 // The calendar should have a basic event provider with no events
52 CalendarEventProvider provider = calendar.getEventProvider();
53 assertNotNull("Event provider should not be null", provider);
55 // Basic event handlers should be registered
56 assertNotNull(calendar.getHandler(BackwardEvent.EVENT_ID));
57 assertNotNull(calendar.getHandler(ForwardEvent.EVENT_ID));
58 assertNotNull(calendar.getHandler(WeekClick.EVENT_ID));
59 assertNotNull(calendar.getHandler(DateClickEvent.EVENT_ID));
60 assertNotNull(calendar.getHandler(MoveEvent.EVENT_ID));
61 assertNotNull(calendar.getHandler(EventResize.EVENT_ID));
63 // Calendar should have undefined size
64 assertTrue(calendar.getWidth() < 0);
65 assertTrue(calendar.getHeight() < 0);
69 public void testConstructorWithCaption() {
70 final String caption = "My Calendar Caption";
71 Calendar calendar = new Calendar(caption);
72 assertEquals(caption, calendar.getCaption());
76 public void testConstructorWithCustomEventProvider() {
77 BasicEventProvider myProvider = new BasicEventProvider();
78 Calendar calendar = new Calendar(myProvider);
79 assertEquals(myProvider, calendar.getEventProvider());
83 public void testConstructorWithCustomEventProviderAndCaption() {
84 BasicEventProvider myProvider = new BasicEventProvider();
85 final String caption = "My Calendar Caption";
86 Calendar calendar = new Calendar(caption, myProvider);
87 assertEquals(caption, calendar.getCaption());
88 assertEquals(myProvider, calendar.getEventProvider());
92 public void testDefaultStartAndEndDates() {
93 Calendar calendar = new Calendar();
95 // If no start and end date is set the calendar will display the current
97 java.util.Calendar c = new GregorianCalendar();
98 java.util.Calendar c2 = new GregorianCalendar();
100 c2.setTime(calendar.getStartDate());
101 assertEquals(c.getFirstDayOfWeek(),
102 c2.get(java.util.Calendar.DAY_OF_WEEK));
103 c2.setTime(calendar.getEndDate());
105 c.set(java.util.Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
106 assertEquals(c.get(java.util.Calendar.DAY_OF_WEEK),
107 c2.get(java.util.Calendar.DAY_OF_WEEK));
111 public void testCustomStartAndEndDates() {
112 Calendar calendar = new Calendar();
113 java.util.Calendar c = new GregorianCalendar();
115 Date start = c.getTime();
116 c.add(java.util.Calendar.DATE, 3);
117 Date end = c.getTime();
119 calendar.setStartDate(start);
120 calendar.setEndDate(end);
122 assertEquals(start.getTime(), calendar.getStartDate().getTime());
123 assertEquals(end.getTime(), calendar.getEndDate().getTime());
127 public void testCustomLocale() {
128 Calendar calendar = new Calendar();
129 calendar.setLocale(Locale.CANADA_FRENCH);
131 // Setting the locale should set the internal calendars locale
132 assertEquals(Locale.CANADA_FRENCH, calendar.getLocale());
133 java.util.Calendar c = new GregorianCalendar(Locale.CANADA_FRENCH);
134 assertEquals(c.getTimeZone().getRawOffset(),
135 calendar.getInternalCalendar().getTimeZone().getRawOffset());
139 public void testTimeFormat() {
140 Calendar calendar = new Calendar();
142 // The default timeformat depends on the current locale
143 calendar.setLocale(Locale.ENGLISH);
144 assertEquals(TimeFormat.Format12H, calendar.getTimeFormat());
146 calendar.setLocale(Locale.ITALIAN);
147 assertEquals(TimeFormat.Format24H, calendar.getTimeFormat());
149 // Setting a specific time format overrides the locale
150 calendar.setTimeFormat(TimeFormat.Format12H);
151 assertEquals(TimeFormat.Format12H, calendar.getTimeFormat());
155 public void testTimeZone() {
156 Calendar calendar = new Calendar();
157 calendar.setLocale(Locale.CANADA_FRENCH);
159 // By default the calendars timezone is returned
160 assertEquals(calendar.getInternalCalendar().getTimeZone(),
161 calendar.getTimeZone());
163 // One can override the default behavior by specifying a timezone
164 TimeZone customTimeZone = TimeZone.getTimeZone("Europe/Helsinki");
165 calendar.setTimeZone(customTimeZone);
166 assertEquals(customTimeZone, calendar.getTimeZone());
170 public void testVisibleDaysOfWeek() {
171 Calendar calendar = new Calendar();
173 // The defaults are the whole week
174 assertEquals(1, calendar.getFirstVisibleDayOfWeek());
175 assertEquals(7, calendar.getLastVisibleDayOfWeek());
177 calendar.setFirstVisibleDayOfWeek(0); // Invalid input
178 assertEquals(1, calendar.getFirstVisibleDayOfWeek());
180 calendar.setLastVisibleDayOfWeek(0); // Invalid input
181 assertEquals(7, calendar.getLastVisibleDayOfWeek());
183 calendar.setFirstVisibleDayOfWeek(8); // Invalid input
184 assertEquals(1, calendar.getFirstVisibleDayOfWeek());
186 calendar.setLastVisibleDayOfWeek(8); // Invalid input
187 assertEquals(7, calendar.getLastVisibleDayOfWeek());
189 calendar.setFirstVisibleDayOfWeek(4);
190 assertEquals(4, calendar.getFirstVisibleDayOfWeek());
192 calendar.setLastVisibleDayOfWeek(6);
193 assertEquals(6, calendar.getLastVisibleDayOfWeek());
195 calendar.setFirstVisibleDayOfWeek(7); // Invalid since last day is 6
196 assertEquals(4, calendar.getFirstVisibleDayOfWeek());
198 calendar.setLastVisibleDayOfWeek(2); // Invalid since first day is 4
199 assertEquals(6, calendar.getLastVisibleDayOfWeek());
203 public void testVisibleHoursInDay() {
204 Calendar calendar = new Calendar();
206 // Defaults are the whole day
207 assertEquals(0, calendar.getFirstVisibleHourOfDay());
208 assertEquals(23, calendar.getLastVisibleHourOfDay());
212 public void isClientChangeAllowed_connectorEnabled() {
213 TestCalendar calendar = new TestCalendar(true);
215 "Calendar with enabled connector doesn't allow client change",
216 calendar.isClientChangeAllowed());
219 // regression test to ensure old functionality is not broken
221 public void defaultFirstDayOfWeek() {
222 Calendar calendar = new Calendar();
223 calendar.setLocale(Locale.GERMAN);
224 // simulating consequences of markAsDirty
225 calendar.beforeClientResponse(true);
226 assertEquals(java.util.Calendar.MONDAY,
227 calendar.getInternalCalendar().getFirstDayOfWeek());
231 public void customFirstDayOfWeek() {
232 Calendar calendar = new Calendar();
233 calendar.setLocale(Locale.GERMAN);
234 calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
236 // simulating consequences of markAsDirty
237 calendar.beforeClientResponse(true);
238 assertEquals(java.util.Calendar.SUNDAY,
239 calendar.getInternalCalendar().getFirstDayOfWeek());
243 public void customFirstDayOfWeekCanSetEvenBeforeLocale() {
244 Calendar calendar = new Calendar();
245 calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
247 calendar.setLocale(Locale.GERMAN);
248 // simulating consequences of markAsDirty
249 calendar.beforeClientResponse(true);
250 assertEquals(java.util.Calendar.SUNDAY,
251 calendar.getInternalCalendar().getFirstDayOfWeek());
255 public void customFirstDayOfWeekSetNullRestoresDefault() {
256 Calendar calendar = new Calendar();
257 calendar.setLocale(Locale.GERMAN);
258 calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
259 calendar.setFirstDayOfWeek(null);
260 // simulating consequences of markAsDirty
261 calendar.beforeClientResponse(true);
262 assertEquals(java.util.Calendar.MONDAY,
263 calendar.getInternalCalendar().getFirstDayOfWeek());
266 @Test(expected = IllegalArgumentException.class)
267 public void customFirstDayOfWeekValidation() {
268 Calendar calendar = new Calendar();
269 int someWrongDayOfWeek = 10;
270 calendar.setFirstDayOfWeek(someWrongDayOfWeek);
273 private static class TestCalendar extends Calendar {
274 TestCalendar(boolean connectorEnabled) {
275 isConnectorEnabled = connectorEnabled;
279 public boolean isConnectorEnabled() {
280 return isConnectorEnabled;
284 public boolean isClientChangeAllowed() {
285 return super.isClientChangeAllowed();
288 private final boolean isConnectorEnabled;