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 ../../regtesthelpers 26 @build Util 27 @author Michael C. Albers 28 @run main bug4202954 29 */ 30 31 import java.awt.*; 32 import java.awt.event.InputEvent; 33 import javax.swing.*; 34 import sun.awt.*; 35 36 public class bug4202954 { 37 static JScrollPane buttonScrollPane; 38 private static final SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 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 for (Component c: scrollBar.getComponents()) { 109 if (c instanceof JButton) { 110 Point p = c.getLocationOnScreen(); 111 if (p.x > minX && p.y > minY) { 112 return (JButton) c; 113 } 114 } 115 } 116 return null; 117 } 118 }); 119 return button; 120 } 121 public static void clickMouseOnComponent(Component c, int buttons) throws Exception { 122 Point p = Util.getCenterPoint(c); 123 robot.mouseMove(p.x, p.y); 124 robot.mousePress(buttons); 125 robot.mouseRelease(buttons); 126 } 127 public static boolean doTest(JButton scrollButton, int buttons, boolean expectScroll) throws Exception { 128 java.util.concurrent.Callable<Integer> horizontalValue = new java.util.concurrent.Callable<Integer>() { 129 @Override 130 public Integer call() throws Exception { 131 return buttonScrollPane.getHorizontalScrollBar().getValue(); 132 } 133 }; 134 java.util.concurrent.Callable<Integer> verticalValue = new java.util.concurrent.Callable<Integer>() { 135 @Override 136 public Integer call() throws Exception { 137 return buttonScrollPane.getVerticalScrollBar().getValue(); 138 } 139 }; 140 Integer oldHValue = Util.invokeOnEDT(horizontalValue); 141 Integer oldVValue = Util.invokeOnEDT(verticalValue); 142 143 clickMouseOnComponent(scrollButton, buttons); 144 toolkit.realSync(); 145 146 int newHValue = Util.invokeOnEDT(horizontalValue); 147 int newVValue = Util.invokeOnEDT(verticalValue); 148 149 return (oldHValue != newHValue || oldVValue != newVValue) == expectScroll; 150 } 151 }