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 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;
  95                 point.y += step / 2 + step * item;
  96                 result[0] = point;
  97             }
  98         });
  99         return result[0];
 100     }
 101 
 102     private static int getSelectedIndex() throws Exception {
 103         final int[] result = new int[1];
 104 
 105         SwingUtilities.invokeAndWait(new Runnable() {
 106             @Override
 107             public void run() {
 108                 result[0] = comboBox.getSelectedIndex();
 109             }
 110         });
 111         return result[0];
 112     }
 113 
 114     private static void createAndShowGUI() {
 115         frame = new JFrame("MouseComboBoxTest");
 116 
 117         comboBox = new JComboBox(items);
 118         comboBox.setEditable(true);
 119         comboBoxUI = new MyComboBoxUI();
 120         comboBox.setUI(comboBoxUI);
 121 
 122         frame.pack();
 123         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 124         frame.setVisible(true);
 125 
 126         JWindow window = new JWindow(frame);
 127         window.add(comboBox);
 128         window.pack();
 129         window.setVisible(true);
 130     }
 131 
 132     private static class MyComboBoxUI extends MetalComboBoxUI {
 133         public ComboPopup getComboPopup() {
 134             return popup;
 135         }
 136     }
 137 }
 138