Path: blob/master/test/jdk/javax/swing/JSplitPane/4164779/JSplitPaneKeyboardNavigationTest.java
66646 views
/*1* Copyright (c) 2002, 2022, 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*/2223import java.awt.BorderLayout;24import java.awt.Point;25import java.awt.Robot;26import java.awt.event.InputEvent;27import java.awt.event.KeyEvent;28import java.util.Arrays;29import java.util.List;30import java.util.concurrent.atomic.AtomicBoolean;31import java.util.concurrent.atomic.AtomicReference;32import java.util.stream.Collectors;33import javax.swing.JButton;34import javax.swing.JFrame;35import javax.swing.JPanel;36import javax.swing.JSplitPane;37import javax.swing.SwingUtilities;38import javax.swing.UIManager;39import javax.swing.UIManager.LookAndFeelInfo;40import javax.swing.UnsupportedLookAndFeelException;4142import static javax.swing.UIManager.getInstalledLookAndFeels;4344/*45* @test46* @key headful47* @bug 416477948* @summary This test confirms that JSplitPane keyboard navigation supports F6 and Ctrl+Tab.49* @run main JSplitPaneKeyboardNavigationTest50*/51public class JSplitPaneKeyboardNavigationTest {5253private static final StringBuffer failedVerifiers = new StringBuffer();54private static JPanel panel;55private static JButton leftButton;56private static JButton rightButton1;57private static JButton rightButton2;58private static JButton topButton;59private static JButton bottomButton;60private static Robot robot;61private static JFrame frame;6263public static void main(String[] s) throws Exception {64robot = new Robot();65robot.setAutoWaitForIdle(true);66robot.setAutoDelay(200);67List<String> lafs = Arrays.stream(getInstalledLookAndFeels())68.map(LookAndFeelInfo::getClassName)69.collect(Collectors.toList());70for (final String laf : lafs) {71try {72AtomicBoolean lafSetSuccess = new AtomicBoolean(false);73SwingUtilities.invokeAndWait(() -> {74lafSetSuccess.set(setLookAndFeel(laf));75if (lafSetSuccess.get()) {76createUI();77}78});79if (!lafSetSuccess.get()) {80continue;81}82robot.waitForIdle();8384// Press Right button 1 and move focus to it.85pressButton(rightButton1);86hitKeys(KeyEvent.VK_F6);8788// Verifier1 - Verifies that, F6 transfers focus to the right/bottom side of the splitpane89if (isFocusOwner(rightButton2)) {90System.out.println("Verifier 1 passed");91} else {92failedVerifiers.append("1,");93System.out.println("Verifier 1 failed, rightButton2 is not focus owner," +94"F6 doesn't transfer focus to the right/bottom side of the splitpane");95}9697// Press Right button 2 and move focus to it.98pressButton(rightButton2);99hitKeys(KeyEvent.VK_F6);100101// Verifier2 - Verifies that, F6 transfers focus to the left side of the parent splitpane,102// if the right/bottom side of splitpane already has focus, and it is contained within another splitpane103if (isFocusOwner(leftButton)) {104System.out.println("Verifier 2 passed");105} else {106failedVerifiers.append("2,");107System.out.println("Verifier 2 failed, leftButton is not focus owner, " +108"F6 doesn't transfer focus to the left side of the splitpane");109}110111// Press Left button and move focus to it.112pressButton(leftButton);113hitKeys(KeyEvent.VK_CONTROL, KeyEvent.VK_TAB);114// Verifier3 - Verifies that, CTRL-TAB navigates forward outside the JSplitPane115if (isFocusOwner(bottomButton)) {116System.out.println("Verifier 3 passed");117} else {118failedVerifiers.append("3,");119System.out.println("Verifier 3 failed, bottomButton is not focus owner, " +120"CTRL-TAB doesn't navigate forward outside the JSplitPane");121}122123// Press Left button and move focus to it.124pressButton(leftButton);125hitKeys(KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);126127// Verifier4 - Verifies that, CTRL-SHIFT-TAB navigates backward outside the JSplitPane128if (isFocusOwner(topButton)) {129System.out.println("Verifier 4 passed");130} else {131failedVerifiers.append("4");132System.out.println("Verifier 4 failed, topButton is not focus owner, " +133"CTRL-SHIFT-TAB doesn't navigate backward outside the JSplitPane");134}135136if (failedVerifiers.toString().isEmpty()) {137System.out.println("Test passed, All verifiers succeeded for " + laf);138} else {139throw new RuntimeException("Test failed, verifiers " + failedVerifiers.toString() + " failed for " + laf);140}141} finally {142SwingUtilities.invokeAndWait(JSplitPaneKeyboardNavigationTest::disposeFrame);143}144}145}146147private static boolean isFocusOwner(JButton button) throws Exception {148final AtomicBoolean isFocusOwner = new AtomicBoolean(false);149SwingUtilities.invokeAndWait(() -> {150isFocusOwner.set(button.isFocusOwner());151});152return isFocusOwner.get();153}154155private static void pressButton(JButton button) throws Exception {156final AtomicReference<Point> loc = new AtomicReference<>();157SwingUtilities.invokeAndWait(() -> {158loc.set(button.getLocationOnScreen());159});160final Point buttonLoc = loc.get();161robot.mouseMove(buttonLoc.x + 8, buttonLoc.y + 8);162robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);163robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);164}165166public static void createUI() {167frame = new JFrame();168panel = new JPanel();169panel.setLayout(new BorderLayout());170leftButton = new JButton("Left Button");171rightButton1 = new JButton("Right Button 1");172rightButton2 = new JButton("Right Button 2");173topButton = new JButton("Top Button");174bottomButton = new JButton("Bottom Button");175panel.add(topButton, BorderLayout.NORTH);176panel.add(bottomButton, BorderLayout.SOUTH);177final JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, rightButton1, rightButton2);178final JSplitPane splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, leftButton, splitPane2);179panel.add(splitPane1, BorderLayout.CENTER);180frame.setContentPane(panel);181frame.setSize(200, 200);182frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);183frame.pack();184frame.setAlwaysOnTop(true);185frame.setLocationRelativeTo(null);186frame.setVisible(true);187}188189private static void hitKeys(int... keys) {190for (int key : keys) {191robot.keyPress(key);192}193194for (int i = keys.length - 1; i >= 0; i--) {195robot.keyRelease(keys[i]);196}197}198199private static boolean setLookAndFeel(String lafName) {200try {201UIManager.setLookAndFeel(lafName);202} catch (UnsupportedLookAndFeelException ignored) {203System.out.println("Ignoring Unsupported L&F: " + lafName);204return false;205} catch (ClassNotFoundException | InstantiationException206| IllegalAccessException e) {207throw new RuntimeException(e);208}209return true;210}211212private static void disposeFrame() {213if (frame != null) {214frame.dispose();215frame = null;216}217}218219}220221222