Path: blob/master/test/jdk/javax/swing/JTextField/4532513/DefaultCaretRequestsFocusTest.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.GridLayout;24import java.awt.Point;25import java.awt.Robot;26import java.awt.event.InputEvent;27import java.awt.event.KeyEvent;28import java.util.Arrays;29import java.util.List;30import java.util.concurrent.atomic.AtomicBoolean;31import java.util.concurrent.atomic.AtomicReference;32import java.util.stream.Collectors;33import javax.swing.InputVerifier;34import javax.swing.JComponent;35import javax.swing.JFrame;36import javax.swing.JPanel;37import javax.swing.JTextField;38import javax.swing.SwingUtilities;39import javax.swing.UIManager;40import javax.swing.UIManager.LookAndFeelInfo;41import javax.swing.UnsupportedLookAndFeelException;4243import static javax.swing.UIManager.getInstalledLookAndFeels;4445/*46* @test47* @key headful48* @bug 453251349* @summary Verifies that DefaultCaret doesn't requests focus in mouseClick and mousePressed50* causing the associated input verifier to fire twice.51* @run main DefaultCaretRequestsFocusTest52*/53public class DefaultCaretRequestsFocusTest {5455private static JTextField jTextField1;56private static JTextField jTextField2;57private static JTextField jTextField3;58private static JFrame frame;59private static Robot robot;60private static volatile int shouldYieldFocusCount;6162public static void main(String[] args) throws Exception {63runTest();64}6566public static void runTest() throws Exception {67robot = new Robot();68robot.setAutoWaitForIdle(true);69robot.setAutoDelay(200);7071List<String> lafs = Arrays.stream(getInstalledLookAndFeels())72.map(LookAndFeelInfo::getClassName)73.collect(Collectors.toList());74for (final String laf : lafs) {75try {76AtomicBoolean lafSetSuccess = new AtomicBoolean(false);77SwingUtilities.invokeAndWait(() -> {78lafSetSuccess.set(setLookAndFeel(laf));79if (lafSetSuccess.get()) {80createUI();81}82});83if (!lafSetSuccess.get()) {84continue;85}86robot.waitForIdle();8788AtomicReference<Point> jTextField1LocRef = new AtomicReference<>();89AtomicReference<Point> jTextField2LocRef = new AtomicReference<>();90SwingUtilities.invokeAndWait(() -> {91jTextField1LocRef.set(jTextField1.getLocationOnScreen());92jTextField2LocRef.set(jTextField2.getLocationOnScreen());93});94final Point jTextField1Loc = jTextField1LocRef.get();95final Point jTextField2Loc = jTextField2LocRef.get();9697shouldYieldFocusCount = 0;9899// Click on TextField2100robot.mouseMove(jTextField2Loc.x + 5, jTextField2Loc.y + 5);101robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);102robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);103104typeSomeText();105106// Click on TextField1107robot.mouseMove(jTextField1Loc.x + 5, jTextField1Loc.y + 5);108robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);109robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);110111typeSomeText();112113if (shouldYieldFocusCount == 1) {114System.out.println("Test passed for " + laf);115} else {116throw new RuntimeException("Test failed for " + laf117+ " as InputVerifier.shouldYieldFocus() was called " + shouldYieldFocusCount118+ " times on jTextField2, but it is expected to be called only once.");119}120121} finally {122SwingUtilities.invokeAndWait(DefaultCaretRequestsFocusTest::disposeFrame);123}124}125}126127private static void typeSomeText() {128robot.keyPress(KeyEvent.VK_T);129robot.keyRelease(KeyEvent.VK_T);130robot.keyPress(KeyEvent.VK_E);131robot.keyRelease(KeyEvent.VK_E);132robot.keyPress(KeyEvent.VK_X);133robot.keyRelease(KeyEvent.VK_X);134robot.keyPress(KeyEvent.VK_T);135robot.keyRelease(KeyEvent.VK_T);136}137138private static void createUI() {139frame = new JFrame();140jTextField1 = new JTextField(6);141jTextField2 = new JTextField(6);142jTextField3 = new JTextField(6);143JPanel panel = new JPanel();144panel.setLayout(new GridLayout(3, 1));145panel.add(jTextField1);146panel.add(jTextField2);147panel.add(jTextField3);148149InputVerifier iv = new InputVerifier() {150public boolean verify(JComponent input) {151System.out.println("InputVerifier.verify() called");152return false;153}154155public boolean shouldYieldFocus(JComponent input) {156++shouldYieldFocusCount;157System.out.println("InputVerifier.shouldYieldFocus() called " + shouldYieldFocusCount);158return false;159}160};161162jTextField2.setInputVerifier(iv);163164frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);165frame.add(panel);166frame.pack();167frame.setAlwaysOnTop(true);168frame.setLocationRelativeTo(null);169frame.setVisible(true);170}171172private static boolean setLookAndFeel(String lafName) {173try {174UIManager.setLookAndFeel(lafName);175} catch (UnsupportedLookAndFeelException ignored) {176System.out.println("Ignoring Unsupported laf : " + lafName);177return false;178} catch (ClassNotFoundException | InstantiationException179| IllegalAccessException e) {180throw new RuntimeException(e);181}182return true;183}184185private static void disposeFrame() {186if (frame != null) {187frame.dispose();188frame = null;189}190}191192}193194195