1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* @test 24 @bug 4202954 25 @library ../../../../lib/testlibrary 26 @library ../../regtesthelpers 27 @build Util jdk.testlibrary.OSInfo 28 @author Michael C. Albers 29 @run main bug4202954 30 */ 31 32 import java.awt.*; 33 import java.awt.event.InputEvent; 34 import javax.swing.*; 35 import jdk.testlibrary.OSInfo; 36 37 public class bug4202954 { 38 static JScrollPane buttonScrollPane; 39 static Robot robot; 40 public static void main(String[] args) throws Exception { 41 if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) { 42 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 43 } 44 45 SwingUtilities.invokeAndWait(new Runnable() { 46 public void run() { 47 createAndShowGUI(); 48 } 49 }); 50 Point centerOfScrollPane = Util.getCenterPoint(buttonScrollPane); 51 JButton rightScrollButton = findJButton(buttonScrollPane.getHorizontalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y); 52 JButton bottomScrollButton = findJButton(buttonScrollPane.getVerticalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y); 53 54 if (rightScrollButton == null || bottomScrollButton == null) { 55 String errMessage = "Test can't be executed: "; 56 errMessage = errMessage + (rightScrollButton == null ? "can't find right button for horizontal scroll bar; " : "" 57 + (bottomScrollButton == null ? "can't find bottom scroll button for vertical scroll bar" : "")); 58 throw new RuntimeException(errMessage); 59 } 60 61 robot = new Robot(); 62 robot.setAutoDelay(50); 63 64 // test right, left and middle mouse buttons for horizontal scroll bar 65 if (!doTest(rightScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) { 66 throw new RuntimeException("Test failed: right arrow button didn't respond on left mouse button."); 67 } 68 if (!doTest(rightScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) { 69 throw new RuntimeException("Test failed: right arrow button respond on right mouse button."); 70 } 71 if (!doTest(rightScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) { 72 throw new RuntimeException("Test failed: right arrow button respond on middle mouse button."); 73 } 74 75 // test right, left and middle mouse buttons for vertical scroll bar 76 if (!doTest(bottomScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) { 77 throw new RuntimeException("Test failed: bottom arrow button didn't respond on left mouse button."); 78 } 79 if (!doTest(bottomScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) { 80 throw new RuntimeException("Test failed: bottom arrow button respond on right mouse button."); 81 } 82 if (!doTest(bottomScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) { 83 throw new RuntimeException("Test failed: bottom arrow button respond on middle mouse button."); 84 } 85 } 86 public static void createAndShowGUI() { 87 JPanel buttonPanel = new JPanel(); 88 buttonPanel.setLayout(new GridLayout(5,5, 15,15)); 89 int buttonCount = 1; 90 while (buttonCount <= 25) { 91 buttonPanel.add(new JButton("Button #"+buttonCount)); 92 buttonCount++; 93 } 94 buttonScrollPane = new JScrollPane(); 95 buttonScrollPane.setViewportView(buttonPanel); 96 97 JFrame testFrame = new JFrame("bug4202954"); 98 testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 99 testFrame.setLayout(new BorderLayout()); 100 testFrame.add(BorderLayout.CENTER, buttonScrollPane); 101 testFrame.setSize(450, 100); 102 testFrame.setVisible(true); 103 } 104 public static JButton findJButton(final JScrollBar scrollBar, final int minX, final int minY) throws Exception { 105 JButton button = Util.invokeOnEDT(new java.util.concurrent.Callable<JButton>() { 106 @Override 107 public JButton call() throws Exception { 108 int currentXorY = 0; 109 JButton scrollButton = null; 110 for (Component c: scrollBar.getComponents()) { 111 if (c instanceof JButton) { 112 Point p = c.getLocationOnScreen(); 113 if (scrollBar.getOrientation() == Adjustable.VERTICAL){ 114 if (currentXorY <= p.y){ 115 currentXorY = p.y; 116 scrollButton = (JButton)c; 117 } 118 }else if (scrollBar.getOrientation() == Adjustable.HORIZONTAL){ 119 if (currentXorY <= p.x){ 120 currentXorY = p.x; 121 scrollButton = (JButton)c; 122 } 123 } 124 } 125 } 126 return scrollButton; 127 } 128 }); 129 return button; 130 } 131 public static void clickMouseOnComponent(Component c, int buttons) throws Exception { 132 Point p = Util.getCenterPoint(c); 133 robot.mouseMove(p.x, p.y); 134 robot.mousePress(buttons); 135 robot.mouseRelease(buttons); 136 } 137 public static boolean doTest(JButton scrollButton, int buttons, boolean expectScroll) throws Exception { 138 java.util.concurrent.Callable<Integer> horizontalValue = new java.util.concurrent.Callable<Integer>() { 139 @Override 140 public Integer call() throws Exception { 141 return buttonScrollPane.getHorizontalScrollBar().getValue(); 142 } 143 }; 144 java.util.concurrent.Callable<Integer> verticalValue = new java.util.concurrent.Callable<Integer>() { 145 @Override 146 public Integer call() throws Exception { 147 return buttonScrollPane.getVerticalScrollBar().getValue(); 148 } 149 }; 150 Integer oldHValue = Util.invokeOnEDT(horizontalValue); 151 robot.waitForIdle(); 152 Integer oldVValue = Util.invokeOnEDT(verticalValue); 153 robot.waitForIdle(); 154 155 clickMouseOnComponent(scrollButton, buttons); 156 robot.waitForIdle(); 157 158 int newHValue = Util.invokeOnEDT(horizontalValue); 159 robot.waitForIdle(); 160 int newVValue = Util.invokeOnEDT(verticalValue); 161 robot.waitForIdle(); 162 163 return (oldHValue != newHValue || oldVValue != newVValue) == expectScroll; 164 } 165 }