Skip to content

Commit e72742e

Browse files
Srinivas Mandalikaprrace
authored andcommitted
8286172: Create an automated test for JDK-4516019
Reviewed-by: prr, achung
1 parent b9de0a7 commit e72742e

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
/*
25+
* @test
26+
* @key headful
27+
* @bug 4516019
28+
* @summary Verify that clicking on the increment/decrement buttons
29+
* of the spinner gives focus to the spinner.
30+
* @run main JSpinnerFocusTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Rectangle;
35+
import java.awt.Robot;
36+
import java.awt.event.InputEvent;
37+
38+
import javax.swing.JButton;
39+
import javax.swing.JFrame;
40+
import javax.swing.JSpinner;
41+
import javax.swing.JSpinner.DefaultEditor;
42+
import javax.swing.SwingUtilities;
43+
44+
public class JSpinnerFocusTest {
45+
46+
JFrame jFrame;
47+
JButton jButton;
48+
JSpinner jSpinner;
49+
Robot robot;
50+
51+
volatile Rectangle bounds;
52+
volatile boolean jTextFieldFocusStatus = false;
53+
54+
private void createGUI() {
55+
jFrame = new JFrame();
56+
jButton = new JButton();
57+
jSpinner = new JSpinner();
58+
59+
jFrame.setLayout(new BorderLayout());
60+
jFrame.add(jButton, BorderLayout.NORTH);
61+
jFrame.add(jSpinner, BorderLayout.CENTER);
62+
jFrame.setLocationRelativeTo(null);
63+
jFrame.setSize(300, 300);
64+
jFrame.setVisible(true);
65+
}
66+
67+
public void doTest() throws Exception {
68+
try {
69+
robot = new Robot();
70+
robot.setAutoDelay(400);
71+
72+
SwingUtilities.invokeAndWait(() -> createGUI());
73+
74+
robot.waitForIdle();
75+
runTest();
76+
77+
robot.waitForIdle();
78+
SwingUtilities.invokeAndWait(() -> {
79+
jTextFieldFocusStatus = ((DefaultEditor) jSpinner.getEditor())
80+
.getTextField().isFocusOwner();
81+
});
82+
if (!jTextFieldFocusStatus) {
83+
throw new RuntimeException(
84+
"Clicking on JSpinner buttons did not"
85+
+ " shift focus to the JSpinner");
86+
}
87+
} finally {
88+
SwingUtilities.invokeAndWait(() -> {
89+
if (jFrame != null) {
90+
jFrame.dispose();
91+
}
92+
});
93+
}
94+
}
95+
96+
private void runTest() throws Exception {
97+
SwingUtilities.invokeAndWait(() -> {
98+
bounds = new Rectangle(jSpinner.getLocationOnScreen(),
99+
jSpinner.getSize());
100+
});
101+
102+
// Move cursor to place it in the spinner editor
103+
robot.mouseMove(bounds.x + bounds.width / 2,
104+
bounds.y + bounds.height / 2);
105+
106+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
107+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
108+
109+
// Move cursor to click spinner up arrow button
110+
robot.mouseMove(bounds.x + bounds.width - 2,
111+
bounds.y + bounds.height / 4);
112+
113+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
114+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
115+
}
116+
117+
public static void main(String[] args) throws Exception {
118+
new JSpinnerFocusTest().doTest();
119+
System.out.println("Test Passed");
120+
}
121+
}

0 commit comments

Comments
 (0)