Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/8136998/bug8136998.java
38854 views
/*1* Copyright (c) 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.AWTException;24import java.awt.Dimension;25import java.awt.Point;26import java.awt.Rectangle;27import java.awt.Robot;28import javax.swing.Box;29import javax.swing.BoxLayout;30import javax.swing.JComboBox;31import javax.swing.JFrame;32import javax.swing.JPanel;33import javax.swing.JScrollPane;34import javax.swing.SwingUtilities;35import javax.swing.UIManager;36import javax.swing.UIManager.LookAndFeelInfo;37import javax.swing.UnsupportedLookAndFeelException;38import javax.swing.WindowConstants;3940/* @test41* @bug 813699842* @summary Checks that JComboBox does not prevent mouse-wheel scrolling JScrollPane.43* @library ../../regtesthelpers44* @build Util45* @run main bug813699846* @author Alexey Ivanov47*/48public class bug8136998 {4950private static final String[] ITEMS = new String[] {51"A", "B", "C", "D", "E", "F"52};5354private final Robot robot;5556private JFrame frame;57private JComboBox comboBox;58private JScrollPane scrollPane;5960public static void main(String[] args) throws Exception {61iterateLookAndFeels(new bug8136998());62}6364protected static void iterateLookAndFeels(final bug8136998 test) throws Exception {65LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();66for (LookAndFeelInfo info : lafInfo) {67try {68UIManager.setLookAndFeel(info.getClassName());69System.out.println("Look and Feel: " + info.getClassName());70test.runTest();71} catch (UnsupportedLookAndFeelException e) {72System.out.println("Skipping unsupported LaF: " + info);73}74}75}7677public bug8136998() throws AWTException {78robot = new Robot();79robot.setAutoDelay(200);80}8182private void setupUI() {83frame = new JFrame();84frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);8586comboBox = new JComboBox<>(ITEMS);8788JPanel scrollable = new JPanel();89scrollable.setLayout(new BoxLayout(scrollable, BoxLayout.Y_AXIS));9091scrollable.add(Box.createVerticalStrut(200));92scrollable.add(comboBox);93scrollable.add(Box.createVerticalStrut(200));9495scrollPane = new JScrollPane(scrollable);9697frame.add(scrollPane);9899frame.setSize(100, 200);100frame.setVisible(true);101}102103public void runTest() throws Exception {104try {105SwingUtilities.invokeAndWait(this::setupUI);106107robot.waitForIdle();108109SwingUtilities.invokeAndWait(() ->110scrollPane.getViewport().scrollRectToVisible(comboBox.getBounds())111);112robot.waitForIdle();113114// Move mouse pointer to the center of the combo box115Point p = comboBox.getLocationOnScreen();116Dimension d = comboBox.getSize();117robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);118119// The currently visible rectangle in scrollPane120Rectangle viewRect0 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);121122// Scroll the scrollPane with mouse wheel123robot.mouseWheel(1);124robot.waitForIdle();125126// The updated rectangle127Rectangle viewRect1 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);128129if (viewRect0.y == viewRect1.y) {130throw new RuntimeException("Mouse wheel should have scrolled the JScrollPane");131}132} finally {133if (frame != null) {134frame.dispose();135}136}137138System.out.println("Test passed");139}140}141142143