Path: blob/master/test/jdk/javax/swing/JScrollBar/bug4202954/bug4202954.java
41153 views
/*1* Copyright (c) 2013, 2018, 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*/22/*23@test24@key headful25@bug 420295426@library /test/lib27@library ../../regtesthelpers28@build Util jdk.test.lib.Platform29@author Michael C. Albers30@run main bug420295431*/3233import java.awt.*;34import java.awt.event.InputEvent;35import javax.swing.*;3637import jdk.test.lib.Platform;3839public class bug4202954 {40static JScrollPane buttonScrollPane;41static Robot robot;42static JFrame testFrame;43public static void main(String[] args) throws Exception {44try {45if (Platform.isOSX()) {46UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());47}4849SwingUtilities.invokeAndWait(new Runnable() {50public void run() {51createAndShowGUI();52}53});54Point centerOfScrollPane = Util.getCenterPoint(buttonScrollPane);55JButton rightScrollButton = findJButton(buttonScrollPane.getHorizontalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);56JButton bottomScrollButton = findJButton(buttonScrollPane.getVerticalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);5758if (rightScrollButton == null || bottomScrollButton == null) {59String errMessage = "Test can't be executed: ";60errMessage = errMessage + (rightScrollButton == null ? "can't find right button for horizontal scroll bar; " : ""61+ (bottomScrollButton == null ? "can't find bottom scroll button for vertical scroll bar" : ""));62throw new RuntimeException(errMessage);63}6465robot = new Robot();66robot.setAutoDelay(50);6768// test right, left and middle mouse buttons for horizontal scroll bar69if (!doTest(rightScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {70throw new RuntimeException("Test failed: right arrow button didn't respond on left mouse button.");71}72if (!doTest(rightScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {73throw new RuntimeException("Test failed: right arrow button respond on right mouse button.");74}75if (!doTest(rightScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {76throw new RuntimeException("Test failed: right arrow button respond on middle mouse button.");77}7879// test right, left and middle mouse buttons for vertical scroll bar80if (!doTest(bottomScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {81throw new RuntimeException("Test failed: bottom arrow button didn't respond on left mouse button.");82}83if (!doTest(bottomScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {84throw new RuntimeException("Test failed: bottom arrow button respond on right mouse button.");85}86if (!doTest(bottomScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {87throw new RuntimeException("Test failed: bottom arrow button respond on middle mouse button.");88}89} finally {90if (testFrame != null) SwingUtilities.invokeAndWait(() -> testFrame.dispose());91}92}93public static void createAndShowGUI() {94JPanel buttonPanel = new JPanel();95buttonPanel.setLayout(new GridLayout(5,5, 15,15));96int buttonCount = 1;97while (buttonCount <= 25) {98buttonPanel.add(new JButton("Button #"+buttonCount));99buttonCount++;100}101buttonScrollPane = new JScrollPane();102buttonScrollPane.setViewportView(buttonPanel);103104testFrame = new JFrame("bug4202954");105testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);106testFrame.setLayout(new BorderLayout());107testFrame.add(BorderLayout.CENTER, buttonScrollPane);108testFrame.setSize(450, 100);109testFrame.setVisible(true);110}111public static JButton findJButton(final JScrollBar scrollBar, final int minX, final int minY) throws Exception {112JButton button = Util.invokeOnEDT(new java.util.concurrent.Callable<JButton>() {113@Override114public JButton call() throws Exception {115int currentXorY = 0;116JButton scrollButton = null;117for (Component c: scrollBar.getComponents()) {118if (c instanceof JButton) {119Point p = c.getLocationOnScreen();120if (scrollBar.getOrientation() == Adjustable.VERTICAL){121if (currentXorY <= p.y){122currentXorY = p.y;123scrollButton = (JButton)c;124}125}else if (scrollBar.getOrientation() == Adjustable.HORIZONTAL){126if (currentXorY <= p.x){127currentXorY = p.x;128scrollButton = (JButton)c;129}130}131}132}133return scrollButton;134}135});136return button;137}138public static void clickMouseOnComponent(Component c, int buttons) throws Exception {139Point p = Util.getCenterPoint(c);140robot.mouseMove(p.x, p.y);141robot.mousePress(buttons);142robot.mouseRelease(buttons);143}144public static boolean doTest(JButton scrollButton, int buttons, boolean expectScroll) throws Exception {145java.util.concurrent.Callable<Integer> horizontalValue = new java.util.concurrent.Callable<Integer>() {146@Override147public Integer call() throws Exception {148return buttonScrollPane.getHorizontalScrollBar().getValue();149}150};151java.util.concurrent.Callable<Integer> verticalValue = new java.util.concurrent.Callable<Integer>() {152@Override153public Integer call() throws Exception {154return buttonScrollPane.getVerticalScrollBar().getValue();155}156};157Integer oldHValue = Util.invokeOnEDT(horizontalValue);158robot.waitForIdle();159Integer oldVValue = Util.invokeOnEDT(verticalValue);160robot.waitForIdle();161162clickMouseOnComponent(scrollButton, buttons);163robot.waitForIdle();164165int newHValue = Util.invokeOnEDT(horizontalValue);166robot.waitForIdle();167int newVValue = Util.invokeOnEDT(verticalValue);168robot.waitForIdle();169170return (oldHValue != newHValue || oldVValue != newVValue) == expectScroll;171}172}173174175