test/java/awt/Mouse/MouseComboBoxTest/MouseComboBoxTest.java

Print this page




  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 sun.awt.SunToolkit;
  31 
  32 import javax.swing.*;
  33 import javax.swing.plaf.basic.BasicComboPopup;
  34 import javax.swing.plaf.basic.ComboPopup;
  35 import javax.swing.plaf.metal.MetalComboBoxUI;
  36 import javax.swing.plaf.metal.MetalLookAndFeel;
  37 import java.awt.*;
  38 import java.awt.event.InputEvent;
  39 import java.awt.event.KeyEvent;
  40 
  41 public class MouseComboBoxTest {
  42     private static final String[] items = {"One", "Two", "Three", "Four", "Five"};
  43 
  44     private static SunToolkit toolkit = null;
  45     private static Robot robot = null;
  46     private static JFrame frame = null;
  47     private static JComboBox comboBox = null;
  48     private static MyComboBoxUI comboBoxUI = null;
  49 
  50     public static void main(String[] args) throws Exception {
  51         toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  52         robot = new Robot();
  53         robot.setAutoDelay(50);
  54 
  55         UIManager.setLookAndFeel(new MetalLookAndFeel());
  56         SwingUtilities.invokeAndWait(new Runnable() {
  57             @Override
  58             public void run() {
  59                 createAndShowGUI();
  60             }
  61         });
  62         toolkit.realSync();
  63 
  64         for (int i = 0; i < items.length; i++) {
  65             // Open popup
  66             robot.keyPress(KeyEvent.VK_DOWN);
  67             robot.keyRelease(KeyEvent.VK_DOWN);
  68             toolkit.realSync();
  69 
  70             Point point = getItemPointToClick(i);
  71             robot.mouseMove(point.x, point.y);
  72             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  73             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  74             toolkit.realSync();
  75 
  76             if (i != getSelectedIndex()) {
  77                 throw new RuntimeException("Test Failed! Incorrect value of selected index = " + getSelectedIndex() +
  78                         ", expected value = " + i);
  79             }
  80         }
  81     }
  82 
  83     private static Point getItemPointToClick(final int item) throws Exception {
  84         final Point[] result = new Point[1];
  85 
  86         SwingUtilities.invokeAndWait(new Runnable() {
  87             @Override
  88             public void run() {
  89                 BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
  90                 Point point = popup.getLocationOnScreen();
  91                 Dimension size = popup.getSize();
  92 
  93                 int step = size.height / items.length;
  94                 point.x += size.width / 2;




  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;