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