]> source.dussan.org Git - vaadin-framework.git/blob
38ee0928a0097829c46656f8ecd75af1e6be0a4e
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2016 Vaadin Ltd.
3  *
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
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
14  * the License.
15  */
16 package com.vaadin.v7.tests.server.component.calendar;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21
22 import java.util.Date;
23 import java.util.GregorianCalendar;
24 import java.util.Locale;
25 import java.util.TimeZone;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29
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;
40
41 /**
42  * Basic API tests for the calendar
43  */
44 public class CalendarBasicsTest {
45
46     @Test
47     public void testEmptyConstructorInitialization() {
48
49         Calendar calendar = new Calendar();
50
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);
54
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));
62
63         // Calendar should have undefined size
64         assertTrue(calendar.getWidth() < 0);
65         assertTrue(calendar.getHeight() < 0);
66     }
67
68     @Test
69     public void testConstructorWithCaption() {
70         final String caption = "My Calendar Caption";
71         Calendar calendar = new Calendar(caption);
72         assertEquals(caption, calendar.getCaption());
73     }
74
75     @Test
76     public void testConstructorWithCustomEventProvider() {
77         BasicEventProvider myProvider = new BasicEventProvider();
78         Calendar calendar = new Calendar(myProvider);
79         assertEquals(myProvider, calendar.getEventProvider());
80     }
81
82     @Test
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());
89     }
90
91     @Test
92     public void testDefaultStartAndEndDates() {
93         Calendar calendar = new Calendar();
94
95         // If no start and end date is set the calendar will display the current
96         // week
97         java.util.Calendar c = new GregorianCalendar();
98         java.util.Calendar c2 = new GregorianCalendar();
99
100         c2.setTime(calendar.getStartDate());
101         assertEquals(c.getFirstDayOfWeek(),
102                 c2.get(java.util.Calendar.DAY_OF_WEEK));
103         c2.setTime(calendar.getEndDate());
104
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));
108     }
109
110     @Test
111     public void testCustomStartAndEndDates() {
112         Calendar calendar = new Calendar();
113         java.util.Calendar c = new GregorianCalendar();
114
115         Date start = c.getTime();
116         c.add(java.util.Calendar.DATE, 3);
117         Date end = c.getTime();
118
119         calendar.setStartDate(start);
120         calendar.setEndDate(end);
121
122         assertEquals(start.getTime(), calendar.getStartDate().getTime());
123         assertEquals(end.getTime(), calendar.getEndDate().getTime());
124     }
125
126     @Test
127     public void testCustomLocale() {
128         Calendar calendar = new Calendar();
129         calendar.setLocale(Locale.CANADA_FRENCH);
130
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());
136     }
137
138     @Test
139     public void testTimeFormat() {
140         Calendar calendar = new Calendar();
141
142         // The default timeformat depends on the current locale
143         calendar.setLocale(Locale.ENGLISH);
144         assertEquals(TimeFormat.Format12H, calendar.getTimeFormat());
145
146         calendar.setLocale(Locale.ITALIAN);
147         assertEquals(TimeFormat.Format24H, calendar.getTimeFormat());
148
149         // Setting a specific time format overrides the locale
150         calendar.setTimeFormat(TimeFormat.Format12H);
151         assertEquals(TimeFormat.Format12H, calendar.getTimeFormat());
152     }
153
154     @Test
155     public void testTimeZone() {
156         Calendar calendar = new Calendar();
157         calendar.setLocale(Locale.CANADA_FRENCH);
158
159         // By default the calendars timezone is returned
160         assertEquals(calendar.getInternalCalendar().getTimeZone(),
161                 calendar.getTimeZone());
162
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());
167     }
168
169     @Test
170     public void testVisibleDaysOfWeek() {
171         Calendar calendar = new Calendar();
172
173         // The defaults are the whole week
174         assertEquals(1, calendar.getFirstVisibleDayOfWeek());
175         assertEquals(7, calendar.getLastVisibleDayOfWeek());
176
177         calendar.setFirstVisibleDayOfWeek(0); // Invalid input
178         assertEquals(1, calendar.getFirstVisibleDayOfWeek());
179
180         calendar.setLastVisibleDayOfWeek(0); // Invalid input
181         assertEquals(7, calendar.getLastVisibleDayOfWeek());
182
183         calendar.setFirstVisibleDayOfWeek(8); // Invalid input
184         assertEquals(1, calendar.getFirstVisibleDayOfWeek());
185
186         calendar.setLastVisibleDayOfWeek(8); // Invalid input
187         assertEquals(7, calendar.getLastVisibleDayOfWeek());
188
189         calendar.setFirstVisibleDayOfWeek(4);
190         assertEquals(4, calendar.getFirstVisibleDayOfWeek());
191
192         calendar.setLastVisibleDayOfWeek(6);
193         assertEquals(6, calendar.getLastVisibleDayOfWeek());
194
195         calendar.setFirstVisibleDayOfWeek(7); // Invalid since last day is 6
196         assertEquals(4, calendar.getFirstVisibleDayOfWeek());
197
198         calendar.setLastVisibleDayOfWeek(2); // Invalid since first day is 4
199         assertEquals(6, calendar.getLastVisibleDayOfWeek());
200     }
201
202     @Test
203     public void testVisibleHoursInDay() {
204         Calendar calendar = new Calendar();
205
206         // Defaults are the whole day
207         assertEquals(0, calendar.getFirstVisibleHourOfDay());
208         assertEquals(23, calendar.getLastVisibleHourOfDay());
209     }
210
211     @Test
212     public void isClientChangeAllowed_connectorEnabled() {
213         TestCalendar calendar = new TestCalendar(true);
214         assertTrue(
215                 "Calendar with enabled connector doesn't allow client change",
216                 calendar.isClientChangeAllowed());
217     }
218
219     // regression test to ensure old functionality is not broken
220     @Test
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());
228     }
229
230     @Test
231     public void customFirstDayOfWeek() {
232         Calendar calendar = new Calendar();
233         calendar.setLocale(Locale.GERMAN);
234         calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
235
236         // simulating consequences of markAsDirty
237         calendar.beforeClientResponse(true);
238         assertEquals(java.util.Calendar.SUNDAY,
239                 calendar.getInternalCalendar().getFirstDayOfWeek());
240     }
241
242     @Test
243     public void customFirstDayOfWeekCanSetEvenBeforeLocale() {
244         Calendar calendar = new Calendar();
245         calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
246
247         calendar.setLocale(Locale.GERMAN);
248         // simulating consequences of markAsDirty
249         calendar.beforeClientResponse(true);
250         assertEquals(java.util.Calendar.SUNDAY,
251                 calendar.getInternalCalendar().getFirstDayOfWeek());
252     }
253
254     @Test
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());
264     }
265
266     @Test(expected = IllegalArgumentException.class)
267     public void customFirstDayOfWeekValidation() {
268         Calendar calendar = new Calendar();
269         int someWrongDayOfWeek = 10;
270         calendar.setFirstDayOfWeek(someWrongDayOfWeek);
271     }
272
273     private static class TestCalendar extends Calendar {
274         TestCalendar(boolean connectorEnabled) {
275             isConnectorEnabled = connectorEnabled;
276         }
277
278         @Override
279         public boolean isConnectorEnabled() {
280             return isConnectorEnabled;
281         }
282
283         @Override
284         public boolean isClientChangeAllowed() {
285             return super.isClientChangeAllowed();
286         }
287
288         private final boolean isConnectorEnabled;
289     }
290 }