Path: blob/master/test/jdk/javax/swing/JComponent/JComponentSetRequestFocusEnabledTest.java
66644 views
/*1* Copyright (c) 2001, 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.Point;24import java.awt.Robot;25import java.awt.event.InputEvent;26import java.util.Arrays;27import java.util.List;28import java.util.concurrent.atomic.AtomicBoolean;29import java.util.concurrent.atomic.AtomicReference;30import java.util.stream.Collectors;31import javax.swing.JButton;32import javax.swing.JFrame;33import javax.swing.JPanel;34import javax.swing.SwingUtilities;35import javax.swing.UIManager;36import javax.swing.UIManager.LookAndFeelInfo;37import javax.swing.UnsupportedLookAndFeelException;3839import static javax.swing.UIManager.getInstalledLookAndFeels;4041/*42* @test43* @key headful44* @bug 437157545* @summary This testcase tests RFE-4371575 request, verifies that46* JComponent.setRequestFocusEnabled() works as expected47* @run main JComponentSetRequestFocusEnabledTest48*/49public class JComponentSetRequestFocusEnabledTest {5051private static JButton button;52private static Robot robot;53private static JFrame frame;5455public static void main(String[] args) throws Exception {56robot = new Robot();57robot.setAutoWaitForIdle(true);58robot.setAutoDelay(200);59List<String> lafs = Arrays.stream(getInstalledLookAndFeels())60.map(LookAndFeelInfo::getClassName)61.collect(Collectors.toList());62for (final String laf : lafs) {63try {64AtomicBoolean lafSetSuccess = new AtomicBoolean(false);65SwingUtilities.invokeAndWait(() -> {66lafSetSuccess.set(setLookAndFeel(laf));67if (lafSetSuccess.get()) {68createUI();69}70});71if (!lafSetSuccess.get()) {72continue;73}74robot.waitForIdle();7576mouseClick(button);7778if (!button.isFocusOwner()) {79System.out.println("Test Passed in " + laf);80} else {81throw new RuntimeException(82"Test Failed, button has focus in " + laf);83}84} finally {85SwingUtilities.invokeAndWait(86JComponentSetRequestFocusEnabledTest::disposeFrame);87}88}89}9091private static void mouseClick(JButton jButton) throws Exception {92final AtomicReference<Point> loc = new AtomicReference<>();93SwingUtilities94.invokeAndWait(() -> loc.set(jButton.getLocationOnScreen()));95final Point location = loc.get();96robot.mouseMove(location.x + 10, location.y + 10);97robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);98robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);99}100101public static void createUI() {102frame = new JFrame();103JPanel panel = new JPanel();104panel.add(new JButton("Focused"));105panel.add(button = new JButton("Unfocusable"));106107button.setRequestFocusEnabled(false);108109frame.setContentPane(panel);110frame.setSize(150, 100);111frame.setLocationRelativeTo(null);112frame.pack();113frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);114frame.setVisible(true);115}116117private static boolean setLookAndFeel(String lafName) {118try {119UIManager.setLookAndFeel(lafName);120} catch (UnsupportedLookAndFeelException ignored) {121System.out.println("Ignoring Unsupported L&F: " + lafName);122return false;123} catch (ClassNotFoundException | InstantiationException124| IllegalAccessException e) {125throw new RuntimeException(e);126}127return true;128}129130private static void disposeFrame() {131if (frame != null) {132frame.dispose();133frame = null;134}135}136137}138139140