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