1 /* 2 * Copyright (c) 2014, 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 24 /* 25 * @test 26 * @key headful 27 * @bug 8032872 28 * @summary Tests JComboBox selection via the mouse 29 * @author Dmitry Markov 30 */ 31 import sun.awt.SunToolkit; 32 33 import javax.swing.*; 34 import javax.swing.plaf.basic.BasicComboPopup; 35 import javax.swing.plaf.basic.ComboPopup; 36 import javax.swing.plaf.metal.MetalComboBoxUI; 37 import javax.swing.plaf.metal.MetalLookAndFeel; 38 import java.awt.*; 39 import java.awt.event.InputEvent; 40 import java.awt.event.KeyEvent; 41 42 public class MouseComboBoxTest { 43 private static final String[] items = {"One", "Two", "Three", "Four", "Five"}; 44 45 private static SunToolkit toolkit = null; 46 private static Robot robot = null; 47 private static JFrame frame = null; 48 private static JComboBox comboBox = null; 49 private static MyComboBoxUI comboBoxUI = null; 50 51 public static void main(String[] args) throws Exception { 52 toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 53 robot = new Robot(); 54 robot.setAutoDelay(50); 55 56 UIManager.setLookAndFeel(new MetalLookAndFeel()); 57 SwingUtilities.invokeAndWait(new Runnable() { 58 @Override 59 public void run() { 60 createAndShowGUI(); 61 } 62 }); 63 toolkit.realSync(); 64 65 for (int i = 0; i < items.length; i++) { 66 // Open popup 67 robot.keyPress(KeyEvent.VK_DOWN); 68 robot.keyRelease(KeyEvent.VK_DOWN); 69 toolkit.realSync(); 70 71 Point point = getItemPointToClick(i); 72 robot.mouseMove(point.x, point.y); 73 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 74 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 75 toolkit.realSync(); 76 77 if (i != getSelectedIndex()) { 78 throw new RuntimeException("Test Failed! Incorrect value of selected index = " + getSelectedIndex() + 79 ", expected value = " + i); 80 } 81 } 82 } 83 84 private static Point getItemPointToClick(final int item) throws Exception { 85 final Point[] result = new Point[1]; 86 87 SwingUtilities.invokeAndWait(new Runnable() { 88 @Override 89 public void run() { 90 BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup(); 91 Point point = popup.getLocationOnScreen(); 92 Dimension size = popup.getSize(); 93 94 int step = size.height / items.length; 95 point.x += size.width / 2; 96 point.y += step / 2 + step * item; 97 result[0] = point; 98 } 99 }); 100 return result[0]; 101 } 102 103 private static int getSelectedIndex() throws Exception { 104 final int[] result = new int[1]; 105 106 SwingUtilities.invokeAndWait(new Runnable() { 107 @Override 108 public void run() { 109 result[0] = comboBox.getSelectedIndex(); 110 } 111 }); 112 return result[0]; 113 } 114 115 private static void createAndShowGUI() { 116 frame = new JFrame("MouseComboBoxTest"); 117 118 comboBox = new JComboBox(items); 119 comboBox.setEditable(true); 120 comboBoxUI = new MyComboBoxUI(); 121 comboBox.setUI(comboBoxUI); 122 123 frame.pack(); 124 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 125 frame.setVisible(true); 126 127 JWindow window = new JWindow(frame); 128 window.add(comboBox); 129 window.pack(); 130 window.setVisible(true); 131 } 132 133 private static class MyComboBoxUI extends MetalComboBoxUI { 134 public ComboPopup getComboPopup() { 135 return popup; 136 } 137 } 138 } 139