Path: blob/master/test/jdk/javax/swing/JComboBox/8136998/bug8136998.java
41153 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/*41* @test42* @key headful43* @bug 813699844* @summary Checks that JComboBox does not prevent mouse-wheel scrolling JScrollPane.45* @library ../../regtesthelpers46* @build Util47* @run main bug813699848* @author Alexey Ivanov49*/50public class bug8136998 {5152private static final String[] ITEMS = new String[] {53"A", "B", "C", "D", "E", "F"54};5556private final Robot robot;5758private JFrame frame;59private JComboBox comboBox;60private JScrollPane scrollPane;6162public static void main(String[] args) throws Exception {63iterateLookAndFeels(new bug8136998());64}6566protected static void iterateLookAndFeels(final bug8136998 test) throws Exception {67LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();68for (LookAndFeelInfo info : lafInfo) {69try {70UIManager.setLookAndFeel(info.getClassName());71System.out.println("Look and Feel: " + info.getClassName());72test.runTest();73} catch (UnsupportedLookAndFeelException e) {74System.out.println("Skipping unsupported LaF: " + info);75}76}77}7879public bug8136998() throws AWTException {80robot = new Robot();81robot.setAutoDelay(200);82}8384private void setupUI() {85frame = new JFrame();86frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);8788comboBox = new JComboBox<>(ITEMS);8990JPanel scrollable = new JPanel();91scrollable.setLayout(new BoxLayout(scrollable, BoxLayout.Y_AXIS));9293scrollable.add(Box.createVerticalStrut(200));94scrollable.add(comboBox);95scrollable.add(Box.createVerticalStrut(200));9697scrollPane = new JScrollPane(scrollable);9899frame.add(scrollPane);100101frame.setSize(100, 200);102frame.setVisible(true);103}104105public void runTest() throws Exception {106try {107SwingUtilities.invokeAndWait(this::setupUI);108109robot.waitForIdle();110111SwingUtilities.invokeAndWait(() ->112scrollPane.getViewport().scrollRectToVisible(comboBox.getBounds())113);114robot.waitForIdle();115116// Move mouse pointer to the center of the combo box117Point p = comboBox.getLocationOnScreen();118Dimension d = comboBox.getSize();119robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);120121// The currently visible rectangle in scrollPane122Rectangle viewRect0 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);123124// Scroll the scrollPane with mouse wheel125robot.mouseWheel(1);126robot.waitForIdle();127128// The updated rectangle129Rectangle viewRect1 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);130131if (viewRect0.y == viewRect1.y) {132throw new RuntimeException("Mouse wheel should have scrolled the JScrollPane");133}134} finally {135if (frame != null) {136frame.dispose();137}138}139140System.out.println("Test passed");141}142}143144145