1 /*
   2  * Copyright (c) 2015, 2016, 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 import java.awt.BorderLayout;
  25 import java.awt.Point;
  26 import java.awt.Robot;
  27 import java.awt.event.KeyEvent;
  28 import javax.swing.JFrame;
  29 import javax.swing.JPanel;
  30 import javax.swing.JScrollPane;
  31 import javax.swing.JTextArea;
  32 import javax.swing.SwingUtilities;
  33 
  34 import sun.awt.OSInfo;
  35 
  36 /**
  37  * @test
  38  * @key headful
  39  * @bug 8033000 8147994
  40  * @author Alexander Scherbatiy
  41  * @summary No Horizontal Mouse Wheel Support In BasicScrollPaneUI
  42  * @run main HorizontalMouseWheelOnShiftPressed
  43  */
  44 public class HorizontalMouseWheelOnShiftPressed {
  45 
  46     private static JScrollPane scrollPane;
  47     private static JTextArea textArea;
  48     private static Point point;
  49     private static final int delta;
  50     private static JFrame frame;
  51 
  52     static {
  53         delta = OSInfo.getOSType().equals(OSInfo.OSType.MACOSX) ? -30 : 30;
  54     }
  55 
  56     public static void main(String[] args) throws Exception {
  57 
  58         Robot robot = new Robot();
  59         robot.setAutoDelay(50);
  60 
  61         SwingUtilities.invokeAndWait(
  62                 HorizontalMouseWheelOnShiftPressed::createAndShowGUI);
  63         robot.waitForIdle();
  64         try {
  65             test(robot);
  66         } finally {
  67             frame.dispose();
  68         }
  69     }
  70 
  71     private static void test(Robot robot) throws Exception {
  72         SwingUtilities.invokeAndWait(() -> {
  73             Point locationOnScreen = scrollPane.getLocationOnScreen();
  74             point = new Point(
  75                     locationOnScreen.x + scrollPane.getWidth() / 2,
  76                     locationOnScreen.y + scrollPane.getHeight() / 2);
  77         });
  78 
  79         robot.mouseMove(point.x, point.y);
  80         robot.waitForIdle();
  81 
  82         // vertical scroll bar is enabled
  83         initScrollPane(true, false);
  84         robot.waitForIdle();
  85         robot.mouseWheel(delta);
  86         robot.waitForIdle();
  87         checkScrollPane(true, false);
  88 
  89         // vertical scroll bar is enabled + shift
  90         initScrollPane(true, false);
  91         robot.waitForIdle();
  92         robot.keyPress(KeyEvent.VK_SHIFT);
  93         robot.mouseWheel(delta);
  94         robot.keyRelease(KeyEvent.VK_SHIFT);
  95         robot.waitForIdle();
  96         checkScrollPane(false, false);
  97 
  98         // horizontal scroll bar is enabled
  99         initScrollPane(false, true);
 100         robot.waitForIdle();
 101         robot.mouseWheel(delta);
 102         robot.waitForIdle();
 103         checkScrollPane(false, true);
 104 
 105         // horizontal scroll bar is enabled + shift
 106         initScrollPane(false, true);
 107         robot.waitForIdle();
 108         robot.keyPress(KeyEvent.VK_SHIFT);
 109         robot.mouseWheel(delta);
 110         robot.keyRelease(KeyEvent.VK_SHIFT);
 111         robot.waitForIdle();
 112         checkScrollPane(false, true);
 113 
 114         // both scroll bars are enabled
 115         initScrollPane(true, true);
 116         robot.waitForIdle();
 117         robot.mouseWheel(delta);
 118         robot.waitForIdle();
 119         checkScrollPane(true, false);
 120 
 121         // both scroll bars are enabled + shift
 122         initScrollPane(true, true);
 123         robot.waitForIdle();
 124         robot.keyPress(KeyEvent.VK_SHIFT);
 125         robot.mouseWheel(delta);
 126         robot.keyRelease(KeyEvent.VK_SHIFT);
 127         robot.waitForIdle();
 128         checkScrollPane(false, true);
 129     }
 130 
 131     static void initScrollPane(boolean vVisible, boolean hVisible) throws Exception {
 132         SwingUtilities.invokeAndWait(() -> {
 133             scrollPane.getVerticalScrollBar().setValue(0);
 134             scrollPane.getHorizontalScrollBar().setValue(0);
 135 
 136             textArea.setRows(vVisible ? 100 : 1);
 137             textArea.setColumns(hVisible ? 100 : 1);
 138             scrollPane.getVerticalScrollBar().setVisible(vVisible);
 139             scrollPane.getHorizontalScrollBar().setVisible(hVisible);
 140         });
 141     }
 142 
 143     static void checkScrollPane(boolean verticalScrolled,
 144                                 boolean horizontalScrolled) throws Exception {
 145         SwingUtilities.invokeAndWait(() -> {
 146 
 147             if (verticalScrolled) {
 148                 if (scrollPane.getVerticalScrollBar().getValue() == 0) {
 149                     throw new RuntimeException("Wrong vertical scrolling!");
 150                 }
 151             } else{
 152                 if (scrollPane.getVerticalScrollBar().getValue() != 0) {
 153                     throw new RuntimeException("Wrong vertical scrolling!");
 154                 }
 155             }
 156             if (horizontalScrolled) {
 157                 if (scrollPane.getHorizontalScrollBar().getValue() == 0) {
 158                     throw new RuntimeException("Wrong horizontal scrolling!");
 159                 }
 160             } else {
 161                 if (scrollPane.getHorizontalScrollBar().getValue() != 0) {
 162                     throw new RuntimeException("Wrong horizontal scrolling!");
 163                 }
 164             }
 165         });
 166     }
 167 
 168     static void createAndShowGUI() {
 169         frame = new JFrame();
 170         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 171         frame.setSize(300, 300);
 172         frame.setLocationRelativeTo(null);
 173         textArea = new JTextArea("Hello World!");
 174         scrollPane = new JScrollPane(textArea);
 175         JPanel panel = new JPanel(new BorderLayout());
 176         panel.add(scrollPane, BorderLayout.CENTER);
 177         frame.getContentPane().add(panel);
 178         frame.setVisible(true);
 179     }
 180 }