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 /* 24 @test 25 @key headful 26 @bug 4202954 27 @library ../../regtesthelpers 28 @build Util 29 @author Michael C. Albers 30 @run main bug4202954 31 */ 32 33 import java.awt.*; 34 import java.awt.event.InputEvent; 35 import javax.swing.*; 36 import sun.awt.*; 37 38 public class bug4202954 { 39 static JScrollPane buttonScrollPane; 40 private static final SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 41 static Robot robot; 42 public static void main(String[] args) throws Exception { 43 if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) { 44 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 45 } 46 47 SwingUtilities.invokeAndWait(new Runnable() { 48 public void run() { 49 createAndShowGUI(); 50 } 51 }); 52 Point centerOfScrollPane = Util.getCenterPoint(buttonScrollPane); 53 JButton rightScrollButton = findJButton(buttonScrollPane.getHorizontalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y); 54 JButton bottomScrollButton = findJButton(buttonScrollPane.getVerticalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y); 55 56 if (rightScrollButton == null || bottomScrollButton == null) { 57 String errMessage = "Test can't be executed: "; 58 errMessage = errMessage + rightScrollButton == null ? "can't find right button for horizontal scroll bar; " : "" 59 + bottomScrollButton == null ? "can't find bottom scroll button for vertical scroll bar" : ""; 60 throw new RuntimeException(errMessage); 61 } 62 63 robot = new Robot(); 64 robot.setAutoDelay(50); 65 66 // test right, left and middle mouse buttons for horizontal scroll bar 67 if (!doTest(rightScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) { 68 throw new RuntimeException("Test failed: right arrow button didn't respond on left mouse button."); 69 } 70 if (!doTest(rightScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) { 71 throw new RuntimeException("Test failed: right arrow button respond on right mouse button."); 72 } 73 if (!doTest(rightScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) { 74 throw new RuntimeException("Test failed: right arrow button respond on middle mouse button."); 75 } 76 77 // test right, left and middle mouse buttons for vertical scroll bar 78 if (!doTest(bottomScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) { 79 throw new RuntimeException("Test failed: bottom arrow button didn't respond on left mouse button."); 80 } 81 if (!doTest(bottomScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) { 82 throw new RuntimeException("Test failed: bottom arrow button respond on right mouse button."); 83 } 84 if (!doTest(bottomScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) { 85 throw new RuntimeException("Test failed: bottom arrow button respond on middle mouse button."); 86 } 87 } 88 public static void createAndShowGUI() { 89 JPanel buttonPanel = new JPanel(); 90 buttonPanel.setLayout(new GridLayout(5,5, 15,15)); 91 int buttonCount = 1; 92 while (buttonCount <= 25) { 93 buttonPanel.add(new JButton("Button #"+buttonCount)); 94 buttonCount++; 95 } 96 buttonScrollPane = new JScrollPane(); 97 buttonScrollPane.setViewportView(buttonPanel); 98 99 JFrame testFrame = new JFrame("bug4202954"); 100 testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 101 testFrame.setLayout(new BorderLayout()); 102 testFrame.add(BorderLayout.CENTER, buttonScrollPane); 103 testFrame.setSize(450, 100); 104 testFrame.setVisible(true); 105 } 106 public static JButton findJButton(final JScrollBar scrollBar, final int minX, final int minY) throws Exception { 107 JButton button = Util.invokeOnEDT(new java.util.concurrent.Callable<JButton>() { 108 @Override 109 public JButton call() throws Exception { 110 for (Component c: scrollBar.getComponents()) { 111 if (c instanceof JButton) { 112 Point p = c.getLocationOnScreen(); 113 if (p.x > minX && p.y > minY) { 114 return (JButton) c; 115 } 116 } 117 } 118 return null; 119 } 120 }); 121 return button; 122 } 123 public static void clickMouseOnComponent(Component c, int buttons) throws Exception { 124 Point p = Util.getCenterPoint(c); 125 robot.mouseMove(p.x, p.y); 126 robot.mousePress(buttons); 127 robot.mouseRelease(buttons); 128 } 129 public static boolean doTest(JButton scrollButton, int buttons, boolean expectScroll) throws Exception { 130 java.util.concurrent.Callable<Integer> horizontalValue = new java.util.concurrent.Callable<Integer>() { 131 @Override 132 public Integer call() throws Exception { 133 return buttonScrollPane.getHorizontalScrollBar().getValue(); 134 } 135 }; 136 java.util.concurrent.Callable<Integer> verticalValue = new java.util.concurrent.Callable<Integer>() { 137 @Override 138 public Integer call() throws Exception { 139 return buttonScrollPane.getVerticalScrollBar().getValue(); 140 } 141 }; 142 Integer oldHValue = Util.invokeOnEDT(horizontalValue); 143 Integer oldVValue = Util.invokeOnEDT(verticalValue); 144 145 clickMouseOnComponent(scrollButton, buttons); 146 toolkit.realSync(); 147 148 int newHValue = Util.invokeOnEDT(horizontalValue); 149 int newVValue = Util.invokeOnEDT(verticalValue); 150 151 return (oldHValue != newHValue || oldVValue != newVValue) == expectScroll; 152 } 153 }