Path: blob/master/test/jdk/javax/swing/JSpinner/8223788/JSpinnerButtonFocusTest.java
41153 views
/*1* Copyright (c) 2020, 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*/2223/*24* @test25* @key headful26* @bug 822378827* @summary JSpinner buttons in JColorChooser dialog may capture focus28* using TAB Key29* @run main JSpinnerButtonFocusTest30*/3132import java.awt.Robot;33import java.awt.BorderLayout;34import java.awt.ContainerOrderFocusTraversalPolicy;35import java.awt.event.FocusAdapter;36import java.awt.event.FocusEvent;37import java.awt.event.KeyEvent;38import java.util.concurrent.CountDownLatch;39import java.util.concurrent.TimeUnit;40import javax.swing.JFrame;41import javax.swing.JSpinner;42import javax.swing.JSpinner.DefaultEditor;43import javax.swing.SwingUtilities;44import javax.swing.UIManager;4546public class JSpinnerButtonFocusTest {47static JFrame frame;48static Robot robot;49static JSpinner spinner1, spinner2;50static DefaultEditor editor1, editor2;51static volatile boolean isJTextFieldFocused;52static volatile CountDownLatch latch1;5354public static void main(String args[]) throws Exception {5556for (UIManager.LookAndFeelInfo LF :57UIManager.getInstalledLookAndFeels()) {58latch1 = new CountDownLatch(1);59try {60UIManager.setLookAndFeel(LF.getClassName());61robot = new Robot();62robot.setAutoDelay(50);6364SwingUtilities.invokeAndWait(() -> {65frame = new JFrame();66spinner1 = new JSpinner();67spinner2 = new JSpinner();6869frame.setLayout(new BorderLayout());70frame.getContentPane().add(spinner1, BorderLayout.NORTH);71frame.getContentPane().add(spinner2, BorderLayout.SOUTH);7273editor1 = ((DefaultEditor)spinner1.getEditor());74editor1.setFocusable(false);75spinner1.setFocusable(false);7677editor2 = (DefaultEditor) spinner2.getEditor();78editor2.setFocusable(false);79spinner2.setFocusable(false);8081frame.setFocusTraversalPolicy(82new ContainerOrderFocusTraversalPolicy());83frame.setFocusTraversalPolicyProvider(true);8485frame.setAlwaysOnTop(true);86frame.pack();87frame.setVisible(true);88});89robot.waitForIdle();9091editor1.getTextField().addFocusListener(new FocusAdapter() {92@Override93public void focusGained(FocusEvent e) {94super.focusGained(e);95robot.keyPress(KeyEvent.VK_TAB);96robot.keyRelease(KeyEvent.VK_TAB);97latch1.countDown();98}99});100101SwingUtilities.invokeAndWait(() -> {102editor1.getTextField().requestFocusInWindow();103});104105if (!latch1.await(15, TimeUnit.MINUTES)) {106throw new RuntimeException(LF.getClassName() +107": Timeout waiting for editor1 to gain focus.");108}109110robot.waitForIdle();111SwingUtilities.invokeAndWait(() -> {112isJTextFieldFocused = editor2.getTextField().isFocusOwner();113});114115if (!isJTextFieldFocused) {116throw new RuntimeException(LF.getClassName() +117": Spinner's Text Field doesn't have focus ");118}119} finally {120if (frame != null) {121SwingUtilities.invokeAndWait(frame::dispose);122}123}124}125}126}127128129