1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* @test 24 @bug 6236162 25 @summary Checks that there is no an inconsistence in combo box 26 behavior when user points an item in combo popup 27 by mouse and then uses UP/DOWN keys. 28 @library ../../regtesthelpers 29 @build Util 30 @author Mikhail Lapshin 31 @run main bug6236162 32 */ 33 34 import sun.awt.SunToolkit; 35 36 import javax.swing.*; 37 import javax.swing.plaf.basic.*; 38 import javax.swing.plaf.metal.MetalComboBoxUI; 39 import java.awt.*; 40 import java.awt.event.KeyEvent; 41 42 public class bug6236162 { 43 private static final SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 44 private static JFrame frame; 45 private static JComboBox combo; 46 private static MyComboUI comboUI; 47 48 public static void main(String[] args) throws Exception { 49 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 50 SwingUtilities.invokeAndWait(new Runnable() { 51 public void run() { 52 createAndShowGUI(); 53 } 54 }); 55 toolkit.realSync(); 56 test(); 57 System.out.println("Test passed"); 58 } 59 60 private static void createAndShowGUI() { 61 frame = new JFrame("bug6236162"); 62 63 combo = new JComboBox(new String[]{"one", "two", "three", "four", "five"}); 64 combo.setEditable(true); 65 comboUI = new MyComboUI(); 66 combo.setUI(comboUI); 67 combo.setSelectedIndex(3); 68 frame.getContentPane().add(combo); 69 70 frame.pack(); 71 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 72 frame.setLocationRelativeTo(null); 73 frame.setVisible(true); 74 } 75 76 private static void test() throws AWTException { 77 Robot robot = new Robot(); 78 robot.setAutoDelay(50); 79 80 // Open popup menu 81 realSync(); 82 Util.hitKeys(robot, KeyEvent.VK_DOWN); 83 84 // Move mouse to the first popup menu item 85 realSync(); 86 Point p = combo.getLocationOnScreen(); 87 Dimension size = combo.getSize(); 88 p.x += size.width / 2; 89 p.y += size.height; 90 float dy = 1; 91 robot.mouseMove(p.x, p.y - 5); 92 for (int i=1; i <= 10; i++) { 93 robot.mouseMove((int)(p.x), (int)(p.y - 5 + dy*i)); 94 } 95 96 // Select the second popup menu item 97 realSync(); 98 Util.hitKeys(robot, KeyEvent.VK_DOWN); 99 100 realSync(); 101 JList list = comboUI.getComboPopup().getList(); 102 if (list.getSelectedIndex() != 1) { 103 throw new RuntimeException("There is an inconsistence in combo box " + 104 "behavior when user points an item in combo popup " + 105 "by mouse and then uses UP/DOWN keys."); 106 } 107 } 108 109 private static void realSync() { 110 ((SunToolkit)Toolkit.getDefaultToolkit()).realSync(); 111 } 112 113 // Gives access to BasicComboBoxUI.popup field 114 private static class MyComboUI extends MetalComboBoxUI { 115 public ComboPopup getComboPopup() { 116 return popup; 117 } 118 } 119 }