Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JScrollPane/HorizontalMouseWheelOnShiftPressed/HorizontalMouseWheelOnShiftPressed.java
38854 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.BorderLayout;24import java.awt.Point;25import java.awt.Robot;26import java.awt.event.KeyEvent;27import javax.swing.JFrame;28import javax.swing.JPanel;29import javax.swing.JScrollPane;30import javax.swing.JTextArea;31import javax.swing.SwingUtilities;3233import sun.awt.OSInfo;3435/**36* @test37* @bug 8033000 814799438* @author Alexander Scherbatiy39* @summary No Horizontal Mouse Wheel Support In BasicScrollPaneUI40* @run main HorizontalMouseWheelOnShiftPressed41*/42public class HorizontalMouseWheelOnShiftPressed {4344private static JScrollPane scrollPane;45private static JTextArea textArea;46private static Point point;47private static final int delta;48private static JFrame frame;4950static {51delta = OSInfo.getOSType().equals(OSInfo.OSType.MACOSX) ? -30 : 30;52}5354public static void main(String[] args) throws Exception {5556Robot robot = new Robot();57robot.setAutoDelay(50);5859SwingUtilities.invokeAndWait(60HorizontalMouseWheelOnShiftPressed::createAndShowGUI);61robot.waitForIdle();62try {63test(robot);64} finally {65frame.dispose();66}67}6869private static void test(Robot robot) throws Exception {70SwingUtilities.invokeAndWait(() -> {71Point locationOnScreen = scrollPane.getLocationOnScreen();72point = new Point(73locationOnScreen.x + scrollPane.getWidth() / 2,74locationOnScreen.y + scrollPane.getHeight() / 2);75});7677robot.mouseMove(point.x, point.y);78robot.waitForIdle();7980// vertical scroll bar is enabled81initScrollPane(true, false);82robot.waitForIdle();83robot.mouseWheel(delta);84robot.waitForIdle();85checkScrollPane(true, false);8687// vertical scroll bar is enabled + shift88initScrollPane(true, false);89robot.waitForIdle();90robot.keyPress(KeyEvent.VK_SHIFT);91robot.mouseWheel(delta);92robot.keyRelease(KeyEvent.VK_SHIFT);93robot.waitForIdle();94checkScrollPane(false, false);9596// horizontal scroll bar is enabled97initScrollPane(false, true);98robot.waitForIdle();99robot.mouseWheel(delta);100robot.waitForIdle();101checkScrollPane(false, true);102103// horizontal scroll bar is enabled + shift104initScrollPane(false, true);105robot.waitForIdle();106robot.keyPress(KeyEvent.VK_SHIFT);107robot.mouseWheel(delta);108robot.keyRelease(KeyEvent.VK_SHIFT);109robot.waitForIdle();110checkScrollPane(false, true);111112// both scroll bars are enabled113initScrollPane(true, true);114robot.waitForIdle();115robot.mouseWheel(delta);116robot.waitForIdle();117checkScrollPane(true, false);118119// both scroll bars are enabled + shift120initScrollPane(true, true);121robot.waitForIdle();122robot.keyPress(KeyEvent.VK_SHIFT);123robot.mouseWheel(delta);124robot.keyRelease(KeyEvent.VK_SHIFT);125robot.waitForIdle();126checkScrollPane(false, true);127}128129static void initScrollPane(boolean vVisible, boolean hVisible) throws Exception {130SwingUtilities.invokeAndWait(() -> {131scrollPane.getVerticalScrollBar().setValue(0);132scrollPane.getHorizontalScrollBar().setValue(0);133134textArea.setRows(vVisible ? 100 : 1);135textArea.setColumns(hVisible ? 100 : 1);136scrollPane.getVerticalScrollBar().setVisible(vVisible);137scrollPane.getHorizontalScrollBar().setVisible(hVisible);138});139}140141static void checkScrollPane(boolean verticalScrolled,142boolean horizontalScrolled) throws Exception {143SwingUtilities.invokeAndWait(() -> {144145if (verticalScrolled) {146if (scrollPane.getVerticalScrollBar().getValue() == 0) {147throw new RuntimeException("Wrong vertical scrolling!");148}149} else{150if (scrollPane.getVerticalScrollBar().getValue() != 0) {151throw new RuntimeException("Wrong vertical scrolling!");152}153}154if (horizontalScrolled) {155if (scrollPane.getHorizontalScrollBar().getValue() == 0) {156throw new RuntimeException("Wrong horizontal scrolling!");157}158} else {159if (scrollPane.getHorizontalScrollBar().getValue() != 0) {160throw new RuntimeException("Wrong horizontal scrolling!");161}162}163});164}165166static void createAndShowGUI() {167frame = new JFrame();168frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);169frame.setSize(300, 300);170frame.setLocationRelativeTo(null);171textArea = new JTextArea("Hello World!");172scrollPane = new JScrollPane(textArea);173JPanel panel = new JPanel(new BorderLayout());174panel.add(scrollPane, BorderLayout.CENTER);175frame.getContentPane().add(panel);176frame.setVisible(true);177}178}179180181