Skip to content

Commit cb36880

Browse files
rajamahaivanov-jdk
authored andcommittedAug 5, 2021
8270116: Expand ButtonGroupLayoutTraversalTest.java to run in all LaFs, including Aqua on macOS
Reviewed-by: psadhukhan, aivanov
1 parent 3ab95d1 commit cb36880

File tree

1 file changed

+86
-70
lines changed

1 file changed

+86
-70
lines changed
 

‎test/jdk/java/awt/Focus/FocusTraversalPolicy/ButtonGroupLayoutTraversal/ButtonGroupLayoutTraversalTest.java

+86-70
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.awt.event.FocusAdapter;
3838
import java.awt.event.FocusEvent;
3939
import java.awt.event.KeyEvent;
40+
import java.util.Arrays;
4041
import javax.swing.JFrame;
4142
import javax.swing.SwingUtilities;
4243
import javax.swing.UIManager;
@@ -50,98 +51,113 @@
5051

5152
public class ButtonGroupLayoutTraversalTest {
5253

53-
static int nx = 3;
54-
static int ny = 3;
54+
private static final int NX = 3;
55+
private static final int NY = 3;
56+
57+
private static final int[] focusCnt = new int[NX * NY];
5558

56-
static int focusCnt[] = new int[nx * ny];
5759
private static JFrame window;
60+
private static Robot robot;
5861

5962
public static void main(String[] args) throws Exception {
63+
robot = new Robot();
64+
robot.setAutoDelay(100);
6065

61-
try {
62-
SwingUtilities.invokeAndWait(() -> changeLAF());
63-
SwingUtilities.invokeAndWait(() -> initLayout(nx, ny));
64-
Robot robot = new Robot();
65-
robot.setAutoDelay(100);
66-
robot.waitForIdle();
67-
robot.delay(1000);
68-
69-
for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
70-
robot.keyPress(KeyEvent.VK_RIGHT);
71-
robot.keyRelease(KeyEvent.VK_RIGHT);
72-
robot.waitForIdle();
66+
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
67+
try {
68+
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
69+
SwingUtilities.invokeAndWait(() -> initLayout(NX, NY));
70+
test();
71+
} finally {
72+
SwingUtilities.invokeAndWait(() -> {
73+
if (window != null) {
74+
window.dispose();
75+
}
76+
window = null;
77+
synchronized (focusCnt) {
78+
Arrays.fill(focusCnt, 0);
79+
}
80+
});
7381
}
82+
}
83+
}
7484

75-
for (int i = 0; i < nx * ny / 2; i++) {
76-
robot.keyPress(KeyEvent.VK_TAB);
77-
robot.keyRelease(KeyEvent.VK_TAB);
78-
robot.waitForIdle();
79-
}
8085

81-
robot.delay(200);
86+
private static void test() {
87+
robot.waitForIdle();
88+
robot.delay(1000);
89+
90+
for (int i = 0; i < NX * NY - NX * NY / 2 - 1; i++) {
91+
robot.keyPress(KeyEvent.VK_RIGHT);
92+
robot.keyRelease(KeyEvent.VK_RIGHT);
93+
robot.waitForIdle();
94+
}
95+
96+
for (int i = 0; i < NX * NY / 2; i++) {
97+
robot.keyPress(KeyEvent.VK_TAB);
98+
robot.keyRelease(KeyEvent.VK_TAB);
99+
robot.waitForIdle();
100+
}
82101

83-
for (int i = 0; i < nx * ny; i++) {
102+
robot.delay(200);
103+
104+
synchronized (focusCnt) {
105+
for (int i = 0; i < NX * NY; i++) {
84106
if (focusCnt[i] < 1) {
85107
throw new RuntimeException("Component " + i
86-
+ " is not reachable in the forward focus cycle");
108+
+ " is not reachable in the forward focus cycle");
87109
} else if (focusCnt[i] > 1) {
88110
throw new RuntimeException("Component " + i
89-
+ " got focus more than once in the forward focus cycle");
111+
+ " got focus more than once in the forward focus cycle");
90112
}
91113
}
114+
}
92115

93-
for (int i = 0; i < nx * ny / 2; i++) {
94-
robot.keyPress(KeyEvent.VK_SHIFT);
95-
robot.keyPress(KeyEvent.VK_TAB);
96-
robot.keyRelease(KeyEvent.VK_TAB);
97-
robot.keyRelease(KeyEvent.VK_SHIFT);
98-
robot.waitForIdle();
99-
}
100-
101-
for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
102-
robot.keyPress(KeyEvent.VK_LEFT);
103-
robot.keyRelease(KeyEvent.VK_LEFT);
104-
robot.waitForIdle();
105-
}
106-
116+
for (int i = 0; i < NX * NY / 2; i++) {
107117
robot.keyPress(KeyEvent.VK_SHIFT);
108118
robot.keyPress(KeyEvent.VK_TAB);
109119
robot.keyRelease(KeyEvent.VK_TAB);
110120
robot.keyRelease(KeyEvent.VK_SHIFT);
111121
robot.waitForIdle();
122+
}
112123

113-
robot.delay(200);
124+
for (int i = 0; i < NX * NY - NX * NY / 2 - 1; i++) {
125+
robot.keyPress(KeyEvent.VK_LEFT);
126+
robot.keyRelease(KeyEvent.VK_LEFT);
127+
robot.waitForIdle();
128+
}
114129

115-
for (int i = 0; i < nx * ny; i++) {
130+
robot.keyPress(KeyEvent.VK_SHIFT);
131+
robot.keyPress(KeyEvent.VK_TAB);
132+
robot.keyRelease(KeyEvent.VK_TAB);
133+
robot.keyRelease(KeyEvent.VK_SHIFT);
134+
robot.waitForIdle();
135+
136+
robot.delay(200);
137+
138+
synchronized (focusCnt) {
139+
for (int i = 0; i < NX * NY; i++) {
116140
if (focusCnt[i] < 2) {
117141
throw new RuntimeException("Component " + i
118-
+ " is not reachable in the backward focus cycle");
142+
+ " is not reachable in the backward focus cycle");
119143
} else if (focusCnt[i] > 2) {
120144
throw new RuntimeException("Component " + i
121-
+ " got focus more than once in the backward focus cycle");
145+
+ " got focus more than once in the backward focus cycle");
122146
}
123147
}
124-
} finally {
125-
SwingUtilities.invokeAndWait(() -> {
126-
if (window != null) {
127-
window.dispose();
128-
}
129-
});
130148
}
149+
131150
}
132151

133-
private static void changeLAF() {
134-
String currentLAF = UIManager.getLookAndFeel().toString();
135-
currentLAF = currentLAF.toLowerCase();
136-
if (currentLAF.contains("aqua") || currentLAF.contains("nimbus")) {
137-
try {
138-
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
139-
} catch (ClassNotFoundException
140-
| IllegalAccessException
141-
| InstantiationException
142-
| UnsupportedLookAndFeelException ex) {
143-
ex.printStackTrace();
144-
}
152+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
153+
try {
154+
UIManager.setLookAndFeel(laf.getClassName());
155+
System.out.println(laf.getName());
156+
} catch (UnsupportedLookAndFeelException ignored){
157+
System.out.println("Unsupported LookAndFeel: " + laf.getClassName());
158+
} catch (ClassNotFoundException | InstantiationException |
159+
IllegalAccessException e) {
160+
throw new RuntimeException(e);
145161
}
146162
}
147163

@@ -168,16 +184,16 @@ public static void initLayout(int nx, int ny) {
168184
comp.addFocusListener(new FocusAdapter() {
169185
@Override
170186
public void focusGained(FocusEvent e) {
171-
focusCnt[fi]++;
172-
if (focusCnt[fi] == 1) {
173-
((JComponent) e.getSource())
174-
.setBackground(Color.yellow);
175-
} else if (focusCnt[fi] == 2) {
176-
((JComponent) e.getSource())
177-
.setBackground(Color.green);
178-
} else {
179-
((JComponent) e.getSource())
180-
.setBackground(Color.red);
187+
synchronized (focusCnt) {
188+
focusCnt[fi]++;
189+
JComponent btn = (JComponent) e.getSource();
190+
if (focusCnt[fi] == 1) {
191+
btn.setBackground(Color.yellow);
192+
} else if (focusCnt[fi] == 2) {
193+
btn.setBackground(Color.green);
194+
} else {
195+
btn.setBackground(Color.red);
196+
}
181197
}
182198
}
183199
});

0 commit comments

Comments
 (0)
Please sign in to comment.