Path: blob/master/test/jdk/javax/swing/JScrollBar/6542335/bug6542335.java
41153 views
/*1* Copyright (c) 2010, 2011, 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 654233527* @summary different behavior on knob of scroll bar between 1.4.2 and 5.028* @author Alexander Potochkin29* @run main bug654233530*/3132import javax.swing.*;33import javax.swing.plaf.basic.BasicScrollBarUI;34import java.awt.*;35import java.awt.event.InputEvent;3637public class bug6542335 {38private static JScrollBar sb;39private static MyScrollBarUI ui;40private static JFrame frame;4142public static void main(String[] args) throws Exception {43try {44final Robot robot = new Robot();45robot.setAutoDelay(10);4647final Rectangle[] thumbBounds = new Rectangle[1];4849SwingUtilities.invokeAndWait(new Runnable() {50public void run() {51frame = new JFrame("bug6542335");52frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);5354sb = new JScrollBar(0, 0, 1, 0, 1);5556ui = new MyScrollBarUI();57sb.setUI(ui);5859sb.setPreferredSize(new Dimension(200, 17));60DefaultBoundedRangeModel rangeModel = new DefaultBoundedRangeModel();61rangeModel.setMaximum(100);62rangeModel.setMinimum(0);63rangeModel.setExtent(50);64rangeModel.setValue(50);6566sb.setModel(rangeModel);67frame.add(sb, BorderLayout.NORTH);6869frame.setSize(200, 100);70frame.setVisible(true);71}72});7374robot.waitForIdle();7576SwingUtilities.invokeAndWait(new Runnable() {77public void run() {78thumbBounds[0] = new Rectangle(ui.getThumbBounds());7980Point l = sb.getLocationOnScreen();8182robot.mouseMove(l.x + (int) (0.75 * sb.getWidth()), l.y + sb.getHeight() / 2);83robot.mousePress(InputEvent.BUTTON1_MASK);84robot.mouseRelease(InputEvent.BUTTON1_MASK);85}86});8788robot.waitForIdle();8990SwingUtilities.invokeAndWait(new Runnable() {91public void run() {92Rectangle newThumbBounds = ui.getThumbBounds();9394if (!thumbBounds[0].equals(newThumbBounds)) {95throw new RuntimeException("Test failed.\nOld bounds: " + thumbBounds[0] +96"\nNew bounds: " + newThumbBounds);97}98}99});100} finally {101if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());102}103}104105static class MyScrollBarUI extends BasicScrollBarUI {106public Rectangle getThumbBounds() {107return super.getThumbBounds();108}109}110}111112113