Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JScrollBar/4708809/bug4708809.java
38918 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* @bug 470880926* @summary JScrollBar functionality slightly different from native scrollbar27* @author Andrey Pikalev28* @run main bug470880929*/30import javax.swing.*;31import java.awt.*;32import java.awt.Point;33import java.awt.event.*;3435public class bug4708809 {3637private static volatile boolean do_test = false;38private static volatile boolean passed = true;39private static JScrollPane spane;40private static JScrollBar sbar;4142public static void main(String[] args) throws Exception {43Robot robot = new Robot();44robot.setAutoDelay(350);4546SwingUtilities.invokeAndWait(new Runnable() {4748public void run() {49createAndShowGUI();50}51});5253robot.waitForIdle();5455SwingUtilities.invokeAndWait(new Runnable() {5657public void run() {58spane.requestFocus();59sbar.setValue(sbar.getMaximum());60}61});6263robot.waitForIdle();6465Point point = getClickPoint(0.5, 0.5);66robot.mouseMove(point.x, point.y);67robot.mousePress(InputEvent.BUTTON1_MASK);6869robot.waitForIdle();7071SwingUtilities.invokeAndWait(new Runnable() {7273public void run() {74final int oldValue = sbar.getValue();75sbar.addAdjustmentListener(new AdjustmentListener() {7677public void adjustmentValueChanged(AdjustmentEvent e) {78if (e.getValue() >= oldValue) {79passed = false;80}81do_test = true;82}83});8485}86});8788robot.waitForIdle();8990point = getClickPoint(0.5, 0.2);91robot.mouseMove(point.x, point.y);92robot.mouseRelease(InputEvent.BUTTON1_MASK);93robot.waitForIdle();9495if (!do_test || !passed) {96throw new Exception("The scrollbar moved with incorrect direction");97}9899}100101private static Point getClickPoint(final double scaleX, final double scaleY) throws Exception {102final Point[] result = new Point[1];103104SwingUtilities.invokeAndWait(new Runnable() {105106@Override107public void run() {108Point p = sbar.getLocationOnScreen();109Rectangle rect = sbar.getBounds();110result[0] = new Point((int) (p.x + scaleX * rect.width),111(int) (p.y + scaleY * rect.height));112}113});114115return result[0];116117}118119private static void createAndShowGUI() {120JFrame fr = new JFrame("Test");121122JLabel label = new JLabel("picture");123label.setPreferredSize(new Dimension(500, 500));124spane = new JScrollPane(label);125fr.getContentPane().add(spane);126sbar = spane.getVerticalScrollBar();127128fr.setSize(200, 200);129fr.setVisible(true);130}131}132133134