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