Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JScrollBar/bug4202954/bug4202954.java
38854 views
/*1* Copyright (c) 2013, 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/* @test23@bug 420295424@library ../../../../lib/testlibrary25@library ../../regtesthelpers26@build Util jdk.testlibrary.OSInfo27@author Michael C. Albers28@run main bug420295429*/3031import java.awt.*;32import java.awt.event.InputEvent;33import javax.swing.*;34import jdk.testlibrary.OSInfo;3536public class bug4202954 {37static JScrollPane buttonScrollPane;38static Robot robot;39public static void main(String[] args) throws Exception {40if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {41UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());42}4344SwingUtilities.invokeAndWait(new Runnable() {45public void run() {46createAndShowGUI();47}48});49Point centerOfScrollPane = Util.getCenterPoint(buttonScrollPane);50JButton rightScrollButton = findJButton(buttonScrollPane.getHorizontalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);51JButton bottomScrollButton = findJButton(buttonScrollPane.getVerticalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);5253if (rightScrollButton == null || bottomScrollButton == null) {54String errMessage = "Test can't be executed: ";55errMessage = errMessage + rightScrollButton == null ? "can't find right button for horizontal scroll bar; " : ""56+ bottomScrollButton == null ? "can't find bottom scroll button for vertical scroll bar" : "";57throw new RuntimeException(errMessage);58}5960robot = new Robot();61robot.setAutoDelay(50);6263// test right, left and middle mouse buttons for horizontal scroll bar64if (!doTest(rightScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {65throw new RuntimeException("Test failed: right arrow button didn't respond on left mouse button.");66}67if (!doTest(rightScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {68throw new RuntimeException("Test failed: right arrow button respond on right mouse button.");69}70if (!doTest(rightScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {71throw new RuntimeException("Test failed: right arrow button respond on middle mouse button.");72}7374// test right, left and middle mouse buttons for vertical scroll bar75if (!doTest(bottomScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {76throw new RuntimeException("Test failed: bottom arrow button didn't respond on left mouse button.");77}78if (!doTest(bottomScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {79throw new RuntimeException("Test failed: bottom arrow button respond on right mouse button.");80}81if (!doTest(bottomScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {82throw new RuntimeException("Test failed: bottom arrow button respond on middle mouse button.");83}84}85public static void createAndShowGUI() {86JPanel buttonPanel = new JPanel();87buttonPanel.setLayout(new GridLayout(5,5, 15,15));88int buttonCount = 1;89while (buttonCount <= 25) {90buttonPanel.add(new JButton("Button #"+buttonCount));91buttonCount++;92}93buttonScrollPane = new JScrollPane();94buttonScrollPane.setViewportView(buttonPanel);9596JFrame testFrame = new JFrame("bug4202954");97testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);98testFrame.setLayout(new BorderLayout());99testFrame.add(BorderLayout.CENTER, buttonScrollPane);100testFrame.setSize(450, 100);101testFrame.setVisible(true);102}103public static JButton findJButton(final JScrollBar scrollBar, final int minX, final int minY) throws Exception {104JButton button = Util.invokeOnEDT(new java.util.concurrent.Callable<JButton>() {105@Override106public JButton call() throws Exception {107for (Component c: scrollBar.getComponents()) {108if (c instanceof JButton) {109Point p = c.getLocationOnScreen();110if (p.x > minX && p.y > minY) {111return (JButton) c;112}113}114}115return null;116}117});118return button;119}120public static void clickMouseOnComponent(Component c, int buttons) throws Exception {121Point p = Util.getCenterPoint(c);122robot.mouseMove(p.x, p.y);123robot.mousePress(buttons);124robot.mouseRelease(buttons);125}126public static boolean doTest(JButton scrollButton, int buttons, boolean expectScroll) throws Exception {127java.util.concurrent.Callable<Integer> horizontalValue = new java.util.concurrent.Callable<Integer>() {128@Override129public Integer call() throws Exception {130return buttonScrollPane.getHorizontalScrollBar().getValue();131}132};133java.util.concurrent.Callable<Integer> verticalValue = new java.util.concurrent.Callable<Integer>() {134@Override135public Integer call() throws Exception {136return buttonScrollPane.getVerticalScrollBar().getValue();137}138};139Integer oldHValue = Util.invokeOnEDT(horizontalValue);140robot.waitForIdle();141Integer oldVValue = Util.invokeOnEDT(verticalValue);142robot.waitForIdle();143144clickMouseOnComponent(scrollButton, buttons);145robot.waitForIdle();146147int newHValue = Util.invokeOnEDT(horizontalValue);148robot.waitForIdle();149int newVValue = Util.invokeOnEDT(verticalValue);150robot.waitForIdle();151152return (oldHValue != newHValue || oldVValue != newVValue) == expectScroll;153}154}155156157