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

Reviewed-by: psadhukhan, aivanov
  • Loading branch information
rajamah authored and aivanov-jdk committed Aug 5, 2021
1 parent 3ab95d1 commit cb36880
Showing 1 changed file with 86 additions and 70 deletions.
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

3 comments on commit cb36880

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@TheRealMDoerr
Copy link
Contributor

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on cb36880 Oct 21, 2021

Choose a reason for hiding this comment

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

@TheRealMDoerr @TheRealMDoerr the backport was successfully created on the branch TheRealMDoerr-backport-cb368802 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

this pull request contains a backport of commit cb368802 from the openjdk/jdk repository.

The commit being backported was authored by Rajat Mahajan on 5 Aug 2021 and was reviewed by Prasanta Sadhukhan and Alexey Ivanov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev TheRealMDoerr-backport-cb368802:TheRealMDoerr-backport-cb368802
$ git checkout TheRealMDoerr-backport-cb368802
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev TheRealMDoerr-backport-cb368802

Please sign in to comment.