Skip to content

Commit

Permalink
8270116: Expand ButtonGroupLayoutTraversalTest.java to run in all LaF…
Browse files Browse the repository at this point in the history
…s, including Aqua on macOS

Backport-of: cb368802815c465f8eef7aa930d6cbb6540d0fa8
  • Loading branch information
rajamah authored and aivanov-jdk committed Sep 29, 2021
1 parent 84f6cb4 commit a702838
Showing 1 changed file with 86 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}
}
});
Expand Down

1 comment on commit a702838

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.