|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.Point; |
| 25 | +import java.awt.Robot; |
| 26 | +import java.awt.event.InputEvent; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
| 31 | +import java.util.stream.Collectors; |
| 32 | +import javax.swing.JButton; |
| 33 | +import javax.swing.JFrame; |
| 34 | +import javax.swing.JPanel; |
| 35 | +import javax.swing.SwingUtilities; |
| 36 | +import javax.swing.UIManager; |
| 37 | +import javax.swing.UIManager.LookAndFeelInfo; |
| 38 | +import javax.swing.UnsupportedLookAndFeelException; |
| 39 | + |
| 40 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 41 | + |
| 42 | +/* |
| 43 | + * @test |
| 44 | + * @key headful |
| 45 | + * @bug 4371575 |
| 46 | + * @summary This testcase tests RFE-4371575 request, verifies that |
| 47 | + * JComponent.setRequestFocusEnabled() works as expected |
| 48 | + * @run main JComponentSetRequestFocusEnabledTest |
| 49 | + */ |
| 50 | +public class JComponentSetRequestFocusEnabledTest { |
| 51 | + |
| 52 | + private static JButton button; |
| 53 | + private static Robot robot; |
| 54 | + private static JFrame frame; |
| 55 | + |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + robot = new Robot(); |
| 58 | + robot.setAutoWaitForIdle(true); |
| 59 | + robot.setAutoDelay(200); |
| 60 | + List<String> lafs = Arrays.stream(getInstalledLookAndFeels()) |
| 61 | + .map(LookAndFeelInfo::getClassName) |
| 62 | + .collect(Collectors.toList()); |
| 63 | + for (final String laf : lafs) { |
| 64 | + try { |
| 65 | + AtomicBoolean lafSetSuccess = new AtomicBoolean(false); |
| 66 | + SwingUtilities.invokeAndWait(() -> { |
| 67 | + lafSetSuccess.set(setLookAndFeel(laf)); |
| 68 | + if (lafSetSuccess.get()) { |
| 69 | + createUI(); |
| 70 | + } |
| 71 | + }); |
| 72 | + if (!lafSetSuccess.get()) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + robot.waitForIdle(); |
| 76 | + |
| 77 | + mouseClick(button); |
| 78 | + |
| 79 | + if (!button.isFocusOwner()) { |
| 80 | + System.out.println("Test Passed in " + laf); |
| 81 | + } else { |
| 82 | + throw new RuntimeException( |
| 83 | + "Test Failed, button has focus in " + laf); |
| 84 | + } |
| 85 | + } finally { |
| 86 | + SwingUtilities.invokeAndWait( |
| 87 | + JComponentSetRequestFocusEnabledTest::disposeFrame); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void mouseClick(JButton jButton) throws Exception { |
| 93 | + final AtomicReference<Point> loc = new AtomicReference<>(); |
| 94 | + SwingUtilities |
| 95 | + .invokeAndWait(() -> loc.set(jButton.getLocationOnScreen())); |
| 96 | + final Point location = loc.get(); |
| 97 | + robot.mouseMove(location.x + 10, location.y + 10); |
| 98 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 99 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 100 | + } |
| 101 | + |
| 102 | + public static void createUI() { |
| 103 | + frame = new JFrame(); |
| 104 | + JPanel panel = new JPanel(); |
| 105 | + panel.add(new JButton("Focused")); |
| 106 | + panel.add(button = new JButton("Unfocusable")); |
| 107 | + |
| 108 | + button.setRequestFocusEnabled(false); |
| 109 | + |
| 110 | + frame.setContentPane(panel); |
| 111 | + frame.setSize(150, 100); |
| 112 | + frame.setLocationRelativeTo(null); |
| 113 | + frame.pack(); |
| 114 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 115 | + frame.setVisible(true); |
| 116 | + } |
| 117 | + |
| 118 | + private static boolean setLookAndFeel(String lafName) { |
| 119 | + try { |
| 120 | + UIManager.setLookAndFeel(lafName); |
| 121 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 122 | + System.out.println("Ignoring Unsupported L&F: " + lafName); |
| 123 | + return false; |
| 124 | + } catch (ClassNotFoundException | InstantiationException |
| 125 | + | IllegalAccessException e) { |
| 126 | + throw new RuntimeException(e); |
| 127 | + } |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + private static void disposeFrame() { |
| 132 | + if (frame != null) { |
| 133 | + frame.dispose(); |
| 134 | + frame = null; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments