Path: blob/master/test/jdk/javax/swing/JSpinner/TestJSpinnerFocusLost.java
41149 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 484086927* @summary JSpinner keeps spinning while JOptionPane is shown on ChangeListener28* @run main TestJSpinnerFocusLost29*/3031import java.awt.Component;32import java.awt.Point;33import java.awt.Rectangle;34import java.awt.Robot;35import java.awt.event.InputEvent;36import java.awt.event.FocusEvent;37import java.awt.event.FocusListener;38import javax.swing.JFrame;39import javax.swing.JSpinner;40import javax.swing.JOptionPane;41import javax.swing.SpinnerNumberModel;42import javax.swing.SwingUtilities;43import javax.swing.event.ChangeEvent;44import javax.swing.event.ChangeListener;45import javax.swing.UIManager;46import javax.swing.UnsupportedLookAndFeelException;4748public class TestJSpinnerFocusLost extends JFrame implements ChangeListener, FocusListener {4950JSpinner spinner;5152boolean spinnerGainedFocus = false;53boolean spinnerLostFocus = false;5455static TestJSpinnerFocusLost b;56Point p;57Rectangle rect;58static Robot robot;5960public static void blockTillDisplayed(Component comp) {61Point p = null;62while (p == null) {63try {64p = comp.getLocationOnScreen();65} catch (IllegalStateException e) {66try {67Thread.sleep(1000);68} catch (InterruptedException ie) {69}70}71}72}7374public TestJSpinnerFocusLost() {75spinner = new JSpinner(new SpinnerNumberModel(10, 1, 100, 1));76spinner.addChangeListener(this);77((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addFocusListener(this);78getContentPane().add(spinner);79}8081public void doTest() throws Exception {82blockTillDisplayed(spinner);83SwingUtilities.invokeAndWait(() -> {84((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().requestFocus();85});8687try {88synchronized (TestJSpinnerFocusLost.this) {89if (!spinnerGainedFocus) {90TestJSpinnerFocusLost.this.wait(2000);91}92}939495SwingUtilities.invokeAndWait(() -> {96p = spinner.getLocationOnScreen();97rect = spinner.getBounds();98});99robot.delay(1000);100robot.mouseMove(p.x+rect.width-5, p.y+3);101robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);102robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);103104synchronized (TestJSpinnerFocusLost.this) {105while (!spinnerLostFocus) {106TestJSpinnerFocusLost.this.wait(2000);107}108}109110} catch(Exception ex) {111ex.printStackTrace();112}113114if ( ((Integer) spinner.getValue()).intValue() != 11 ) {115System.out.println("spinner value " + ((Integer) spinner.getValue()).intValue());116throw new RuntimeException("Spinner value shouldn't be other than 11");117}118}119120121private boolean changing = false;122123public void stateChanged(ChangeEvent e) {124if (changing) {125return;126}127JSpinner spinner = (JSpinner)e.getSource();128int value = ((Integer) spinner.getValue()).intValue();129if (value > 10) {130changing = true;131JOptionPane.showMessageDialog(spinner, "10 exceeded");132}133}134135public void focusGained(FocusEvent e) {136synchronized (TestJSpinnerFocusLost.this) {137spinnerGainedFocus = true;138TestJSpinnerFocusLost.this.notifyAll();139}140}141142public void focusLost(FocusEvent e) {143synchronized (TestJSpinnerFocusLost.this) {144spinnerLostFocus = true;145TestJSpinnerFocusLost.this.notifyAll();146}147}148149private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {150try {151UIManager.setLookAndFeel(laf.getClassName());152} catch (UnsupportedLookAndFeelException ignored) {153System.out.println("Unsupported L&F: " + laf.getClassName());154} catch (ClassNotFoundException | InstantiationException155| IllegalAccessException e) {156throw new RuntimeException(e);157}158}159160public static void main(String[] argv) throws Exception {161robot = new Robot();162robot.setAutoWaitForIdle(true);163robot.setAutoDelay(250);164for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {165System.out.println("Testing L&F: " + laf.getClassName());166SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));167try {168SwingUtilities.invokeAndWait(() -> {169b = new TestJSpinnerFocusLost();170b.pack();171b.setLocationRelativeTo(null);172b.setVisible(true);173});174robot.waitForIdle();175b.doTest();176robot.delay(500);177} finally {178SwingUtilities.invokeAndWait(() -> {179if (b != null) b.dispose();180});181}182robot.delay(1000);183}184}185}186187188