|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 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.FocusAdapter; |
| 27 | +import java.awt.event.FocusEvent; |
| 28 | +import java.awt.event.InputEvent; |
| 29 | +import java.awt.event.KeyEvent; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
| 36 | +import java.util.stream.Collectors; |
| 37 | +import javax.swing.JButton; |
| 38 | +import javax.swing.JFrame; |
| 39 | +import javax.swing.JPanel; |
| 40 | +import javax.swing.JTextArea; |
| 41 | +import javax.swing.SwingUtilities; |
| 42 | +import javax.swing.UIManager; |
| 43 | +import javax.swing.UnsupportedLookAndFeelException; |
| 44 | + |
| 45 | + |
| 46 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 47 | + |
| 48 | +/* |
| 49 | + * @test |
| 50 | + * @key headful |
| 51 | + * @bug 4514331 |
| 52 | + * @summary Check whether pressing <Tab> key always shift focus to next component, |
| 53 | + * even though the current focus is in JTextArea and some text is already selected. |
| 54 | + * @run main TabShiftsFocusToNextComponent |
| 55 | + */ |
| 56 | +public class TabShiftsFocusToNextComponent { |
| 57 | + |
| 58 | + private static JFrame frame; |
| 59 | + private static JTextArea textArea; |
| 60 | + private static Robot robot; |
| 61 | + private static CountDownLatch textAreaGainedFocusLatch; |
| 62 | + private static CountDownLatch buttonGainedFocusLatch; |
| 63 | + |
| 64 | + public static void main(String[] s) throws Exception { |
| 65 | + runTest(); |
| 66 | + } |
| 67 | + |
| 68 | + public static void runTest() throws Exception { |
| 69 | + robot = new Robot(); |
| 70 | + robot.setAutoWaitForIdle(true); |
| 71 | + robot.setAutoDelay(200); |
| 72 | + List<String> lafs = Arrays.stream(getInstalledLookAndFeels()) |
| 73 | + .map(UIManager.LookAndFeelInfo::getClassName) |
| 74 | + .collect(Collectors.toList()); |
| 75 | + for (final String laf : lafs) { |
| 76 | + textAreaGainedFocusLatch = new CountDownLatch(1); |
| 77 | + buttonGainedFocusLatch = new CountDownLatch(1); |
| 78 | + try { |
| 79 | + AtomicBoolean lafSetSuccess = new AtomicBoolean(false); |
| 80 | + SwingUtilities.invokeAndWait(() -> { |
| 81 | + lafSetSuccess.set(setLookAndFeel(laf)); |
| 82 | + if (lafSetSuccess.get()) { |
| 83 | + createUI(); |
| 84 | + } |
| 85 | + }); |
| 86 | + if (!lafSetSuccess.get()) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + robot.waitForIdle(); |
| 90 | + |
| 91 | + SwingUtilities.invokeAndWait(() -> textArea.requestFocusInWindow()); |
| 92 | + |
| 93 | + // Waits until the textArea gains focus. |
| 94 | + if (!textAreaGainedFocusLatch.await(3, TimeUnit.SECONDS)) { |
| 95 | + throw new RuntimeException("Test Failed, waited for long, " + |
| 96 | + "but the JTextArea can't gain focus for L&F: " + laf); |
| 97 | + } |
| 98 | + |
| 99 | + AtomicReference<Point> textAreaLoc = new AtomicReference<Point>(); |
| 100 | + SwingUtilities.invokeAndWait(() -> { |
| 101 | + textAreaLoc.set(textArea.getLocationOnScreen()); |
| 102 | + }); |
| 103 | + |
| 104 | + final int x = textAreaLoc.get().x; |
| 105 | + final int y = textAreaLoc.get().y; |
| 106 | + robot.mouseMove(x + 5, y + 5); |
| 107 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 108 | + robot.mouseMove(x + 20, y + 5); |
| 109 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 110 | + robot.keyPress(KeyEvent.VK_TAB); |
| 111 | + robot.keyRelease(KeyEvent.VK_TAB); |
| 112 | + |
| 113 | + // Waits until the button gains focus. |
| 114 | + if (!buttonGainedFocusLatch.await(3, TimeUnit.SECONDS)) { |
| 115 | + throw new RuntimeException("Test Failed, waited for long, " + |
| 116 | + "but the Button can't gain focus when 'Tab' key pressed for L&F: " + laf); |
| 117 | + } else { |
| 118 | + System.out.println(" Test passed for " + laf); |
| 119 | + } |
| 120 | + } finally { |
| 121 | + SwingUtilities.invokeAndWait(TabShiftsFocusToNextComponent::disposeFrame); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + private static void createUI() { |
| 128 | + frame = new JFrame(); |
| 129 | + JPanel panel = new JPanel(); |
| 130 | + textArea = new JTextArea("I am a JTextArea"); |
| 131 | + textArea.addFocusListener(new FocusAdapter() { |
| 132 | + @Override |
| 133 | + public void focusGained(FocusEvent e) { |
| 134 | + textAreaGainedFocusLatch.countDown(); |
| 135 | + } |
| 136 | + }); |
| 137 | + textArea.setEditable(false); |
| 138 | + panel.add(textArea); |
| 139 | + JButton button = new JButton("Button"); |
| 140 | + panel.add(button); |
| 141 | + button.addFocusListener(new FocusAdapter() { |
| 142 | + @Override |
| 143 | + public void focusGained(FocusEvent e) { |
| 144 | + buttonGainedFocusLatch.countDown(); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + frame.add(panel); |
| 149 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 150 | + frame.setUndecorated(true); |
| 151 | + frame.pack(); |
| 152 | + frame.setAlwaysOnTop(true); |
| 153 | + frame.setLocationRelativeTo(null); |
| 154 | + frame.setVisible(true); |
| 155 | + } |
| 156 | + |
| 157 | + private static boolean setLookAndFeel(String lafName) { |
| 158 | + try { |
| 159 | + UIManager.setLookAndFeel(lafName); |
| 160 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 161 | + System.out.println("Ignoring Unsupported L&F: " + lafName); |
| 162 | + return false; |
| 163 | + } catch (ClassNotFoundException | InstantiationException |
| 164 | + | IllegalAccessException e) { |
| 165 | + throw new RuntimeException(e); |
| 166 | + } |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + private static void disposeFrame() { |
| 171 | + if (frame != null) { |
| 172 | + frame.dispose(); |
| 173 | + frame = null; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments