Skip to content

Commit d2f257f

Browse files
author
duke
committedFeb 11, 2022
Automatic merge of jdk:master into master
2 parents 80bc15c + 0786ddb commit d2f257f

File tree

1 file changed

+282
-0
lines changed

1 file changed

+282
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
/*
2+
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.ComponentOrientation;
25+
import java.awt.Point;
26+
import java.awt.Robot;
27+
import java.awt.event.InputEvent;
28+
import java.awt.event.KeyEvent;
29+
import java.util.Arrays;
30+
import java.util.Calendar;
31+
import java.util.Date;
32+
import java.util.List;
33+
import java.util.stream.Collectors;
34+
import javax.swing.JFrame;
35+
import javax.swing.JPanel;
36+
import javax.swing.JSpinner;
37+
import javax.swing.SpinnerDateModel;
38+
import javax.swing.SwingUtilities;
39+
import javax.swing.UIManager;
40+
41+
import static javax.swing.UIManager.getInstalledLookAndFeels;
42+
43+
/*
44+
* @test
45+
* @key headful
46+
* @bug 4670051
47+
* @summary Checks whether JSpinner with a SpinnerDateModel
48+
* spins the field where cursor is located.
49+
* @run main DateFieldUnderCursorTest
50+
*/
51+
public class DateFieldUnderCursorTest {
52+
53+
private static final Calendar expected = Calendar.getInstance();
54+
private static final Calendar actual = Calendar.getInstance();
55+
private static Robot robot;
56+
private static JSpinner spinner;
57+
private static Date initValue;
58+
private static Date upValue;
59+
private static Date downValue;
60+
private static JFrame frame;
61+
private static boolean passed = true;
62+
private static volatile Point spinnerUpButtonCenter;
63+
private static volatile Point spinnerDownButtonCenter;
64+
private static volatile Date spinnerValue;
65+
66+
public static void main(String[] s) throws Exception {
67+
runTest();
68+
}
69+
70+
public static void runTest() throws Exception {
71+
robot = new Robot();
72+
robot.setAutoWaitForIdle(true);
73+
robot.setAutoDelay(100);
74+
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
75+
.map(UIManager.LookAndFeelInfo::getClassName)
76+
.collect(Collectors.toList());
77+
for (final String laf : lafs) {
78+
try {
79+
SwingUtilities.invokeAndWait(() -> {
80+
setLookAndFeel(laf);
81+
createUI();
82+
});
83+
SwingUtilities.invokeAndWait(() -> {
84+
Point loc = spinner.getLocationOnScreen();
85+
int editorWidth = spinner.getEditor().getWidth();
86+
int buttonWidth = spinner.getWidth() - editorWidth;
87+
int quarterHeight = spinner.getHeight() / 4;
88+
89+
spinnerUpButtonCenter = new Point(loc.x + editorWidth
90+
+ (buttonWidth / 2),
91+
loc.y + quarterHeight);
92+
spinnerDownButtonCenter = new Point(spinnerUpButtonCenter.x,
93+
loc.y + (3 * quarterHeight));
94+
});
95+
96+
// Cursor at Day field.
97+
// Increment Day
98+
initValue = getSpinnerValue();
99+
mousePressOnUpButton();
100+
upValue = getSpinnerValue();
101+
verifyDayIncrement();
102+
// Decrement Day
103+
initValue = getSpinnerValue();
104+
mousePressOnDownButton();
105+
downValue = getSpinnerValue();
106+
verifyDayDecrement();
107+
108+
// Cursor at Month Field
109+
pressRightArrowKey();
110+
// Increment Month
111+
initValue = getSpinnerValue();
112+
mousePressOnUpButton();
113+
upValue = getSpinnerValue();
114+
verifyMonthIncrement();
115+
// Decrement Month
116+
initValue = getSpinnerValue();
117+
mousePressOnDownButton();
118+
downValue = getSpinnerValue();
119+
verifyMonthDecrement();
120+
121+
// Cursor at Year Field
122+
pressRightArrowKey();
123+
// Increment Year
124+
initValue = getSpinnerValue();
125+
mousePressOnUpButton();
126+
upValue = getSpinnerValue();
127+
verifyYearIncrement();
128+
// Decrement Year
129+
initValue = getSpinnerValue();
130+
mousePressOnDownButton();
131+
downValue = getSpinnerValue();
132+
verifyYearDecrement();
133+
134+
if (passed) {
135+
System.out.println("Test Passed");
136+
} else {
137+
throw new RuntimeException("Test Failed as one or more cases failed");
138+
}
139+
} finally {
140+
SwingUtilities.invokeAndWait(DateFieldUnderCursorTest::disposeFrame);
141+
}
142+
}
143+
}
144+
145+
private static Date getSpinnerValue() throws Exception {
146+
SwingUtilities.invokeAndWait(() -> spinnerValue = (Date) spinner.getValue());
147+
return spinnerValue;
148+
}
149+
150+
public static void pressRightArrowKey() {
151+
robot.keyPress(KeyEvent.VK_RIGHT);
152+
robot.keyRelease(KeyEvent.VK_RIGHT);
153+
}
154+
155+
public static void mousePressOnUpButton() {
156+
robot.mouseMove(spinnerUpButtonCenter.x, spinnerUpButtonCenter.y);
157+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
158+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
159+
}
160+
161+
public static void mousePressOnDownButton() {
162+
robot.mouseMove(spinnerDownButtonCenter.x, spinnerDownButtonCenter.y);
163+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
164+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
165+
}
166+
167+
168+
public static boolean compareDates() {
169+
return (expected.get(Calendar.DATE) == actual.get(Calendar.DATE))
170+
&& (expected.get(Calendar.MONTH) == actual.get(Calendar.MONTH))
171+
&& (expected.get(Calendar.YEAR) == actual.get(Calendar.YEAR));
172+
}
173+
174+
private static void checkResult() {
175+
if (compareDates()) {
176+
System.out.println(" Case Passed");
177+
} else {
178+
passed = false;
179+
System.out.println(" Case Failed because the expected: " + expected.getTime()
180+
+ " and actual: " + actual.getTime() + " outputs do not match.");
181+
}
182+
}
183+
184+
private static void updateCalendarObjects(Date finalValue) {
185+
expected.setTime(initValue);
186+
actual.setTime(finalValue);
187+
}
188+
189+
/**
190+
* Verifying that JSpinner increments the date field when cursor is on date field
191+
*/
192+
private static void verifyDayIncrement() {
193+
System.out.print("verifyDateIncrement");
194+
updateCalendarObjects(upValue);
195+
expected.add(Calendar.DATE, 1);
196+
checkResult();
197+
}
198+
199+
/**
200+
* Verifying that JSpinner decrements the date field when cursor is on date field
201+
*/
202+
private static void verifyDayDecrement() {
203+
System.out.print("verifyDateDecrement");
204+
updateCalendarObjects(downValue);
205+
expected.add(Calendar.DATE, -1);
206+
checkResult();
207+
}
208+
209+
/**
210+
* Verifying that JSpinner increments the month field when cursor is on month field
211+
*/
212+
private static void verifyMonthIncrement() {
213+
System.out.print("verifyMonthIncrement");
214+
updateCalendarObjects(upValue);
215+
expected.add(Calendar.MONTH, 1);
216+
checkResult();
217+
}
218+
219+
/**
220+
* Verifying that JSpinner decrements the month field when cursor is on month field
221+
*/
222+
private static void verifyMonthDecrement() {
223+
System.out.print("verifyMonthDecrement");
224+
updateCalendarObjects(downValue);
225+
expected.add(Calendar.MONTH, -1);
226+
checkResult();
227+
}
228+
229+
/**
230+
* Verifying that, JSpinner decrements the year field when the cursor is on year field.
231+
*/
232+
private static void verifyYearDecrement() {
233+
System.out.print("verifyYearDecrement");
234+
updateCalendarObjects(downValue);
235+
expected.add(Calendar.YEAR, -1);
236+
checkResult();
237+
}
238+
239+
/**
240+
* Verifying that JSpinner increments the year field when cursor is on year field
241+
*/
242+
private static void verifyYearIncrement() {
243+
System.out.print("verifyYearIncrement");
244+
updateCalendarObjects(upValue);
245+
expected.add(Calendar.YEAR, 1);
246+
checkResult();
247+
}
248+
249+
private static void createUI() {
250+
frame = new JFrame();
251+
JPanel panel = new JPanel();
252+
spinner = new JSpinner(new SpinnerDateModel());
253+
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, " dd/MM/yy ");
254+
spinner.setEditor(editor);
255+
spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
256+
panel.add(spinner);
257+
258+
frame.add(panel);
259+
frame.setUndecorated(true);
260+
frame.pack();
261+
frame.setAlwaysOnTop(true);
262+
frame.setLocationRelativeTo(null);
263+
frame.setVisible(true);
264+
}
265+
266+
private static void setLookAndFeel(final String laf) {
267+
try {
268+
UIManager.setLookAndFeel(laf);
269+
System.out.println("LookAndFeel: " + laf);
270+
} catch (Exception e) {
271+
throw new RuntimeException(e);
272+
}
273+
}
274+
275+
private static void disposeFrame() {
276+
if (frame != null) {
277+
frame.dispose();
278+
frame = null;
279+
}
280+
}
281+
282+
}

0 commit comments

Comments
 (0)
Please sign in to comment.