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