Path: blob/master/test/jdk/javax/swing/JScrollPane/HorizontalMouseWheelOnShiftPressed/HorizontalMouseWheelOnShiftPressed.java
41153 views
/*1* Copyright (c) 2015, 2018, 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 jdk.test.lib.Platform;3435/**36* @test37* @key headful38* @bug 8033000 814799439* @author Alexander Scherbatiy40* @summary No Horizontal Mouse Wheel Support In BasicScrollPaneUI41* @library /test/lib42* @build jdk.test.lib.Platform43* @run main HorizontalMouseWheelOnShiftPressed44*/45public class HorizontalMouseWheelOnShiftPressed {4647private static JScrollPane scrollPane;48private static JTextArea textArea;49private static Point point;50private static final int delta;51private static JFrame frame;5253static {54delta = Platform.isOSX() ? -30 : 30;55}5657public static void main(String[] args) throws Exception {5859Robot robot = new Robot();60robot.setAutoDelay(50);6162SwingUtilities.invokeAndWait(63HorizontalMouseWheelOnShiftPressed::createAndShowGUI);64robot.waitForIdle();65try {66test(robot);67} finally {68frame.dispose();69}70}7172private static void test(Robot robot) throws Exception {73SwingUtilities.invokeAndWait(() -> {74Point locationOnScreen = scrollPane.getLocationOnScreen();75point = new Point(76locationOnScreen.x + scrollPane.getWidth() / 2,77locationOnScreen.y + scrollPane.getHeight() / 2);78});7980robot.mouseMove(point.x, point.y);81robot.waitForIdle();8283// vertical scroll bar is enabled84initScrollPane(true, false);85robot.waitForIdle();86robot.mouseWheel(delta);87robot.waitForIdle();88checkScrollPane(true, false);8990// vertical scroll bar is enabled + shift91initScrollPane(true, false);92robot.waitForIdle();93robot.keyPress(KeyEvent.VK_SHIFT);94robot.mouseWheel(delta);95robot.keyRelease(KeyEvent.VK_SHIFT);96robot.waitForIdle();97checkScrollPane(false, false);9899// horizontal scroll bar is enabled100initScrollPane(false, true);101robot.waitForIdle();102robot.mouseWheel(delta);103robot.waitForIdle();104checkScrollPane(false, true);105106// horizontal scroll bar is enabled + shift107initScrollPane(false, true);108robot.waitForIdle();109robot.keyPress(KeyEvent.VK_SHIFT);110robot.mouseWheel(delta);111robot.keyRelease(KeyEvent.VK_SHIFT);112robot.waitForIdle();113checkScrollPane(false, true);114115// both scroll bars are enabled116initScrollPane(true, true);117robot.waitForIdle();118robot.mouseWheel(delta);119robot.waitForIdle();120checkScrollPane(true, false);121122// both scroll bars are enabled + shift123initScrollPane(true, true);124robot.waitForIdle();125robot.keyPress(KeyEvent.VK_SHIFT);126robot.mouseWheel(delta);127robot.keyRelease(KeyEvent.VK_SHIFT);128robot.waitForIdle();129checkScrollPane(false, true);130}131132static void initScrollPane(boolean vVisible, boolean hVisible) throws Exception {133SwingUtilities.invokeAndWait(() -> {134scrollPane.getVerticalScrollBar().setValue(0);135scrollPane.getHorizontalScrollBar().setValue(0);136137textArea.setRows(vVisible ? 100 : 1);138textArea.setColumns(hVisible ? 100 : 1);139scrollPane.getVerticalScrollBar().setVisible(vVisible);140scrollPane.getHorizontalScrollBar().setVisible(hVisible);141});142}143144static void checkScrollPane(boolean verticalScrolled,145boolean horizontalScrolled) throws Exception {146SwingUtilities.invokeAndWait(() -> {147148if (verticalScrolled) {149if (scrollPane.getVerticalScrollBar().getValue() == 0) {150throw new RuntimeException("Wrong vertical scrolling!");151}152} else{153if (scrollPane.getVerticalScrollBar().getValue() != 0) {154throw new RuntimeException("Wrong vertical scrolling!");155}156}157if (horizontalScrolled) {158if (scrollPane.getHorizontalScrollBar().getValue() == 0) {159throw new RuntimeException("Wrong horizontal scrolling!");160}161} else {162if (scrollPane.getHorizontalScrollBar().getValue() != 0) {163throw new RuntimeException("Wrong horizontal scrolling!");164}165}166});167}168169static void createAndShowGUI() {170frame = new JFrame();171frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);172frame.setSize(300, 300);173frame.setLocationRelativeTo(null);174textArea = new JTextArea("Hello World!");175scrollPane = new JScrollPane(textArea);176JPanel panel = new JPanel(new BorderLayout());177panel.add(scrollPane, BorderLayout.CENTER);178frame.getContentPane().add(panel);179frame.setVisible(true);180}181}182183184