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