Path: blob/master/test/jdk/java/awt/Mouse/MouseComboBoxTest/MouseComboBoxTest.java
41153 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 803287227* @summary Tests JComboBox selection via the mouse28* @author Dmitry Markov29*/30import javax.swing.*;31import javax.swing.plaf.basic.BasicComboPopup;32import javax.swing.plaf.basic.ComboPopup;33import javax.swing.plaf.metal.MetalComboBoxUI;34import javax.swing.plaf.metal.MetalLookAndFeel;35import java.awt.*;36import java.awt.event.InputEvent;37import java.awt.event.KeyEvent;3839public class MouseComboBoxTest {40private static final String[] items = {"One", "Two", "Three", "Four", "Five"};4142private static Robot robot = null;43private static JFrame frame = null;44private static JComboBox comboBox = null;45private static MyComboBoxUI comboBoxUI = null;4647public static void main(String[] args) throws Exception {48robot = new Robot();49robot.setAutoDelay(100);5051UIManager.setLookAndFeel(new MetalLookAndFeel());52SwingUtilities.invokeAndWait(new Runnable() {53@Override54public void run() {55createAndShowGUI();56}57});58robot.waitForIdle();59robot.delay(1000);6061for (int i = 0; i < items.length; i++) {62// Open popup63robot.keyPress(KeyEvent.VK_DOWN);64robot.keyRelease(KeyEvent.VK_DOWN);65robot.waitForIdle();6667Point point = getItemPointToClick(i);68robot.mouseMove(point.x, point.y);69robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);70robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);71robot.waitForIdle();7273if (i != getSelectedIndex()) {74throw new RuntimeException("Test Failed! Incorrect value of selected index = " + getSelectedIndex() +75", expected value = " + i);76}77}78}7980private static Point getItemPointToClick(final int item) throws Exception {81final Point[] result = new Point[1];8283SwingUtilities.invokeAndWait(new Runnable() {84@Override85public void run() {86BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();87Point point = popup.getLocationOnScreen();88Dimension size = popup.getSize();8990int step = size.height / items.length;91point.x += size.width / 2;92point.y += step / 2 + step * item;93result[0] = point;94}95});96return result[0];97}9899private static int getSelectedIndex() throws Exception {100final int[] result = new int[1];101102SwingUtilities.invokeAndWait(new Runnable() {103@Override104public void run() {105result[0] = comboBox.getSelectedIndex();106}107});108return result[0];109}110111private static void createAndShowGUI() {112frame = new JFrame("MouseComboBoxTest");113114comboBox = new JComboBox(items);115comboBox.setEditable(true);116comboBoxUI = new MyComboBoxUI();117comboBox.setUI(comboBoxUI);118119frame.setUndecorated(true);120frame.pack();121frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);122frame.setVisible(true);123124JWindow window = new JWindow(frame);125window.add(comboBox);126window.pack();127window.setLocationRelativeTo(null);128window.setVisible(true);129}130131private static class MyComboBoxUI extends MetalComboBoxUI {132public ComboPopup getComboPopup() {133return popup;134}135}136}137138139140