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 ../../../../lib/testlibrary 28 @library ../../regtesthelpers 29 @build Util jdk.testlibrary.OSInfo 30 @author Michael C. Albers 31 @run main bug4202954 32 */ 33 34 import java.awt.*; 35 import java.awt.event.InputEvent; 36 import javax.swing.*; 37 import jdk.testlibrary.OSInfo; 38 39 public class bug4202954 { 40 static JScrollPane buttonScrollPane; 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 int currentXorY = 0; 111 JButton scrollButton = null; 112 for (Component c: scrollBar.getComponents()) { 113 if (c instanceof JButton) { 114 Point p = c.getLocationOnScreen(); 115 if (scrollBar.getOrientation() == Adjustable.VERTICAL){ 116 if (currentXorY <= p.y){ 117 currentXorY = p.y; 118 scrollButton = (JButton)c; 119 } 120 }else if (scrollBar.getOrientation() == Adjustable.HORIZONTAL){ 121 if (currentXorY <= p.x){ 122 currentXorY = p.x; 123 scrollButton = (JButton)c; 124 } 125 } 126 } 127 } 128 return scrollButton; 129 } 130 }); 131 return button; 132 } 133 public static void clickMouseOnComponent(Component c, int buttons) throws Exception { 134 Point p = Util.getCenterPoint(c); 135 robot.mouseMove(p.x, p.y); 136 robot.mousePress(buttons); 137 robot.mouseRelease(buttons); 138 } 139 public static boolean doTest(JButton scrollButton, int buttons, boolean expectScroll) throws Exception { 140 java.util.concurrent.Callable<Integer> horizontalValue = new java.util.concurrent.Callable<Integer>() { 141 @Override 142 public Integer call() throws Exception { 143 return buttonScrollPane.getHorizontalScrollBar().getValue(); 144 } 145 }; 146 java.util.concurrent.Callable<Integer> verticalValue = new java.util.concurrent.Callable<Integer>() { 147 @Override 148 public Integer call() throws Exception { 149 return buttonScrollPane.getVerticalScrollBar().getValue(); 150 } 151 }; 152 Integer oldHValue = Util.invokeOnEDT(horizontalValue); 153 robot.waitForIdle(); 154 Integer oldVValue = Util.invokeOnEDT(verticalValue); 155 robot.waitForIdle(); 156 157 clickMouseOnComponent(scrollButton, buttons); 158 robot.waitForIdle(); 159 160 int newHValue = Util.invokeOnEDT(horizontalValue); 161 robot.waitForIdle(); 162 int newVValue = Util.invokeOnEDT(verticalValue); 163 robot.waitForIdle(); 164 165 return (oldHValue != newHValue || oldVValue != newVValue) == expectScroll; 166 } 167 }