Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/8033069/bug8033069NoScrollBar.java
38854 views
/*1* Copyright (c) 2015, 2019, 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.GridLayout;26import java.awt.Point;27import java.awt.Robot;28import java.awt.event.InputEvent;29import javax.swing.JComboBox;30import javax.swing.JFrame;31import javax.swing.JPanel;32import javax.swing.SwingUtilities;33import javax.swing.UIManager;34import javax.swing.UIManager.LookAndFeelInfo;35import javax.swing.UnsupportedLookAndFeelException;3637/* @test38* @bug 803306939* @summary Checks that JComboBox popup does not close when mouse wheel is40* rotated over combo box and over its popup. The case where popup41* has no scroll bar.42* @library ../../regtesthelpers43* @build Util44* @run main bug8033069NoScrollBar45* @author Alexey Ivanov46*/47public class bug8033069NoScrollBar implements Runnable {4849private static final String[] NO_SCROLL_ITEMS = new String[] {50"AA", "B", "C", "D", "E", "F"51};5253private final Robot robot;5455private final String[] items;5657private JFrame frame;58private JComboBox cb1;59private JComboBox cb2;6061public static void main(String[] args) throws Exception {62iterateLookAndFeels(new bug8033069NoScrollBar(NO_SCROLL_ITEMS));63}6465protected static void iterateLookAndFeels(final bug8033069NoScrollBar test) throws Exception {66LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();67for (LookAndFeelInfo info : lafInfo) {68try {69UIManager.setLookAndFeel(info.getClassName());70System.out.println("Look and Feel: " + info.getClassName());71test.runTest();72} catch (UnsupportedLookAndFeelException e) {73System.out.println("Skipping unsupported LaF: " + info);74}75}76}7778public bug8033069NoScrollBar(String[] items) throws AWTException {79this.items = items;8081robot = new Robot();82robot.setAutoDelay(200);83}8485private void setupUI() {86frame = new JFrame();87frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);8889cb1 = new JComboBox<>(items);90cb2 = new JComboBox<>(items);91JPanel panel = new JPanel(new GridLayout(1, 2));92panel.add(cb1);93panel.add(cb2);9495frame.add(panel);9697frame.pack();98frame.setVisible(true);99}100101public void runTest() throws Exception {102try {103SwingUtilities.invokeAndWait(this);104105robot.waitForIdle();106assertFalse("cb1 popup is visible",107Util.invokeOnEDT(cb1::isPopupVisible));108109// Move mouse pointer to the center of the fist combo box110Point p = cb1.getLocationOnScreen();111Dimension d = cb1.getSize();112System.out.println(d.width + "," + d.height);113robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);114// Click it to open popup115robot.mousePress(InputEvent.BUTTON1_MASK);116robot.mouseRelease(InputEvent.BUTTON1_MASK);117118robot.waitForIdle();119assertTrue("cb1 popup is not visible",120Util.invokeOnEDT(cb1::isPopupVisible));121122robot.mouseWheel(1);123robot.waitForIdle();124assertTrue("cb1 popup is not visible after mouse wheel up on combo box",125Util.invokeOnEDT(cb1::isPopupVisible));126127robot.mouseWheel(-1);128robot.waitForIdle();129assertTrue("cb1 popup is not visible after mouse wheel down on combo box",130Util.invokeOnEDT(cb1::isPopupVisible));131132// Move mouse down on the popup133robot.mouseMove(p.x + d.width / 2, p.y + d.height * 3);134135robot.mouseWheel(1);136robot.waitForIdle();137assertTrue("cb1 popup is not visible after mouse wheel up on popup",138Util.invokeOnEDT(cb1::isPopupVisible));139140robot.mouseWheel(-1);141robot.waitForIdle();142assertTrue("cb1 popup is not visible after mouse wheel down on popup",143Util.invokeOnEDT(cb1::isPopupVisible));144145146// Move mouse pointer to the center of the second combo box147p = cb2.getLocationOnScreen();148d = cb2.getSize();149robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);150151robot.mouseWheel(1);152robot.waitForIdle();153assertFalse("cb1 popup is visible after mouse wheel up on cb2",154Util.invokeOnEDT(cb1::isPopupVisible));155} finally {156if (frame != null) {157frame.dispose();158}159}160161System.out.println("Test passed");162}163164@Override165public void run() {166setupUI();167}168169private static void assertTrue(String message, boolean value) {170assertEquals(message, true, value);171}172173private static void assertFalse(String message, boolean value) {174assertEquals(message, false, value);175}176177private static void assertEquals(String message, boolean expected, boolean actual) {178if (expected != actual) {179throw new RuntimeException(message);180}181}182}183184185