Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/6236162/bug6236162.java
38854 views
/*1* Copyright (c) 2013, 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*/22/* @test23@bug 623616224@summary Checks that there is no an inconsistence in combo box25behavior when user points an item in combo popup26by mouse and then uses UP/DOWN keys.27@library ../../regtesthelpers28@build Util29@author Mikhail Lapshin30@run main bug623616231*/3233import javax.swing.*;34import javax.swing.plaf.basic.*;35import javax.swing.plaf.metal.MetalComboBoxUI;36import java.awt.*;37import java.awt.event.KeyEvent;3839public class bug6236162 {40private static JFrame frame;41private static JComboBox combo;42private static MyComboUI comboUI;4344public static void main(String[] args) throws Exception {45UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());46SwingUtilities.invokeAndWait(new Runnable() {47public void run() {48createAndShowGUI();49}50});51test();52System.out.println("Test passed");53}5455private static void createAndShowGUI() {56frame = new JFrame("bug6236162");5758combo = new JComboBox(new String[]{"one", "two", "three", "four", "five"});59combo.setEditable(true);60comboUI = new MyComboUI();61combo.setUI(comboUI);62combo.setSelectedIndex(3);63frame.getContentPane().add(combo);6465frame.pack();66frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);67frame.setLocationRelativeTo(null);68frame.setVisible(true);69}7071private static void test() throws AWTException {72Robot robot = new Robot();73robot.setAutoDelay(50);7475// Open popup menu76robot.waitForIdle();77Util.hitKeys(robot, KeyEvent.VK_DOWN);7879// Move mouse to the first popup menu item80robot.waitForIdle();81Point p = combo.getLocationOnScreen();82Dimension size = combo.getSize();83p.x += size.width / 2;84p.y += size.height;85float dy = 1;86robot.mouseMove(p.x, p.y - 5);87for (int i=1; i <= 10; i++) {88robot.mouseMove((int)(p.x), (int)(p.y - 5 + dy*i));89}9091// Select the second popup menu item92robot.waitForIdle();93Util.hitKeys(robot, KeyEvent.VK_DOWN);9495robot.waitForIdle();96JList list = comboUI.getComboPopup().getList();97if (list.getSelectedIndex() != 1) {98throw new RuntimeException("There is an inconsistence in combo box " +99"behavior when user points an item in combo popup " +100"by mouse and then uses UP/DOWN keys.");101}102}103104105// Gives access to BasicComboBoxUI.popup field106private static class MyComboUI extends MetalComboBoxUI {107public ComboPopup getComboPopup() {108return popup;109}110}111}112113114