Path: blob/master/test/jdk/javax/swing/JSpinner/4788637/bug4788637.java
41153 views
/*1* Copyright (c) 2003, 2019, 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.GridBagConstraints;24import java.awt.GridBagLayout;25import java.awt.Point;26import java.awt.Rectangle;27import java.awt.Robot;28import java.awt.event.InputEvent;2930import javax.swing.JFrame;31import javax.swing.JSpinner;32import javax.swing.SpinnerModel;33import javax.swing.SpinnerNumberModel;34import javax.swing.SwingUtilities;35import javax.swing.UIManager;36import javax.swing.UnsupportedLookAndFeelException;37import javax.swing.event.ChangeEvent;38import javax.swing.event.ChangeListener;3940import static javax.swing.UIManager.getInstalledLookAndFeels;4142/**43* @test44* @bug 4788637 712430745* @key headful46* @summary JSpinner buttons don't conform to most platform conventions47*/48public final class bug4788637 {4950private static JSpinner spinner;51private static JFrame fr;5253private static Robot robot;54private int step;55private boolean spinnerValueChanged[] = {false, false, false};5657private static Point p;58private static Rectangle rect;5960public static void main(final String[] args) throws Exception {61robot = new Robot();62robot.setAutoDelay(50);63robot.setAutoWaitForIdle(true);64for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {65SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));66bug4788637 app = new bug4788637();67try {68SwingUtilities.invokeAndWait(app::createAndShowGUI);69robot.waitForIdle();70SwingUtilities.invokeAndWait(()-> {71spinner.requestFocus();72p = spinner.getLocationOnScreen();73rect = spinner.getBounds();74});75app.start();76} finally {77SwingUtilities.invokeAndWait(app::destroy);78}79}80}8182public void createAndShowGUI() {83fr = new JFrame("Test");84fr.setLayout( new GridBagLayout() );8586SpinnerModel model = new SpinnerNumberModel(50, 1, 100, 1);87spinner = new JSpinner(model);88fr.add(spinner,new GridBagConstraints());8990spinner.addChangeListener(new ChangeListener() {91public void stateChanged(ChangeEvent e) {92synchronized (bug4788637.this) {93spinnerValueChanged[step] = true;94bug4788637.this.notifyAll();95}96}97});9899fr.setSize(200, 200);100fr.setLocationRelativeTo(null);101fr.setVisible(true);102fr.toFront();103}104105public void start() {106try {107Thread.sleep(1000);108// Move mouse to the up arrow button109robot.mouseMove(p.x+rect.width-3, p.y+3);110robot.mousePress(InputEvent.BUTTON1_MASK);111synchronized (bug4788637.this) {112if (!spinnerValueChanged[step]) {113bug4788637.this.wait(3000);114}115}116117// Move mouse out of JSpinner118robot.mouseMove(p.x+rect.width-3, p.y-3);119synchronized (bug4788637.this) {120step++;121if (!spinnerValueChanged[step]) {122bug4788637.this.wait(3000);123}124}125126// Move mouse to the up arrow button127robot.mouseMove(p.x+rect.width-3, p.y+3);128synchronized (bug4788637.this) {129step++;130if (!spinnerValueChanged[step]) {131bug4788637.this.wait(3000);132}133}134135robot.mouseRelease(InputEvent.BUTTON1_MASK);136} catch(Throwable t) {137throw new RuntimeException(t);138}139}140141public void destroy() {142fr.dispose();143synchronized (bug4788637.this) {144if (!spinnerValueChanged[0] ||145spinnerValueChanged[1] ||146!spinnerValueChanged[2]) {147throw new Error("JSpinner buttons don't conform to most platform conventions");148}149}150}151152private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {153try {154UIManager.setLookAndFeel(laf.getClassName());155System.out.println("LookAndFeel: " + laf.getClassName());156} catch (ClassNotFoundException | InstantiationException |157UnsupportedLookAndFeelException | IllegalAccessException e) {158throw new RuntimeException(e);159}160}161}162163164