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