-
Notifications
You must be signed in to change notification settings - Fork 96
/
Bug370605.java
350 lines (274 loc) · 10.8 KB
/
Bug370605.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/****************************************************************************
* Copyright (c) 2012 Scott Klein
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Scott Klein <scott.klein@goldenhour.com> - initial API and implementation
*****************************************************************************/
package org.eclipse.nebula.widgets.cdatetime;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.TimeZone;
import org.eclipse.nebula.cwt.test.AbstractVTestCase;
import org.eclipse.swt.SWT;
public class Bug370605 extends AbstractVTestCase {
private static final String FALL_EPOCH = "11/04/2012 00:00.00 PDT";
private static Date now = new Date();
private static final String PATTERN_TIMESTAMP = "MM/dd/yyyy HH:mm.ss z";
private static final String SPRING_EPOCH = "03/11/2012 01:00.00 PST";
private static final TimeZone TIMEZONE_DST = TimeZone
.getTimeZone("US/Pacific");
private static final TimeZone TIMEZONE_INITIAL = TimeZone
.getTimeZone("Pacific/Kiritimati");
private static Collection<TimeZone> timezones = new ArrayList<TimeZone>();
private static final String[] TIMEZONES = { "UTC", "MIT", "HST", "AST",
"PST", "MST", "CST", "EST", "PRT", "AGT", "BET", "CET", "EET",
"NET", "IST", "VST", "JST", "NST" };
private CdtTester tester;
/**
* Using traverse arrow keys, move through each field of the control
*
* Stop traversing if we have cycled back to the starting field, or we reach
* the time zone field
*
* @return true, if the time zone field has been found. false otherwise.
*/
private boolean moveToTimeZoneField(int calendarFieldSelection) {
CDateTime cdt = tester.getCDateTime();
int initialField = cdt.getCalendarField();
int currentField = -1;
do {
keyPress(SWT.ARROW_RIGHT);
keyPress('\r');
currentField = cdt.getCalendarField();
} while (currentField != initialField
&& currentField != calendarFieldSelection);
return currentField != Calendar.ZONE_OFFSET;
}
public void setUp() throws Exception {
timezones.clear();
for (final String timezone : TIMEZONES) {
timezones.add(TimeZone.getTimeZone(timezone));
}
tester = new CdtTester(getShell(), CDT.BORDER | CDT.TAB_FIELDS
| CDT.CLOCK_24_HOUR | CDT.DATE_LONG | CDT.TIME_MEDIUM);
}
/**
* This uses a single test with a known DST zone (PST).
*
* <ol>
* Test Plans
* <li>roll over the epoch time and ensure we roll over both 1 AM times
* (Daylight and Standard).</li>
* <li>roll backwards and ensure we can do that also</li>
* <li>manually type in the epoch time and ensure we get the Java default
* time</li>
* </ol>
*
* @throws Exception
*/
public void testDaylightSavingsFallRoll() throws Exception {
TimeZone.setDefault(TIMEZONE_DST);
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIMESTAMP);
Date fallEpochDate = sdf.parse(FALL_EPOCH);
Calendar fallEpochCal = Calendar.getInstance();
fallEpochCal.setTime(fallEpochDate);
tester.setPattern(PATTERN_TIMESTAMP);
tester.setTimeZone(TIMEZONE_DST);
tester.setSelection(fallEpochDate);
click(tester.getTextWidget());
moveToTimeZoneField(Calendar.HOUR_OF_DAY);
// VERIFY: Move from 12 AM to 1 AM
// Ignoring time zone DST during this test
keyPress(SWT.ARROW_UP);
keyPress('\r');
Date modifiedDate = tester.getCDateTime().getCalendarTime();
Calendar modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(fallEpochCal.get(Calendar.HOUR_OF_DAY) + 1,
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERFIY: Move from 1 AM to 1 AM
// Ignoring time zone DST during this test
keyPress(SWT.ARROW_UP);
keyPress('\r');
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(fallEpochCal.get(Calendar.HOUR_OF_DAY) + 1,
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERIFY: Move from 1 AM to 2 AM
keyPress(SWT.ARROW_UP);
keyPress('\r');
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(fallEpochCal.get(Calendar.HOUR_OF_DAY) + 2,
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERIFY: Move backwards from 2 AM to 1 AM
keyPress(SWT.ARROW_DOWN);
keyPress('\r');
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(fallEpochCal.get(Calendar.HOUR_OF_DAY) + 1,
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERIFY: Move backwards from 1 AM to 1 AM
keyPress(SWT.ARROW_DOWN);
keyPress('\r');
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(fallEpochCal.get(Calendar.HOUR_OF_DAY) + 1,
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERIFY: Move backwards from 1 AM to 12 AM
keyPress(SWT.ARROW_DOWN);
keyPress('\r');
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(fallEpochCal.get(Calendar.HOUR_OF_DAY),
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERIFY: Manually type in 1 AM
keyPress(SWT.KEYPAD_0);
keyPress(SWT.KEYPAD_1);
keyPress('\r');
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(1, modifiedCal.get(Calendar.HOUR_OF_DAY));
}
/**
* This uses a single test with a known DST zone (PST).
*
* <ol>
* Test Plans
* <li>roll over the epoch time and ensure we roll over 1 AM directly into 3
* AM</li>
* <li>roll backwards and ensure we can do that also</li>
* <li>manually type in the epoch time and ensure we get the Java default
* time</li>
* </ol>
*
* @throws Exception
*/
public void testDaylightSavingsSpringRoll() throws Exception {
TimeZone.setDefault(TIMEZONE_DST);
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIMESTAMP);
Date springEpochDate = sdf.parse(SPRING_EPOCH);
Calendar springEpochCal = Calendar.getInstance();
springEpochCal.setTime(springEpochDate);
tester.setPattern(PATTERN_TIMESTAMP);
tester.setTimeZone(TIMEZONE_DST);
tester.setSelection(springEpochDate);
click(tester.getTextWidget());
moveToTimeZoneField(Calendar.HOUR_OF_DAY);
// VERIFY: Spring from 1 AM to 3 AM using arrow keys
keyPress(SWT.ARROW_UP);
keyPress('\r');
Date modifiedDate = tester.getCDateTime().getCalendarTime();
Calendar modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(springEpochCal.get(Calendar.HOUR_OF_DAY) + 2,
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERIFY: Spring backwards from 3 AM to 1 AM using arrow keys
keyPress(SWT.ARROW_DOWN);
keyPress('\r');
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(springEpochCal.get(Calendar.HOUR_OF_DAY),
modifiedCal.get(Calendar.HOUR_OF_DAY));
// VERIFY: Manully enter 2 AM and verify it reverts to 3 AM
keyPress(SWT.KEYPAD_0);
keyPress(SWT.KEYPAD_2);
modifiedDate = tester.getCDateTime().getCalendarTime();
modifiedCal = Calendar.getInstance();
modifiedCal.setTime(modifiedDate);
assertEquals(3, modifiedCal.get(Calendar.HOUR_OF_DAY));
}
/**
* Using a CDateTime that is not configured for altering time zone, verify
* that the time zone field is simply not able to be traversed into.
*
* @throws Exception
*/
public void testNoTimeZoneSelection() throws Exception {
tester.setPattern(PATTERN_TIMESTAMP);
tester.setTimeZone(TIMEZONE_INITIAL);
tester.setSelection(now);
click(tester.getTextWidget());
// VERIFY: TimeZone is set to the initial time zone
final TimeZone wtzOriginal = tester.getCDateTime().getTimeZone();
assertEquals(TIMEZONE_INITIAL.getDisplayName(),
wtzOriginal.getDisplayName());
// VERIFY: Move over to time zone, but this should not be
// possible, so the utility method should return false
assertTrue(moveToTimeZoneField(Calendar.ZONE_OFFSET));
}
/**
* Using a CDateTime that is configured for altering the time zone, verify
* that our original time zone that we set to the control is still in the
* control on instantiation. Here we use a time zone that is *not* in the
* list to ensure we do no alter the programatic expectation.
*
* We then step over to the time zone field and verify that the controls
* time zone is altered in accordance with our list of time zones that we
* set into the control
*
* @throws Exception
*/
public void testTimeZoneSelection() throws Exception {
tester.setPattern(PATTERN_TIMESTAMP, timezones.toArray(new TimeZone[0]));
tester.setTimeZone(TIMEZONE_INITIAL);
tester.setSelection(now);
// VERIFY: TimeZone is set to the initial time zone
final TimeZone wtzOriginal = tester.getCDateTime().getTimeZone();
assertEquals(TIMEZONE_INITIAL.getDisplayName(),
wtzOriginal.getDisplayName());
// Move to the TimeZone field
click(tester.getTextWidget());
moveToTimeZoneField(Calendar.ZONE_OFFSET);
SimpleDateFormat sdf = new SimpleDateFormat("z");
// VERIFY: Flip forward through each available timezone and that it
// changes in the control
for (String timezone : TIMEZONES) {
keyPress(SWT.ARROW_UP);
keyPress('\r');
final TimeZone actualTimeZone = tester.getCDateTime().getTimeZone();
final TimeZone expectedTimeZone = TimeZone.getTimeZone(timezone);
// get the actual timezone format we expect, and the actual one
sdf.setTimeZone(actualTimeZone);
final String actualTimeZoneDisplay = sdf.format(new Date());
sdf.setTimeZone(expectedTimeZone);
final String expectedTimeZoneDisplay = sdf.format(new Date());
assertEquals(actualTimeZoneDisplay, expectedTimeZoneDisplay);
}
// Get back to the "1st" time zone in the list
keyPress(SWT.ARROW_UP);
keyPress('\r');
// VERIFY: Flip backward through each available timezone and that it
// changes in the control
for (int idx = TIMEZONES.length - 1; idx >= 0; idx--) {
keyPress(SWT.ARROW_DOWN);
keyPress('\r');
final TimeZone actualTimeZone = tester.getCDateTime().getTimeZone();
final TimeZone expectedTimeZone = TimeZone
.getTimeZone(TIMEZONES[idx]);
// get the actual timezone format we expect, and the actual one
sdf.setTimeZone(actualTimeZone);
final String actualTimeZoneDisplay = sdf.format(new Date());
sdf.setTimeZone(expectedTimeZone);
final String expectedTimeZoneDisplay = sdf.format(new Date());
assertEquals(actualTimeZoneDisplay, expectedTimeZoneDisplay);
}
}
}