Path: blob/master/test/jdk/javax/swing/JSpinner/4973721/bug4973721.java
41153 views
/*1* Copyright (c) 2013, 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 497372127@summary Up and Down Arrow key buttons are not working for the JSpinner in28@ Synth LAF29@library ../../regtesthelpers30@build Util31@author Oleg Mokhovikov32@run main bug497372133*/3435import java.awt.Robot;36import javax.swing.event.ChangeListener;37import javax.swing.event.ChangeEvent;38import java.awt.event.KeyEvent;39import java.awt.event.FocusListener;40import java.awt.event.FocusEvent;41import javax.swing.*;4243public class bug4973721 implements ChangeListener, FocusListener {44static volatile boolean bStateChanged = false;45static volatile boolean bFocusGained = false;46static JSpinner spinner;47static final Object listener = new bug4973721();48static JFrame frame;4950public void focusLost(FocusEvent e) {}5152public synchronized void focusGained(FocusEvent e) {53System.out.println("focusGained");54bFocusGained = true;55notifyAll();56}5758public synchronized void stateChanged(ChangeEvent e) {59System.out.println("stateChanged");60bStateChanged = true;61notifyAll();62}6364public static void main(String[] args) throws Exception {65UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");6667try {68SwingUtilities.invokeAndWait(new Runnable() {69public void run() {70frame = new JFrame();71spinner = new JSpinner();72frame.getContentPane().add(spinner);73frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);7475frame.pack();76frame.setVisible(true);77spinner.addChangeListener((ChangeListener)listener);78spinner.addFocusListener((FocusListener)listener);79spinner.requestFocus();8081}82});8384synchronized(listener) {85if (!bFocusGained) {86System.out.println("waiting focusGained...");87try {88listener.wait(5000);89}90catch (InterruptedException e) {}91}92}9394boolean hasFocus = Util.invokeOnEDT(95new java.util.concurrent.Callable<Boolean>() {96@Override97public Boolean call() throws Exception {98return spinner.hasFocus();99}100});101102if (!bFocusGained && !hasFocus) {103throw new RuntimeException("Couldn't request focus for" +104" spinner");105}106Robot robot = new Robot();107robot.setAutoDelay(50);108109Util.hitKeys(robot, KeyEvent.VK_UP);110robot.waitForIdle();111Thread.sleep(1000);112113if (!bStateChanged) {114throw new RuntimeException("Up arrow key button doesn't work" +115" for a spinner in Synth L&F");116}117118bStateChanged = false;119120Util.hitKeys(robot, KeyEvent.VK_DOWN);121robot.waitForIdle();122Thread.sleep(1000);123124if (!bStateChanged) {125throw new RuntimeException("Down arrow key button doesn't" +126" work for a spinner in Synth L&F");127}128} finally {129if (frame != null) {130SwingUtilities.invokeAndWait(() -> frame.dispose());131}132}133}134}135136137