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