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