Path: blob/master/test/jdk/javax/swing/JComboBox/8033069/bug8033069NoScrollBar.java
41153 views
/*1* Copyright (c) 2015, 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/*38* @test39* @key headful40* @bug 803306941* @summary Checks that JComboBox popup does not close when mouse wheel is42* rotated over combo box and over its popup. The case where popup43* has no scroll bar.44* @library ../../regtesthelpers45* @build Util46* @run main bug8033069NoScrollBar47*/48public class bug8033069NoScrollBar {4950private static final String[] NO_SCROLL_ITEMS = new String[] {51"A", "B", "C", "D", "E", "F"52};5354private final Robot robot;5556private final String[] items;5758private JFrame frame;59private JComboBox cb1;60private JComboBox cb2;6162private volatile Point p;63private volatile Dimension d;6465public static void main(String[] args) throws Exception {66iterateLookAndFeels(new bug8033069NoScrollBar(NO_SCROLL_ITEMS));67}6869protected static void iterateLookAndFeels(final bug8033069NoScrollBar test) throws Exception {70LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();71for (LookAndFeelInfo info : lafInfo) {72try {73UIManager.setLookAndFeel(info.getClassName());74System.out.println("Look and Feel: " + info.getClassName());75test.runTest();76} catch (UnsupportedLookAndFeelException e) {77System.out.println("Skipping unsupported LaF: " + info);78}79}80}8182public bug8033069NoScrollBar(String[] items) throws AWTException {83this.items = items;8485robot = new Robot();86robot.setAutoDelay(200);87}8889private void setupUI() {90frame = new JFrame();91frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);9293cb1 = new JComboBox<>(items);94cb2 = new JComboBox<>(items);95JPanel panel = new JPanel(new GridLayout(1, 2));96panel.add(cb1);97panel.add(cb2);9899frame.add(panel);100101frame.pack();102frame.setLocationRelativeTo(null);103frame.setVisible(true);104}105106private void disposeUI() {107if (frame != null) {108frame.dispose();109}110}111112public void runTest() throws Exception {113try {114SwingUtilities.invokeAndWait(this::setupUI);115116robot.waitForIdle();117assertFalse("cb1 popup is visible",118Util.invokeOnEDT(cb1::isPopupVisible));119120// Move mouse pointer to the center of the fist combo box121SwingUtilities.invokeAndWait(() -> {122p = cb1.getLocationOnScreen();123d = cb1.getSize();124});125126robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);127// Click it to open popup128robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);129robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);130131robot.waitForIdle();132assertTrue("cb1 popup is not visible",133Util.invokeOnEDT(cb1::isPopupVisible));134135robot.mouseWheel(1);136robot.waitForIdle();137assertTrue("cb1 popup is not visible after mouse wheel up on combo box",138Util.invokeOnEDT(cb1::isPopupVisible));139140robot.mouseWheel(-1);141robot.waitForIdle();142assertTrue("cb1 popup is not visible after mouse wheel down on combo box",143Util.invokeOnEDT(cb1::isPopupVisible));144145// Move mouse down on the popup146robot.mouseMove(p.x + d.width / 2, p.y + d.height * 3);147148robot.mouseWheel(1);149robot.waitForIdle();150assertTrue("cb1 popup is not visible after mouse wheel up on popup",151Util.invokeOnEDT(cb1::isPopupVisible));152153robot.mouseWheel(-1);154robot.waitForIdle();155assertTrue("cb1 popup is not visible after mouse wheel down on popup",156Util.invokeOnEDT(cb1::isPopupVisible));157158159// Move mouse pointer to the center of the second combo box160SwingUtilities.invokeAndWait(() -> {161p = cb2.getLocationOnScreen();162d = cb2.getSize();163});164165robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);166167robot.mouseWheel(1);168robot.waitForIdle();169assertFalse("cb1 popup is visible after mouse wheel up on cb2",170Util.invokeOnEDT(cb1::isPopupVisible));171} finally {172SwingUtilities.invokeAndWait(this::disposeUI);173}174175System.out.println("Test passed");176}177178private static void assertTrue(String message, boolean value) {179assertEquals(message, true, value);180}181182private static void assertFalse(String message, boolean value) {183assertEquals(message, false, value);184}185186private static void assertEquals(String message, boolean expected, boolean actual) {187if (expected != actual) {188throw new RuntimeException(message);189}190}191}192193194