Path: blob/master/test/jdk/javax/swing/JRadioButton/FocusTraversal/FocusTraversal.java
41155 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @key headful25* @bug 8129940 8132770 8161470 816316926* @summary JRadioButton should run custom FocusTraversalKeys for all LaFs27* @run main FocusTraversal28*/29import java.awt.BorderLayout;30import java.awt.Component;31import java.awt.KeyboardFocusManager;32import java.awt.Robot;33import java.awt.event.KeyEvent;34import java.util.HashSet;35import java.util.Set;36import javax.swing.ButtonGroup;37import javax.swing.FocusManager;38import javax.swing.JButton;39import javax.swing.JFrame;40import javax.swing.JPanel;41import javax.swing.JRadioButton;42import javax.swing.JTextField;43import javax.swing.KeyStroke;44import javax.swing.SwingUtilities;45import javax.swing.UIManager;46import javax.swing.UnsupportedLookAndFeelException;4748public class FocusTraversal {4950private static JFrame frame;51private static JRadioButton a;52private static JRadioButton b;53private static JRadioButton c;54private static JRadioButton d;55private static JTextField next;56private static JTextField prev;57private static Robot robot;5859public static void main(String[] args) throws Exception {6061robot = new Robot();62robot.setAutoDelay(100);63robot.waitForIdle();64UIManager.LookAndFeelInfo[] lookAndFeelArray65= UIManager.getInstalledLookAndFeels();66for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {67executeCase(lookAndFeelItem.getClassName());68}69}7071private static void executeCase(String lookAndFeelString)72throws Exception {73if (tryLookAndFeel(lookAndFeelString)) {74createUI(lookAndFeelString);75robot.waitForIdle();76runTestCase();77robot.waitForIdle();78cleanUp();79robot.waitForIdle();80}81}8283private static void createUI(final String lookAndFeelString)84throws Exception {85SwingUtilities.invokeAndWait(new Runnable() {86@Override87public void run() {88Set<KeyStroke> keystrokes = new HashSet<KeyStroke>();89keystrokes.add(KeyStroke.getKeyStroke("TAB"));90keystrokes.add(KeyStroke.getKeyStroke("ENTER"));91frame = new JFrame("FocusTraversalTest " + lookAndFeelString);92frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);93frame.setUndecorated(true);94frame.setFocusTraversalKeys(95KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,96keystrokes);9798a = new JRadioButton("a");99b = new JRadioButton("b");100c = new JRadioButton("c");101d = new JRadioButton("d");102103ButtonGroup radioButtonGroup = new ButtonGroup();104radioButtonGroup.add(a);105radioButtonGroup.add(b);106radioButtonGroup.add(c);107radioButtonGroup.add(d);108109JPanel panel = new JPanel();110prev = new JTextField("text");111panel.add(prev);112panel.add(a);113panel.add(b);114panel.add(c);115panel.add(d);116next = new JTextField("text");117panel.add(next);118119JPanel root = new JPanel();120root.setLayout(new BorderLayout());121root.add(panel, BorderLayout.CENTER);122root.add(new JButton("OK"), BorderLayout.SOUTH);123124frame.add(root);125frame.pack();126frame.setLocationRelativeTo(null);127frame.setVisible(true);128frame.toFront();129}130});131}132133private static void runTestCase() throws Exception {134focusOn(a);135136robot.waitForIdle();137robot.delay(500);138robot.keyPress(KeyEvent.VK_ENTER);139robot.keyRelease(KeyEvent.VK_ENTER);140robot.waitForIdle();141isFocusOwner(next, "forward");142robot.keyPress(KeyEvent.VK_SHIFT);143robot.keyPress(KeyEvent.VK_TAB);144robot.keyRelease(KeyEvent.VK_TAB);145robot.keyRelease(KeyEvent.VK_SHIFT);146robot.waitForIdle();147isFocusOwner(a, "backward");148149}150151private static void focusOn(Component component)152throws Exception {153SwingUtilities.invokeAndWait(new Runnable() {154@Override155public void run() {156component.requestFocusInWindow();157}158});159}160161private static void isFocusOwner(Component queriedFocusOwner,162String direction)163throws Exception {164SwingUtilities.invokeAndWait(new Runnable() {165@Override166public void run() {167Component actualFocusOwner168= FocusManager.getCurrentManager().getFocusOwner();169if (actualFocusOwner != queriedFocusOwner) {170frame.dispose();171throw new RuntimeException(172"Focus component is wrong after " + direction173+ " direction ");174175}176}177});178}179180private static boolean tryLookAndFeel(String lookAndFeelString)181throws Exception {182183try {184UIManager.setLookAndFeel(185lookAndFeelString);186187} catch (UnsupportedLookAndFeelException188| ClassNotFoundException189| InstantiationException190| IllegalAccessException e) {191return false;192}193System.out.println("Testing lookAndFeel " + lookAndFeelString);194return true;195}196197private static void cleanUp() throws Exception {198SwingUtilities.invokeAndWait(new Runnable() {199@Override200public void run() {201frame.dispose();202}203});204}205}206207208