Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8270116: Expand ButtonGroupLayoutTraversalTest.java to run in all LaFs, including Aqua on macOS #4937

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -37,6 +37,7 @@
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
@@ -50,98 +51,113 @@

public class ButtonGroupLayoutTraversalTest {

static int nx = 3;
static int ny = 3;
private static final int NX = 3;
private static final int NY = 3;

private static final int[] focusCnt = new int[NX * NY];

static int focusCnt[] = new int[nx * ny];
private static JFrame window;
private static Robot robot;

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

try {
SwingUtilities.invokeAndWait(() -> changeLAF());
SwingUtilities.invokeAndWait(() -> initLayout(nx, ny));
Robot robot = new Robot();
robot.setAutoDelay(100);
robot.waitForIdle();
robot.delay(1000);

for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
robot.waitForIdle();
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
try {
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
SwingUtilities.invokeAndWait(() -> initLayout(NX, NY));
test();
} finally {
SwingUtilities.invokeAndWait(() -> {
if (window != null) {
window.dispose();
}
window = null;
synchronized (focusCnt) {
Arrays.fill(focusCnt, 0);
}
});
}
}
}

for (int i = 0; i < nx * ny / 2; i++) {
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.waitForIdle();
}

robot.delay(200);
private static void test() {
robot.waitForIdle();
robot.delay(1000);

for (int i = 0; i < NX * NY - NX * NY / 2 - 1; i++) {
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
robot.waitForIdle();
}

for (int i = 0; i < NX * NY / 2; i++) {
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.waitForIdle();
}

for (int i = 0; i < nx * ny; i++) {
robot.delay(200);

synchronized (focusCnt) {
for (int i = 0; i < NX * NY; i++) {
if (focusCnt[i] < 1) {
throw new RuntimeException("Component " + i
+ " is not reachable in the forward focus cycle");
+ " is not reachable in the forward focus cycle");
} else if (focusCnt[i] > 1) {
throw new RuntimeException("Component " + i
+ " got focus more than once in the forward focus cycle");
+ " got focus more than once in the forward focus cycle");
}
}
}

for (int i = 0; i < nx * ny / 2; i++) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
}

for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
robot.keyPress(KeyEvent.VK_LEFT);
robot.keyRelease(KeyEvent.VK_LEFT);
robot.waitForIdle();
}

for (int i = 0; i < NX * NY / 2; i++) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
}

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

for (int i = 0; i < nx * ny; i++) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();

robot.delay(200);

synchronized (focusCnt) {
for (int i = 0; i < NX * NY; i++) {
if (focusCnt[i] < 2) {
throw new RuntimeException("Component " + i
+ " is not reachable in the backward focus cycle");
+ " is not reachable in the backward focus cycle");
} else if (focusCnt[i] > 2) {
throw new RuntimeException("Component " + i
+ " got focus more than once in the backward focus cycle");
+ " got focus more than once in the backward focus cycle");
}
}
} finally {
SwingUtilities.invokeAndWait(() -> {
if (window != null) {
window.dispose();
}
});
}

}

private static void changeLAF() {
String currentLAF = UIManager.getLookAndFeel().toString();
currentLAF = currentLAF.toLowerCase();
if (currentLAF.contains("aqua") || currentLAF.contains("nimbus")) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (ClassNotFoundException
| IllegalAccessException
| InstantiationException
| UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
try {
UIManager.setLookAndFeel(laf.getClassName());
System.out.println(laf.getName());
} catch (UnsupportedLookAndFeelException ignored){
System.out.println("Unsupported LookAndFeel: " + laf.getClassName());
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException e) {
throw new RuntimeException(e);
}
}

@@ -168,16 +184,16 @@ public static void initLayout(int nx, int ny) {
comp.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
focusCnt[fi]++;
if (focusCnt[fi] == 1) {
((JComponent) e.getSource())
.setBackground(Color.yellow);
} else if (focusCnt[fi] == 2) {
((JComponent) e.getSource())
.setBackground(Color.green);
} else {
((JComponent) e.getSource())
.setBackground(Color.red);
synchronized (focusCnt) {
focusCnt[fi]++;
JComponent btn = (JComponent) e.getSource();
if (focusCnt[fi] == 1) {
btn.setBackground(Color.yellow);
} else if (focusCnt[fi] == 2) {
btn.setBackground(Color.green);
} else {
btn.setBackground(Color.red);
}
}
}
});