Path: blob/master/test/jdk/javax/swing/JScrollBar/4708809/bug4708809.java
41153 views
/*1* Copyright (c) 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 470880927* @summary JScrollBar functionality slightly different from native scrollbar28* @author Andrey Pikalev29* @run main bug470880930*/31import javax.swing.*;32import java.awt.*;33import java.awt.Point;34import java.awt.event.*;3536public class bug4708809 {3738private static volatile boolean do_test = false;39private static volatile boolean passed = true;40private static JScrollPane spane;41private static JScrollBar sbar;42private static JFrame fr;4344public static void main(String[] args) throws Exception {45try {46Robot robot = new Robot();47robot.setAutoDelay(350);4849SwingUtilities.invokeAndWait(new Runnable() {5051public void run() {52createAndShowGUI();53}54});5556robot.waitForIdle();5758SwingUtilities.invokeAndWait(new Runnable() {5960public void run() {61spane.requestFocus();62sbar.setValue(sbar.getMaximum());63}64});6566robot.waitForIdle();6768Point point = getClickPoint(0.5, 0.5);69robot.mouseMove(point.x, point.y);70robot.mousePress(InputEvent.BUTTON1_MASK);7172robot.waitForIdle();7374SwingUtilities.invokeAndWait(new Runnable() {7576public void run() {77final int oldValue = sbar.getValue();78sbar.addAdjustmentListener(new AdjustmentListener() {7980public void adjustmentValueChanged(AdjustmentEvent e) {81if (e.getValue() >= oldValue) {82passed = false;83}84do_test = true;85}86});8788}89});9091robot.waitForIdle();9293point = getClickPoint(0.5, 0.2);94robot.mouseMove(point.x, point.y);95robot.mouseRelease(InputEvent.BUTTON1_MASK);96robot.waitForIdle();9798if (!do_test || !passed) {99throw new Exception("The scrollbar moved with incorrect direction");100}101} finally {102if (fr != null) SwingUtilities.invokeAndWait(() -> fr.dispose());103}104105}106107private static Point getClickPoint(final double scaleX, final double scaleY) throws Exception {108final Point[] result = new Point[1];109110SwingUtilities.invokeAndWait(new Runnable() {111112@Override113public void run() {114Point p = sbar.getLocationOnScreen();115Rectangle rect = sbar.getBounds();116result[0] = new Point((int) (p.x + scaleX * rect.width),117(int) (p.y + scaleY * rect.height));118}119});120121return result[0];122123}124125private static void createAndShowGUI() {126fr = new JFrame("Test");127128JLabel label = new JLabel("picture");129label.setPreferredSize(new Dimension(500, 500));130spane = new JScrollPane(label);131fr.getContentPane().add(spane);132sbar = spane.getVerticalScrollBar();133134fr.setSize(200, 200);135fr.setVisible(true);136}137}138139140