8132770: Test javax/swing/JRadioButton/FocusTraversal/FocusTraversal.java fails in MacOSX

Reviewed-by: rchamyal, alexsch
This commit is contained in:
Avik Niyogi 2015-11-17 19:29:04 +04:00 committed by Alexander Scherbatiy
parent 2e94af3f3d
commit 1e176777db

View file

@ -22,43 +22,82 @@
*/ */
/* @test /* @test
@bug 8129940 @bug 8129940 8132770
@summary JRadioButton does not honor non-standard FocusTraversalKeys @summary JRadioButton should run custom FocusTraversalKeys for all LaFs
@author Semyon Sadetsky @run main FocusTraversal
*/ */
import java.awt.BorderLayout;
import javax.swing.*; import java.awt.Component;
import java.awt.*; import java.awt.KeyboardFocusManager;
import java.awt.Robot;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.swing.ButtonGroup;
import javax.swing.FocusManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FocusTraversal { public class FocusTraversal {
private static JFrame frame; private static JFrame frame;
private static JRadioButton a; private static JRadioButton a;
private static JRadioButton b;
private static JRadioButton c;
private static JRadioButton d; private static JRadioButton d;
private static JTextField next; private static JTextField next;
private static JTextField prev; private static JTextField prev;
private static Robot robot;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
robot = new Robot();
robot.delay(2000);
UIManager.LookAndFeelInfo[] lookAndFeelArray
= UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
executeCase(lookAndFeelItem.getClassName());
}
}
private static void executeCase(String lookAndFeelString)
throws Exception {
if (tryLookAndFeel(lookAndFeelString)) {
createUI(lookAndFeelString);
robot.delay(2000);
runTestCase();
robot.delay(2000);
cleanUp();
robot.delay(2000);
}
}
private static void createUI(final String lookAndFeelString)
throws Exception {
SwingUtilities.invokeAndWait(new Runnable() { SwingUtilities.invokeAndWait(new Runnable() {
@Override @Override
public void run() { public void run() {
frame = new JFrame("FocusTraversalTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setUndecorated(true);
Set<KeyStroke> keystrokes = new HashSet<KeyStroke>(); Set<KeyStroke> keystrokes = new HashSet<KeyStroke>();
keystrokes.add(KeyStroke.getKeyStroke("TAB")); keystrokes.add(KeyStroke.getKeyStroke("TAB"));
keystrokes.add(KeyStroke.getKeyStroke("ENTER")); keystrokes.add(KeyStroke.getKeyStroke("ENTER"));
frame = new JFrame("FocusTraversalTest " + lookAndFeelString);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setUndecorated(true);
frame.setFocusTraversalKeys( frame.setFocusTraversalKeys(
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
keystrokes); keystrokes);
a = new JRadioButton("a"); a = new JRadioButton("a");
JRadioButton b = new JRadioButton("b"); b = new JRadioButton("b");
JRadioButton c = new JRadioButton("c"); c = new JRadioButton("c");
d = new JRadioButton("d"); d = new JRadioButton("d");
ButtonGroup radioButtonGroup = new ButtonGroup(); ButtonGroup radioButtonGroup = new ButtonGroup();
@ -84,61 +123,102 @@ public class FocusTraversal {
frame.add(root); frame.add(root);
frame.pack(); frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true); frame.setVisible(true);
frame.toFront();
} }
}); });
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
a.requestFocus();
} }
});
Robot robot = new Robot();
robot.waitForIdle();
robot.setAutoDelay(200);
private static void runTestCase() throws Exception {
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
focusOn(a);
if (isExcludedLookAndFeel(lookAndFeel)) {
robot.keyPress(KeyEvent.VK_ENTER); robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER);
robot.waitForIdle(); robot.waitForIdle();
isFocusOwner(b, "forward");
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
Component focusOwner =
FocusManager.getCurrentManager().getFocusOwner();
if (focusOwner != next) {
throw new RuntimeException(
"Focus component is wrong after forward key " + focusOwner);
}
}
});
robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB); robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT); robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle(); robot.waitForIdle();
isFocusOwner(a, "backward");
} else {
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.waitForIdle();
isFocusOwner(next, "forward");
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
isFocusOwner(d, "backward");
}
}
private static boolean isExcludedLookAndFeel(LookAndFeel lookAndFeel) {
return lookAndFeel.toString().toLowerCase().contains("aqua")
|| lookAndFeel.toString().toLowerCase().contains("nimbus")
|| lookAndFeel.toString().toLowerCase().contains("gtk");
}
private static void focusOn(Component component)
throws Exception {
SwingUtilities.invokeAndWait(new Runnable() { SwingUtilities.invokeAndWait(new Runnable() {
@Override @Override
public void run() { public void run() {
Component focusOwner = component.requestFocusInWindow();
FocusManager.getCurrentManager().getFocusOwner(); }
if (focusOwner != d) { });
}
private static void isFocusOwner(Component queriedFocusOwner,
String direction)
throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
Component actualFocusOwner
= FocusManager.getCurrentManager().getFocusOwner();
if (actualFocusOwner != queriedFocusOwner) {
frame.dispose();
throw new RuntimeException( throw new RuntimeException(
"Focus component is wrong after backward key " + focusOwner); "Focus component is wrong after " + direction
+ " direction ");
} }
} }
}); });
SwingUtilities.invokeLater(new Runnable() { }
private static boolean tryLookAndFeel(String lookAndFeelString)
throws Exception {
try {
UIManager.setLookAndFeel(
lookAndFeelString);
} catch (UnsupportedLookAndFeelException
| ClassNotFoundException
| InstantiationException
| IllegalAccessException e) {
return false;
}
return true;
}
private static void cleanUp() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override @Override
public void run() { public void run() {
frame.dispose(); frame.dispose();
} }
}); });
System.out.println("ok");
} }
} }