Path: blob/master/test/jdk/javax/swing/JComboBox/6236162/bug6236162.java
41154 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*/2223/*24* @test25* @key headful26* @bug 623616227* @summary Checks that there is no an inconsistence in combo box28* behavior when user points an item in combo popup29* by mouse and then uses UP/DOWN keys.30* @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;43private static Robot robot;4445public static void main(String[] args) throws Exception {46robot = new Robot();47robot.setAutoDelay(100);48try {49UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());50SwingUtilities.invokeAndWait(new Runnable() {51public void run() {52createAndShowGUI();53}54});55robot.waitForIdle();56robot.delay(1000);57test();58System.out.println("Test passed");59} finally {60if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());61}62}6364private static void createAndShowGUI() {65frame = new JFrame("bug6236162");6667combo = new JComboBox(new String[]{"one", "two", "three", "four", "five"});68combo.setEditable(true);69comboUI = new MyComboUI();70combo.setUI(comboUI);71combo.setSelectedIndex(3);72frame.getContentPane().add(combo);7374frame.pack();75frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);76frame.setLocationRelativeTo(null);77frame.setVisible(true);78frame.toFront();79}8081private static void test() throws AWTException {8283// Open popup menu84robot.keyPress(KeyEvent.VK_DOWN);85robot.keyRelease(KeyEvent.VK_DOWN);8687// Move mouse to the first popup menu item88robot.waitForIdle();89Point p = combo.getLocationOnScreen();90Dimension size = combo.getSize();91p.x += size.width / 2;92p.y += size.height;93float dy = 1;94robot.mouseMove(p.x, p.y - 5);95for (int i=1; i <= 10; i++) {96robot.mouseMove((int)(p.x), (int)(p.y - 5 + dy*i));97}9899// Select the second popup menu item100robot.waitForIdle();101robot.keyPress(KeyEvent.VK_DOWN);102robot.keyRelease(KeyEvent.VK_DOWN);103104robot.waitForIdle();105JList list = comboUI.getComboPopup().getList();106if (list.getSelectedIndex() != 1) {107throw new RuntimeException("There is an inconsistence in combo box " +108"behavior when user points an item in combo popup " +109"by mouse and then uses UP/DOWN keys.");110}111}112113114// Gives access to BasicComboBoxUI.popup field115private static class MyComboUI extends MetalComboBoxUI {116public ComboPopup getComboPopup() {117return popup;118}119}120}121122123